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: import com.sun.midp.configurator.Constants;
031:
032: public class TestStringItemNoLabelSizing extends TestCase {
033:
034: private final int ITEM_PAD = 4;
035: private final int I_W_TEXT = 3;
036: private final int TEXT_FONT_H = 15;
037:
038: // make a string with count repetitions of ch
039: private String makeString(int count, char ch) {
040: char arr[] = new char[count];
041: for (int i = 0; i < count; i++) {
042: arr[i] = ch;
043: }
044: return new String(arr);
045: }
046:
047: private String makeStringThatFits(char ch, int char_width, int width) {
048: char arr[] = new char[width / char_width];
049: for (int i = 0; i < arr.length; i++) {
050: arr[i] = ch;
051: }
052: return new String(arr);
053: }
054:
055: private int getW(StringItem strItem) {
056: return strItem.stringItemLF.lGetPreferredWidth(-1);
057: }
058:
059: private int getH(StringItem strItem) {
060: return strItem.stringItemLF.lGetPreferredHeight(-1);
061: }
062:
063: private void checkSizes(StringItem si, int expectedW, int expectedH) {
064: int w = getW(si);
065: int h = getH(si);
066: assertEquals("w: " + expectedW + " != " + w, expectedW, w);
067: assertEquals("h: " + expectedH + " != " + h, expectedH, h);
068: }
069:
070: private void checkNonemptySizes(String content, int expectedConW,
071: int expectedConH) {
072: checkSizes(new StringItem(null, content), ITEM_PAD
073: + expectedConW + ITEM_PAD, ITEM_PAD + expectedConH
074: + ITEM_PAD);
075: }
076:
077: public void runTests() {
078:
079: // null content
080: declare("testNull");
081: checkSizes(new StringItem(null, null), 0, 0);
082:
083: // empty content
084: declare("testEmpty");
085: checkSizes(new StringItem(null, ""), 0, 0);
086:
087: // nonemtpy content, no newline,fits on one line
088: declare("testI");
089: checkNonemptySizes("i", I_W_TEXT, TEXT_FONT_H);
090:
091: // nonempty content, no newline, just fits on one line
092: declare("test70i");
093: checkNonemptySizes(makeString(70, 'i'), 70 * I_W_TEXT,
094: TEXT_FONT_H);
095:
096: // nonempty content, has newline => multiline
097: // content consists just of "\n" (width is 0 but height is not)
098: declare("test\\n");
099: checkNonemptySizes("\n", 0, 2 * TEXT_FONT_H);
100:
101: // nonempty content, has newline => multiline
102: // content consists of "\ni" (first line is empty)
103: declare("test\\ni");
104: checkNonemptySizes("\ni", I_W_TEXT, 2 * TEXT_FONT_H);
105:
106: // nonempty content, has newline => multiline
107: // content consists of "i\n" (second line is empty)
108: declare("testI\\n");
109: checkNonemptySizes("i\n", I_W_TEXT, 2 * TEXT_FONT_H);
110:
111: // nonempty content, has newline => multiline
112: // content consists of "i\nii" (second line is longer)
113: declare("testI\\nii");
114: checkNonemptySizes("i\nii", 2 * I_W_TEXT, 2 * TEXT_FONT_H);
115:
116: // nonempty content, has newline => multiline
117: // content consists of "ii\ni" (first line is longer)
118: declare("testIi\\ni");
119: checkNonemptySizes("ii\ni", 2 * I_W_TEXT, 2 * TEXT_FONT_H);
120:
121: // nonempty very long content without newline which has to wrap
122: declare("test75i");
123: String strFitsInWidth = makeStringThatFits('i', I_W_TEXT,
124: Constants.NORMALWIDTH - Constants.VERT_SCROLLBAR_WIDTH
125: - ITEM_PAD - ITEM_PAD);
126:
127: checkNonemptySizes(strFitsInWidth + "ii", strFitsInWidth
128: .length()
129: * I_W_TEXT, 2 * TEXT_FONT_H);
130: }
131: }
|