001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.lcdui;
028:
029: import com.sun.midp.i3test.*;
030:
031: /**
032: * This test is designed to test Chameleon's ItemLFImpl.lDoInternalLayout()
033: * routine only. It does not test label size or content size calculations.
034: * It tests label location and content location calculations.
035: *
036: * Internal layout for all items is handled in ItemLFImpl.
037: * lDoInternalLayout() is given label and content sizes and available
038: * width, height, it should place label and content in correct place.
039: * Here is what to expect:
040: * If label and content are empty, both bounds are set to 0.
041: * If label is empty, content is given as much space as it needs but
042: * not more than available.
043: * If content is empty, label is given as much space as it needs but
044: * not more than available.
045: * If label is a single line label (its height <= label font height) then
046: * there is an attempt to fit label, content and 3 pixel padding between
047: * them on the same line.
048: * Otherwise content is placed under label with 2 pixel horizontal padding
049: * in between.
050: *
051: * Furthermore, if label and content are on the same line than
052: * shorter part (label or content) is vertically centered
053: * against the taller one (content or label).
054: *
055: * Then if there is extra space (item can fit in smaller size than
056: * it is given) then the space is distributed based on the layout
057: * primitives set.
058: *
059: * ItemLFImpl and Item area abstract in javax.microedition.lcdui.
060: * We extend them to test lDoInternalLayout().
061: */
062:
063: public class TestItemLFImplLDoInternalLayout extends TestCase {
064:
065: public void runTests() {
066:
067: // no extra space, null label, null content
068: declare("test1");
069: checkLayout(new TestItemLFImpl(0, 0, // labelSize
070: 0, 0, // contentSize
071: 0, 0, // itemSize
072: 10), // label font height
073: 0, 0, // checked label location
074: 0, 0); // checked content location
075:
076: // no extra space, null label, non null content
077: declare("test2");
078: checkLayout(new TestItemLFImpl(0, 0, // labelSize
079: 100, 100, // contentSize
080: 100, 100, // itemSize
081: 10), // label font height
082: 0, 0, // checked label location
083: 0, 0); // checked content location
084:
085: // no extra space, non null label, null content
086: declare("test3");
087: checkLayout(new TestItemLFImpl(100, 100, // labelSize
088: 0, 0, // contentSize
089: 100, 100, // itemSize
090: 10), // label font height
091: 0, 0, // checked label location
092: 0, 0); // checked content location
093:
094: // no extra space, non null label, non null content;
095: // label and content are on the same line;
096: // label height > content height
097: declare("test4");
098: checkLayout(new TestItemLFImpl(50, 20, // labelSize
099: 50, 10, // contentSize
100: 103, 20, // itemSize
101: 20), // labelHeight to be single ln
102: 0, 0, // checked label location
103: 53, 5); // checked content location
104:
105: // no extra space, non null label, non null content
106: // label and content are on the same line
107: // label height < content height
108: declare("test5");
109: checkLayout(new TestItemLFImpl(50, 10, // labelSize
110: 50, 20, // contentSize
111: 103, 20, // itemSize
112: 10), // labelHeight to be single ln
113: 0, 5, // checked label location
114: 53, 0); // checked content location
115:
116: // no extra space, non null label, non null content
117: // label and content are on different lines (due to width)
118: // label width < content width
119: declare("test6");
120: checkLayout(new TestItemLFImpl(30, 10, // labelSize
121: 50, 20, // contentSize
122: 50, 32, // itemSize
123: 10), // labelHeight to be single ln
124: 0, 0, // checked label location
125: 0, 12); // checked content location
126:
127: // no extra space, non null label, non null content
128: // label and content are on different lines (due to multiline label)
129: // label width < content width
130: declare("test7");
131: checkLayout(new TestItemLFImpl(30, 20, // labelSize
132: 50, 30, // contentSize
133: 50, 52, // itemSize
134: 10), // labelHeight to be single ln
135: 0, 0, // checked label location
136: 0, 22); // checked content location
137:
138: // no extra space, non null label, non null content
139: // label and content are on different lines (due to width)
140: // label width > content width
141: declare("test8");
142: checkLayout(new TestItemLFImpl(50, 10, // labelSize
143: 30, 20, // contentSize
144: 50, 32, // itemSize
145: 10), // labelHeight to be single ln
146: 0, 0, // checked label location
147: 0, 12); // checked content location
148:
149: // no extra space, non null label, non null content
150: // label and content are on different lines (due to mulitline label)
151: // label width > content width
152: declare("test9");
153: checkLayout(new TestItemLFImpl(50, 20, // labelSize
154: 30, 30, // contentSize
155: 50, 52, // itemSize
156: 10), // labelHeight to be single ln
157: 0, 0, // checked label location
158: 0, 22); // checked content location
159:
160: // ************ more space HORIZONTALLY **************************
161: // ****************************************************************
162:
163: // there IS extra space horizontally,
164: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
165: // null label, non null content
166: declare("test10");
167: checkLayout(new TestItemLFImpl(0, 0, // labelSize
168: 50, 50, // contentSize
169: 100, 50, // itemSize
170: 10), // labelHeight to be single ln
171: 0, 0, // checked label location
172: 0, 0); // checked content location
173:
174: // there IS extra space horizontally,
175: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
176: // non null label, null content
177: declare("test11");
178: checkLayout(new TestItemLFImpl(50, 50, // labelSize
179: 0, 0, // contentSize
180: 100, 50, // itemSize
181: 10), // labelHeight to be single ln
182: 0, 0, // checked label location
183: 0, 0); // checked content location
184:
185: // there IS extra space horizontally,
186: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
187: // non null label, non null content
188: // label and content on the same line;
189: // label height > content height
190: declare("test11");
191: checkLayout(new TestItemLFImpl(47, 20, // labelSize
192: 50, 10, // contentSize
193: 200, 20, // itemSize
194: 20), // labelHeight to be single ln
195: 0, 0, // checked label location
196: 50, 5); // checked content location
197:
198: // there IS extra space horizontally,
199: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
200: // non null label, non null content
201: // label and content on the same line
202: // label height < content height
203: declare("test12");
204: checkLayout(new TestItemLFImpl(47, 10, // labelSize
205: 50, 20, // contentSize
206: 200, 20, // itemSize
207: 10), // labelHeight to be single ln
208: 0, 5, // checked label location
209: 50, 0); // checked content location
210:
211: // there IS extra space horizontally,
212: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
213: // non null label, non null content
214: // label and content on different lines
215: // label width < content width
216: declare("test13");
217: checkLayout(new TestItemLFImpl(30, 20, // labelSize
218: 50, 20, // contentSize
219: 100, 32, // itemSize
220: 10), // labelHeight to be single ln
221: 0, 0, // checked label location
222: 0, 22); // checked content location
223:
224: // there IS extra space horizontally,
225: // LAYOUT_CENTER and LAYOUT_RIGHT are NOT set
226: // non null label, non null content
227: // label and content on different lines
228: // label width > content width
229: declare("test14");
230: checkLayout(new TestItemLFImpl(50, 20, // labelSize
231: 30, 20, // contentSize
232: 100, 32, // itemSize
233: 10), // labelHeight to be single ln
234: 0, 0, // checked label location
235: 0, 22); // checked content location
236:
237: // **********************************************
238:
239: // there IS extra space horizontally,
240: // LAYOUT_CENTER is set
241: // non null label, non null content
242: // label and content on the same line;
243: // label height > content height
244: declare("test15");
245: checkLayout(new TestItemLFImpl(47, 20, // labelSize
246: 50, 10, // contentSize
247: 200, 20, // itemSize
248: 20, // labelHeight to be single ln
249: Item.LAYOUT_CENTER), 50, 0, // checked label location
250: 100, 5); // checked content location
251:
252: // there IS extra space horizontally,
253: // LAYOUT_CENTER is set
254: // non null label, non null content
255: // label and content on the same line
256: // label height < content height
257: declare("test16");
258: checkLayout(new TestItemLFImpl(47, 10, // labelSize
259: 50, 20, // contentSize
260: 200, 20, // itemSize
261: 10, // labelHeight to be single ln
262: Item.LAYOUT_CENTER), 50, 5, // checked label location
263: 100, 0); // checked content location
264:
265: // there IS extra space horizontally,
266: // LAYOUT_CENTER is set
267: // non null label, non null content
268: // label and content on different lines
269: // label width < content width
270: declare("test17");
271: checkLayout(new TestItemLFImpl(30, 20, // labelSize
272: 50, 20, // contentSize
273: 100, 32, // itemSize
274: 10, // labelHeight to be single ln
275: Item.LAYOUT_CENTER), 35, 0, // checked label location
276: 25, 22); // checked content location
277:
278: // there IS extra space horizontally,
279: // LAYOUT_CENTER is set
280: // non null label, non null content
281: // label and content on different lines
282: // label width > content width
283: declare("test18");
284: checkLayout(new TestItemLFImpl(50, 20, // labelSize
285: 30, 20, // contentSize
286: 100, 32, // itemSize
287: 10, // labelHeight to be single ln
288: Item.LAYOUT_CENTER), 25, 0, // checked label location
289: 35, 22); // checked content location
290:
291: // *****************
292:
293: // there IS extra space horizontally,
294: // LAYOUT_RIGHT is set
295: // non null label, non null content
296: // label and content on the same line;
297: // label height > content height
298: declare("test19");
299: checkLayout(new TestItemLFImpl(47, 20, // labelSize
300: 50, 10, // contentSize
301: 200, 20, // itemSize
302: 20, // labelHeight to be single ln
303: Item.LAYOUT_RIGHT), 100, 0, // checked label location
304: 150, 5); // checked content location
305:
306: // there IS extra space horizontally,
307: // LAYOUT_RIGHT is set
308: // non null label, non null content
309: // label and content on the same line
310: // label height < content height
311: declare("test20");
312: checkLayout(new TestItemLFImpl(47, 10, // labelSize
313: 50, 20, // contentSize
314: 200, 20, // itemSize
315: 10, // labelHeight to be single ln
316: Item.LAYOUT_RIGHT), 100, 5, // checked label location
317: 150, 0); // checked content location
318:
319: // there IS extra space horizontally,
320: // LAYOUT_RIGHT is set
321: // non null label, non null content
322: // label and content on different lines
323: // label width < content width
324: declare("test21");
325: checkLayout(new TestItemLFImpl(30, 20, // labelSize
326: 50, 20, // contentSize
327: 100, 32, // itemSize
328: 10, // labelHeight to be single ln
329: Item.LAYOUT_RIGHT), 50, 0, // checked label location
330: 50, 22); // checked content location
331:
332: // there IS extra space horizontally,
333: // LAYOUT_RIGHT is set
334: // non null label, non null content
335: // label and content on different lines
336: // label width > content width
337: declare("test22");
338: checkLayout(new TestItemLFImpl(30, 20, // labelSize
339: 50, 20, // contentSize
340: 100, 32, // itemSize
341: 10, // labelHeight to be single ln
342: Item.LAYOUT_RIGHT), 50, 0, // checked label location
343: 50, 22); // checked content location
344:
345: // ************ more space VERTICALLY **************************
346: // ****************************************************************
347:
348: // there IS extra space vertically,
349: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
350: // null label, non null content
351: declare("test23");
352: checkLayout(new TestItemLFImpl(0, 0, // labelSize
353: 50, 50, // contentSize
354: 50, 100, // itemSize
355: 10), // labelHeight to be single ln
356: 0, 0, // checked label location
357: 0, 0); // checked content location
358:
359: // there IS extra space vertically,
360: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
361: // non null label, null content
362: declare("test24");
363: checkLayout(new TestItemLFImpl(50, 50, // labelSize
364: 0, 0, // contentSize
365: 50, 100, // itemSize
366: 10), // labelHeight to be single ln
367: 0, 0, // checked label location
368: 0, 0); // checked content location
369:
370: // there IS extra space vertically,
371: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
372: // non null label, non null content
373: // label and content on the same line;
374: // label height > content height
375: declare("test25");
376: checkLayout(new TestItemLFImpl(47, 20, // labelSize
377: 50, 10, // contentSize
378: 100, 100, // itemSize
379: 20), // labelHeight to be single ln
380: 0, 0, // checked label location
381: 50, 5); // checked content location
382:
383: // there IS extra space vertically,
384: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
385: // non null label, non null content
386: // label and content on the same line
387: // label height < content height
388: declare("test26");
389: checkLayout(new TestItemLFImpl(47, 10, // labelSize
390: 50, 20, // contentSize
391: 100, 100, // itemSize
392: 10), // labelHeight to be single ln
393: 0, 5, // checked label location
394: 50, 0); // checked content location
395:
396: // there IS extra space vertically,
397: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
398: // non null label, non null content
399: // label and content are on different lines
400: // label width < content width
401: declare("test27");
402: checkLayout(new TestItemLFImpl(30, 20, // labelSize
403: 50, 20, // contentSize
404: 50, 100, // itemSize
405: 10), // labelHeight to be single ln
406: 0, 0, // checked label location
407: 0, 22); // checked content location
408:
409: // there IS extra space vertically,
410: // LAYOUT_VCENTER and LAYOUT_BOTTOM are NOT set
411: // non null label, non null content
412: // label and content are on different lines
413: // label width > content width
414: declare("test28");
415: checkLayout(new TestItemLFImpl(50, 20, // labelSize
416: 30, 20, // contentSize
417: 50, 100, // itemSize
418: 10), // labelHeight to be single ln
419: 0, 0, // checked label location
420: 0, 22); // checked content location
421:
422: // **********************************************
423:
424: // there IS extra space vertically,
425: // LAYOUT_VCENTER is set
426: // non null label, non null content
427: // label and content on the same line;
428: // label height > content height
429: declare("test29");
430: checkLayout(new TestItemLFImpl(47, 20, // labelSize
431: 50, 10, // contentSize
432: 100, 100, // itemSize
433: 20, // labelHeight to be single ln
434: Item.LAYOUT_VCENTER), 0, 40, // checked label location
435: 50, 45); // checked content location
436:
437: // there IS extra space vertically,
438: // LAYOUT_VCENTER is set
439: // non null label, non null content
440: // label and content on the same line
441: // label height < content height
442: declare("test30");
443: checkLayout(new TestItemLFImpl(47, 10, // labelSize
444: 50, 20, // contentSize
445: 100, 100, // itemSize
446: 10, // labelHeight to be single ln
447: Item.LAYOUT_VCENTER), 0, 45, // checked label location
448: 50, 40); // checked content location
449:
450: // there IS extra space vertically,
451: // LAYOUT_VCENTER is set
452: // non null label, non null content
453: // label and content on different lines
454: // label width < content width
455: declare("test31");
456: checkLayout(new TestItemLFImpl(30, 18, // labelSize
457: 50, 20, // contentSize
458: 50, 100, // itemSize
459: 10, // labelHeight to be single ln
460: Item.LAYOUT_VCENTER), 0, 30, // checked label location
461: 0, 50); // checked content location
462:
463: // there IS extra space vertically,
464: // LAYOUT_VCENTER is set
465: // non null label, non null content
466: // label and content on different lines
467: // label width > content width
468: declare("test32");
469: checkLayout(new TestItemLFImpl(50, 18, // labelSize
470: 30, 20, // contentSize
471: 50, 100, // itemSize
472: 10, // labelHeight to be single ln
473: Item.LAYOUT_VCENTER), 0, 30, // checked label location
474: 0, 50); // checked content location
475:
476: // *************************************************************
477:
478: // there IS extra space vertically,
479: // LAYOUT_BOTTOM is set
480: // non null label, non null content
481: // label and content are on the same line;
482: // label height > content height
483: declare("test33");
484: checkLayout(new TestItemLFImpl(47, 20, // labelSize
485: 50, 10, // contentSize
486: 100, 100, // itemSize
487: 20, // labelHeight to be single ln
488: Item.LAYOUT_BOTTOM), 0, 80, // checked label location
489: 50, 85); // checked content location
490:
491: // there IS extra space vertically,
492: // LAYOUT_BOTTOM is set
493: // non null label, non null content
494: // label and content on the same line
495: // label height < content height
496: declare("test34");
497: checkLayout(new TestItemLFImpl(47, 10, // labelSize
498: 50, 20, // contentSize
499: 100, 100, // itemSize
500: 10, // labelHeight to be single ln
501: Item.LAYOUT_BOTTOM), 0, 85, // checked label location
502: 50, 80); // checked content location
503:
504: // there IS extra space vertically,
505: // LAYOUT_BOTTOM is set
506: // non null label, non null content
507: // label and content on different lines
508: // label width < content width
509: declare("test35");
510: checkLayout(new TestItemLFImpl(30, 18, // labelSize
511: 50, 20, // contentSize
512: 50, 100, // itemSize
513: 10, // labelHeight to be single ln
514: Item.LAYOUT_BOTTOM), 0, 60, // checked label location
515: 0, 80); // checked content location
516:
517: // there IS extra space vertically,
518: // LAYOUT_BOTTOM is set
519: // non null label, non null content
520: // label and content on different lines
521: // label width > content width
522: declare("test36");
523: checkLayout(new TestItemLFImpl(30, 18, // labelSize
524: 50, 20, // contentSize
525: 50, 100, // itemSize
526: 10, // labelHeight to be single ln
527: Item.LAYOUT_BOTTOM), 0, 60, // checked label location
528: 0, 80); // checked content location
529:
530: // ***** more space VERTICALLY and HORIZONTALLY ******************
531: // ****************************************************************
532:
533: // there IS extra space vertically and horizontally
534: // LAYOUT_CENTER & LAYOUT_VCENTER is set
535: // non null label, non null content
536: // label and content are on the same line;
537: declare("test37");
538: checkLayout(new TestItemLFImpl(47, 20, // labelSize
539: 50, 10, // contentSize
540: 200, 100, // itemSize
541: 20, // labelHeight to be single ln
542: Item.LAYOUT_CENTER | Item.LAYOUT_VCENTER), 50, 40, // checked label location
543: 100, 45); // checked content location
544:
545: // there IS extra space vertically and horizontally,
546: // LAYOUT_CENTER & LAYOUT_VCENTER is set
547: // non null label, non null content
548: // label and content are on different lines
549: declare("test38");
550: checkLayout(new TestItemLFImpl(30, 18, // labelSize
551: 50, 20, // contentSize
552: 100, 100, // itemSize
553: 10, // labelHeight to be single ln
554: Item.LAYOUT_CENTER | Item.LAYOUT_VCENTER), 35, 30, // checked label location
555: 25, 50); // checked content location
556:
557: // there IS extra space vertically and horizontally
558: // LAYOUT_RIGHT & LAYOUT_BOTTOM is set
559: // non null label, non null content
560: // label and content are on the same line;
561: declare("test39");
562: checkLayout(new TestItemLFImpl(47, 20, // labelSize
563: 50, 10, // contentSize
564: 200, 100, // itemSize
565: 20, // labelHeight to be single ln
566: Item.LAYOUT_RIGHT | Item.LAYOUT_BOTTOM), 100, 80, // checked label location
567: 150, 85); // checked content location
568:
569: // there IS extra space vertically and horizontally,
570: // LAYOUT_RIGHT & LAYOUT_BOTTOM is set
571: // non null label, non null content
572: // label and content are on different lines
573: declare("test40");
574: checkLayout(new TestItemLFImpl(30, 18, // labelSize
575: 50, 20, // contentSize
576: 100, 100, // itemSize
577: 10, // labelHeight to be single ln
578: Item.LAYOUT_RIGHT | Item.LAYOUT_BOTTOM), 50, 60, // checked label location
579: 50, 80); // checked content location
580: }
581:
582: private void checkLayout(TestItemLFImpl item, int labelX,
583: int labelY, int contentX, int contetnY) {
584: item.checkLayout(labelX, labelY, contentX, contetnY);
585: }
586:
587: class TestItem extends Item {
588: TestItem() {
589: super (null);
590: }
591: }
592:
593: class TestItemLFImpl extends ItemLFImpl {
594: TestItemLFImpl(int labelWidth, int labelHeight,
595: int contentWidth, int contentHeight, int w, int h,
596: int labelFontHeight) {
597: this (labelWidth, labelHeight, contentWidth, contentHeight,
598: w, h, labelFontHeight, Item.LAYOUT_DEFAULT);
599: }
600:
601: TestItemLFImpl(int labelWidth, int labelHeight,
602: int contentWidth, int contentHeight, int w, int h,
603: int labelFontHeight, int layout) {
604: super (new TestItem());
605: item.itemLF = this ;
606:
607: item.setLayout(layout);
608:
609: this .labelFontHeight = labelFontHeight;
610:
611: labelBounds[X] = labelBounds[Y] = -1;
612: labelBounds[WIDTH] = labelWidth;
613: labelBounds[HEIGHT] = labelHeight;
614:
615: contentBounds[X] = contentBounds[Y] = -1;
616: contentBounds[WIDTH] = contentWidth;
617: contentBounds[HEIGHT] = contentHeight;
618:
619: // We assume that labelBounds and contentBounds
620: // passed into lDoInternalLayout() are correct
621: // that is why we need cachedWidth == w
622: cachedWidth = w;
623:
624: lDoInternalLayout(labelBounds, contentBounds, w, h);
625: }
626:
627: void checkLayout(int labelX, int labelY, int contentX,
628: int contentY) {
629:
630: assertEquals(labelBounds[X], labelX);
631: assertEquals(labelBounds[Y], labelY);
632: assertEquals(contentBounds[X], contentX);
633: assertEquals(contentBounds[Y], contentY);
634: }
635:
636: // overriding this function removes dependency on the
637: // font height actually used on the platform.
638: boolean labelAndContentOnSameLine(int labelHeight) {
639: return labelHeight <= labelFontHeight;
640: }
641:
642: int getHorizontalPad() {
643: return 3;
644: }
645:
646: int getVerticalPad() {
647: return 2;
648: }
649:
650: private int labelFontHeight;
651: }
652: }
|