001: /*
002: * @(#)JavaScriptMaker.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.uicapture.v1.javamaker;
028:
029: import net.sourceforge.groboutils.uicapture.v1.VirtualWindowController;
030: import net.sourceforge.groboutils.uicapture.v1.IScriptMaker;
031:
032: import java.io.Writer;
033: import java.io.FileWriter;
034: import java.io.PrintWriter;
035: import java.io.IOException;
036: import java.io.File;
037:
038: /**
039: * Creates a Java playback sourcefile. The Java file will quit with an error
040: * code of 1 if the playback did not go perfectly.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version Jan 7, 2002
044: */
045: public class JavaMaker implements IScriptMaker {
046: private Writer outF = null;
047: private PrintWriter out = null;
048: private String packageName = null;
049: private String className = null;
050: private int step = 0;
051:
052: public JavaMaker(File outFile, String packageName, String className)
053: throws IOException {
054: if (outFile == null || packageName == null || className == null) {
055: throw new IllegalArgumentException("no null args");
056: }
057: this .outF = new FileWriter(outFile);
058: this .out = new PrintWriter(outF);
059:
060: this .packageName = packageName;
061: this .className = className;
062: }
063:
064: public JavaMaker(Writer w, String packageName, String className) {
065: if (w == null || packageName == null || className == null) {
066: throw new IllegalArgumentException("no null args");
067: }
068: this .outF = w;
069: this .out = new PrintWriter(w);
070:
071: this .packageName = packageName;
072: this .className = className;
073: }
074:
075: //-------------------------------------------------------------------------
076: // Public methods
077:
078: /**
079: * Starts the script generation.
080: */
081: public void start() {
082: if (this .step > 0) {
083: throw new IllegalStateException(
084: "Already started generation.");
085: }
086:
087: wrln("/" + "*");
088: wrln(" * Pregenerated script file.");
089: wrln(" * Created " + (new java.util.Date()).toString());
090: wrln(" *" + "/");
091: wrln();
092: wrln("package " + this .packageName + ";");
093: wrln();
094: wrln("import " + VirtualWindowController.class.getName() + ";");
095: wrln("import " + File.class.getName() + ";");
096: wrln();
097: wrln("public class " + this .className);
098: wrln("{");
099: wrln(" public static void main( String[] args )");
100: wrln(" throws Exception");
101: wrln(" {");
102: step("Setup.");
103: wrln(" VirtualWindowController vwc = new ");
104: wrln(" VirtualWindowController();");
105: wrln(" vwc.begin();");
106: wrln(" try {");
107: wrln(" vwc.sleep( 2000 );");
108:
109: // bug 618295
110: this .step = 1;
111: }
112:
113: /**
114: * Terminates the script generation.
115: */
116: public void end() {
117: assertActive();
118:
119: wrln(" /" + "/ end of script");
120: wrln(" System.out.println( \"No errors found in playback.\" ):");
121: wrln(" System.exit( 0 );");
122: wrln(" } finally {");
123: wrln(" vwc.end();");
124: wrln(" vwc = null;");
125: wrln(" }");
126: wrln(" }");
127: wrln("}");
128:
129: // bug 618295
130: this .step = 2;
131:
132: try {
133: this .out.close();
134: this .outF.close();
135: } catch (IOException ioe) {
136: handleException(ioe);
137: } finally {
138: this .out = null;
139: this .outF = null;
140: }
141: }
142:
143: /**
144: * Cause the script to delay for the given number of milliseconds.
145: */
146: public void generateDelay(long milliseconds) {
147: assertActive();
148:
149: step("sleep for " + milliseconds + " milliseconds.");
150: wrln(" vwc.sleep( " + milliseconds + " );");
151: }
152:
153: /**
154: * Cause the script to rotate the mouse wheel.
155: */
156: public void generateMoveMouseWheel(int rotate) {
157: assertActive();
158:
159: step("rotate mouse wheel " + rotate + " clicks.");
160: wrln(" vwc.moveMouseWheel( " + rotate + " );");
161: }
162:
163: /**
164: * Cause the script to move the mouse.
165: */
166: public void generateMoveMouse(int x, int y) {
167: assertActive();
168:
169: step("move mouse to (" + x + ", " + y + ").");
170: wrln(" vwc.moveMouse( " + x + ", " + y + " );");
171: }
172:
173: /**
174: * Cause the script to press a mouse button.
175: */
176: public void generatePressMouse(int modifier) {
177: assertActive();
178:
179: step("press mouse with " + modifier + ".");
180: wrln(" vwc.pressMouse( " + modifier + " );");
181: }
182:
183: /**
184: * Cause the script to release a mouse button.
185: */
186: public void generateReleaseMouse(int modifier) {
187: assertActive();
188:
189: step("release mouse with " + modifier + ".");
190: wrln(" vwc.releaseMouse( " + modifier + " );");
191: }
192:
193: /**
194: * Cause the script to press a key.
195: */
196: public void generatePressKey(int keyCode) {
197: assertActive();
198:
199: step("press key with " + keyCode + ".");
200: wrln(" vwc.pressKey( " + keyCode + " );");
201: }
202:
203: /**
204: * Cause the script to release a key.
205: */
206: public void generateReleaseKey(int keyCode) {
207: assertActive();
208:
209: step("release key with " + keyCode + ".");
210: wrln(" vwc.releaseKey( " + keyCode + " );");
211: }
212:
213: /**
214: * Cause the script to capture a screen shot. Probably, it should compare
215: * the captured image against the original saved image.
216: *
217: * @param x screen image bounds.
218: * @param y screen image bounds.
219: * @param width screen image bounds.
220: * @param height screen image bounds.
221: */
222: public void generateScreenCapture(File originalImage, int x, int y,
223: int width, int height) {
224: assertActive();
225:
226: step("capture and compare screen in (" + x + ", " + y + ", "
227: + width + ", " + height + " ) against " + originalImage
228: + ".");
229: wrln(" File f" + this .step + " = new File( \""
230: + className + "-" + this .step
231: + ".\" + vwc.getImageExtension() );");
232: wrln(" vwc.saveScreenImage( f" + this .step
233: + ".toString(), " + x + ", " + y + ", " + width + ", "
234: + height + " );");
235: wrln(" if (!vwc.compareFiles( f" + step
236: + ", new File( \"" + originalImage + "\" ))");
237: wrln(" {");
238: wrln(" System.out.println( \"Images did not match on step "
239: + this .step + ".\" );");
240: wrln(" System.out.println( \"Test Failed.\" );");
241: wrln(" vwc.end();");
242: wrln(" vwc = null;");
243: wrln(" Thread.sleep( 1000 );");
244: wrln(" System.exit(1);");
245: wrln(" }");
246: wrln(" System.out.println( \"** Images match!\" );");
247: }
248:
249: //-------------------------------------------------------------------------
250:
251: protected void handleException(Throwable t) {
252: t.printStackTrace();
253: this .out = null;
254: this .outF = null;
255: throw new IllegalStateException("Encountered exception " + t);
256: }
257:
258: protected void assertActive() {
259: if (this .out == null) {
260: throw new IllegalStateException("Not open for writing.");
261: }
262: if (this .step <= 0) {
263: throw new IllegalStateException("Never started writing.");
264: }
265: }
266:
267: protected void wr(String text) {
268: this .out.print(text);
269: }
270:
271: protected void wrln(String text) {
272: this .out.println(text);
273: }
274:
275: protected void wrln() {
276: this .out.println("");
277: }
278:
279: protected void step(String msg) {
280: ++this .step;
281: wrln(" System.out.println( \"Step " + this .step + ": "
282: + msg + "\" );");
283: }
284: }
|