KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webcontainer > test > GridProcessorTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webcontainer.test;
31
32 import nextapp.echo2.app.Extent;
33 import nextapp.echo2.app.Grid;
34 import nextapp.echo2.app.Label;
35 import nextapp.echo2.app.layout.GridLayoutData;
36 import nextapp.echo2.webcontainer.syncpeer.GridProcessor;
37 import junit.framework.TestCase;
38
39 /**
40  * Unit tests for <code>GridProcessor</code>.
41  */

42 public class GridProcessorTest extends TestCase {
43     
44     /**
45      * Test retrieving column widths and row heights.
46      */

47     public void testBasicColumnWidthAndRowHeight() {
48         Grid grid = new Grid(3);
49         for (int i = 0; i < 12; ++i) {
50             grid.add(new Label());
51         }
52         for (int i = 0; i < 4; ++i) {
53             grid.setColumnWidth(i, new Extent(i + 1));
54             grid.setRowHeight(i, new Extent((i + 1)* 10));
55         }
56         
57         GridProcessor gridProcessor = new GridProcessor(grid);
58         assertEquals(3, gridProcessor.getColumnCount());
59         assertEquals(4, gridProcessor.getRowCount());
60         assertEquals(new Extent(1), gridProcessor.getColumnWidth(0));
61         assertEquals(new Extent(2), gridProcessor.getColumnWidth(1));
62         assertEquals(new Extent(3), gridProcessor.getColumnWidth(2));
63         assertEquals(new Extent(10), gridProcessor.getRowHeight(0));
64         assertEquals(new Extent(20), gridProcessor.getRowHeight(1));
65         assertEquals(new Extent(30), gridProcessor.getRowHeight(2));
66         assertEquals(new Extent(40), gridProcessor.getRowHeight(3));
67         
68         grid.setOrientation(Grid.ORIENTATION_VERTICAL);
69         gridProcessor = new GridProcessor(grid);
70         assertEquals(4, gridProcessor.getColumnCount());
71         assertEquals(3, gridProcessor.getRowCount());
72         assertEquals(new Extent(1), gridProcessor.getColumnWidth(0));
73         assertEquals(new Extent(2), gridProcessor.getColumnWidth(1));
74         assertEquals(new Extent(3), gridProcessor.getColumnWidth(2));
75         assertEquals(new Extent(4), gridProcessor.getColumnWidth(3));
76         assertEquals(new Extent(10), gridProcessor.getRowHeight(0));
77         assertEquals(new Extent(20), gridProcessor.getRowHeight(1));
78         assertEquals(new Extent(30), gridProcessor.getRowHeight(2));
79     }
80     
81     /**
82      * Test a grid with no child components.
83      */

84     public void testEmptyGrid() {
85         Grid grid = new Grid();
86         GridProcessor gridProcessor = new GridProcessor(grid);
87         assertEquals(0, gridProcessor.getColumnCount());
88         assertEquals(0, gridProcessor.getRowCount());
89     }
90     
91     /**
92      * Test behavior of Grid with very little content.
93      * This is a special case for the <code>GridProcessor</code>.
94      * __ __ __ __
95      * | |XX|XX|XX|
96      * |__|XX|XX|XX|
97      */

98     
99     public void testLessThanSize() {
100         Grid grid = new Grid();
101         grid.setSize(4);
102         
103         grid.add(new Label());
104         
105         GridProcessor gridProcessor = new GridProcessor(grid);
106         assertEquals(1, gridProcessor.getColumnCount());
107         assertEquals(1, gridProcessor.getRowCount());
108     }
109     
110     /**
111      * Test behavior of Grid with very little content and a needless
112      * row span.
113      * This is a special case for the <code>GridProcessor</code>.
114      * __ __ __ __
115      * | |XX|XX|XX|
116      * | |XX|XX|XX|
117      * | |XX|XX|XX|
118      * | |XX|XX|XX|
119      * | |XX|XX|XX|
120      * |__|XX|XX|XX|
121      */

122     
123     public void testLessThanSizeWithRowSpan() {
124         Grid grid = new Grid();
125         grid.setSize(4);
126         
127         Label label = new Label();
128         GridLayoutData gridLayoutData = new GridLayoutData();
129         gridLayoutData.setRowSpan(3);
130         label.setLayoutData(gridLayoutData);
131         grid.add(label);
132         
133         GridProcessor gridProcessor = new GridProcessor(grid);
134         assertEquals(1, gridProcessor.getColumnCount());
135         assertEquals(1, gridProcessor.getRowCount());
136     }
137     
138     /**
139      * Tests span collisions.
140      *
141      * Requested: Rendered:
142      * __ __ __ __ __ __
143      * |0 |VC|1 | |0 |VC|1 |
144      * |__| |__| |__| |__|
145      * |HC \/ | |HC| |2 |
146      * |__ /\ __| |__| |__|
147      * |2 | |3 | |3 | | |
148      * |__|__|__| |__|__|__|
149      */

150     public void testInvalidAttemptCollision() {
151         GridLayoutData layoutData;
152         Grid grid = new Grid(3);
153         grid.add(new Label("0"));
154         Label verticalCollider = new Label("VC");
155         layoutData = new GridLayoutData();
156         layoutData.setRowSpan(3);
157         verticalCollider.setLayoutData(layoutData);
158         grid.add(verticalCollider);
159         grid.add(new Label("1"));
160         Label horizontalCollider = new Label("HC");
161         layoutData = new GridLayoutData();
162         layoutData.setColumnSpan(3);
163         horizontalCollider.setLayoutData(layoutData);
164         grid.add(horizontalCollider);
165         grid.add(new Label("2"));
166         grid.add(new Label("3"));
167         GridProcessor gridProcessor = new GridProcessor(grid);
168         assertEquals("0", ((Label) gridProcessor.getContent(0, 0)).getText());
169         assertEquals("VC", ((Label) gridProcessor.getContent(1, 0)).getText());
170         assertEquals("1", ((Label) gridProcessor.getContent(2, 0)).getText());
171         assertEquals("HC", ((Label) gridProcessor.getContent(0, 1)).getText());
172         assertEquals("VC", ((Label) gridProcessor.getContent(1, 1)).getText());
173         assertEquals("2", ((Label) gridProcessor.getContent(2, 1)).getText());
174         assertEquals("3", ((Label) gridProcessor.getContent(0, 2)).getText());
175         assertEquals("VC", ((Label) gridProcessor.getContent(1, 2)).getText());
176     }
177     
178     /**
179      * Tests row reduction to ensure proper rendering of the following:
180      *
181      * (specified) (rendered)
182      * __ __ __ __
183      * |0 |1 | 0 |0 |1 |
184      * | | | |__| |
185      * |XX| | 1 |XX| |
186      * |XX| | |XX|__|
187      * |XX| | 2
188      * |XX| |
189      * |XX| | 3
190      * |XX|__|
191      */

192     public void testOutOfBoundsRowSpan() {
193         Grid grid = new Grid();
194         grid.add(new Label());
195
196         Label label = new Label();
197         GridLayoutData layoutData = new GridLayoutData();
198         layoutData.setRowSpan(4);
199         label.setLayoutData(layoutData);
200         grid.add(label);
201         
202         GridProcessor gridProcessor = new GridProcessor(grid);
203
204         // Verify Grid size is correct.
205
assertEquals(2, gridProcessor.getColumnCount());
206         assertEquals(2, gridProcessor.getRowCount());
207         
208         // Verify components are at correct positions.
209
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
210         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
211         assertEquals(-1, gridProcessor.getComponentIndex(0, 1));
212         assertEquals(1, gridProcessor.getComponentIndex(1, 1));
213         
214         // Verify x-spans were untouched.
215
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
216         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
217         assertEquals(-1, gridProcessor.getColumnSpan(0, 1));
218         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
219
220         // Verify y-spans were properly reduced.
221
assertEquals(1, gridProcessor.getRowSpan(0, 0));
222         assertEquals(2, gridProcessor.getRowSpan(1, 0));
223         assertEquals(-1, gridProcessor.getRowSpan(0, 1));
224         assertEquals(2, gridProcessor.getRowSpan(1, 1));
225     }
226     
227     /**
228      * Tests column reduction to ensure proper rendering of the following:
229      *
230      * 0 1 2 3 4 5 6 (specified column)
231      * __ _____ ___________
232      * |0 |1 |2 |3 |
233      * |__|_____|_____|_____|
234      * |4 |5 |6 |7 |8 |
235      * |__|_____|__|__| |
236      * |9 |10 |11 | |
237      * |__|_____|_____|_____|
238      *
239      * 0 1 2 3 4 (rendered column)
240      */

241     public void testReduceColumnComplex() {
242         Grid grid = new Grid();
243         
244         // Configure Grid Content
245
grid.setSize(7);
246         for (int i = 0; i < 12; ++i) {
247             Label label = new Label(Integer.toString(i));
248             if (i == 1 || i == 2 || i == 3 || i == 5 || i == 8 || i == 10 || i == 11) {
249                 GridLayoutData layoutData = new GridLayoutData();
250                 layoutData.setColumnSpan(2);
251                 if (i == 8) {
252                     layoutData.setRowSpan(2);
253                 }
254                 label.setLayoutData(layoutData);
255             }
256             grid.add(label);
257         }
258         
259         // Set Column Sizes
260
grid.setColumnWidth(0, new Extent(1));
261         grid.setColumnWidth(1, new Extent(2));
262         grid.setColumnWidth(2, new Extent(4));
263         grid.setColumnWidth(3, new Extent(8));
264         grid.setColumnWidth(4, new Extent(16));
265         grid.setColumnWidth(5, new Extent(32));
266         grid.setColumnWidth(6, new Extent(64));
267
268         GridProcessor gridProcessor = new GridProcessor(grid);
269         
270         // Verify Grid size is correct.
271
assertEquals(5, gridProcessor.getColumnCount());
272         assertEquals(3, gridProcessor.getRowCount());
273         
274         // Verify components are at correct positions.
275
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
276         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
277         assertEquals(2, gridProcessor.getComponentIndex(2, 0));
278         assertEquals(2, gridProcessor.getComponentIndex(3, 0));
279         assertEquals(3, gridProcessor.getComponentIndex(4, 0));
280         assertEquals(4, gridProcessor.getComponentIndex(0, 1));
281         assertEquals(5, gridProcessor.getComponentIndex(1, 1));
282         assertEquals(6, gridProcessor.getComponentIndex(2, 1));
283         assertEquals(7, gridProcessor.getComponentIndex(3, 1));
284         assertEquals(8, gridProcessor.getComponentIndex(4, 1));
285         assertEquals(9, gridProcessor.getComponentIndex(0, 2));
286         assertEquals(10, gridProcessor.getComponentIndex(1, 2));
287         assertEquals(11, gridProcessor.getComponentIndex(2, 2));
288         assertEquals(11, gridProcessor.getComponentIndex(3, 2));
289         assertEquals(8, gridProcessor.getComponentIndex(4, 2));
290         
291         // Verify x-spans were property reduced.
292
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
293         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
294         assertEquals(2, gridProcessor.getColumnSpan(2, 0));
295         assertEquals(2, gridProcessor.getColumnSpan(3, 0));
296         assertEquals(1, gridProcessor.getColumnSpan(4, 0));
297         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
298         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
299         assertEquals(1, gridProcessor.getColumnSpan(2, 1));
300         assertEquals(1, gridProcessor.getColumnSpan(3, 1));
301         assertEquals(1, gridProcessor.getColumnSpan(4, 1));
302         assertEquals(1, gridProcessor.getColumnSpan(0, 2));
303         assertEquals(1, gridProcessor.getColumnSpan(1, 2));
304         assertEquals(2, gridProcessor.getColumnSpan(2, 2));
305         assertEquals(2, gridProcessor.getColumnSpan(3, 2));
306         assertEquals(1, gridProcessor.getColumnSpan(4, 2));
307         
308         // Verify y-spans were untouched.
309
assertEquals(1, gridProcessor.getRowSpan(0, 0));
310         assertEquals(1, gridProcessor.getRowSpan(1, 0));
311         assertEquals(1, gridProcessor.getRowSpan(2, 0));
312         assertEquals(1, gridProcessor.getRowSpan(3, 0));
313         assertEquals(1, gridProcessor.getRowSpan(4, 0));
314         assertEquals(1, gridProcessor.getRowSpan(0, 1));
315         assertEquals(1, gridProcessor.getRowSpan(1, 1));
316         assertEquals(1, gridProcessor.getRowSpan(3, 1));
317         assertEquals(2, gridProcessor.getRowSpan(4, 1));
318         assertEquals(1, gridProcessor.getRowSpan(0, 2));
319         assertEquals(1, gridProcessor.getRowSpan(1, 2));
320         assertEquals(1, gridProcessor.getRowSpan(2, 2));
321         assertEquals(1, gridProcessor.getRowSpan(3, 2));
322         assertEquals(2, gridProcessor.getRowSpan(4, 2));
323         
324         // Verify column sizes were properly combined.
325
assertEquals(new Extent(1), gridProcessor.getColumnWidth(0));
326         assertEquals(new Extent(6), gridProcessor.getColumnWidth(1));
327         assertEquals(new Extent(8), gridProcessor.getColumnWidth(2));
328         assertEquals(new Extent(16), gridProcessor.getColumnWidth(3));
329         assertEquals(new Extent(96), gridProcessor.getColumnWidth(4));
330     }
331     
332     /**
333      * Tests column reduction to ensure the column #2 is not rendered.
334      *
335      * 0 1 2 3 (specified column)
336      * __ _____ __
337      * |0 |1 |2 |
338      * |__|_____|__|
339      * |3 |4 |5 |
340      * |__|_____|__|
341      *
342      * 0 1 2 (rendered column)
343      */

344     public void testReduceColumnSimple() {
345         Grid grid = new Grid();
346         grid.setSize(4);
347         for (int i = 0; i < 6; ++i) {
348             Label label = new Label(Integer.toString(i));
349             if (i == 1 || i == 4) {
350                 GridLayoutData layoutData = new GridLayoutData();
351                 layoutData.setColumnSpan(2);
352                 label.setLayoutData(layoutData);
353             }
354             grid.add(label);
355         }
356         GridProcessor gridProcessor = new GridProcessor(grid);
357         assertEquals(3, gridProcessor.getColumnCount());
358         assertEquals(2, gridProcessor.getRowCount());
359         assertEquals(0, gridProcessor.getComponentIndex(0, 0));
360         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
361         assertEquals(2, gridProcessor.getComponentIndex(2, 0));
362         assertEquals(3, gridProcessor.getComponentIndex(0, 1));
363         assertEquals(4, gridProcessor.getComponentIndex(1, 1));
364         assertEquals(5, gridProcessor.getComponentIndex(2, 1));
365     }
366     
367     /**
368      * Tests column reduction to ensure proper rendering of the following:
369      * specified rendered
370      * __ __ __ row: row:
371      * |0 |1 |2 |
372      * |__|__|__| 0 0
373      * |3 |4 |5 |
374      * | | | | 1 1
375      * | | | |
376      * |__|__|__| 2
377      * |6 |7 |8 |
378      * | |__| | 3 2
379      * | |9 | |
380      * |__|__|__| 4 3
381      * |10|11 |
382      * | | | 5 4
383      * | | |
384      * |__|_____| 6
385      */

386     public void testReduceRowComplex() {
387         Grid grid = new Grid();
388         
389         // Configure Grid Content
390
grid.setSize(3);
391         for (int i = 0; i < 12; ++i) {
392             Label label = new Label(Integer.toString(i));
393             if (i == 3 || i == 4 || i == 5 || i == 6 || i == 8 || i == 10 || i == 11) {
394                 GridLayoutData layoutData = new GridLayoutData();
395                 layoutData.setRowSpan(2);
396                 if (i == 11) {
397                     layoutData.setColumnSpan(2);
398                 }
399                 label.setLayoutData(layoutData);
400             }
401             grid.add(label);
402         }
403         
404         // Set Row Sizes
405
grid.setRowHeight(0, new Extent(1));
406         grid.setRowHeight(1, new Extent(2));
407         grid.setRowHeight(2, new Extent(4));
408         grid.setRowHeight(3, new Extent(8));
409         grid.setRowHeight(4, new Extent(16));
410         grid.setRowHeight(5, new Extent(32));
411         grid.setRowHeight(6, new Extent(64));
412
413         GridProcessor gridProcessor = new GridProcessor(grid);
414         
415         // Verify Grid size is correct.
416
assertEquals(3, gridProcessor.getColumnCount());
417         assertEquals(5, gridProcessor.getRowCount());
418
419         // Verify components are at correct positions.
420
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
421         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
422         assertEquals(2, gridProcessor.getComponentIndex(2, 0));
423         assertEquals(3, gridProcessor.getComponentIndex(0, 1));
424         assertEquals(4, gridProcessor.getComponentIndex(1, 1));
425         assertEquals(5, gridProcessor.getComponentIndex(2, 1));
426         assertEquals(6, gridProcessor.getComponentIndex(0, 2));
427         assertEquals(7, gridProcessor.getComponentIndex(1, 2));
428         assertEquals(8, gridProcessor.getComponentIndex(2, 2));
429         assertEquals(6, gridProcessor.getComponentIndex(0, 3));
430         assertEquals(9, gridProcessor.getComponentIndex(1, 3));
431         assertEquals(8, gridProcessor.getComponentIndex(2, 3));
432         assertEquals(10, gridProcessor.getComponentIndex(0, 4));
433         assertEquals(11, gridProcessor.getComponentIndex(1, 4));
434         assertEquals(11, gridProcessor.getComponentIndex(2, 4));
435         
436         // Verify x-spans were untouched.
437
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
438         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
439         assertEquals(1, gridProcessor.getColumnSpan(2, 0));
440         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
441         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
442         assertEquals(1, gridProcessor.getColumnSpan(2, 1));
443         assertEquals(1, gridProcessor.getColumnSpan(0, 2));
444         assertEquals(1, gridProcessor.getColumnSpan(1, 2));
445         assertEquals(1, gridProcessor.getColumnSpan(2, 2));
446         assertEquals(1, gridProcessor.getColumnSpan(0, 3));
447         assertEquals(1, gridProcessor.getColumnSpan(1, 3));
448         assertEquals(1, gridProcessor.getColumnSpan(2, 3));
449         assertEquals(1, gridProcessor.getColumnSpan(0, 4));
450         assertEquals(2, gridProcessor.getColumnSpan(1, 4));
451         assertEquals(2, gridProcessor.getColumnSpan(2, 4));
452         
453         // Verify y-spans were properly reduced.
454
assertEquals(1, gridProcessor.getRowSpan(0, 0));
455         assertEquals(1, gridProcessor.getRowSpan(1, 0));
456         assertEquals(1, gridProcessor.getRowSpan(2, 0));
457         assertEquals(1, gridProcessor.getRowSpan(0, 1));
458         assertEquals(1, gridProcessor.getRowSpan(1, 1));
459         assertEquals(1, gridProcessor.getRowSpan(2, 1));
460         assertEquals(2, gridProcessor.getRowSpan(0, 2));
461         assertEquals(1, gridProcessor.getRowSpan(1, 2));
462         assertEquals(2, gridProcessor.getRowSpan(2, 2));
463         assertEquals(2, gridProcessor.getRowSpan(0, 3));
464         assertEquals(1, gridProcessor.getRowSpan(1, 3));
465         assertEquals(2, gridProcessor.getRowSpan(2, 3));
466         assertEquals(1, gridProcessor.getRowSpan(0, 4));
467         assertEquals(1, gridProcessor.getRowSpan(1, 4));
468         assertEquals(1, gridProcessor.getRowSpan(2, 4));
469         
470         // Verify column sizes were properly combined.
471
assertEquals(new Extent(1), gridProcessor.getRowHeight(0));
472         assertEquals(new Extent(6), gridProcessor.getRowHeight(1));
473         assertEquals(new Extent(8), gridProcessor.getRowHeight(2));
474         assertEquals(new Extent(16), gridProcessor.getRowHeight(3));
475         assertEquals(new Extent(96), gridProcessor.getRowHeight(4));
476     }
477     
478     /**
479      * Tests row reduction to ensure the row #2 is not rendered.
480      *
481      * __ __ Row:
482      * |0 |1 | 0
483      * |__|__|
484      * |2 |3 | 1
485      * | | |
486      * | | | 2
487      * |__|__|
488      * |4 |5 | 3
489      * |__|__|
490      */

491     public void testReduceRowSimple() {
492         Grid grid = new Grid();
493         grid.setSize(2);
494         for (int i = 0; i < 6; ++i) {
495             Label label = new Label(Integer.toString(i));
496             if (i == 2 || i == 3) {
497                 GridLayoutData layoutData = new GridLayoutData();
498                 layoutData.setRowSpan(2);
499                 label.setLayoutData(layoutData);
500             }
501             grid.add(label);
502         }
503         GridProcessor gridProcessor = new GridProcessor(grid);
504         assertEquals(2, gridProcessor.getColumnCount());
505         assertEquals(3, gridProcessor.getRowCount());
506         assertEquals(0, gridProcessor.getComponentIndex(0, 0));
507         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
508         assertEquals(2, gridProcessor.getComponentIndex(0, 1));
509         assertEquals(3, gridProcessor.getComponentIndex(1, 1));
510         assertEquals(4, gridProcessor.getComponentIndex(0, 2));
511         assertEquals(5, gridProcessor.getComponentIndex(1, 2));
512     }
513     
514     /**
515      * Test Grid with row and column reduction
516      *
517      * (specified)
518      * 0 1 2 (spec) (rend)
519      * __ _____
520      * |0 |1 | 0 0
521      * |__|_____|
522      * |2 |3 | 1 1
523      * | | |
524      * | | | 2
525      * |__|_____|
526      *
527      * 0 1
528      * (rendered)
529      */

530     public void testReduceColumnAndRowSimple() {
531         Grid grid = new Grid();
532         grid.setSize(3);
533         for (int i = 0; i < 4; ++i) {
534             Label label = new Label(Integer.toString(i));
535             if (i > 0) {
536                 GridLayoutData layoutData = new GridLayoutData();
537                 if (i == 1 || i == 3) {
538                     layoutData.setColumnSpan(2);
539                 }
540                 if (i == 2 || i == 3) {
541                     layoutData.setRowSpan(2);
542                 }
543                 label.setLayoutData(layoutData);
544             }
545             grid.add(label);
546         }
547
548         GridProcessor gridProcessor = new GridProcessor(grid);
549         
550         // Verify Grid size is correct.
551
assertEquals(2, gridProcessor.getColumnCount());
552         assertEquals(2, gridProcessor.getRowCount());
553
554         // Verify components are at correct positions.
555
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
556         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
557         assertEquals(2, gridProcessor.getComponentIndex(0, 1));
558         assertEquals(3, gridProcessor.getComponentIndex(1, 1));
559         
560         // Verify x-spans were properly reduced.
561
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
562         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
563         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
564         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
565
566         // Verify y-spans were properly reduced.
567
assertEquals(1, gridProcessor.getRowSpan(0, 0));
568         assertEquals(1, gridProcessor.getRowSpan(1, 0));
569         assertEquals(1, gridProcessor.getRowSpan(0, 1));
570         assertEquals(1, gridProcessor.getRowSpan(1, 1));
571     }
572     
573     
574     /**
575      * Test reduction of symmetric column spans.
576      *
577      * 0 1 2 3 (specified)
578      * ________ __
579      * | | |
580      * |________|__|
581      * | | |
582      * |________|__|
583      * | | |
584      * |________|__|
585      *
586      * 0 1 (rendered)
587      */

588     public void testReduceColumnLong() {
589         Grid grid = new Grid();
590         grid.setSize(4);
591         
592         for (int i = 0; i < 6; ++i) {
593             Label label = new Label(Integer.toString(i));
594             if (i % 2 == 0) {
595                 GridLayoutData layoutData = new GridLayoutData();
596                 layoutData.setColumnSpan(3);
597                 label.setLayoutData(layoutData);
598             }
599             grid.add(label);
600         }
601
602         GridProcessor gridProcessor = new GridProcessor(grid);
603         
604         // Verify Grid size is correct.
605
assertEquals(2, gridProcessor.getColumnCount());
606         assertEquals(3, gridProcessor.getRowCount());
607
608         // Verify components are at correct positions.
609
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
610         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
611         assertEquals(2, gridProcessor.getComponentIndex(0, 1));
612         assertEquals(3, gridProcessor.getComponentIndex(1, 1));
613         assertEquals(4, gridProcessor.getComponentIndex(0, 2));
614         assertEquals(5, gridProcessor.getComponentIndex(1, 2));
615
616         // Verify x-spans were properly reduced.
617
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
618         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
619         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
620         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
621         assertEquals(1, gridProcessor.getColumnSpan(0, 2));
622         assertEquals(1, gridProcessor.getColumnSpan(1, 2));
623
624         // Verify y-spans were untouched.
625
assertEquals(1, gridProcessor.getRowSpan(0, 0));
626         assertEquals(1, gridProcessor.getRowSpan(1, 0));
627         assertEquals(1, gridProcessor.getRowSpan(0, 1));
628         assertEquals(1, gridProcessor.getRowSpan(1, 1));
629         assertEquals(1, gridProcessor.getRowSpan(0, 2));
630         assertEquals(1, gridProcessor.getRowSpan(1, 2));
631     }
632     
633     /**
634      * Test reduction of symmetric row spans.
635      *
636      * __ __ __ (spec) (rend)
637      * | | | |
638      * | | | | 0 0
639      * | | | |
640      * | | | | 1
641      * | | | |
642      * |__|__|__| 2
643      * | | | |
644      * |__|__|__| 3 1
645      */

646     public void testReduceRowLong() {
647         Grid grid = new Grid();
648         grid.setSize(3);
649         
650         for (int i = 0; i < 6; ++i) {
651             Label label = new Label(Integer.toString(i));
652             if (i < 3) {
653                 GridLayoutData layoutData = new GridLayoutData();
654                 layoutData.setRowSpan(3);
655                 label.setLayoutData(layoutData);
656             }
657             grid.add(label);
658         }
659         
660         GridProcessor gridProcessor = new GridProcessor(grid);
661         
662         // Verify Grid size is correct.
663
assertEquals(3, gridProcessor.getColumnCount());
664         assertEquals(2, gridProcessor.getRowCount());
665         
666         // Verify components are at correct positions.
667
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
668         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
669         assertEquals(2, gridProcessor.getComponentIndex(2, 0));
670         assertEquals(3, gridProcessor.getComponentIndex(0, 1));
671         assertEquals(4, gridProcessor.getComponentIndex(1, 1));
672         assertEquals(5, gridProcessor.getComponentIndex(2, 1));
673
674         // Verify x-spans were untouched.
675
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
676         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
677         assertEquals(1, gridProcessor.getColumnSpan(2, 0));
678         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
679         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
680         assertEquals(1, gridProcessor.getColumnSpan(2, 1));
681
682         // Verify y-spans were properly reduced.
683
assertEquals(1, gridProcessor.getRowSpan(0, 0));
684         assertEquals(1, gridProcessor.getRowSpan(1, 0));
685         assertEquals(1, gridProcessor.getRowSpan(2, 0));
686         assertEquals(1, gridProcessor.getRowSpan(0, 1));
687         assertEquals(1, gridProcessor.getRowSpan(1, 1));
688         assertEquals(1, gridProcessor.getRowSpan(2, 1));
689     }
690
691     /**
692      * Test Grid that consists a single row-spanned and column-spanned cell.
693      *
694      * 0 1 2
695      * __ __ __
696      * |0 | 0
697      * | |
698      * | | 1
699      * | |
700      * | | 2
701      * | |
702      * | | 3
703      * | |
704      * | | 4
705      * |________|
706      */

707     public void testReduceColumnAndRowOneBigCell() {
708         Grid grid = new Grid();
709         grid.setSize(3);
710         Label label = new Label("0");
711         GridLayoutData layoutData = new GridLayoutData();
712         layoutData.setColumnSpan(3);
713         layoutData.setRowSpan(5);
714         label.setLayoutData(layoutData);
715         grid.add(label);
716
717         GridProcessor gridProcessor = new GridProcessor(grid);
718         
719         // Verify Grid size is correct.
720
assertEquals(1, gridProcessor.getColumnCount());
721         assertEquals(1, gridProcessor.getRowCount());
722         
723         // Verify components are at correct positions.
724
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
725
726         // Verify x-spans and y-spans were properly reduced.
727
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
728         assertEquals(1, gridProcessor.getRowSpan(0, 0));
729     }
730     
731     /**
732      * Tests a grid with a cell spanning two rows:
733      * __ __
734      * |0 |1 |
735      * |__|__|
736      * |2 |3 |
737      * | |__|
738      * | |4 |
739      * |__|__|
740      * |5 |6 |
741      * |__|__|
742      */

743     public void testRowSpanSimple() {
744         Grid grid = new Grid();
745         for (int i = 0; i < 7; ++i) {
746             Label label = new Label(Integer.toString(i));
747             if (i == 2) {
748                 GridLayoutData layoutData = new GridLayoutData();
749                 layoutData.setRowSpan(2);
750                 label.setLayoutData(layoutData);
751             }
752             grid.add(label);
753         }
754         
755         GridProcessor gridProcessor = new GridProcessor(grid);
756         
757         // Verify Grid size is correct.
758
assertEquals(2, gridProcessor.getColumnCount());
759         assertEquals(4, gridProcessor.getRowCount());
760         
761         // Verify components are at correct positions.
762
assertEquals("0", ((Label) gridProcessor.getContent(0, 0)).getText());
763         assertEquals("1", ((Label) gridProcessor.getContent(1, 0)).getText());
764         assertEquals("2", ((Label) gridProcessor.getContent(0, 1)).getText());
765         assertEquals("3", ((Label) gridProcessor.getContent(1, 1)).getText());
766         assertEquals("2", ((Label) gridProcessor.getContent(0, 2)).getText());
767         assertEquals("4", ((Label) gridProcessor.getContent(1, 2)).getText());
768         assertEquals("5", ((Label) gridProcessor.getContent(0, 3)).getText());
769         assertEquals("6", ((Label) gridProcessor.getContent(1, 3)).getText());
770     }
771
772     /**
773      * Test a simple grid consisting of ten cells in default 2 columns and 5 rows.
774      * __ __
775      * |0 |1 |
776      * |__|__|
777      * |2 |3 |
778      * |__|__|
779      * |4 |5 |
780      * |__|__|
781      * |6 |7 |
782      * |__|__|
783      * |8 |9 |
784      * |__|__|
785      */

786     public void testSimple() {
787         Grid grid = new Grid();
788         for (int i = 0; i < 10; ++i) {
789             grid.add(new Label("test"));
790         }
791
792         GridProcessor gridProcessor = new GridProcessor(grid);
793         
794         // Verify Grid size is correct.
795
assertEquals(2, gridProcessor.getColumnCount());
796         assertEquals(5, gridProcessor.getRowCount());
797     }
798     
799     /**
800      * Test a a simple Grid consisting of ten cells with one of them
801      * spanning two columns.
802      * __ __
803      * |0 |1 |
804      * |__|__|
805      * |2 |3 |
806      * |__|__|
807      * |4 |
808      * |_____|
809      * |5 |6 |
810      * |__|__|
811      * |7 |8 |
812      * |__|__|
813      * |9 |XX|
814      * |__|XX|
815      */

816     public void testSimpleWithColumnSpan() {
817         Grid grid = new Grid();
818         for (int i = 0; i < 10; ++i) {
819             Label label = new Label();
820             if (i == 4) {
821                 GridLayoutData layoutData = new GridLayoutData();
822                 layoutData.setColumnSpan(2);
823                 label.setLayoutData(layoutData);
824             }
825             grid.add(label);
826         }
827         
828         GridProcessor gridProcessor = new GridProcessor(grid);
829         
830         // Verify Grid size is correct.
831
assertEquals(2, gridProcessor.getColumnCount());
832         assertEquals(6, gridProcessor.getRowCount());
833         
834         // Verify components are at correct positions.
835
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
836         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
837         assertEquals(2, gridProcessor.getComponentIndex(0, 1));
838         assertEquals(3, gridProcessor.getComponentIndex(1, 1));
839         assertEquals(4, gridProcessor.getComponentIndex(0, 2));
840         assertEquals(4, gridProcessor.getComponentIndex(1, 2));
841         assertEquals(5, gridProcessor.getComponentIndex(0, 3));
842         assertEquals(6, gridProcessor.getComponentIndex(1, 3));
843         assertEquals(7, gridProcessor.getComponentIndex(0, 4));
844         assertEquals(8, gridProcessor.getComponentIndex(1, 4));
845         assertEquals(9, gridProcessor.getComponentIndex(0, 5));
846         assertEquals(-1, gridProcessor.getComponentIndex(1, 5));
847         
848         // Verify x-spans were untouched.
849
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
850         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
851         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
852         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
853         assertEquals(2, gridProcessor.getColumnSpan(0, 2));
854         assertEquals(2, gridProcessor.getColumnSpan(1, 2));
855         assertEquals(1, gridProcessor.getColumnSpan(0, 3));
856         assertEquals(1, gridProcessor.getColumnSpan(1, 3));
857         assertEquals(1, gridProcessor.getColumnSpan(0, 4));
858         assertEquals(1, gridProcessor.getColumnSpan(1, 4));
859         assertEquals(1, gridProcessor.getColumnSpan(0, 5));
860         assertEquals(-1, gridProcessor.getColumnSpan(1, 5));
861
862         // Verify y-spans were properly reduced.
863
assertEquals(1, gridProcessor.getRowSpan(0, 0));
864         assertEquals(1, gridProcessor.getRowSpan(1, 0));
865         assertEquals(1, gridProcessor.getRowSpan(0, 1));
866         assertEquals(1, gridProcessor.getRowSpan(1, 1));
867         assertEquals(1, gridProcessor.getRowSpan(0, 2));
868         assertEquals(1, gridProcessor.getRowSpan(1, 2));
869         assertEquals(1, gridProcessor.getRowSpan(0, 3));
870         assertEquals(1, gridProcessor.getRowSpan(1, 3));
871         assertEquals(1, gridProcessor.getRowSpan(0, 4));
872         assertEquals(1, gridProcessor.getRowSpan(1, 4));
873         assertEquals(1, gridProcessor.getRowSpan(0, 5));
874         assertEquals(-1, gridProcessor.getRowSpan(1, 5));
875     }
876     
877     /**
878      * Test a a simple Grid consisting of ten cells with one of them
879      * spanning three columns (even though only two are available).
880      * __ __
881      * |0 |1 |
882      * |__|__|
883      * |2 |3 |
884      * |__|__|__
885      * |4 |
886      * |________|
887      * |5 |6 |
888      * |__|__|
889      * |7 |8 |
890      * |__|__|
891      * |9 |XX|
892      * |__|XX|
893      */

894     public void testSimpleWithOversidedColumnSpan() {
895         Grid grid = new Grid();
896         for (int i = 0; i < 10; ++i) {
897             Label label = new Label();
898             if (i == 4) {
899                 GridLayoutData layoutData = new GridLayoutData();
900                 layoutData.setColumnSpan(3);
901                 label.setLayoutData(layoutData);
902             }
903             grid.add(label);
904         }
905         
906         GridProcessor gridProcessor = new GridProcessor(grid);
907         
908         // Verify Grid size is correct.
909
assertEquals(2, gridProcessor.getColumnCount());
910         assertEquals(6, gridProcessor.getRowCount());
911         
912         // Verify components are at correct positions.
913
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
914         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
915         assertEquals(2, gridProcessor.getComponentIndex(0, 1));
916         assertEquals(3, gridProcessor.getComponentIndex(1, 1));
917         assertEquals(4, gridProcessor.getComponentIndex(0, 2));
918         assertEquals(4, gridProcessor.getComponentIndex(1, 2));
919         assertEquals(5, gridProcessor.getComponentIndex(0, 3));
920         assertEquals(6, gridProcessor.getComponentIndex(1, 3));
921         assertEquals(7, gridProcessor.getComponentIndex(0, 4));
922         assertEquals(8, gridProcessor.getComponentIndex(1, 4));
923         assertEquals(9, gridProcessor.getComponentIndex(0, 5));
924         assertEquals(-1, gridProcessor.getComponentIndex(1, 5));
925         
926         // Verify x-spans were untouched for the most part,
927
// except for oversized cell which is reduced.
928
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
929         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
930         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
931         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
932         assertEquals(2, gridProcessor.getColumnSpan(0, 2));
933         assertEquals(2, gridProcessor.getColumnSpan(1, 2));
934         assertEquals(1, gridProcessor.getColumnSpan(0, 3));
935         assertEquals(1, gridProcessor.getColumnSpan(1, 3));
936         assertEquals(1, gridProcessor.getColumnSpan(0, 4));
937         assertEquals(1, gridProcessor.getColumnSpan(1, 4));
938         assertEquals(1, gridProcessor.getColumnSpan(0, 5));
939         assertEquals(-1, gridProcessor.getColumnSpan(1, 5));
940
941         // Verify y-spans were properly reduced.
942
assertEquals(1, gridProcessor.getRowSpan(0, 0));
943         assertEquals(1, gridProcessor.getRowSpan(1, 0));
944         assertEquals(1, gridProcessor.getRowSpan(0, 1));
945         assertEquals(1, gridProcessor.getRowSpan(1, 1));
946         assertEquals(1, gridProcessor.getRowSpan(0, 2));
947         assertEquals(1, gridProcessor.getRowSpan(1, 2));
948         assertEquals(1, gridProcessor.getRowSpan(0, 3));
949         assertEquals(1, gridProcessor.getRowSpan(1, 3));
950         assertEquals(1, gridProcessor.getRowSpan(0, 4));
951         assertEquals(1, gridProcessor.getRowSpan(1, 4));
952         assertEquals(1, gridProcessor.getRowSpan(0, 5));
953         assertEquals(-1, gridProcessor.getRowSpan(1, 5));
954     }
955     
956     /**
957      * Test staggered row-spanned cells:
958      * __ __
959      * |0 |1 |
960      * | |__|
961      * | |2 |
962      * |__| |
963      * |3 | |
964      * |__|__|
965      */

966     public void testStaggeredSpans() {
967         Grid grid = new Grid();
968         for (int i = 0; i < 4; ++i) {
969             Label label = new Label();
970             if (i % 2 == 0) {
971                 GridLayoutData gridLayoutData = new GridLayoutData();
972                 gridLayoutData.setRowSpan(2);
973                 label.setLayoutData(gridLayoutData);
974             }
975             grid.add(label);
976         }
977         
978         GridProcessor gridProcessor = new GridProcessor(grid);
979         
980         // Verify Grid size is correct.
981
assertEquals(2, gridProcessor.getColumnCount());
982         assertEquals(3, gridProcessor.getRowCount());
983         
984         // Verify components are at correct positions.
985
assertEquals(0, gridProcessor.getComponentIndex(0, 0));
986         assertEquals(1, gridProcessor.getComponentIndex(1, 0));
987         assertEquals(0, gridProcessor.getComponentIndex(0, 1));
988         assertEquals(2, gridProcessor.getComponentIndex(1, 1));
989         assertEquals(3, gridProcessor.getComponentIndex(0, 2));
990         assertEquals(2, gridProcessor.getComponentIndex(1, 2));
991         
992         // Verify y-spans were untouched.
993
assertEquals(1, gridProcessor.getColumnSpan(0, 0));
994         assertEquals(1, gridProcessor.getColumnSpan(1, 0));
995         assertEquals(1, gridProcessor.getColumnSpan(0, 1));
996         assertEquals(1, gridProcessor.getColumnSpan(1, 1));
997         assertEquals(1, gridProcessor.getColumnSpan(0, 2));
998         assertEquals(1, gridProcessor.getColumnSpan(1, 2));
999         
1000        // Verify y-spans were untouched.
1001
assertEquals(2, gridProcessor.getRowSpan(0, 0));
1002        assertEquals(1, gridProcessor.getRowSpan(1, 0));
1003        assertEquals(2, gridProcessor.getRowSpan(0, 1));
1004        assertEquals(2, gridProcessor.getRowSpan(1, 1));
1005        assertEquals(1, gridProcessor.getRowSpan(0, 2));
1006        assertEquals(2, gridProcessor.getRowSpan(1, 2));
1007    }
1008}
1009
Popular Tags