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.util;
028:
029: import com.sun.midp.midlet.MIDletStateHandler;
030:
031: import javax.microedition.lcdui.Display;
032: import javax.microedition.midlet.MIDlet;
033: import javax.microedition.midlet.MIDletStateChangeException;
034:
035: /**
036: * A MIDlet for use by i3tests. This MIDlet includes utility
037: * functions that start the midlet, activate it, and bring its display into
038: * the foreground. Invoke this from a test class with the statement
039: *
040: * LcduiTestMIDlet.invoke();
041: *
042: * and clean it up with the statement
043: *
044: * LcduiTestMIDlet.cleanup();
045: */
046: public class LcduiTestMIDlet extends MIDlet {
047:
048: static LcduiTestMIDlet midlet;
049: static Display display;
050:
051: static Object lock;
052: static boolean canvasPainted;
053: static LcduiTestCanvas cv;
054:
055: /**
056: * Starts the test MIDlet and waits for it to be created, activated, and
057: * for its Display to be put into the foreground and painted. Returns true
058: * if this all worked; returns false if the operation timed out. Passes on
059: * any exceptions that occurred in starting up the MIDlet.
060: */
061: public static boolean invoke() throws Throwable {
062:
063: midlet = null;
064: display = null;
065: cv = new LcduiTestCanvas();
066:
067: MIDletStateHandler.getMidletStateHandler().startMIDlet(
068: "com.sun.midp.util.LcduiTestMIDlet",
069: "LCDUI Test MIDlet");
070:
071: return cv.awaitPaint();
072: }
073:
074: /**
075: * Destroys the test MIDlet and waits for the AMS to finish
076: * its processing.
077: */
078: public static void cleanup() {
079: midlet.notifyDestroyed();
080:
081: try {
082: midlet.destroyApp(true);
083: } catch (MIDletStateChangeException ignore) {
084: }
085:
086: // Wait for the AMS to finish processing.
087: // IMPL NOTE: there should be a better way than to sleep.
088: try {
089: Thread.sleep(1000);
090: } catch (InterruptedException ignore) {
091: }
092: }
093:
094: /**
095: * Gets the MIDlet instance. This is non-null only between calls
096: * to invoke() and cleanup().
097: */
098: public static MIDlet getMIDlet() {
099: return midlet;
100: }
101:
102: /**
103: * Gets the Display object for the test MIDlet. This is non-null only
104: * between calls to invoke() and cleanup().
105: */
106: public static Display getDisplay() {
107: return display;
108: }
109:
110: /**
111: * Should be called only by the MIDlet runtime.
112: */
113: public LcduiTestMIDlet() {
114: midlet = this ;
115: display = Display.getDisplay(this );
116: }
117:
118: /**
119: * Should be called only by the MIDlet runtime.
120: */
121: public void startApp() throws MIDletStateChangeException {
122: display.setCurrent(cv);
123: }
124:
125: /**
126: * Should be called only by the MIDlet runtime.
127: */
128: public void pauseApp() {
129: }
130:
131: /**
132: * Should be called only by the MIDlet runtime.
133: */
134: public void destroyApp(boolean unconditional)
135: throws MIDletStateChangeException {
136: midlet = null;
137: display = null;
138: }
139: }
|