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: FrameOperator.java,v 1.12 2007/10/05 11:35:28 jskrivanek Exp $ $Revision: 1.12 $ $Date: 2007/10/05 11:35:28 $
044: *
045: */
046:
047: package org.netbeans.jemmy.operators;
048:
049: import org.netbeans.jemmy.ComponentChooser;
050: import org.netbeans.jemmy.ComponentSearcher;
051: import org.netbeans.jemmy.FrameWaiter;
052: import org.netbeans.jemmy.Outputable;
053: import org.netbeans.jemmy.TestOut;
054: import org.netbeans.jemmy.TimeoutExpiredException;
055: import org.netbeans.jemmy.Timeouts;
056:
057: import org.netbeans.jemmy.drivers.FrameDriver;
058: import org.netbeans.jemmy.drivers.DriverManager;
059:
060: import java.awt.Component;
061: import java.awt.Image;
062: import java.awt.Frame;
063: import java.awt.MenuBar;
064:
065: import java.util.Hashtable;
066:
067: /**
068: * <BR><BR>Timeouts used: <BR>
069: * FrameWaiter.WaitFrameTimeout - time to wait frame displayed <BR>
070: * FrameWaiter.AfterFrameTimeout - time to sleep after frame has been dispayed <BR>
071: * ComponentOperator.WaitStateTimeout - time to wait for text <BR>.
072: *
073: * @see org.netbeans.jemmy.Timeouts
074: *
075: * @author Alexandre Iline (alexandre.iline@sun.com)
076: *
077: */
078:
079: public class FrameOperator extends WindowOperator implements Outputable {
080:
081: /**
082: * Identifier for a title property.
083: * @see #getDump
084: */
085: public static final String TITLE_DPROP = "Title";
086:
087: /**
088: * Identifier for a state property.
089: * @see #getDump
090: */
091: public static final String STATE_DPROP = "State";
092:
093: /**
094: * Identifier for a "normal state" state property value.
095: * @see #getDump
096: */
097: public static final String STATE_NORMAL_DPROP_VALUE = "NORMAL";
098:
099: /**
100: * Identifier for a "iconified state" state property value.
101: * @see #getDump
102: */
103: public static final String STATE_ICONIFIED_DPROP_VALUE = "ICONIFIED";
104:
105: /**
106: * Identifier for a resizable property.
107: * @see #getDump
108: */
109: public static final String IS_RESIZABLE_DPROP = "Resizable";
110:
111: TestOut output;
112: FrameDriver driver;
113:
114: /**
115: * Constructs a FrameOperator object.
116: * @param w window
117: */
118: public FrameOperator(Frame w) {
119: super (w);
120: driver = DriverManager.getFrameDriver(getClass());
121: }
122:
123: /**
124: * Constructs a FrameOperator object.
125: * @param chooser a component chooser specifying searching criteria.
126: * @param index an index between appropriate ones.
127: * @param env an operator to copy environment from.
128: */
129: public FrameOperator(ComponentChooser chooser, int index,
130: Operator env) {
131: this (waitFrame(new FrameFinder(chooser), index, env
132: .getTimeouts(), env.getOutput()));
133: copyEnvironment(env);
134: }
135:
136: /**
137: * Constructs a FrameOperator object.
138: * @param chooser a component chooser specifying searching criteria.
139: * @param index an index between appropriate ones.
140: */
141: public FrameOperator(ComponentChooser chooser, int index) {
142: this (chooser, index, Operator.getEnvironmentOperator());
143: }
144:
145: /**
146: * Constructs a FrameOperator object.
147: * @param chooser a component chooser specifying searching criteria.
148: */
149: public FrameOperator(ComponentChooser chooser) {
150: this (chooser, 0);
151: }
152:
153: /**
154: * Constructor.
155: * Waits for the frame with "title" subtitle.
156: * Constructor can be used in complicated cases when
157: * output or timeouts should differ from default.
158: * @param title a window title
159: * @param index Ordinal component index.
160: * @param env an operator to copy environment from.
161: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
162: * @throws TimeoutExpiredException
163: */
164: public FrameOperator(String title, int index, Operator env) {
165: this (waitFrame(new FrameByTitleFinder(title, env
166: .getComparator()), index, env.getTimeouts(), env
167: .getOutput()));
168: copyEnvironment(env);
169: }
170:
171: /**
172: * Constructor.
173: * Waits for the frame with "title" subtitle.
174: * Uses current timeouts and output values.
175: * @param title a window title
176: * @param index Ordinal component index.
177: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
178: * @see JemmyProperties#getCurrentTimeouts()
179: * @see JemmyProperties#getCurrentOutput()
180: * @throws TimeoutExpiredException
181: */
182: public FrameOperator(String title, int index) {
183: this (title, index, ComponentOperator.getEnvironmentOperator());
184: }
185:
186: /**
187: * Constructor.
188: * Waits for the frame with "title" subtitle.
189: * Uses current timeouts and output values.
190: * @param title a window title
191: * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean)
192: * @see JemmyProperties#getCurrentTimeouts()
193: * @see JemmyProperties#getCurrentOutput()
194: * @throws TimeoutExpiredException
195: */
196: public FrameOperator(String title) {
197: this (title, 0);
198: }
199:
200: /**
201: * Constructor.
202: * Waits for the index'th frame.
203: * Uses current timeout and output for waiting and to init operator.
204: * @param index Ordinal component index.
205: * @throws TimeoutExpiredException
206: */
207: public FrameOperator(int index) {
208: this ((Frame) waitFrame(new FrameFinder(), index,
209: ComponentOperator.getEnvironmentOperator()
210: .getTimeouts(), ComponentOperator
211: .getEnvironmentOperator().getOutput()));
212: copyEnvironment(ComponentOperator.getEnvironmentOperator());
213: }
214:
215: /**
216: * Constructor.
217: * Waits for the first frame.
218: * Uses current timeout and output for waiting and to init operator.
219: * @throws TimeoutExpiredException
220: */
221: public FrameOperator() {
222: this (0);
223: }
224:
225: public void setOutput(TestOut out) {
226: super .setOutput(out);
227: output = out;
228: }
229:
230: public TestOut getOutput() {
231: return (output);
232: }
233:
234: public void copyEnvironment(Operator anotherOperator) {
235: super .copyEnvironment(anotherOperator);
236: driver = (FrameDriver) DriverManager.getDriver(
237: DriverManager.FRAME_DRIVER_ID, getClass(),
238: anotherOperator.getProperties());
239: }
240:
241: /**
242: * Waits for title. Uses getComparator() comparator.
243: * @param title Title to wait for.
244: */
245: public void waitTitle(final String title) {
246: getOutput().printLine(
247: "Wait \"" + title + "\" title of frame \n : "
248: + toStringSource());
249: getOutput().printGolden("Wait \"" + title + "\" title");
250: waitState(new FrameByTitleFinder(title, getComparator()));
251: }
252:
253: /**
254: * Iconifies the frame.
255: */
256: public void iconify() {
257: output.printLine("Iconifying frame\n " + toStringSource());
258: output.printGolden("Iconifying frame");
259: driver.iconify(this );
260: if (getVerification()) {
261: waitState(Frame.ICONIFIED);
262: }
263: }
264:
265: /**
266: * Deiconifies the frame.
267: */
268: public void deiconify() {
269: output.printLine("Deiconifying frame\n " + toStringSource());
270: output.printGolden("Deiconifying frame");
271: driver.deiconify(this );
272: if (getVerification()) {
273: waitState(Frame.NORMAL);
274: }
275: }
276:
277: /**
278: * Maximizes the frame.
279: */
280: public void maximize() {
281: output.printLine("Maximizing frame\n " + toStringSource());
282: output.printGolden("Maximizing frame");
283: driver.maximize(this );
284: if (getVerification()) {
285: waitState(Frame.NORMAL);
286: }
287: }
288:
289: /**
290: * Demaximizes the frame.
291: */
292: public void demaximize() {
293: output.printLine("Demaximizing frame\n " + toStringSource());
294: output.printGolden("Demaximizing frame");
295: driver.demaximize(this );
296: if (getVerification()) {
297: waitState(Frame.NORMAL);
298: }
299: }
300:
301: /**
302: * Waits for the frame to have a specified state.
303: * @param state a state for the frame to have.
304: */
305: public void waitState(final int state) {
306: getOutput().printLine(
307: "Wait frame to have " + Integer.toString(state)
308: + " state \n : " + toStringSource());
309: getOutput().printGolden(
310: "Wait frame to have " + Integer.toString(state)
311: + " state");
312: waitState(new ComponentChooser() {
313: public boolean checkComponent(Component comp) {
314: return (((Frame) comp).getState() == state);
315: }
316:
317: public String getDescription() {
318: return (Integer.toString(state) + " state");
319: }
320: });
321: }
322:
323: /**
324: * Returns information about component.
325: */
326: public Hashtable getDump() {
327: Hashtable result = super .getDump();
328: if (((Frame) getSource()).getTitle() != null) {
329: result.put(TITLE_DPROP, ((Frame) getSource()).getTitle());
330: }
331: result
332: .put(
333: STATE_DPROP,
334: (((Frame) getSource()).getState() == Frame.ICONIFIED) ? STATE_ICONIFIED_DPROP_VALUE
335: : STATE_NORMAL_DPROP_VALUE);
336: result.put(IS_RESIZABLE_DPROP, ((Frame) getSource())
337: .isResizable() ? "true" : "false");
338: return (result);
339: }
340:
341: ////////////////////////////////////////////////////////
342: //Mapping //
343:
344: /**Maps <code>Frame.getIconImage()</code> through queue*/
345: public Image getIconImage() {
346: return ((Image) runMapping(new MapAction("getIconImage") {
347: public Object map() {
348: return (((Frame) getSource()).getIconImage());
349: }
350: }));
351: }
352:
353: /**Maps <code>Frame.getMenuBar()</code> through queue*/
354: public MenuBar getMenuBar() {
355: return ((MenuBar) runMapping(new MapAction("getMenuBar") {
356: public Object map() {
357: return (((Frame) getSource()).getMenuBar());
358: }
359: }));
360: }
361:
362: /**Maps <code>Frame.getState()</code> through queue*/
363: public int getState() {
364: return (runMapping(new MapIntegerAction("getState") {
365: public int map() {
366: return (((Frame) getSource()).getState());
367: }
368: }));
369: }
370:
371: /**Maps <code>Frame.getTitle()</code> through queue*/
372: public String getTitle() {
373: return ((String) runMapping(new MapAction("getTitle") {
374: public Object map() {
375: return (((Frame) getSource()).getTitle());
376: }
377: }));
378: }
379:
380: /**Maps <code>Frame.isResizable()</code> through queue*/
381: public boolean isResizable() {
382: return (runMapping(new MapBooleanAction("isResizable") {
383: public boolean map() {
384: return (((Frame) getSource()).isResizable());
385: }
386: }));
387: }
388:
389: /**Maps <code>Frame.setIconImage(Image)</code> through queue*/
390: public void setIconImage(final Image image) {
391: runMapping(new MapVoidAction("setIconImage") {
392: public void map() {
393: ((Frame) getSource()).setIconImage(image);
394: }
395: });
396: }
397:
398: /**Maps <code>Frame.setMenuBar(MenuBar)</code> through queue*/
399: public void setMenuBar(final MenuBar menuBar) {
400: runMapping(new MapVoidAction("setMenuBar") {
401: public void map() {
402: ((Frame) getSource()).setMenuBar(menuBar);
403: }
404: });
405: }
406:
407: /**Maps <code>Frame.setResizable(boolean)</code> through queue*/
408: public void setResizable(final boolean b) {
409: runMapping(new MapVoidAction("setResizable") {
410: public void map() {
411: ((Frame) getSource()).setResizable(b);
412: }
413: });
414: }
415:
416: /**Maps <code>Frame.setState(int)</code> through queue*/
417: public void setState(final int i) {
418: runMapping(new MapVoidAction("setState") {
419: public void map() {
420: ((Frame) getSource()).setState(i);
421: }
422: });
423: }
424:
425: /**Maps <code>Frame.setTitle(String)</code> through queue*/
426: public void setTitle(final String string) {
427: runMapping(new MapVoidAction("setTitle") {
428: public void map() {
429: ((Frame) getSource()).setTitle(string);
430: }
431: });
432: }
433:
434: //End of mapping //
435: ////////////////////////////////////////////////////////
436:
437: /**
438: * A method to be used from subclasses.
439: * Uses timeouts and output passed as parameters during the waiting.
440: * @param chooser org.netbeans.jemmy.ComponentChooser implementation.
441: * @param index Ordinal component index.
442: * @param timeouts timeouts to be used during the waiting.
443: * @param output an output to be used during the waiting.
444: * @return Component instance or null if component was not found.
445: * @throws TimeoutExpiredException
446: */
447: protected static Frame waitFrame(ComponentChooser chooser,
448: int index, Timeouts timeouts, TestOut output) {
449: try {
450: FrameWaiter waiter = new FrameWaiter();
451: waiter.setTimeouts(timeouts);
452: waiter.setOutput(output);
453: return ((Frame) waiter.waitFrame(new FrameFinder(chooser),
454: index));
455: } catch (InterruptedException e) {
456: output.printStackTrace(e);
457: return (null);
458: }
459: }
460:
461: /**
462: * Checks component type.
463: */
464: public static class FrameFinder extends Finder {
465: /**
466: * Constructs FrameFinder.
467: * @param sf other searching criteria.
468: */
469: public FrameFinder(ComponentChooser sf) {
470: super (Frame.class, sf);
471: }
472:
473: /**
474: * Constructs FrameFinder.
475: */
476: public FrameFinder() {
477: super (Frame.class);
478: }
479: }
480:
481: /**
482: * Allows to find component by title.
483: */
484: public static class FrameByTitleFinder implements ComponentChooser {
485: String title;
486: StringComparator comparator;
487:
488: /**
489: * Constructs FrameByTitleFinder.
490: * @param t a text pattern
491: * @param comparator specifies string comparision algorithm.
492: */
493: public FrameByTitleFinder(String t, StringComparator comparator) {
494: title = t;
495: this .comparator = comparator;
496: }
497:
498: /**
499: * Constructs FrameByTitleFinder.
500: * @param t a text pattern
501: */
502: public FrameByTitleFinder(String t) {
503: this (t, Operator.getDefaultStringComparator());
504: }
505:
506: public boolean checkComponent(Component comp) {
507: if (comp instanceof Frame) {
508: if (((Frame) comp).isShowing()
509: && ((Frame) comp).getTitle() != null) {
510: return (comparator.equals(
511: ((Frame) comp).getTitle(), title));
512: }
513: }
514: return (false);
515: }
516:
517: public String getDescription() {
518: return ("Frame with title \"" + title + "\"");
519: }
520: }
521: }
|