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