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 com.sun.midp.appmanager;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import com.sun.midp.main.*;
032: import com.sun.midp.midlet.MIDletSuite;
033:
034: public class TestUserNotification extends TestCase implements
035: MIDletProxyListListener {
036:
037: private static final String DUMMY_MIDLET1_CLASS_NAME = "com.sun.midp.appmanager.DummyMIDlet1";
038: private static final String DUMMY_MIDLET2_CLASS_NAME = "com.sun.midp.appmanager.DummyMIDlet2";
039:
040: private MIDletProxyList proxyList;
041: private MIDletProxy midlet1, midlet2;
042: private boolean midlet1InForeground;
043: private boolean midlet2InForeground;
044:
045: private void setUp() {
046: proxyList = MIDletProxyList.getMIDletProxyList();
047: proxyList.addListener(this );
048:
049: IndicatorManager.init(proxyList);
050: }
051:
052: private void startMIDlet1() {
053: try {
054: // Start a new instance of DummyMIDlet1
055: MIDletSuiteUtils.execute(MIDletSuite.INTERNAL_SUITE_ID,
056: DUMMY_MIDLET1_CLASS_NAME, "DummyMIDlet1");
057:
058: // Wait for async request to be processed
059: synchronized (this ) {
060: if (midlet1 == null) {
061: // We only wait the full time on a failure
062: wait(10000);
063: }
064: }
065:
066: assertTrue(DUMMY_MIDLET1_CLASS_NAME + " not started",
067: midlet1 != null);
068: } catch (Exception e) {
069: e.printStackTrace();
070: }
071: }
072:
073: private void startMIDlet2() {
074: try {
075: // Start a new instance of DummyMIDlet2
076: MIDletSuiteUtils.execute(MIDletSuite.INTERNAL_SUITE_ID,
077: DUMMY_MIDLET2_CLASS_NAME, "DummyMIDlet2");
078:
079: // Wait for async request to be processed
080: synchronized (this ) {
081: if (midlet2 == null) {
082: // We only wait the full time on a failure
083: wait(10000);
084: }
085: }
086:
087: assertTrue(DUMMY_MIDLET2_CLASS_NAME + " not started",
088: midlet2 != null);
089: } catch (Exception e) {
090: e.printStackTrace();
091: }
092: }
093:
094: private void test1() {
095: declare("One");
096: // Launch MIDlet 1
097: startMIDlet1();
098:
099: // Wait for async request to be processed
100: // Headless MIDlet also occupy foreground
101: synchronized (midlet1) {
102: if (!midlet1InForeground) {
103: waitForMIDletToGetForeground(midlet1);
104: }
105:
106: assertTrue(DUMMY_MIDLET1_CLASS_NAME + " not in foreground",
107: midlet1InForeground);
108: }
109:
110: /*
111: * Launching a headless MIDlet shouldn't trigger UNS
112: */
113: assertTrue(IndicatorManager.getHomeIconState() == false);
114: }
115:
116: private void test3() {
117: declare("Three");
118:
119: // Launch MIDlet 2
120: startMIDlet2();
121:
122: /*
123: * MIDlet 2 will automatically get foreground when it requests it
124: * in startApp and MIDlet 1 will be put in background
125: */
126:
127: // Wait for async request to be processed
128: synchronized (midlet2) {
129: if (!midlet2InForeground) {
130: waitForMIDletToGetForeground(midlet2);
131: }
132:
133: assertTrue(DUMMY_MIDLET2_CLASS_NAME + " not in foreground",
134: midlet2InForeground);
135: }
136:
137: // Merely moving MIDlet 1 to background shouldn't trigger home icon
138: assertTrue(IndicatorManager.getHomeIconState() == false);
139: }
140:
141: private void test4() {
142: declare("Four");
143:
144: /*
145: * Force MIDlet 1 in the background to request foreground,
146: * by getting startApp to be called a third time.
147: */
148: midlet1.pauseMidlet();
149:
150: // Give pauseApp time to get called before activating
151: try {
152: Thread.sleep(100);
153: } catch (InterruptedException ie) {
154: // ignore
155: }
156:
157: midlet1.activateMidlet();
158:
159: // No foreground change so just wait 1 sec.
160: try {
161: Thread.sleep(1000);
162: } catch (InterruptedException ie) {
163: // ignore
164: }
165:
166: // Home icon should be on upon background display foreground request
167: assertTrue(IndicatorManager.getHomeIconState() == true);
168: }
169:
170: private void test5() {
171: declare("Five");
172:
173: // Background Display requests for background
174: midlet1.pauseMidlet();
175:
176: // The test manager may get the background so just wait 1 sec.
177: try {
178: Thread.sleep(1000);
179: } catch (InterruptedException ie) {
180: // ignore
181: }
182:
183: // previous home icon request should be cancelled
184: assertTrue(IndicatorManager.getHomeIconState() == false);
185: }
186:
187: private void tearDown() {
188: proxyList.removeListener(this );
189:
190: if (midlet1 != null) {
191: midlet1.destroyMidlet();
192: }
193:
194: if (midlet2 != null) {
195: midlet2.destroyMidlet();
196: }
197: }
198:
199: private void waitForMIDletToGetForeground(MIDletProxy midlet) {
200: synchronized (midlet) {
201: try {
202: // We only wait the full time on a failure
203: midlet.wait(10000);
204: } catch (InterruptedException ie) {
205: // ignore
206: }
207: }
208: }
209:
210: public void runTests() {
211: setUp();
212:
213: try {
214: test1();
215: // test2() is obsolete
216: test3();
217: test4();
218: test5();
219: } finally {
220: tearDown();
221: }
222: }
223:
224: /**
225: * Called when a MIDlet is added to the list.
226: *
227: * @param midlet The proxy of the MIDlet being added
228: */
229: public void midletAdded(MIDletProxy midlet) {
230: if (DUMMY_MIDLET1_CLASS_NAME.equals(midlet.getClassName())) {
231: synchronized (this ) {
232: midlet1 = midlet;
233: notifyAll();
234: }
235:
236: return;
237: }
238:
239: if (DUMMY_MIDLET2_CLASS_NAME.equals(midlet.getClassName())) {
240: synchronized (this ) {
241: midlet2 = midlet;
242: notifyAll();
243: }
244:
245: return;
246: }
247: }
248:
249: /**
250: * Called when the state of a MIDlet in the list is updated.
251: *
252: * @param midlet The proxy of the MIDlet that was updated
253: * @param fieldId code for which field of the proxy was updated
254: */
255: public void midletUpdated(MIDletProxy midlet, int fieldId) {
256: MIDletProxy foreground = proxyList.getForegroundMIDlet();
257:
258: if (midlet1 != null && midlet1 == foreground) {
259: synchronized (midlet1) {
260: midlet2InForeground = false;
261: midlet1InForeground = true;
262: midlet1.notifyAll();
263: }
264: } else if (midlet2 != null && midlet2 == foreground) {
265: synchronized (midlet2) {
266: midlet1InForeground = false;
267: midlet2InForeground = true;
268: midlet2.notifyAll();
269: }
270: } else {
271: midlet1InForeground = false;
272: midlet2InForeground = false;
273: }
274:
275: }
276:
277: /**
278: * Called when a MIDlet is removed from the list.
279: *
280: * @param midlet The proxy of the removed MIDlet
281: */
282: public void midletRemoved(MIDletProxy midlet) {
283: }
284:
285: /**
286: * Called when error occurred while starting a MIDlet object.
287: *
288: * @param externalAppId ID assigned by the external application manager
289: * @param suiteId Suite ID of the MIDlet
290: * @param className Class name of the MIDlet
291: * @param errorCode start error code
292: * @param errorDetails start error code
293: */
294: public void midletStartError(int externalAppId, int suiteId,
295: String className, int errorCode, String errorDetails) {
296: }
297: }
|