001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import java.lang.reflect.InvocationTargetException;
050:
051: import org.netbeans.jemmy.ClassReference;
052: import org.netbeans.jemmy.ComponentChooser;
053: import org.netbeans.jemmy.ComponentSearcher;
054: import org.netbeans.jemmy.JemmyException;
055: import org.netbeans.jemmy.JemmyProperties;
056: import org.netbeans.jemmy.Outputable;
057: import org.netbeans.jemmy.TestOut;
058: import org.netbeans.jemmy.Timeouts;
059: import org.netbeans.jemmy.TimeoutExpiredException;
060: import org.netbeans.jemmy.Waitable;
061: import org.netbeans.jemmy.Waiter;
062: import org.netbeans.jemmy.WindowWaiter;
063:
064: import org.netbeans.jemmy.drivers.WindowDriver;
065: import org.netbeans.jemmy.drivers.DriverManager;
066:
067: import java.awt.Component;
068: import java.awt.Window;
069:
070: import java.awt.event.WindowEvent;
071: import java.awt.event.WindowListener;
072:
073: import java.util.ResourceBundle;
074:
075: /**
076: * <BR><BR>Timeouts used: <BR>
077: * WindowWaiter.WaitWindowTimeout - time to wait window displayed <BR>
078: * WindowWaiter.AfterWindowTimeout - time to sleep after window has been dispayed <BR>.
079: *
080: * @see org.netbeans.jemmy.Timeouts
081: *
082: * @author Alexandre Iline (alexandre.iline@sun.com)
083: *
084: */
085:
086: public class WindowOperator extends ContainerOperator implements
087: Outputable {
088:
089: TestOut output;
090: WindowDriver driver;
091:
092: /**
093: * Constructor.
094: * @param w a component
095: */
096: public WindowOperator(Window w) {
097: super (w);
098: driver = DriverManager.getWindowDriver(getClass());
099: }
100:
101: /**
102: * Constructs a DialogOperator object.
103: * @param owner window - owner
104: * @param chooser a component chooser specifying searching criteria.
105: * @param index an index between appropriate ones.
106: */
107: public WindowOperator(WindowOperator owner,
108: ComponentChooser chooser, int index) {
109: this ((Window) owner.waitSubWindow(chooser, index));
110: copyEnvironment(owner);
111: }
112:
113: /**
114: * Constructs a DialogOperator object.
115: * @param owner window - owner
116: * @param chooser a component chooser specifying searching criteria.
117: */
118: public WindowOperator(WindowOperator owner, ComponentChooser chooser) {
119: this (owner, chooser, 0);
120: }
121:
122: /**
123: * Constructor.
124: * Waits for the index'th displayed owner's child.
125: * Uses owner's timeout and output for waiting and to init operator.
126: * @param owner Operator pointing on a window owner.
127: * @param index an index between appropriate ones.
128: * @throws TimeoutExpiredException
129: */
130: public WindowOperator(WindowOperator owner, int index) {
131: this (waitWindow(owner, ComponentSearcher
132: .getTrueChooser("Any Window"), index));
133: copyEnvironment(owner);
134: }
135:
136: /**
137: * Constructor.
138: * Waits for the first displayed owner's child.
139: * Uses owner's timeout and output for waiting and to init operator.
140: * @param owner Operator pointing on a window owner.
141: * @throws TimeoutExpiredException
142: */
143: public WindowOperator(WindowOperator owner) {
144: this (owner, 0);
145: }
146:
147: /**
148: * Constructor.
149: * Waits for the index'th displayed window.
150: * Constructor can be used in complicated cases when
151: * output or timeouts should differ from default.
152: * @param index an index between appropriate ones.
153: * @param env an operator to copy environment from.
154: * @throws TimeoutExpiredException
155: */
156: public WindowOperator(int index, Operator env) {
157: this (waitWindow(ComponentSearcher.getTrueChooser("Any Window"),
158: index, env.getTimeouts(), env.getOutput()));
159: copyEnvironment(env);
160: }
161:
162: /**
163: * Constructor.
164: * Waits for the index'th displayed window.
165: * Uses current timeouts and output values.
166: * @see JemmyProperties#getCurrentTimeouts()
167: * @see JemmyProperties#getCurrentOutput()
168: * @param index an index between appropriate ones.
169: * @throws TimeoutExpiredException
170: */
171: public WindowOperator(int index) {
172: this (index, getEnvironmentOperator());
173: }
174:
175: /**
176: * Constructor.
177: * Waits for the first displayed window.
178: * Uses current timeouts and output values.
179: * @see JemmyProperties#getCurrentTimeouts()
180: * @see JemmyProperties#getCurrentOutput()
181: * @throws TimeoutExpiredException
182: */
183: public WindowOperator() {
184: this (0);
185: }
186:
187: /**
188: * Searches an index'th window.
189: * @param chooser a component chooser specifying searching criteria.
190: * @param index an index between appropriate ones.
191: * @return a window
192: */
193: public static Window findWindow(ComponentChooser chooser, int index) {
194: return (WindowWaiter.getWindow(chooser, index));
195: }
196:
197: /**
198: * Searches a window.
199: * @param chooser a component chooser specifying searching criteria.
200: * @return a window
201: */
202: public static Window findWindow(ComponentChooser chooser) {
203: return (findWindow(chooser, 0));
204: }
205:
206: /**
207: * Searches an index'th window.
208: * @param owner Window - owner.
209: * @param chooser a component chooser specifying searching criteria.
210: * @param index an index between appropriate ones.
211: * @return a window
212: */
213: public static Window findWindow(Window owner,
214: ComponentChooser chooser, int index) {
215: return (WindowWaiter.getWindow(owner, chooser, index));
216: }
217:
218: /**
219: * Searches a window.
220: * @param owner Window - owner.
221: * @param chooser a component chooser specifying searching criteria.
222: * @return a window
223: */
224: public static Window findWindow(Window owner,
225: ComponentChooser chooser) {
226: return (findWindow(owner, chooser, 0));
227: }
228:
229: /**
230: * Waits an index'th window.
231: * @param chooser a component chooser specifying searching criteria.
232: * @param index an index between appropriate ones.
233: * @throws TimeoutExpiredException
234: * @return a window
235: */
236: public static Window waitWindow(ComponentChooser chooser, int index) {
237: return (waitWindow(chooser, index, JemmyProperties
238: .getCurrentTimeouts(), JemmyProperties
239: .getCurrentOutput()));
240: }
241:
242: /**
243: * Waits a window.
244: * @param chooser a component chooser specifying searching criteria.
245: * @throws TimeoutExpiredException
246: * @return a window
247: */
248: public static Window waitWindow(ComponentChooser chooser) {
249: return (waitWindow(chooser, 0));
250: }
251:
252: /**
253: * Waits an index'th window.
254: * @param owner Window - owner.
255: * @param chooser a component chooser specifying searching criteria.
256: * @param index an index between appropriate ones.
257: * @throws TimeoutExpiredException
258: * @return a window
259: */
260: public static Window waitWindow(Window owner,
261: ComponentChooser chooser, int index) {
262: return (waitWindow(owner, chooser, index, JemmyProperties
263: .getCurrentTimeouts(), JemmyProperties
264: .getCurrentOutput()));
265: }
266:
267: /**
268: * Waits a window.
269: * @param owner Window - owner.
270: * @param chooser a component chooser specifying searching criteria.
271: * @throws TimeoutExpiredException
272: * @return a window
273: */
274: public static Window waitWindow(Window owner,
275: ComponentChooser chooser) {
276: return (waitWindow(owner, chooser, 0));
277: }
278:
279: public void setOutput(TestOut out) {
280: super .setOutput(out);
281: output = out;
282: }
283:
284: public TestOut getOutput() {
285: return (output);
286: }
287:
288: public void copyEnvironment(Operator anotherOperator) {
289: super .copyEnvironment(anotherOperator);
290: driver = (WindowDriver) DriverManager.getDriver(
291: DriverManager.WINDOW_DRIVER_ID, getClass(),
292: anotherOperator.getProperties());
293: }
294:
295: /**
296: * Activates the window.
297: * Uses WindowDriver registered for the operator type.
298: */
299: public void activate() {
300: output.printLine("Activate window\n "
301: + getSource().toString());
302: output.printGolden("Activate window");
303: driver.activate(this );
304: }
305:
306: /**
307: * Closes the window.
308: * Uses WindowDriver registered for the operator type.
309: */
310: public void close() {
311: output.printLine("Closing window\n "
312: + getSource().toString());
313: output.printGolden("Closing window");
314: driver.close(this );
315: if (getVerification()) {
316: waitClosed();
317: }
318: }
319:
320: /**
321: * Moves the window to another location.
322: * Uses WindowDriver registered for the operator type.
323: * @param x coordinate in screen coordinate system
324: * @param y coordinate in screen coordinate system
325: */
326: public void move(int x, int y) {
327: output.printLine("Moving frame\n " + getSource().toString());
328: output.printGolden("Moving frame");
329: driver.move(this , x, y);
330: }
331:
332: /**
333: * Resizes the window.
334: * Uses WindowDriver registered for the operator type.
335: * @param width new width
336: * @param height new height
337: */
338: public void resize(int width, int height) {
339: output.printLine("Resizing frame\n "
340: + getSource().toString());
341: output.printGolden("Resizing frame");
342: driver.resize(this , width, height);
343: }
344:
345: /**
346: * Searches an index'th window between windows owned by this window.
347: * @param chooser a component chooser specifying searching criteria.
348: * @param index an index between appropriate ones.
349: * @return a subwindow
350: */
351: public Window findSubWindow(ComponentChooser chooser, int index) {
352: getOutput().printLine(
353: "Looking for \"" + chooser.getDescription()
354: + "\" subwindow");
355: return (findWindow((Window) getSource(), chooser, index));
356: }
357:
358: /**
359: * Searches a window between windows owned by this window.
360: * @param chooser a component chooser specifying searching criteria.
361: * @return a subwindow
362: */
363: public Window findSubWindow(ComponentChooser chooser) {
364: return (findSubWindow(chooser, 0));
365: }
366:
367: /**
368: * Waits an index'th window between windows owned by this window.
369: * @param chooser a component chooser specifying searching criteria.
370: * @param index an index between appropriate ones.
371: * @return a subwindow
372: */
373: public Window waitSubWindow(ComponentChooser chooser, int index) {
374: getOutput().printLine(
375: "Waiting for \"" + chooser.getDescription()
376: + "\" subwindow");
377: WindowWaiter ww = new WindowWaiter();
378: ww.setOutput(getOutput());
379: ww.setTimeouts(getTimeouts());
380: try {
381: return (ww.waitWindow((Window) getSource(), chooser, index));
382: } catch (InterruptedException e) {
383: throw (new JemmyException("Waiting for \""
384: + chooser.getDescription()
385: + "\" window has been interrupted", e));
386: }
387: }
388:
389: /**
390: * Waits a window between windows owned by this window.
391: * @param chooser a component chooser specifying searching criteria.
392: * @return a subwindow
393: */
394: public Window waitSubWindow(ComponentChooser chooser) {
395: return (waitSubWindow(chooser, 0));
396: }
397:
398: /**
399: * Waits the window to be closed.
400: */
401: public void waitClosed() {
402: getOutput().printLine(
403: "Wait window to be closed \n : "
404: + getSource().toString());
405: getOutput().printGolden("Wait window to be closed");
406: waitState(new ComponentChooser() {
407: public boolean checkComponent(Component comp) {
408: return (!comp.isVisible());
409: }
410:
411: public String getDescription() {
412: return ("Closed window");
413: }
414: });
415: }
416:
417: ////////////////////////////////////////////////////////
418: //Mapping //
419:
420: /**Maps <code>Window.addWindowListener(WindowListener)</code> through queue*/
421: public void addWindowListener(final WindowListener windowListener) {
422: runMapping(new MapVoidAction("addWindowListener") {
423: public void map() {
424: ((Window) getSource())
425: .addWindowListener(windowListener);
426: }
427: });
428: }
429:
430: /**Maps <code>Window.applyResourceBundle(String)</code> through queue*/
431: public void applyResourceBundle(final String string) {
432: runMapping(new MapVoidAction("applyResourceBundle") {
433: public void map() {
434: ((Window) getSource()).applyResourceBundle(string);
435: }
436: });
437: }
438:
439: /**Maps <code>Window.applyResourceBundle(ResourceBundle)</code> through queue*/
440: public void applyResourceBundle(final ResourceBundle resourceBundle) {
441: runMapping(new MapVoidAction("applyResourceBundle") {
442: public void map() {
443: ((Window) getSource())
444: .applyResourceBundle(resourceBundle);
445: }
446: });
447: }
448:
449: /**Maps <code>Window.dispose()</code> through queue*/
450: public void dispose() {
451: runMapping(new MapVoidAction("dispose") {
452: public void map() {
453: ((Window) getSource()).dispose();
454: }
455: });
456: }
457:
458: /**Maps <code>Window.getFocusOwner()</code> through queue*/
459: public Component getFocusOwner() {
460: return ((Component) runMapping(new MapAction("getFocusOwner") {
461: public Object map() {
462: return (((Window) getSource()).getFocusOwner());
463: }
464: }));
465: }
466:
467: /**Maps <code>Window.getOwnedWindows()</code> through queue*/
468: public Window[] getOwnedWindows() {
469: return ((Window[]) runMapping(new MapAction("getOwnedWindows") {
470: public Object map() {
471: return (((Window) getSource()).getOwnedWindows());
472: }
473: }));
474: }
475:
476: /**Maps <code>Window.getOwner()</code> through queue*/
477: public Window getOwner() {
478: return ((Window) runMapping(new MapAction("getOwner") {
479: public Object map() {
480: return (((Window) getSource()).getOwner());
481: }
482: }));
483: }
484:
485: /**Maps <code>Window.getWarningString()</code> through queue*/
486: public String getWarningString() {
487: return ((String) runMapping(new MapAction("getWarningString") {
488: public Object map() {
489: return (((Window) getSource()).getWarningString());
490: }
491: }));
492: }
493:
494: /**Maps <code>Window.pack()</code> through queue*/
495: public void pack() {
496: runMapping(new MapVoidAction("pack") {
497: public void map() {
498: ((Window) getSource()).pack();
499: }
500: });
501: }
502:
503: /**Maps <code>Window.removeWindowListener(WindowListener)</code> through queue*/
504: public void removeWindowListener(final WindowListener windowListener) {
505: runMapping(new MapVoidAction("removeWindowListener") {
506: public void map() {
507: ((Window) getSource())
508: .removeWindowListener(windowListener);
509: }
510: });
511: }
512:
513: /**Maps <code>Window.toBack()</code> through queue*/
514: public void toBack() {
515: runMapping(new MapVoidAction("toBack") {
516: public void map() {
517: ((Window) getSource()).toBack();
518: }
519: });
520: }
521:
522: /**Maps <code>Window.toFront()</code> through queue*/
523: public void toFront() {
524: runMapping(new MapVoidAction("toFront") {
525: public void map() {
526: ((Window) getSource()).toFront();
527: }
528: });
529: }
530:
531: //End of mapping //
532: ////////////////////////////////////////////////////////
533:
534: ////////////////////////////////////////////////////////
535: //Mapping 1.4 //
536:
537: /**Maps <code>Window.isFocused()</code> through queue.
538: @return result of the mapped method */
539: public boolean isFocused() {
540: if (System.getProperty("java.specification.version").compareTo(
541: "1.3") > 0) {
542: return (runMapping(new MapBooleanAction("isFocused") {
543: public boolean map() {
544: try {
545: return (((Boolean) new ClassReference(
546: getSource()).invokeMethod("isFocused",
547: null, null)).booleanValue());
548: } catch (InvocationTargetException e) {
549: return (false);
550: } catch (NoSuchMethodException e) {
551: return (false);
552: } catch (IllegalAccessException e) {
553: return (false);
554: }
555: }
556: }));
557: } else {
558: return (getFocusOwner() != null);
559: }
560: }
561:
562: /**Maps <code>Window.isActive()</code> through queue.
563: @return result of the mapped method */
564: public boolean isActive() {
565: if (System.getProperty("java.specification.version").compareTo(
566: "1.3") > 0) {
567: return (runMapping(new MapBooleanAction("isActive") {
568: public boolean map() {
569: try {
570: return (((Boolean) new ClassReference(
571: getSource()).invokeMethod("isActive",
572: null, null)).booleanValue());
573: } catch (InvocationTargetException e) {
574: return (false);
575: } catch (NoSuchMethodException e) {
576: return (false);
577: } catch (IllegalAccessException e) {
578: return (false);
579: }
580: }
581: }));
582: } else {
583: return (isShowing());
584: }
585: }
586:
587: //End of mapping 1.4 //
588: ////////////////////////////////////////////////////////
589:
590: /**
591: * A method to be used from subclasses.
592: * Uses timeouts and output passed as parameters during the waiting.
593: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
594: * @param index Ordinal component index.
595: * @param timeouts timeouts to be used during the waiting.
596: * @param output an output to be used during the waiting.
597: * @return Component instance or null if component was not found.
598: */
599: protected static Window waitWindow(ComponentChooser chooser,
600: int index, Timeouts timeouts, TestOut output) {
601: try {
602: WindowWaiter waiter = new WindowWaiter();
603: waiter.setTimeouts(timeouts);
604: waiter.setOutput(output);
605: return (waiter.waitWindow(chooser, index));
606: } catch (InterruptedException e) {
607: output.printStackTrace(e);
608: return (null);
609: }
610: }
611:
612: /**
613: * A method to be used from subclasses.
614: * Uses <code>owner</code>'s timeouts and output during the waiting.
615: * @param owner a window - dialog owner.
616: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
617: * @param index Ordinal component index.
618: * @return Component instance or null if component was not found.
619: */
620: protected static Window waitWindow(WindowOperator owner,
621: ComponentChooser chooser, int index) {
622: return (waitWindow((Window) owner.getSource(), chooser, index,
623: owner.getTimeouts(), owner.getOutput()));
624: }
625:
626: /**
627: * A method to be used from subclasses.
628: * Uses timeouts and output passed as parameters during the waiting.
629: * @param owner a window - dialog owner.
630: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
631: * @param index Ordinal component index.
632: * @param timeouts timeouts to be used during the waiting.
633: * @param output an output to be used during the waiting.
634: * @return Component instance or null if component was not found.
635: */
636: protected static Window waitWindow(Window owner,
637: ComponentChooser chooser, int index, Timeouts timeouts,
638: TestOut output) {
639: try {
640: WindowWaiter waiter = new WindowWaiter();
641: waiter.setTimeouts(timeouts);
642: waiter.setOutput(output);
643: return (waiter.waitWindow(owner, chooser, index));
644: } catch (InterruptedException e) {
645: JemmyProperties.getCurrentOutput().printStackTrace(e);
646: return (null);
647: }
648: }
649:
650: }
|