middistribinteractivetest.java
来自「cqME :java framework for TCK test.」· Java 代码 · 共 312 行
JAVA
312 行
/* * $Id: MidDistribInteractiveTest.java 340 2007-02-12 23:00:40Z vsizikov $ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.midp.lib;import java.io.IOException;import java.io.PrintStream;import java.util.Enumeration;import java.util.Vector;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import com.sun.tck.cldc.lib.Status;import com.sun.tck.j2me.services.messagingService.J2MEDistributedTest;import com.sun.tck.midp.javatest.agent.MIDletAgent;/** * A base class for distributed interactive tests for MIDP. This part * of distributed test infrastructure is supposed to be run on the * device side. */public abstract class MidDistribInteractiveTest extends J2MEDistributedTest { private Status status; private String title = "Initializing ..."; private Form infoForm; // Description/Introduction form private Displayable testDisplayable; // First display of test class public Display display = null; public MListener ml = new MListener(); protected Command nextCommand = new Command("Next", Command.SCREEN, 1); protected Command backCommand = new Command("Back", Command.CANCEL, 1); /** * Indicates whether the message client has been closed or not. * This field could be used for <code>waitForDone</code> * overwriting. * * @see #waitForDone */ protected boolean done = false; private String from; private boolean received = false; public synchronized void waitForMessage() { try { while (!received) wait(10000); received = false; } catch (InterruptedException e) { } } public synchronized void waitForMessage(int timeout) { try { while (!received) wait(timeout); received = false; } catch (InterruptedException e) { } } public synchronized void messageReceived() { received = true; notifyAll(); } public MidDistribInteractiveTest(String name) { super(name); } public Status run(String[] argv, PrintStream log, PrintStream ref) { setUp(); Status allTestStatus = super.run(argv, log, ref); try { send(from, new String[] {"endRun"}); waitForMessage(); } catch (IOException e) { } return allTestStatus; } /** * General set up method. */ protected void setUp() { display = MIDletAgent.getContextDisplay(); infoForm = new Form(title); } /** * Performs setup that is specific to the derived test. */ protected void doTestSetUp() {} /** * Checks the test's result and declares pass/fail. * Overridden by self-checking tests to determine the test result after * user presses the Finish button. * This method should be overriden if the derived test uses the Done user * interface. The checkResult method should invoke setStatus after * determining the test result. */ protected void checkResult() { setStatus(Status.failed("No result checking was performed by test.")); } /** * Add another test into the set for consideration. * * @param status The outcome of this test case. It will be ignored. */ protected void addStatus(Status status) { boolean passed = false; done = false; display.setCurrent(infoForm); doTestSetUp(); // Ignores input parameter passed = waitForDone(); super.addStatus(passed ? Status.passed("OKAY") : Status.failed("Some problems occurred. See log" + " for details.")); cleanUp(); } /** * Sets titles of info and end forms. * * @param title Title to be displayed. */ protected void setFormTitle(String title) { infoForm.setTitle(title); } /** * Updates info form with new instructions. * * @param info Instruction for a current test. */ protected void addInfo(String info) { for (int i = 0; i < infoForm.size(); i++) { infoForm.delete(i); } infoForm.append(info); } /** * Sets the test display. * * @param testDisplay Displayable containing test. */ protected void setTestDisplay(Displayable testDisplay) { testDisplayable = testDisplay; testDisplayable.setCommandListener(ml); testDisplayable.addCommand(backCommand); infoForm.addCommand(nextCommand); infoForm.setCommandListener(new CommandListener () { public void commandAction(Command c, Displayable s) { if (c == nextCommand) { display.setCurrent(testDisplayable); } } }); ml.addListener(new CommandListener () { public void commandAction(Command c, Displayable s) { if (s == testDisplayable && c == backCommand) { display.setCurrent(infoForm); } } }); } /** * Called when the test is done. */ protected void cleanUp() { status = null; display.setCurrent(new Form(null)); ml.removeAllListeners(); doTestCleanUp(); } /** * Performs cleanup that is specific to the derived test. * Test should override this method if the test needs to do cleanup * before exiting. */ protected void doTestCleanUp() {} /** * Waits for the user to indicate test completion. * Times out if the user doesn't respond within the timeout limit. */ public boolean waitForDone() { // wait until server is done synchronized (this) { try { while (!done) { wait(100); } } catch (InterruptedException e) { log.println(e.toString()); return false; } } return true; } /** * This method is called when message client has been closed. * It sets <code>done</code> variable to true to notify that test execution * completed. */ public synchronized void handle_done(String from, String[] args) { this.from = from; ref.println("This test done"); done = true; notifyAll(); } public synchronized void handle_endRun(String from, String[] args) { ref.println("All tests done"); notifyAll(); } /** * Sets pass/fail test status. * Called by listener when the user indicates that the test is done by * pressing the Yes button, the No button. * * @see javasoft.sqe.javatest.Status * * @param status Pass/Fail status. */ protected synchronized void setStatus(Status status) { this.status = status; notify(); } /** * Message printed if test passes. * Set by invoking setStatusMessages(). */ protected String passMessage = ""; /** * Message printed if test fails. * Set by invoking setStatusMessages(). */ protected String failMessage = ""; //------------------------------------------------ /** * Supplementary class to allow more than one listeners be * registered and to receive commandAction events * In a test class simply call "ml.addListener(TestCommandListener l)" */ protected static class MListener implements CommandListener { private Vector bucket = new Vector(1, 1); public void addListener(CommandListener l) { bucket.addElement(l); } // to be called at test in clean up code public void removeAllListeners() { for (int i = 0; i < bucket.size() - 1; i++) { bucket.removeElement(bucket.elementAt(i+1)); } } public void commandAction(Command c, Displayable s) { Enumeration e = bucket.elements(); while (e.hasMoreElements()) { ((CommandListener)e.nextElement()).commandAction(c, s); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?