001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025:
026: package javax.microedition.lcdui;
027:
028: import com.sun.midp.i3test.*;
029: import javax.microedition.midlet.*;
030:
031: public class CustomItemTest extends TestCase {
032: FormLFImpl ownerLFImpl = null;
033: int vpY_beforeScroll = 0;
034: int vpY_AfterScroll = 0;
035:
036: void testCustomItemInternalTraversal() {
037: Display display = DisplayTestUtils.createDisplay();
038: Form mainForm = new Form("CustomItem Demo");
039: CustomItemImpl customItem = new CustomItemImpl("CustomItem",
040: 80, display, false);
041: mainForm.append(customItem);
042: display.setCurrent(mainForm);
043:
044: // make sure the form is visible
045: try {
046: Thread.sleep(2000);
047: } catch (Exception e) {
048: }
049:
050: assertSame(display.getCurrent(), mainForm);
051:
052: CustomItemLF customItemLF = customItem.customItemLF;
053: Item myitem = ((CustomItemLFImpl) customItemLF).item;
054:
055: // call keypress with "up" keyCode -1
056: ((CustomItemLFImpl) customItemLF).uCallKeyPressed(-1);
057: ((CustomItemLFImpl) customItemLF).uCallKeyReleased(-1);
058:
059: assertTrue("traverse not called", customItem.traverse_flag);
060: Display.displayManagerImpl.uRequestForegroundNone();
061: assertSame(Display.displayManagerImpl.noForeground,
062: Display.displayManagerImpl.foreground);
063: }
064:
065: /**
066: * check that the viewport scrolls to make the
067: * returned vis_Rect from traverse method visible
068: *
069: * This test is specific to platform_widget. Enable it only
070: * for platform-widget if possible.
071: *
072: void testCustomItemInternalTraversal2() {
073: Display display = DisplayTestUtils.createDisplay();
074:
075: Form mainForm = new Form("CustomItem 2 Demo");
076: TextField tf = new TextField("Text Field", "some text", 100, 0);
077:
078: CustomItemImpl customItem1 =
079: new CustomItemImpl("CustomItem1", 80, display, false);
080: customItem1.setLayout(Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_BEFORE |
081: Item.LAYOUT_NEWLINE_AFTER);
082:
083: CustomItemImpl customItem2 =
084: new CustomItemImpl("CustomItem2", 80, display, false);
085: customItem2.setLayout(Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_BEFORE |
086: Item.LAYOUT_NEWLINE_AFTER);
087:
088: StringItem si = new StringItem("a string", "some text");
089:
090: mainForm.append(tf);
091: mainForm.append(customItem1);
092: mainForm.append(si);
093: mainForm.append(customItem2);
094:
095: display.setCurrentItem(customItem2);
096:
097: // make sure the form is visible
098: try {
099: Thread.sleep(2000);
100: } catch (Exception e) {}
101:
102: assertSame(display.getCurrent(), mainForm);
103:
104: // part of the custom item is made visible
105: CustomItemLF customItemLF = customItem2.customItemLF;
106: Item myitem = ((CustomItemLFImpl)customItemLF).item;
107:
108: Screen owner = myitem.owner;
109: if (owner != null) {
110: ownerLFImpl =
111: (FormLFImpl)owner.getLF();
112: }
113:
114: // vpY1 the y coordinate of the top left visible pixel
115: // current scroll position
116: vpY_beforeScroll = ownerLFImpl.getScrollPosition0();
117:
118: // call key down
119: // KEYCODE_DOWN Value="-2"
120: ((CustomItemLFImpl)customItemLF).uCallKeyPressed(-2);
121: ((CustomItemLFImpl)customItemLF).uCallKeyReleased(-2);
122:
123: assertTrue("traverse not called: first call",
124: customItem2.traverse_flag);
125:
126:
127: ((CustomItemLFImpl)customItemLF).uCallKeyPressed(-2);
128: ((CustomItemLFImpl)customItemLF).uCallKeyReleased(-2);
129:
130: assertTrue("traverse not called: second call",
131: customItem2.traverse_flag);
132:
133: // check that the viewport scrolls to make the
134: // returned vis_Rect visible
135: // vpY1 the y coordinate of the top left visible pixel
136:
137: vpY_AfterScroll = ownerLFImpl.getScrollPosition0();
138:
139: boolean screenScrolled =
140: vpY_AfterScroll > vpY_beforeScroll ? true : false;
141: assertTrue("screen not Scrolled", screenScrolled);
142: Display.displayManagerImpl.uRequestForegroundNone();
143: assertSame(Display.displayManagerImpl.noForeground,
144: Display.displayManagerImpl.foreground);
145: }
146: */
147:
148: // main test driver
149: public void runTests() {
150: declare("testCustomItemInternalTraversal");
151: testCustomItemInternalTraversal();
152:
153: /*
154: * This test is platform_widget specific.
155: *
156: declare("testCustomItemInternalTraversal2");
157: testCustomItemInternalTraversal2();
158: */
159: }
160: }
161:
162: class CustomItemImpl extends CustomItem {
163: private int size, rgb_bgrnd, rgb_fgrnd, rgb_highlighted_bgrnd,
164: rgb_highlighted_fgrnd;
165: private int startIndex = 0;
166: private int highlightedIndex = 0;
167: private int currentIndex = 0;
168: private boolean initialTraversal = false;
169: private int[] highlightedRect = new int[4];
170: int[] visRect = new int[4];
171: boolean traverse_flag = false;
172:
173: private boolean debugFlag = false;
174: private String[] content = { "String0", "String1", "String2",
175: "String3", "String4", "String5", "String6", "String7",
176: "String8", "String9" };
177:
178: public CustomItemImpl(String label, int size, Display display,
179: boolean debug) {
180: super (label);
181: debugFlag = debug;
182: rgb_bgrnd = display.getColor(Display.COLOR_BACKGROUND);
183: rgb_highlighted_bgrnd = display
184: .getColor(Display.COLOR_HIGHLIGHTED_BACKGROUND);
185: rgb_fgrnd = display.getColor(Display.COLOR_FOREGROUND);
186: rgb_highlighted_fgrnd = display
187: .getColor(Display.COLOR_HIGHLIGHTED_FOREGROUND);
188:
189: this .size = size;
190: highlightedRect[0] = 1;
191: highlightedRect[1] = 1;
192: highlightedRect[2] = 55;
193: highlightedRect[3] = 14;
194: }
195:
196: protected int getMinContentHeight() {
197: return size;
198: }
199:
200: protected int getMinContentWidth() {
201: return size;
202: }
203:
204: protected int getPrefContentHeight(int width) {
205: if (debugFlag) {
206: System.err.println("getPrefContentHeight called. width = "
207: + width);
208: }
209: return size;
210: }
211:
212: protected int getPrefContentWidth(int height) {
213: if (debugFlag) {
214: System.err.println("getPrefContentWidth called. height = "
215: + height);
216: }
217: return size;
218: }
219:
220: protected void paint(Graphics g, int w, int h) {
221: if (debugFlag) {
222: System.err.println("\n\n" + getLabel()
223: + "'s paint() called");
224: System.err.println("w = " + w + " , h = " + h);
225: }
226: g.setColor(rgb_bgrnd);
227: g.fillRect(0, 0, w, h);
228: g.setColor(rgb_fgrnd);
229: g.drawRect(0, 0, w - 1, h - 1);
230: Font f = g.getFont();
231:
232: for (int i = 0; i < 5; i++) {
233: g.drawString(content[startIndex + i], 1, 1 + i
234: * f.getHeight(), Graphics.TOP | Graphics.LEFT);
235: }
236:
237: if (traverse_flag) {
238: // change the highlighting
239: g.setColor(rgb_highlighted_bgrnd);
240: g.fillRect(highlightedRect[0], highlightedRect[1],
241: highlightedRect[2], highlightedRect[3]);
242: g.setColor(rgb_highlighted_fgrnd);
243: g.drawString(content[currentIndex], highlightedRect[0],
244: highlightedRect[1], Graphics.TOP | Graphics.LEFT);
245: }
246:
247: if (debugFlag) {
248: System.err.println("highlightedRect[0] = "
249: + highlightedRect[0] + "\nhighlightedRect[1] = "
250: + highlightedRect[1] + "\nhighlightedRect[2] = "
251: + highlightedRect[2] + "\nhighlightedRect[3] = "
252: + highlightedRect[3]);
253:
254: }
255: }
256:
257: protected void traverseOut() {
258: if (debugFlag) {
259: System.err.println("*** traverseOut was called ***");
260: }
261: traverse_flag = false;
262: repaint();
263: }
264:
265: protected boolean traverse(int dir, int viewportWidth,
266: int viewportHeight, int[] visRect_inout) {
267:
268: if ((!traverse_flag) && (dir != 0)) {
269: traverse_flag = true;
270: return true;
271: }
272:
273: visRect[0] = visRect_inout[0];
274: visRect[1] = visRect_inout[1];
275: visRect[2] = visRect_inout[2];
276: visRect[3] = visRect_inout[3];
277:
278: if (debugFlag) {
279: System.err.println("\n\n" + "*** traverse was called ***");
280:
281: System.err.println("dir = " + dir);
282: System.err.println("viewportWidth = " + viewportWidth);
283: System.err.println("viewportHeight = " + viewportHeight);
284: System.err
285: .println("visRect_inout[0] = " + visRect_inout[0]);
286:
287: System.err
288: .println("visRect_inout[1] = " + visRect_inout[1]);
289:
290: System.err
291: .println("visRect_inout[2] = " + visRect_inout[2]);
292:
293: System.err
294: .println("visRect_inout[3] = " + visRect_inout[3]);
295: }
296:
297: if (dir == Canvas.DOWN) {
298: if (startIndex < 5) {
299: if (highlightedIndex < 4) {
300: highlightedRect[1] += 14;
301: highlightedIndex += 1;
302: } else {
303: startIndex += 1;
304: }
305: currentIndex += 1;
306:
307: } else {
308: return false;
309: }
310: }
311:
312: if (dir == Canvas.UP) {
313: if (highlightedIndex > 0) {
314: highlightedRect[1] -= 14;
315: highlightedIndex -= 1;
316: } else if (currentIndex > 0) {
317: startIndex -= 1;
318: } else {
319: return false;
320: }
321: currentIndex -= 1;
322: }
323: // repaint();
324: return true;
325: }
326:
327: }
|