001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive;
031:
032: import nextapp.echo2.app.Button;
033: import nextapp.echo2.app.TaskQueueHandle;
034:
035: /**
036: * Note to developers who might use this class as an example:
037: * Don't. This is a *very unusual* use of asynchronous tasks.
038: * See the documentation for examples of how asynchronous tasks
039: * might normally be used.
040: */
041: public class GhostTask {
042:
043: private InteractiveApp app;
044: private int clicksPerIteration = 1;
045: private boolean indefiniteAllowed;
046: private int iteration = 0;
047: private String[] script;
048: private int scriptIndex = 0;
049: private String[] startupScript;
050:
051: private TaskQueueHandle taskQueue;
052:
053: private int totalIterations = -1;
054: private int runTime = -1;
055: private long stopTime = -1;
056:
057: private Runnable task = new Runnable() {
058:
059: /**
060: * @see java.lang.Runnable#run()
061: */
062: public void run() {
063: for (int i = 0; i < clicksPerIteration; ++i) {
064: if (script == null) {
065: RandomClick.clickRandomButton();
066: } else {
067: Button button = (Button) app.getDefaultWindow()
068: .getComponent(script[scriptIndex]);
069: button.doAction();
070: ++scriptIndex;
071: if (scriptIndex >= script.length) {
072: scriptIndex = 0;
073: }
074: }
075: }
076: if (stopTime != -1 && System.currentTimeMillis() > stopTime) {
077: app.setGhostIterationWindowTitle(-1);
078: // Test complete.
079: app.stopGhostTest();
080: } else if (totalIterations != -1
081: && iteration >= totalIterations) {
082: app.setGhostIterationWindowTitle(-1);
083: // Test complete.
084: app.stopGhostTest();
085: } else if (indefiniteAllowed) {
086: ++iteration;
087: app.setGhostIterationWindowTitle(iteration);
088: app.enqueueTask(taskQueue, this );
089: }
090: }
091: };
092:
093: /**
094: * Creates a new <code>GhostTask</code>.
095: *
096: * @param app the application to test
097: * @param taskQueue the <code>TaskQueueHandle</code> to which tasks will be
098: * added
099: */
100: public GhostTask() {
101: super ();
102: indefiniteAllowed = !InteractiveApp.LIVE_DEMO_SERVER;
103: }
104:
105: public int getClicksPerIteration() {
106: return clicksPerIteration;
107: }
108:
109: public int getRunTime() {
110: return runTime;
111: }
112:
113: public String[] getScript() {
114: return script;
115: }
116:
117: public String[] getStartupScript() {
118: return startupScript;
119: }
120:
121: public int getTotalIterations() {
122: return totalIterations;
123: }
124:
125: public void setClicksPerIteration(int clicksPerIteration) {
126: this .clicksPerIteration = clicksPerIteration;
127: }
128:
129: public void setScript(String[] script) {
130: this .script = script;
131: }
132:
133: public void setRunTime(int runTime) {
134: this .runTime = runTime;
135: this .totalIterations = -1;
136: }
137:
138: public void setStartupScript(String[] startupScript) {
139: this .startupScript = startupScript;
140: }
141:
142: public void setTotalIterations(int totalIterations) {
143: this .totalIterations = totalIterations;
144: this .runTime = -1;
145: }
146:
147: void startTask(InteractiveApp app, TaskQueueHandle taskQueue) {
148: this .app = app;
149: this .taskQueue = taskQueue;
150: if (runTime != -1) {
151: stopTime = System.currentTimeMillis() + runTime;
152: }
153: app.enqueueTask(taskQueue, task);
154: }
155: }
|