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: import javax.microedition.midlet.*;
032: import com.sun.midp.midlet.MIDletStateHandler;
033: import com.sun.midp.util.DummyMIDlet;
034: import com.sun.midp.util.DummyMIDlet2;
035: import com.sun.midp.lcdui.*;
036:
037: /**
038: * This test is designed to verify values returned by Displayable.isShown.
039: * According to the spec isShown should be true only
040: * Displayable is visible in the foreground and no system screen/menu
041: * is up.
042: */
043: public class TestDisplayPreempt extends TestCase {
044:
045: MIDletStateHandler msHandler;
046: DisplayEventHandlerImpl deHandler;
047: MIDlet midlet[] = new MIDlet[2];
048: Display display[] = new Display[2];
049: Display preempt = null;
050:
051: SystemAlert A[][] = new SystemAlert[2][2];
052: Form F[][] = new Form[2][2];
053: Command C[][][] = new Command[2][2][3];
054:
055: boolean isShown(Display d, Displayable s) {
056: return d.isShown(s.displayableLF);
057: }
058:
059: public static void sleep(int time) {
060: try {
061: Thread.sleep(time);
062: } catch (Exception e) {
063: }
064: }
065:
066: public void runTests() {
067: int i, j;
068:
069: for (i = 0; i < 2; i++) {
070: for (j = 0; j < 2; j++) {
071: String suffix = "_" + i + "_" + j;
072: //A[i][j] = new Alert("A" + suffix);
073: F[i][j] = new Form("F" + suffix);
074:
075: C[i][j][0] = new Command("SCREEN" + suffix,
076: Command.SCREEN, 1);
077: C[i][j][1] = new Command("BACK" + suffix, Command.BACK,
078: 1);
079: C[i][j][2] = new Command("ITEM" + suffix, Command.ITEM,
080: 1);
081:
082: F[i][j].addCommand(C[i][j][0]);
083: F[i][j].addCommand(C[i][j][1]);
084: F[i][j].addCommand(C[i][j][2]);
085: }
086: }
087:
088: msHandler = MIDletStateHandler.getMidletStateHandler();
089: deHandler = Display.displayEventHandlerImpl;
090:
091: // -------------------------------------------------------
092:
093: test3();
094: test1();
095: test2();
096:
097: // -------------------------------------------------------
098:
099: }
100:
101: /**
102: * starts DummyMIDlet
103: */
104: protected void startMIDlet0() {
105: try {
106: // Start a new instance of DummyMIDlet
107: msHandler.startMIDlet("com.sun.midp.util.DummyMIDlet",
108: "DummyMIDlet");
109:
110: // Wait for async request to be processed
111: while (DummyMIDlet.midlet == null) {
112: try {
113: Thread.sleep(100);
114: } catch (InterruptedException ie) {
115: }
116: }
117: midlet[0] = DummyMIDlet.midlet;
118: display[0] = Display.getDisplay(midlet[0]);
119: display[0].setCurrent(null);
120:
121: } catch (Exception e) {
122: e.printStackTrace();
123: }
124: }
125:
126: /**
127: * starts DummyMIDlet2
128: */
129: protected void startMIDlet1() {
130: try {
131: // Start a new instance of DummyMIDlet
132: msHandler.startMIDlet("com.sun.midp.util.DummyMIDlet2",
133: "DummyMIDlet2");
134:
135: // Wait for async request to be processed
136: while (DummyMIDlet2.midlet == null) {
137: try {
138: Thread.sleep(100);
139: } catch (InterruptedException ie) {
140: }
141: }
142: midlet[1] = DummyMIDlet2.midlet;
143: display[1] = Display.getDisplay(midlet[1]);
144: display[1].setCurrent(null);
145:
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: }
150:
151: /**
152: * Terminates DummyMIDlet
153: */
154: protected void endMIDlet0() {
155: midlet[0].notifyDestroyed();
156: // Since we are calling MIDlet.notifyDestroyed(), MIDlet.destroyApp()
157: // will not be called by System. So make it up here.
158: try {
159: ((DummyMIDlet) midlet[0]).destroyApp(true);
160: } catch (MIDletStateChangeException ignore) {
161: }
162: }
163:
164: /**
165: * Terminates DummyMIDlet2
166: */
167: protected void endMIDlet1() {
168: midlet[1].notifyDestroyed();
169: // Since we are calling MIDlet.notifyDestroyed(), MIDlet.destroyApp()
170: // will not be called by System. So make it up here.
171: try {
172: ((DummyMIDlet2) midlet[1]).destroyApp(true);
173: } catch (MIDletStateChangeException ignore) {
174: }
175: }
176:
177: /**
178: * test checks how SystemAlert preempts headless midlet
179: */
180: protected void test1() {
181: declare("TestDisplayPreempt 1");
182:
183: // start midlet w/o screens - no modal dialogs started
184: startMIDlet0();
185: display[0].setCurrent(null);
186: midlet[0].resumeRequest();
187: sleep(500);
188: // modal dialog not yet exists - empty screen is in background
189: assertFalse("MIDlet before Alert : Display 0 has foreground",
190: display[0].hasForeground);
191: sleep(500);
192: // create modal dialog - midlet w/o screens exists
193: A[0][1] = new SystemAlert(deHandler, "A_0_1",
194: "Alert with MIDlet", null, AlertType.INFO);
195: A[0][1].run();
196: A[0][1].setTimeout(1500);
197: // preempt = deHandler.preemptingDisplay.getDisplay();
198: sleep(500);
199: // modal dialog is active ... - empty screen is hidden
200: // assertTrue("MIDlet with Alert : Preempt Display has foreground", preempt.hasForeground);
201: assertFalse("MIDlet with Alert : Display 0 has foreground",
202: display[0].hasForeground);
203: sleep(1500);
204: // modal dialog has gone ... - empty screen appears in background again
205: // assertFalse("MIDlet after Alert : Preempt Display has foreground", preempt.hasForeground);
206: assertFalse("MIDlet after Alert : Display 0 has foreground",
207: display[0].hasForeground);
208:
209: // destroy midlet
210: display[0].setCurrent(null);
211: sleep(500);
212: endMIDlet0();
213: }
214:
215: /**
216: * test checks how SystemAlert preempts normal midlet (with screens)
217: */
218: protected void test2() {
219: declare("TestDisplayPreempt 2");
220:
221: // start midlet with screens - no modal dialogs started
222: startMIDlet1();
223: display[1].setCurrent(F[1][0]);
224: midlet[1].resumeRequest();
225: sleep(500);
226: // modal dialog not yet exists - midlet screen is active
227: assertTrue("MIDlet before Alert : Display 1 has foreground",
228: display[1].hasForeground);
229: assertTrue("MIDlet before Alert : F 1 0 is shown", isShown(
230: display[1], F[1][0]));
231: sleep(500);
232: // create modal dialog - midlet with screens exists
233: A[1][1] = new SystemAlert(deHandler, "A_1_1",
234: "Alert with MIDlet", null, AlertType.INFO);
235: A[1][1].run();
236: A[1][1].setTimeout(1500);
237: // preempt = deHandler.preemptingDisplay.getDisplay();
238: sleep(500);
239: // assertTrue("MIDlet with Alert : Preempt Display has foreground", preempt.hasForeground);
240: assertFalse("MIDlet with Alert : Display 0 has foreground",
241: display[1].hasForeground);
242: assertFalse("MIDlet with Alert : F 1 0 is shown", isShown(
243: display[1], F[1][0]));
244: // while modal dialog is active - change current midlet screen
245: display[1].setCurrent(F[1][1]);
246: sleep(500);
247: // modal dialog is active ... - midlet screen is hidden
248: // assertTrue("Alert with MIDlet : Preempt Display has foreground", preempt.hasForeground);
249: assertFalse("Alert with MIDlet : Display 1 has foreground",
250: display[1].hasForeground);
251: assertFalse("Alert with MIDlet : F 1 0 is shown", isShown(
252: display[1], F[1][0]));
253: assertFalse("Alert with MIDlet : F 1 1 is shown", isShown(
254: display[1], F[1][1]));
255: sleep(1500);
256: // modal dialog has gone ... - midlet screen appears again
257: // assertFalse("MIDlet after Alert : Preempt Display has foreground", preempt.hasForeground);
258: assertTrue("MIDlet after Alert : Display 0 has foreground",
259: display[1].hasForeground);
260: assertFalse("MIDlet after Alert : F 1 0 is shown", isShown(
261: display[1], F[1][0]));
262: assertTrue("MIDlet after Alert : F 1 1 is shown", isShown(
263: display[1], F[1][1]));
264:
265: display[0].setCurrent(null);
266: sleep(500);
267: endMIDlet1();
268: }
269:
270: /**
271: * test checks how systemAlert w/o midlets works
272: */
273: protected void test3() {
274: declare("TestDisplayPreempt 3");
275:
276: // create modal dialog (no midlets exist)
277: A[0][0] = new SystemAlert(deHandler, "A_0_0",
278: "Alert w/o MIDlet", null, AlertType.WARNING);
279: A[0][0].run();
280: A[0][0].setTimeout(1000);
281: // preempt = deHandler.preemptingDisplay.getDisplay();
282: sleep(500);
283: // dialog is active
284: // assertTrue("Alert w/o MIDlet : Preempt Display has foreground", display[1].hasForeground);
285: sleep(1000);
286: // dialog has gone !
287: // assertFalse("Alert w/o MIDlet : Preempt Display has foreground", display[1].hasForeground);
288:
289: sleep(500);
290:
291: assertTrue(true);
292: }
293: }
|