FrameOperator.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.netbeans.jemmy.operators; 24 25import java.awt.Component; 26import java.awt.Frame; 27import java.awt.Image; 28import java.awt.MenuBar; 29import java.util.Hashtable; 30 31import org.netbeans.jemmy.ComponentChooser; 32import org.netbeans.jemmy.FrameWaiter; 33import org.netbeans.jemmy.JemmyProperties; 34import org.netbeans.jemmy.Outputable; 35import org.netbeans.jemmy.TestOut; 36import org.netbeans.jemmy.TimeoutExpiredException; 37import org.netbeans.jemmy.Timeouts; 38import org.netbeans.jemmy.drivers.DriverManager; 39import org.netbeans.jemmy.drivers.FrameDriver; 40 41/** 42 * <BR><BR>Timeouts used: <BR> 43 * FrameWaiter.WaitFrameTimeout - time to wait frame displayed <BR> 44 * FrameWaiter.AfterFrameTimeout - time to sleep after frame has been dispayed 45 * <BR> 46 * ComponentOperator.WaitStateTimeout - time to wait for text <BR>. 47 * 48 * @see org.netbeans.jemmy.Timeouts 49 * 50 * @author Alexandre Iline (alexandre.iline@oracle.com) 51 * 52 */ 53public class FrameOperator extends WindowOperator implements Outputable { 54 55 /** 56 * Identifier for a title property. 57 * 58 * @see #getDump 59 */ 60 public static final String TITLE_DPROP = "Title"; 61 62 /** 63 * Identifier for a state property. 64 * 65 * @see #getDump 66 */ 67 public static final String STATE_DPROP = "State"; 68 69 /** 70 * Identifier for a "normal state" state property value. 71 * 72 * @see #getDump 73 */ 74 public static final String STATE_NORMAL_DPROP_VALUE = "NORMAL"; 75 76 /** 77 * Identifier for a "iconified state" state property value. 78 * 79 * @see #getDump 80 */ 81 public static final String STATE_ICONIFIED_DPROP_VALUE = "ICONIFIED"; 82 83 /** 84 * Identifier for a resizable property. 85 * 86 * @see #getDump 87 */ 88 public static final String IS_RESIZABLE_DPROP = "Resizable"; 89 90 TestOut output; 91 FrameDriver driver; 92 93 /** 94 * Constructs a FrameOperator object. 95 * 96 * @param w window 97 */ 98 public FrameOperator(Frame w) { 99 super(w); 100 driver = DriverManager.getFrameDriver(getClass()); 101 } 102 103 /** 104 * Constructs a FrameOperator object. 105 * 106 * @param chooser a component chooser specifying searching criteria. 107 * @param index an index between appropriate ones. 108 * @param env an operator to copy environment from. 109 */ 110 public FrameOperator(ComponentChooser chooser, int index, Operator env) { 111 this(waitFrame(new FrameFinder(chooser), 112 index, 113 env.getTimeouts(), 114 env.getOutput())); 115 copyEnvironment(env); 116 } 117 118 /** 119 * Constructs a FrameOperator object. 120 * 121 * @param chooser a component chooser specifying searching criteria. 122 * @param index an index between appropriate ones. 123 */ 124 public FrameOperator(ComponentChooser chooser, int index) { 125 this(chooser, index, Operator.getEnvironmentOperator()); 126 } 127 128 /** 129 * Constructs a FrameOperator object. 130 * 131 * @param chooser a component chooser specifying searching criteria. 132 */ 133 public FrameOperator(ComponentChooser chooser) { 134 this(chooser, 0); 135 } 136 137 /** 138 * Constructor. Waits for the frame with "title" subtitle. Constructor can 139 * be used in complicated cases when output or timeouts should differ from 140 * default. 141 * 142 * @param title a window title 143 * @param index Ordinal component index. 144 * @param env an operator to copy environment from. 145 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 146 * @throws TimeoutExpiredException 147 */ 148 public FrameOperator(String title, int index, Operator env) { 149 this(waitFrame(new FrameByTitleFinder(title, 150 env.getComparator()), 151 index, 152 env.getTimeouts(), 153 env.getOutput())); 154 copyEnvironment(env); 155 } 156 157 /** 158 * Constructor. Waits for the frame with "title" subtitle. Uses current 159 * timeouts and output values. 160 * 161 * @param title a window title 162 * @param index Ordinal component index. 163 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 164 * @see JemmyProperties#getCurrentTimeouts() 165 * @see JemmyProperties#getCurrentOutput() 166 * @throws TimeoutExpiredException 167 */ 168 public FrameOperator(String title, int index) { 169 this(title, index, 170 ComponentOperator.getEnvironmentOperator()); 171 } 172 173 /** 174 * Constructor. Waits for the frame with "title" subtitle. Uses current 175 * timeouts and output values. 176 * 177 * @param title a window title 178 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 179 * @see JemmyProperties#getCurrentTimeouts() 180 * @see JemmyProperties#getCurrentOutput() 181 * @throws TimeoutExpiredException 182 */ 183 public FrameOperator(String title) { 184 this(title, 0); 185 } 186 187 /** 188 * Constructor. Waits for the index'th frame. Uses current timeout and 189 * output for waiting and to init operator. 190 * 191 * @param index Ordinal component index. 192 * @throws TimeoutExpiredException 193 */ 194 public FrameOperator(int index) { 195 this(waitFrame(new FrameFinder(), 196 index, 197 ComponentOperator.getEnvironmentOperator().getTimeouts(), 198 ComponentOperator.getEnvironmentOperator().getOutput())); 199 copyEnvironment(ComponentOperator.getEnvironmentOperator()); 200 } 201 202 /** 203 * Constructor. Waits for the first frame. Uses current timeout and output 204 * for waiting and to init operator. 205 * 206 * @throws TimeoutExpiredException 207 */ 208 public FrameOperator() { 209 this(0); 210 } 211 212 @Override 213 public void setOutput(TestOut out) { 214 super.setOutput(out); 215 output = out; 216 } 217 218 @Override 219 public TestOut getOutput() { 220 return output; 221 } 222 223 @Override 224 public void copyEnvironment(Operator anotherOperator) { 225 super.copyEnvironment(anotherOperator); 226 driver 227 = (FrameDriver) DriverManager. 228 getDriver(DriverManager.FRAME_DRIVER_ID, 229 getClass(), 230 anotherOperator.getProperties()); 231 } 232 233 /** 234 * Waits for title. Uses getComparator() comparator. 235 * 236 * @param title Title to wait for. 237 */ 238 public void waitTitle(final String title) { 239 getOutput().printLine("Wait \"" + title + "\" title of frame \n : " 240 + toStringSource()); 241 getOutput().printGolden("Wait \"" + title + "\" title"); 242 waitState(new FrameByTitleFinder(title, getComparator())); 243 } 244 245 /** 246 * Iconifies the frame. 247 */ 248 public void iconify() { 249 output.printLine("Iconifying frame\n " + toStringSource()); 250 output.printGolden("Iconifying frame"); 251 driver.iconify(this); 252 if (getVerification()) { 253 waitState(Frame.ICONIFIED); 254 } 255 } 256 257 /** 258 * Deiconifies the frame. 259 */ 260 public void deiconify() { 261 output.printLine("Deiconifying frame\n " + toStringSource()); 262 output.printGolden("Deiconifying frame"); 263 driver.deiconify(this); 264 if (getVerification()) { 265 waitState(Frame.NORMAL); 266 } 267 } 268 269 /** 270 * Maximizes the frame. 271 */ 272 public void maximize() { 273 output.printLine("Maximizing frame\n " + toStringSource()); 274 output.printGolden("Maximizing frame"); 275 driver.maximize(this); 276 if (getVerification()) { 277 waitState(Frame.NORMAL); 278 } 279 } 280 281 /** 282 * Demaximizes the frame. 283 */ 284 public void demaximize() { 285 output.printLine("Demaximizing frame\n " + toStringSource()); 286 output.printGolden("Demaximizing frame"); 287 driver.demaximize(this); 288 if (getVerification()) { 289 waitState(Frame.NORMAL); 290 } 291 } 292 293 /** 294 * Waits for the frame to have a specified state. 295 * 296 * @param state a state for the frame to have. 297 */ 298 public void waitState(final int state) { 299 getOutput().printLine("Wait frame to have " 300 + Integer.toString(state) 301 + " state \n : " 302 + toStringSource()); 303 getOutput().printGolden("Wait frame to have " 304 + Integer.toString(state) 305 + " state"); 306 waitState(new ComponentChooser() { 307 @Override 308 public boolean checkComponent(Component comp) { 309 return ((Frame) comp).getState() == state; 310 } 311 312 @Override 313 public String getDescription() { 314 return Integer.toString(state) + " state"; 315 } 316 317 @Override 318 public String toString() { 319 return "FrameOperator.waitState.ComponentChooser{description = " + getDescription() + '}'; 320 } 321 }); 322 } 323 324 /** 325 * Returns information about component. 326 */ 327 @Override 328 public Hashtable<String, Object> getDump() { 329 Hashtable<String, Object> result = super.getDump(); 330 if (((Frame) getSource()).getTitle() != null) { 331 result.put(TITLE_DPROP, ((Frame) getSource()).getTitle()); 332 } 333 result.put(STATE_DPROP, 334 (((Frame) getSource()).getState() == Frame.ICONIFIED) 335 ? STATE_ICONIFIED_DPROP_VALUE : STATE_NORMAL_DPROP_VALUE); 336 result.put(IS_RESIZABLE_DPROP, ((Frame) getSource()).isResizable() ? "true" : "false"); 337 return result; 338 } 339 340 //////////////////////////////////////////////////////// 341 //Mapping // 342 /** 343 * Maps {@code Frame.getIconImage()} through queue 344 */ 345 public Image getIconImage() { 346 return (runMapping(new MapAction<Image>("getIconImage") { 347 @Override 348 public Image map() { 349 return ((Frame) getSource()).getIconImage(); 350 } 351 })); 352 } 353 354 /** 355 * Maps {@code Frame.getMenuBar()} through queue 356 */ 357 public MenuBar getMenuBar() { 358 return (runMapping(new MapAction<MenuBar>("getMenuBar") { 359 @Override 360 public MenuBar map() { 361 return ((Frame) getSource()).getMenuBar(); 362 } 363 })); 364 } 365 366 /** 367 * Maps {@code Frame.getState()} through queue 368 */ 369 public int getState() { 370 return (runMapping(new MapIntegerAction("getState") { 371 @Override 372 public int map() { 373 return ((Frame) getSource()).getState(); 374 } 375 })); 376 } 377 378 /** 379 * Maps {@code Frame.getTitle()} through queue 380 */ 381 public String getTitle() { 382 return (runMapping(new MapAction<String>("getTitle") { 383 @Override 384 public String map() { 385 return ((Frame) getSource()).getTitle(); 386 } 387 })); 388 } 389 390 /** 391 * Maps {@code Frame.isResizable()} through queue 392 */ 393 public boolean isResizable() { 394 return (runMapping(new MapBooleanAction("isResizable") { 395 @Override 396 public boolean map() { 397 return ((Frame) getSource()).isResizable(); 398 } 399 })); 400 } 401 402 /** 403 * Maps {@code Frame.setIconImage(Image)} through queue 404 */ 405 public void setIconImage(final Image image) { 406 runMapping(new MapVoidAction("setIconImage") { 407 @Override 408 public void map() { 409 ((Frame) getSource()).setIconImage(image); 410 } 411 }); 412 } 413 414 /** 415 * Maps {@code Frame.setMenuBar(MenuBar)} through queue 416 */ 417 public void setMenuBar(final MenuBar menuBar) { 418 runMapping(new MapVoidAction("setMenuBar") { 419 @Override 420 public void map() { 421 ((Frame) getSource()).setMenuBar(menuBar); 422 } 423 }); 424 } 425 426 /** 427 * Maps {@code Frame.setResizable(boolean)} through queue 428 */ 429 public void setResizable(final boolean b) { 430 runMapping(new MapVoidAction("setResizable") { 431 @Override 432 public void map() { 433 ((Frame) getSource()).setResizable(b); 434 } 435 }); 436 } 437 438 /** 439 * Maps {@code Frame.setState(int)} through queue 440 */ 441 public void setState(final int i) { 442 runMapping(new MapVoidAction("setState") { 443 @Override 444 public void map() { 445 ((Frame) getSource()).setState(i); 446 } 447 }); 448 } 449 450 /** 451 * Maps {@code Frame.setTitle(String)} through queue 452 */ 453 public void setTitle(final String string) { 454 runMapping(new MapVoidAction("setTitle") { 455 @Override 456 public void map() { 457 ((Frame) getSource()).setTitle(string); 458 } 459 }); 460 } 461 462 //End of mapping // 463 //////////////////////////////////////////////////////// 464 /** 465 * A method to be used from subclasses. Uses timeouts and output passed as 466 * parameters during the waiting. 467 * 468 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 469 * @param index Ordinal component index. 470 * @param timeouts timeouts to be used during the waiting. 471 * @param output an output to be used during the waiting. 472 * @return Component instance or null if component was not found. 473 * @throws TimeoutExpiredException 474 */ 475 protected static Frame waitFrame(ComponentChooser chooser, int index, 476 Timeouts timeouts, TestOut output) { 477 try { 478 FrameWaiter waiter = new FrameWaiter(); 479 waiter.setTimeouts(timeouts); 480 waiter.setOutput(output); 481 return waiter.waitFrame(new FrameFinder(chooser), index); 482 } catch (InterruptedException e) { 483 output.printStackTrace(e); 484 return null; 485 } 486 } 487 488 /** 489 * Checks component type. 490 */ 491 public static class FrameFinder extends Finder { 492 493 /** 494 * Constructs FrameFinder. 495 * 496 * @param sf other searching criteria. 497 */ 498 public FrameFinder(ComponentChooser sf) { 499 super(Frame.class, sf); 500 } 501 502 /** 503 * Constructs FrameFinder. 504 */ 505 public FrameFinder() { 506 super(Frame.class); 507 } 508 } 509 510 /** 511 * Allows to find component by title. 512 */ 513 public static class FrameByTitleFinder implements ComponentChooser { 514 515 String title; 516 StringComparator comparator; 517 518 /** 519 * Constructs FrameByTitleFinder. 520 * 521 * @param t a text pattern 522 * @param comparator specifies string comparision algorithm. 523 */ 524 public FrameByTitleFinder(String t, StringComparator comparator) { 525 title = t; 526 this.comparator = comparator; 527 } 528 529 /** 530 * Constructs FrameByTitleFinder. 531 * 532 * @param t a text pattern 533 */ 534 public FrameByTitleFinder(String t) { 535 this(t, Operator.getDefaultStringComparator()); 536 } 537 538 @Override 539 public boolean checkComponent(Component comp) { 540 if (comp instanceof Frame) { 541 if (comp.isShowing() && ((Frame) comp).getTitle() != null) { 542 return comparator.equals(((Frame) comp).getTitle(), title); 543 } 544 } 545 return false; 546 } 547 548 @Override 549 public String getDescription() { 550 return "Frame with title \"" + title + "\""; 551 } 552 553 @Override 554 public String toString() { 555 return "FrameByTitleFinder{" + "title=" + title + ", comparator=" + comparator + '}'; 556 } 557 } 558} 559