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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.netbeans.jemmy.operators; 26 27 import java.awt.Component; 28 import java.awt.Container; 29 import java.util.Hashtable; 30 31 import javax.swing.BoundedRangeModel; 32 import javax.swing.JProgressBar; 33 import javax.swing.event.ChangeListener; 34 import javax.swing.plaf.ProgressBarUI; 35 36 import org.netbeans.jemmy.ComponentChooser; 37 import org.netbeans.jemmy.ComponentSearcher; 38 import org.netbeans.jemmy.JemmyException; 39 import org.netbeans.jemmy.Outputable; 40 import org.netbeans.jemmy.TestOut; 41 import org.netbeans.jemmy.TimeoutExpiredException; 42 import org.netbeans.jemmy.Timeoutable; 43 import org.netbeans.jemmy.Timeouts; 44 import org.netbeans.jemmy.Waitable; 45 import org.netbeans.jemmy.Waiter; 46 47 /** 48 * 49 * Operator is supposed to be used to operate with an instance of 50 * javax.swing.JProgressBar class. 51 * 52 * <BR><BR>Timeouts used: <BR> 53 * JProgressBarOperator.WaitValueTimeout - used from waitValue() method <BR>. 54 * 55 * @see org.netbeans.jemmy.Timeouts 56 * 57 * @author Alexandre Iline (alexandre.iline@oracle.com) 58 */ 59 public class JProgressBarOperator extends JComponentOperator 60 implements Timeoutable, Outputable { 61 62 /** 63 * Identifier for a "minimum" property. 64 * 65 * @see #getDump 66 */ 67 public static final String MINIMUM_DPROP = "Minimum"; 68 69 /** 70 * Identifier for a "maximum" property. 71 * 72 * @see #getDump 73 */ 74 public static final String MAXIMUM_DPROP = "Maximum"; 75 76 /** 77 * Identifier for a "value" property. 78 * 79 * @see #getDump 80 */ 81 public static final String VALUE_DPROP = "Value"; 82 83 private static long WAIT_VALUE_TIMEOUT = 60000; 84 85 private Timeouts timeouts; 86 private TestOut output; 87 88 /** 89 * Constructor. 90 * 91 * @param b JProgressBar component. 92 */ JProgressBarOperator(JProgressBar b)93 public JProgressBarOperator(JProgressBar b) { 94 super(b); 95 } 96 97 /** 98 * Constructs a JProgressBarOperator object. 99 * 100 * @param cont a container 101 * @param chooser a component chooser specifying searching criteria. 102 * @param index an index between appropriate ones. 103 */ JProgressBarOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index)104 public JProgressBarOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 105 this((JProgressBar) cont. 106 waitSubComponent(new JProgressBarFinder(chooser), 107 index)); 108 copyEnvironment(cont); 109 } 110 111 /** 112 * Constructs a JProgressBarOperator object. 113 * 114 * @param cont a container 115 * @param chooser a component chooser specifying searching criteria. 116 */ JProgressBarOperator(ContainerOperator<?> cont, ComponentChooser chooser)117 public JProgressBarOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 118 this(cont, chooser, 0); 119 } 120 121 /** 122 * Constructor. Waits component in container first. Uses cont's timeout and 123 * output for waiting and to init operator. 124 * 125 * @param cont Operator pointing a container to search component in. 126 * @param index Ordinal component index. 127 * @throws TimeoutExpiredException 128 */ JProgressBarOperator(ContainerOperator<?> cont, int index)129 public JProgressBarOperator(ContainerOperator<?> cont, int index) { 130 this((JProgressBar) waitComponent(cont, 131 new JProgressBarFinder(), 132 index)); 133 copyEnvironment(cont); 134 } 135 136 /** 137 * Constructor. Waits component in container first. Uses cont's timeout and 138 * output for waiting and to init operator. 139 * 140 * @param cont Operator pointing a container to search component in. 141 * @throws TimeoutExpiredException 142 */ JProgressBarOperator(ContainerOperator<?> cont)143 public JProgressBarOperator(ContainerOperator<?> cont) { 144 this(cont, 0); 145 } 146 147 /** 148 * Searches JProgressBar in container. 149 * 150 * @param cont Container to search component in. 151 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 152 * @param index Ordinal component index. 153 * @return JProgressBar instance or null if component was not found. 154 */ findJProgressBar(Container cont, ComponentChooser chooser, int index)155 public static JProgressBar findJProgressBar(Container cont, ComponentChooser chooser, int index) { 156 return (JProgressBar) findComponent(cont, new JProgressBarFinder(chooser), index); 157 } 158 159 /** 160 * Searches 0'th JProgressBar in container. 161 * 162 * @param cont Container to search component in. 163 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 164 * @return JProgressBar instance or null if component was not found. 165 */ findJProgressBar(Container cont, ComponentChooser chooser)166 public static JProgressBar findJProgressBar(Container cont, ComponentChooser chooser) { 167 return findJProgressBar(cont, chooser, 0); 168 } 169 170 /** 171 * Searches JProgressBar in container. 172 * 173 * @param cont Container to search component in. 174 * @param index Ordinal component index. 175 * @return JProgressBar instance or null if component was not found. 176 */ findJProgressBar(Container cont, int index)177 public static JProgressBar findJProgressBar(Container cont, int index) { 178 return findJProgressBar(cont, ComponentSearcher.getTrueChooser(Integer.toString(index) + "'th JProgressBar instance"), index); 179 } 180 181 /** 182 * Searches 0'th JProgressBar in container. 183 * 184 * @param cont Container to search component in. 185 * @return JProgressBar instance or null if component was not found. 186 */ findJProgressBar(Container cont)187 public static JProgressBar findJProgressBar(Container cont) { 188 return findJProgressBar(cont, 0); 189 } 190 191 /** 192 * Waits JProgressBar in container. 193 * 194 * @param cont Container to search component in. 195 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 196 * @param index Ordinal component index. 197 * @return JProgressBar instance or null if component was not displayed. 198 * @throws TimeoutExpiredException 199 */ waitJProgressBar(Container cont, ComponentChooser chooser, int index)200 public static JProgressBar waitJProgressBar(Container cont, ComponentChooser chooser, int index) { 201 return (JProgressBar) waitComponent(cont, new JProgressBarFinder(chooser), index); 202 } 203 204 /** 205 * Waits 0'th JProgressBar in container. 206 * 207 * @param cont Container to search component in. 208 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 209 * @return JProgressBar instance or null if component was not displayed. 210 * @throws TimeoutExpiredException 211 */ waitJProgressBar(Container cont, ComponentChooser chooser)212 public static JProgressBar waitJProgressBar(Container cont, ComponentChooser chooser) { 213 return waitJProgressBar(cont, chooser, 0); 214 } 215 216 /** 217 * Waits JProgressBar in container. 218 * 219 * @param cont Container to search component in. 220 * @param index Ordinal component index. 221 * @return JProgressBar instance or null if component was not displayed. 222 * @throws TimeoutExpiredException 223 */ waitJProgressBar(Container cont, int index)224 public static JProgressBar waitJProgressBar(Container cont, int index) { 225 return waitJProgressBar(cont, ComponentSearcher.getTrueChooser(Integer.toString(index) + "'th JProgressBar instance"), index); 226 } 227 228 /** 229 * Waits 0'th JProgressBar in container. 230 * 231 * @param cont Container to search component in. 232 * @return JProgressBar instance or null if component was not displayed. 233 * @throws TimeoutExpiredException 234 */ waitJProgressBar(Container cont)235 public static JProgressBar waitJProgressBar(Container cont) { 236 return waitJProgressBar(cont, 0); 237 } 238 239 static { 240 Timeouts.initDefault("JProgressBarOperator.WaitValueTimeout", WAIT_VALUE_TIMEOUT); 241 } 242 243 @Override setTimeouts(Timeouts timeouts)244 public void setTimeouts(Timeouts timeouts) { 245 this.timeouts = timeouts; 246 super.setTimeouts(timeouts); 247 } 248 249 @Override getTimeouts()250 public Timeouts getTimeouts() { 251 return timeouts; 252 } 253 254 @Override setOutput(TestOut out)255 public void setOutput(TestOut out) { 256 output = out; 257 super.setOutput(output.createErrorOutput()); 258 } 259 260 @Override getOutput()261 public TestOut getOutput() { 262 return output; 263 } 264 265 /** 266 * Waits for criteria defined by {@code chooser} to be reached. 267 * 268 * @param chooser an object specifying waiting criteria. 269 * @see #waitValue(int) 270 * @deprecated Use waitState(ComponentChooser) instead. 271 */ 272 @Deprecated waitValue(final ValueChooser chooser)273 public void waitValue(final ValueChooser chooser) { 274 output.printLine("Wait \"" + chooser.getDescription() 275 + "\" value in progressbar\n : " 276 + toStringSource()); 277 output.printGolden("Wait \"" + chooser.getDescription() 278 + "\" value in progressbar"); 279 Waiter<String, Void> wt = new Waiter<>(new Waitable<String, Void>() { 280 @Override 281 public String actionProduced(Void obj) { 282 return (chooser.checkValue(((JProgressBar) getSource()).getValue()) 283 ? "" : null); 284 } 285 286 @Override 287 public String getDescription() { 288 return "\"" + chooser.getDescription() + "\" value"; 289 } 290 291 @Override 292 public String toString() { 293 return "JProgressBarOperator.waitValue.Waitable{description = " + getDescription() + '}'; 294 } 295 }); 296 wt.setTimeoutsToCloneOf(timeouts, "JProgressBarOperator.WaitValueTimeout"); 297 wt.setOutput(output.createErrorOutput()); 298 try { 299 wt.waitAction(null); 300 } catch (InterruptedException e) { 301 throw (new JemmyException("Exception during progressbar value waiting", e)); 302 } 303 } 304 305 /** 306 * Waits progress bar value to be less or equal to {@code value} 307 * parameter. Can be used for typical progress bar (when value is 308 * increasing). 309 * 310 * @param value a value to reach. 311 * @see Operator#waitState(ComponentChooser) 312 */ waitValue(final int value)313 public void waitValue(final int value) { 314 output.printLine("Wait \"" + value 315 + "\" value in progressbar\n : " 316 + toStringSource()); 317 output.printGolden("Wait \"" + value 318 + "\" value in progressbar"); 319 waitState(new ComponentChooser() { 320 @Override 321 public boolean checkComponent(Component comp) { 322 return ((JProgressBar) comp).getValue() >= value; 323 } 324 325 @Override 326 public String getDescription() { 327 return "greater then " + Integer.toString(value); 328 } 329 330 @Override 331 public String toString() { 332 return "JProgressBarOperator.waitValue.ComponentChooser{description = " + getDescription() + '}'; 333 } 334 }); 335 } 336 337 /** 338 * Waits progress bar string to match {@code value} parameter. 339 * 340 * @param value a string value. 341 * @see Operator#waitState(ComponentChooser) 342 */ waitValue(final String value)343 public void waitValue(final String value) { 344 output.printLine("Wait \"" + value 345 + "\" string in progressbar\n : " 346 + toStringSource()); 347 output.printGolden("Wait \"" + value 348 + "\" string in progressbar"); 349 waitState(new ComponentChooser() { 350 @Override 351 public boolean checkComponent(Component comp) { 352 return getComparator().equals(((JProgressBar) comp).getString(), value); 353 } 354 355 @Override 356 public String getDescription() { 357 return "'" + value + "' string"; 358 } 359 360 @Override 361 public String toString() { 362 return "JProgressBarOperator.waitValue.ComponentChooser{description = " + getDescription() + '}'; 363 } 364 }); 365 } 366 367 @Override getDump()368 public Hashtable<String, Object> getDump() { 369 Hashtable<String, Object> result = super.getDump(); 370 result.put(MINIMUM_DPROP, Integer.toString(((JProgressBar) getSource()).getMinimum())); 371 result.put(MAXIMUM_DPROP, Integer.toString(((JProgressBar) getSource()).getMaximum())); 372 result.put(VALUE_DPROP, Integer.toString(((JProgressBar) getSource()).getValue())); 373 return result; 374 } 375 376 //////////////////////////////////////////////////////// 377 //Mapping // 378 /** 379 * Maps {@code JProgressBar.addChangeListener(ChangeListener)} through queue 380 */ addChangeListener(final ChangeListener changeListener)381 public void addChangeListener(final ChangeListener changeListener) { 382 runMapping(new MapVoidAction("addChangeListener") { 383 @Override 384 public void map() { 385 ((JProgressBar) getSource()).addChangeListener(changeListener); 386 } 387 }); 388 } 389 390 /** 391 * Maps {@code JProgressBar.getMaximum()} through queue 392 */ getMaximum()393 public int getMaximum() { 394 return (runMapping(new MapIntegerAction("getMaximum") { 395 @Override 396 public int map() { 397 return ((JProgressBar) getSource()).getMaximum(); 398 } 399 })); 400 } 401 402 /** 403 * Maps {@code JProgressBar.getMinimum()} through queue 404 */ 405 public int getMinimum() { 406 return (runMapping(new MapIntegerAction("getMinimum") { 407 @Override 408 public int map() { 409 return ((JProgressBar) getSource()).getMinimum(); 410 } 411 })); 412 } 413 414 /** 415 * Maps {@code JProgressBar.getModel()} through queue 416 */ 417 public BoundedRangeModel getModel() { 418 return (runMapping(new MapAction<BoundedRangeModel>("getModel") { 419 @Override 420 public BoundedRangeModel map() { 421 return ((JProgressBar) getSource()).getModel(); 422 } 423 })); 424 } 425 426 /** 427 * Maps {@code JProgressBar.getOrientation()} through queue 428 */ 429 public int getOrientation() { 430 return (runMapping(new MapIntegerAction("getOrientation") { 431 @Override 432 public int map() { 433 return ((JProgressBar) getSource()).getOrientation(); 434 } 435 })); 436 } 437 438 /** 439 * Maps {@code JProgressBar.getPercentComplete()} through queue 440 */ 441 public double getPercentComplete() { 442 return (runMapping(new MapDoubleAction("getPercentComplete") { 443 @Override 444 public double map() { 445 return ((JProgressBar) getSource()).getPercentComplete(); 446 } 447 })); 448 } 449 450 /** 451 * Maps {@code JProgressBar.getString()} through queue 452 */ 453 public String getString() { 454 return (runMapping(new MapAction<String>("getString") { 455 @Override 456 public String map() { 457 return ((JProgressBar) getSource()).getString(); 458 } 459 })); 460 } 461 462 /** 463 * Maps {@code JProgressBar.getUI()} through queue 464 */ 465 public ProgressBarUI getUI() { 466 return (runMapping(new MapAction<ProgressBarUI>("getUI") { 467 @Override 468 public ProgressBarUI map() { 469 return ((JProgressBar) getSource()).getUI(); 470 } 471 })); 472 } 473 474 /** 475 * Maps {@code JProgressBar.getValue()} through queue 476 */ 477 public int getValue() { 478 return (runMapping(new MapIntegerAction("getValue") { 479 @Override 480 public int map() { 481 return ((JProgressBar) getSource()).getValue(); 482 } 483 })); 484 } 485 486 /** 487 * Maps {@code JProgressBar.isBorderPainted()} through queue 488 */ 489 public boolean isBorderPainted() { 490 return (runMapping(new MapBooleanAction("isBorderPainted") { 491 @Override 492 public boolean map() { 493 return ((JProgressBar) getSource()).isBorderPainted(); 494 } 495 })); 496 } 497 498 /** 499 * Maps {@code JProgressBar.isStringPainted()} through queue 500 */ 501 public boolean isStringPainted() { 502 return (runMapping(new MapBooleanAction("isStringPainted") { 503 @Override 504 public boolean map() { 505 return ((JProgressBar) getSource()).isStringPainted(); 506 } 507 })); 508 } 509 510 /** 511 * Maps {@code JProgressBar.removeChangeListener(ChangeListener)} 512 * through queue 513 */ 514 public void removeChangeListener(final ChangeListener changeListener) { 515 runMapping(new MapVoidAction("removeChangeListener") { 516 @Override 517 public void map() { 518 ((JProgressBar) getSource()).removeChangeListener(changeListener); 519 } 520 }); 521 } 522 523 /** 524 * Maps {@code JProgressBar.setBorderPainted(boolean)} through queue 525 */ 526 public void setBorderPainted(final boolean b) { 527 runMapping(new MapVoidAction("setBorderPainted") { 528 @Override 529 public void map() { 530 ((JProgressBar) getSource()).setBorderPainted(b); 531 } 532 }); 533 } 534 535 /** 536 * Maps {@code JProgressBar.setMaximum(int)} through queue 537 */ 538 public void setMaximum(final int i) { 539 runMapping(new MapVoidAction("setMaximum") { 540 @Override 541 public void map() { 542 ((JProgressBar) getSource()).setMaximum(i); 543 } 544 }); 545 } 546 547 /** 548 * Maps {@code JProgressBar.setMinimum(int)} through queue 549 */ 550 public void setMinimum(final int i) { 551 runMapping(new MapVoidAction("setMinimum") { 552 @Override 553 public void map() { 554 ((JProgressBar) getSource()).setMinimum(i); 555 } 556 }); 557 } 558 559 /** 560 * Maps {@code JProgressBar.setModel(BoundedRangeModel)} through queue 561 */ 562 public void setModel(final BoundedRangeModel boundedRangeModel) { 563 runMapping(new MapVoidAction("setModel") { 564 @Override 565 public void map() { 566 ((JProgressBar) getSource()).setModel(boundedRangeModel); 567 } 568 }); 569 } 570 571 /** 572 * Maps {@code JProgressBar.setOrientation(int)} through queue 573 */ 574 public void setOrientation(final int i) { 575 runMapping(new MapVoidAction("setOrientation") { 576 @Override 577 public void map() { 578 ((JProgressBar) getSource()).setOrientation(i); 579 } 580 }); 581 } 582 583 /** 584 * Maps {@code JProgressBar.setString(String)} through queue 585 */ 586 public void setString(final String string) { 587 runMapping(new MapVoidAction("setString") { 588 @Override 589 public void map() { 590 ((JProgressBar) getSource()).setString(string); 591 } 592 }); 593 } 594 595 /** 596 * Maps {@code JProgressBar.setStringPainted(boolean)} through queue 597 */ 598 public void setStringPainted(final boolean b) { 599 runMapping(new MapVoidAction("setStringPainted") { 600 @Override 601 public void map() { 602 ((JProgressBar) getSource()).setStringPainted(b); 603 } 604 }); 605 } 606 607 /** 608 * Maps {@code JProgressBar.setUI(ProgressBarUI)} through queue 609 */ 610 public void setUI(final ProgressBarUI progressBarUI) { 611 runMapping(new MapVoidAction("setUI") { 612 @Override 613 public void map() { 614 ((JProgressBar) getSource()).setUI(progressBarUI); 615 } 616 }); 617 } 618 619 /** 620 * Maps {@code JProgressBar.setValue(int)} through queue 621 */ 622 public void setValue(final int i) { 623 runMapping(new MapVoidAction("setValue") { 624 @Override 625 public void map() { 626 ((JProgressBar) getSource()).setValue(i); 627 } 628 }); 629 } 630 631 //End of mapping // 632 //////////////////////////////////////////////////////// 633 /** 634 * Interface to define criteria for {@code waitValue(ValueChooser)} 635 * method. 636 * 637 * @see #waitValue(int) 638 * @deprecated Use waitState(ComponentChooser) instead. 639 */ 640 @Deprecated 641 public interface ValueChooser { 642 643 /** 644 * Check if criteria jave been reached. 645 * 646 * @param value current value. 647 * @return true if criteria reached. 648 */ 649 public boolean checkValue(int value); 650 651 /** 652 * A description. 653 * 654 * @return a description. 655 */ 656 public String getDescription(); 657 } 658 659 /** 660 * Checks component type. 661 */ 662 public static class JProgressBarFinder extends Finder { 663 664 /** 665 * Constructs JProgressBarFinder. 666 * 667 * @param sf other searching criteria. 668 */ 669 public JProgressBarFinder(ComponentChooser sf) { 670 super(JProgressBar.class, sf); 671 } 672 673 /** 674 * Constructs JProgressBarFinder. 675 */ 676 public JProgressBarFinder() { 677 super(JProgressBar.class); 678 } 679 } 680 681 } 682