midletguiagent.java
来自「cqME :java framework for TCK test.」· Java 代码 · 共 357 行
JAVA
357 行
/* * $Id$ * * 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.javatest.agent;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.Item;import javax.microedition.lcdui.StringItem;import javax.microedition.lcdui.Command;import javax.microedition.rms.RecordStore;import javax.microedition.rms.RecordStoreException;import javax.microedition.rms.RecordStoreNotFoundException;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.IOException;import com.sun.tck.cldc.javatest.agent.CldcAgent;import com.sun.tck.cldc.javatest.agent.CancelableCldcAgent;import com.sun.tck.cldc.javatest.agent.TestProgressObserver;import com.sun.tck.cldc.javatest.agent.TestResult;import com.sun.tck.cldc.javatest.util.UTFConverter;import com.sun.tck.cldc.lib.Status;import com.sun.tck.midp.ui.Action;import com.sun.tck.midp.ui.MenuHandler;/** * MIDlet agent which shows run tests progress on the device screen. * Allows the user to interrupt hanged tests via provided GUI interface. */public class MIDletGUIAgent extends MIDletAgent implements TestProgressObserver { static final String STORE_NAME = "test_status"; private CldcAgent baseAgent; private int passedCount; private int failedCount; private int itemTestNameNum; private RecordStore recordStore; private boolean saveContinually; private ByteArrayOutputStream refBaos = new ByteArrayOutputStream(); private ByteArrayOutputStream logBaos = new ByteArrayOutputStream(); private MenuHandler menuHandler; private Form form; private StringItem itemTestCount; private StringItem itemTestName; private Action cancelTestAction; private Action cancelBundleAction; private Action saveStatusAction; /** * Creates a MIDletGUIAgent. */ public MIDletGUIAgent() { menuHandler = MenuHandler.getMenuHandler(); } /** * Initializes parameters by arguments. * @param args - midlet agent arguments */ private void parseArgs(String[] args) { for (int i = 0; i < args.length; i += decodeArg(args, i)) {} } /** * Parses arguments starting from <code>ind</code> from <code>args</code>. * @param args array of midlet agent arguments. * @param ind index to start with. * @return number of parsed parameters. */ protected int decodeArg(String[] args, int ind) { if ("-saveRMSContinually".equals(args[ind])) { if (ind + 1 < args.length && !args[ind + 1].startsWith("-")) { saveContinually = "true".equals(args[ind + 1]); return 2; } else { throw new RuntimeException( "Save to RMS continually parameter not set"); } } else { throw new RuntimeException( "Unrecognized argument for the agent: " + args[ind]); } } /** * This implementation creates instance of {@link CancelableCldcAgent} for * test execution. * * @return new CancelableCldcAgent object */ protected CldcAgent createBaseAgent() { baseAgent = new CancelableCldcAgent(); return baseAgent; } /** * Saves status to device RMS. */ private void saveStatus() { TestResult tr = getBaseAgent().getTestResult(); if (tr != null) { String[] out = new String[4]; out[0] = tr.getName(); out[1] = refBaos.toString(); out[2] = logBaos.toString(); out[3] = tr.getStatus() != null ? tr.getStatus().toString() : ""; try { byte[] buf = UTFConverter.stringsToBytes(out); getRecordStore().addRecord(buf, 0, buf.length); } catch (RecordStoreException rse) { rse.printStackTrace(); } } } /** * This implementation deletes the record store with previous * results, if any, and then delegates the main test execution to the super * implementation. */ void executeTests() { deleteRecordStore(); try { super.executeTests(); } finally { closeRecordStore(); try { refBaos.close(); } catch (IOException ioe) { // silently ignore } try { logBaos.close(); } catch (IOException ioe) { // silently ignore } } } /** * Creates if necessary and returns agent record store. * @return Agent record store. */ private RecordStore getRecordStore() { if (recordStore == null) { try { recordStore = RecordStore.openRecordStore(STORE_NAME, true, RecordStore.AUTHMODE_ANY, false); } catch (RecordStoreException rse) { rse.printStackTrace(); } } return recordStore; } /** * Deletes agent record store (if any). */ private void deleteRecordStore() { try { RecordStore.deleteRecordStore(STORE_NAME); } catch (RecordStoreNotFoundException rsnfe) { /* just ignore */ } catch (RecordStoreException rse) { rse.printStackTrace(); } } /** * Closes agent record store. */ private void closeRecordStore() { if (recordStore != null) { try { recordStore.closeRecordStore(); recordStore = null; } catch (RecordStoreException rse) { rse.printStackTrace(); } } } /** * Initializes the main form of current MIDlet. * Adds test progress observer to show currently running test in GUI * and to save test results to RMS. * * @see com.sun.tck.cldc.javatest.agent.TestProgressObserver * @see com.sun.tck.cldc.javatest.agent.CancelableCldcAgent#addTestProgressObserver */ protected void baseAgentStarting() { super.baseAgentStarting(); getBaseAgent().addTestProgressObserver(this); parseArgs(getArgsFromResource("/mid_agent.dat")); initForm(); } /** * Inits buffers for log and ref * and updates information about currently running test. * {@inheritDoc} */ public void testStarted(String testName) { refBaos.reset(); logBaos.reset(); itemTestName.setText(testName); } /** * Saves test status and updates tests count. * {@inheritDoc} */ public void testFinished(TestResult tr) { if (saveContinually) { saveStatus(); } StringBuffer sb = new StringBuffer("Tests done: "); if (tr.getStatus().getType() == Status.PASSED) { passedCount++; } else { failedCount++; } sb.append(passedCount + failedCount); sb.append(". Passed: ").append(passedCount); sb.append(". Failed: ").append(failedCount); itemTestCount.setText(sb.toString()); } /** * {@inheritDoc} */ public OutputStream getLog() { return logBaos; } /** * {@inheritDoc} */ public OutputStream getRef() { return refBaos; } /** * Extracts array of strings from resource. * @param name name of resource. * @ return array of strings read from resource. */ private String[] getArgsFromResource(String name) { InputStream argsStream = getClass().getResourceAsStream(name); if(argsStream == null) { throw new RuntimeException( "Agent configuration resource can not be opened"); } return UTFConverter.UTFStreamToStrings(argsStream); } /** * Gets {@link CancelableCldcAgent} instance to control test execution. * @return agent. */ private synchronized CancelableCldcAgent getBaseAgent() { return (CancelableCldcAgent) baseAgent; } /** * Inits main form of the MIDlet. */ private void initForm() { form = new Form("Test agent"); cancelTestAction = new Action(new Command("Cancel test", Command.CANCEL, 1)) { public void actionPerformed() { getBaseAgent().cancelTest(); } }; cancelBundleAction = new Action(new Command("Cancel bundle", Command.CANCEL, 2)) { public void actionPerformed() { getBaseAgent().cancelTestBundle(); itemTestName.setText("Cancelling test bundle..."); } }; saveStatusAction = new Action(new Command("Save status", Command.SCREEN, 3)) { public void actionPerformed() { saveStatus(); } }; menuHandler.addAction(form, cancelTestAction); menuHandler.addAction(form, cancelBundleAction); menuHandler.addAction(form, saveStatusAction); itemTestCount = new StringItem(null, "Tests done: 0"); itemTestCount.setLayout(Item.LAYOUT_NEWLINE_AFTER); form.append(itemTestCount); itemTestName = new StringItem(null, null); itemTestNameNum = form.append(itemTestName); getContextDisplay().setCurrent(form); } /** * Gets main form of the MIDlet. * @return main form. */ Form getMainForm() { return form; } /** * Removes commands which are suitable only for the time * while tests are running. */ void removeRuntimeCommands() { menuHandler.removeAction(form, cancelTestAction); menuHandler.removeAction(form, cancelBundleAction); menuHandler.removeAction(form, saveStatusAction); form.delete(itemTestNameNum); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?