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.suspend.test;
028:
029: import com.sun.midp.security.ImplicitlyTrustedClass;
030: import com.sun.midp.security.SecurityToken;
031: import com.sun.midp.security.SecurityInitializer;
032: import com.sun.midp.suspend.SuspendSystem;
033: import com.sun.midp.midlet.MIDletPeer;
034: import com.sun.midp.midlet.MIDletStateHandler;
035:
036: import javax.microedition.midlet.MIDlet;
037:
038: /**
039: * Utilities for suspend/resume testing.
040: */
041: public class TestUtil {
042: /** Utilities for getting MIDlet state. */
043: public static class MidletState {
044: /** MIDlet state can not be determined. */
045: public static final int UNKNOWN = 0;
046: /** Paused state. */
047: public static final int PAUSED = 1;
048: /** Active state. */
049: public static final int ACTIVE = 2;
050: /** Destroyed state. */
051: public static final int DESTROYED = 3;
052:
053: /**
054: * Retrieves current MIDlet state.
055: * @param midlet the MIDlet to get state for.
056: * @return one of UNKNOWN, PAUSED, ACTIVE, DESTROYED
057: */
058: public static int get(MIDlet midlet) {
059: int state = MIDletStateHandler.getMIDletState(midlet);
060: switch (state) {
061: case MIDletPeer.PAUSED:
062: return PAUSED;
063: case MIDletPeer.ACTIVE:
064: return ACTIVE;
065: case MIDletPeer.DESTROYED:
066: return DESTROYED;
067: default:
068: return UNKNOWN;
069: }
070: }
071: }
072:
073: /** Class registered in SecurityInitializer. */
074: private static class SecurityTrusted implements
075: ImplicitlyTrustedClass {
076: }
077:
078: /** Security token for provileged access to internal API's. */
079: private static SecurityToken securityToken = SecurityInitializer
080: .requestToken(new SecurityTrusted());
081:
082: /**
083: * Default delay time.
084: */
085: public static final int DELAY = 3000;
086:
087: /**
088: * Provides execution delay.
089: * @param ms delay time in milliseconds
090: */
091: public static void sleep(long ms) {
092: try {
093: Thread.sleep(ms);
094: } catch (InterruptedException e) {
095: // ignoring
096: }
097: }
098:
099: /**
100: * Retrieves SuspendSystem instance using privileged security token.
101: * @return SuspendSystem singleton instance.
102: */
103: public static SuspendSystem getSuspendSystem() {
104: return SuspendSystem.getInstance(securityToken);
105: }
106:
107: /**
108: * Provides default execution delay.
109: */
110: public static void sleep() {
111: sleep(DELAY);
112: }
113:
114: /**
115: * Sends MIDP suspend request.
116: */
117: public static native void suspendMidp();
118:
119: /**
120: * Sends MIDP resume request.
121: */
122: public static native void resumeMidp();
123:
124: /**
125: * Requests MIDP to suspend and then resume by timeout.
126: * @param timeout resume timeout.
127: */
128: public static native void suspendAndResumeMidp(int timeout);
129:
130: /**
131: * Sets special testing suspend mode that does not suspend VM.
132: */
133: public static native void setNoVMSuspendMode();
134:
135: /**
136: * Sets suspend mode that suspends both resources and VM.
137: */
138: public static native void setVMSuspendMode();
139: }
|