JMenuItemOperator.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.Container; 27import java.awt.event.KeyEvent; 28import java.awt.event.MouseEvent; 29import java.util.Hashtable; 30 31import javax.swing.JMenu; 32import javax.swing.JMenuItem; 33import javax.swing.KeyStroke; 34import javax.swing.MenuElement; 35import javax.swing.MenuSelectionManager; 36import javax.swing.event.MenuDragMouseEvent; 37import javax.swing.event.MenuDragMouseListener; 38import javax.swing.event.MenuKeyEvent; 39import javax.swing.event.MenuKeyListener; 40import javax.swing.plaf.MenuItemUI; 41 42import org.netbeans.jemmy.ComponentChooser; 43import org.netbeans.jemmy.JemmyProperties; 44import org.netbeans.jemmy.Outputable; 45import org.netbeans.jemmy.TestOut; 46import org.netbeans.jemmy.TimeoutExpiredException; 47import org.netbeans.jemmy.Timeoutable; 48import org.netbeans.jemmy.Timeouts; 49import org.netbeans.jemmy.util.EmptyVisualizer; 50 51/** 52 * 53 * <BR><BR>Timeouts used: <BR> 54 * JMenuItemOperator.PushMenuTimeout - time between button pressing and 55 * releasing<BR> 56 * ComponentOperator.WaitComponentTimeout - time to wait button displayed <BR> 57 * ComponentOperator.WaitComponentEnabledTimeout - time to wait button enabled 58 * <BR>. 59 * 60 * @see org.netbeans.jemmy.Timeouts 61 * 62 * @author Alexandre Iline (alexandre.iline@oracle.com) 63 * 64 */ 65public class JMenuItemOperator extends AbstractButtonOperator 66 implements Timeoutable, Outputable { 67 68 private final static long PUSH_MENU_TIMEOUT = 0; 69 70 private Timeouts timeouts; 71 private TestOut output; 72 73 /** 74 * Constructor. 75 * 76 * @param item a component 77 */ 78 public JMenuItemOperator(JMenuItem item) { 79 super(item); 80 setTimeouts(JemmyProperties.getProperties().getTimeouts()); 81 setOutput(JemmyProperties.getProperties().getOutput()); 82 } 83 84 /** 85 * Constructs a JMenuItemOperator object. 86 * 87 * @param cont a container 88 * @param chooser a component chooser specifying searching criteria. 89 * @param index an index between appropriate ones. 90 */ 91 public JMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser, int index) { 92 this((JMenuItem) cont. 93 waitSubComponent(new JMenuItemFinder(chooser), 94 index)); 95 copyEnvironment(cont); 96 } 97 98 /** 99 * Constructs a JMenuItemOperator object. 100 * 101 * @param cont a container 102 * @param chooser a component chooser specifying searching criteria. 103 */ 104 public JMenuItemOperator(ContainerOperator<?> cont, ComponentChooser chooser) { 105 this(cont, chooser, 0); 106 } 107 108 /** 109 * Constructor. Waits component in container first. Uses cont's timeout and 110 * output for waiting and to init operator. 111 * 112 * @param cont a container 113 * @param text Button text. 114 * @param index Ordinal component index. 115 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 116 * @throws TimeoutExpiredException 117 */ 118 public JMenuItemOperator(ContainerOperator<?> cont, String text, int index) { 119 this((JMenuItem) waitComponent(cont, 120 new JMenuItemByLabelFinder(text, 121 cont.getComparator()), 122 index)); 123 setTimeouts(cont.getTimeouts()); 124 setOutput(cont.getOutput()); 125 } 126 127 /** 128 * Constructor. Waits component in container first. Uses cont's timeout and 129 * output for waiting and to init operator. 130 * 131 * @param cont a container 132 * @param text Button text. 133 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 134 * @throws TimeoutExpiredException 135 */ 136 public JMenuItemOperator(ContainerOperator<?> cont, String text) { 137 this(cont, text, 0); 138 } 139 140 /** 141 * Constructor. Waits component in container first. Uses cont's timeout and 142 * output for waiting and to init operator. 143 * 144 * @param cont a container 145 * @param index Ordinal component index. 146 * @throws TimeoutExpiredException 147 */ 148 public JMenuItemOperator(ContainerOperator<?> cont, int index) { 149 this((JMenuItem) waitComponent(cont, 150 new JMenuItemFinder(), 151 index)); 152 copyEnvironment(cont); 153 } 154 155 /** 156 * Constructor. Waits component in container first. Uses cont's timeout and 157 * output for waiting and to init operator. 158 * 159 * @param cont a container 160 * @throws TimeoutExpiredException 161 */ 162 public JMenuItemOperator(ContainerOperator<?> cont) { 163 this(cont, 0); 164 } 165 166 /** 167 * Searches JMenuItem in container. 168 * 169 * @param menu Container to search component in. 170 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 171 * @param index Ordinal component index. 172 * @return JMenuItem instance or null if component was not found. 173 */ 174 public static JMenuItem findJMenuItem(Container menu, ComponentChooser chooser, int index) { 175 return (JMenuItem) findComponent(menu, new JMenuItemFinder(chooser), index); 176 } 177 178 /** 179 * Searches 0'th JMenuItem in container. 180 * 181 * @param menu Container to search component in. 182 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 183 * @return JMenuItem instance or null if component was not found. 184 */ 185 public static JMenuItem findJMenuItem(Container menu, ComponentChooser chooser) { 186 return findJMenuItem(menu, chooser, 0); 187 } 188 189 /** 190 * Searches JMenuItem by text. 191 * 192 * @param menu Container to search component in. 193 * @param text Button text. If null, contents is not checked. 194 * @param ce Compare text exactly. 195 * @param ccs Compare text case sensitively. 196 * @param index Ordinal component index. 197 * @return JMenuItem instance or null if component was not found. 198 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 199 */ 200 public static JMenuItem findJMenuItem(Container menu, String text, boolean ce, boolean ccs, int index) { 201 return (findJMenuItem(menu, 202 new JMenuItemByLabelFinder(text, 203 new DefaultStringComparator(ce, 204 ccs)), 205 index)); 206 } 207 208 /** 209 * Searches JMenuItem by text. 210 * 211 * @param menu Container to search component in. 212 * @param text Button text. If null, contents is not checked. 213 * @param ce Compare text exactly. 214 * @param ccs Compare text case sensitively. 215 * @return JMenuItem instance or null if component was not found. 216 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 217 */ 218 public static JMenuItem findJMenuItem(Container menu, String text, boolean ce, boolean ccs) { 219 return findJMenuItem(menu, text, ce, ccs, 0); 220 } 221 222 /** 223 * Waits JMenuItem in container. 224 * 225 * @param menu Container to search component in. 226 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 227 * @param index Ordinal component index. 228 * @return JMenuItem instance. 229 * @throws TimeoutExpiredException 230 */ 231 public static JMenuItem waitJMenuItem(Container menu, ComponentChooser chooser, int index) { 232 return (JMenuItem) waitComponent(menu, new JMenuItemFinder(chooser), index); 233 } 234 235 /** 236 * Waits 0'th JMenuItem in container. 237 * 238 * @param menu Container to search component in. 239 * @param chooser org.netbeans.jemmy.ComponentChooser implementation. 240 * @return JMenuItem instance. 241 * @throws TimeoutExpiredException 242 */ 243 public static JMenuItem waitJMenuItem(Container menu, ComponentChooser chooser) { 244 return waitJMenuItem(menu, chooser, 0); 245 } 246 247 /** 248 * Waits JMenuItem by text. 249 * 250 * @param menu Container to search component in. 251 * @param text Button text. If null, contents is not checked. 252 * @param ce Compare text exactly. 253 * @param ccs Compare text case sensitively. 254 * @param index Ordinal component index. 255 * @return JMenuItem instance. 256 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 257 * @throws TimeoutExpiredException 258 */ 259 public static JMenuItem waitJMenuItem(Container menu, String text, boolean ce, boolean ccs, int index) { 260 return (waitJMenuItem(menu, 261 new JMenuItemByLabelFinder(text, 262 new DefaultStringComparator(ce, ccs)), 263 index)); 264 } 265 266 /** 267 * Waits JMenuItem by text. 268 * 269 * @param menu Container to search component in. 270 * @param text Button text. If null, contents is not checked. 271 * @param ce Compare text exactly. 272 * @param ccs Compare text case sensitively. 273 * @return JMenuItem instance. 274 * @see ComponentOperator#isCaptionEqual(String, String, boolean, boolean) 275 * @throws TimeoutExpiredException 276 */ 277 public static JMenuItem waitJMenuItem(Container menu, String text, boolean ce, boolean ccs) { 278 return waitJMenuItem(menu, text, ce, ccs, 0); 279 } 280 281 static { 282 Timeouts.initDefault("JMenuItemOperator.PushMenuTimeout", PUSH_MENU_TIMEOUT); 283 } 284 285 @Override 286 public void setTimeouts(Timeouts timeouts) { 287 super.setTimeouts(timeouts); 288 this.timeouts = timeouts; 289 } 290 291 @Override 292 public Timeouts getTimeouts() { 293 return timeouts; 294 } 295 296 @Override 297 public void setOutput(TestOut out) { 298 super.setOutput(out); 299 output = out; 300 } 301 302 @Override 303 public TestOut getOutput() { 304 return output; 305 } 306 307 @Override 308 public Hashtable<String, Object> getDump() { 309 Hashtable<String, Object> result = super.getDump(); 310 result.remove(AbstractButtonOperator.IS_SELECTED_DPROP); 311 return result; 312 } 313 314 /** 315 * Push this menu item. 316 */ 317 @Override 318 public void push() { 319 setVisualizer(new EmptyVisualizer()); 320 super.push(); 321 } 322 323 /** 324 * Push this menu item and no block further execution. 325 */ 326 @Override 327 public void pushNoBlock() { 328 setVisualizer(new EmptyVisualizer()); 329 super.pushNoBlock(); 330 } 331 332 //////////////////////////////////////////////////////// 333 //Mapping // 334 /** 335 * Maps 336 * {@code JMenuItem.addMenuDragMouseListener(MenuDragMouseListener)} 337 * through queue 338 */ 339 public void addMenuDragMouseListener(final MenuDragMouseListener menuDragMouseListener) { 340 runMapping(new MapVoidAction("addMenuDragMouseListener") { 341 @Override 342 public void map() { 343 ((JMenuItem) getSource()).addMenuDragMouseListener(menuDragMouseListener); 344 } 345 }); 346 } 347 348 /** 349 * Maps {@code JMenuItem.addMenuKeyListener(MenuKeyListener)} through queue 350 */ 351 public void addMenuKeyListener(final MenuKeyListener menuKeyListener) { 352 runMapping(new MapVoidAction("addMenuKeyListener") { 353 @Override 354 public void map() { 355 ((JMenuItem) getSource()).addMenuKeyListener(menuKeyListener); 356 } 357 }); 358 } 359 360 /** 361 * Maps {@code JMenuItem.getAccelerator()} through queue 362 */ 363 public KeyStroke getAccelerator() { 364 return (runMapping(new MapAction<KeyStroke>("getAccelerator") { 365 @Override 366 public KeyStroke map() { 367 return ((JMenuItem) getSource()).getAccelerator(); 368 } 369 })); 370 } 371 372 /** 373 * Maps {@code JMenuItem.getComponent()} through queue 374 */ 375 public Component getComponent() { 376 return (runMapping(new MapAction<Component>("getComponent") { 377 @Override 378 public Component map() { 379 return ((JMenuItem) getSource()).getComponent(); 380 } 381 })); 382 } 383 384 /** 385 * Maps {@code JMenuItem.getSubElements()} through queue 386 */ 387 public MenuElement[] getSubElements() { 388 return ((MenuElement[]) runMapping(new MapAction<Object>("getSubElements") { 389 @Override 390 public Object map() { 391 return ((JMenuItem) getSource()).getSubElements(); 392 } 393 })); 394 } 395 396 /** 397 * Maps {@code JMenuItem.isArmed()} through queue 398 */ 399 public boolean isArmed() { 400 return (runMapping(new MapBooleanAction("isArmed") { 401 @Override 402 public boolean map() { 403 return ((JMenuItem) getSource()).isArmed(); 404 } 405 })); 406 } 407 408 /** 409 * Maps {@code JMenuItem.menuSelectionChanged(boolean)} through queue 410 */ 411 public void menuSelectionChanged(final boolean b) { 412 runMapping(new MapVoidAction("menuSelectionChanged") { 413 @Override 414 public void map() { 415 ((JMenuItem) getSource()).menuSelectionChanged(b); 416 } 417 }); 418 } 419 420 /** 421 * Maps 422 * {@code JMenuItem.processKeyEvent(KeyEvent, MenuElement[], MenuSelectionManager)} 423 * through queue 424 */ 425 public void processKeyEvent(final KeyEvent keyEvent, final MenuElement[] menuElement, final MenuSelectionManager menuSelectionManager) { 426 runMapping(new MapVoidAction("processKeyEvent") { 427 @Override 428 public void map() { 429 ((JMenuItem) getSource()).processKeyEvent(keyEvent, menuElement, menuSelectionManager); 430 } 431 }); 432 } 433 434 /** 435 * Maps {@code JMenuItem.processMenuDragMouseEvent(MenuDragMouseEvent)} 436 * through queue 437 */ 438 public void processMenuDragMouseEvent(final MenuDragMouseEvent menuDragMouseEvent) { 439 runMapping(new MapVoidAction("processMenuDragMouseEvent") { 440 @Override 441 public void map() { 442 ((JMenuItem) getSource()).processMenuDragMouseEvent(menuDragMouseEvent); 443 } 444 }); 445 } 446 447 /** 448 * Maps {@code JMenuItem.processMenuKeyEvent(MenuKeyEvent)} through queue 449 */ 450 public void processMenuKeyEvent(final MenuKeyEvent menuKeyEvent) { 451 runMapping(new MapVoidAction("processMenuKeyEvent") { 452 @Override 453 public void map() { 454 ((JMenuItem) getSource()).processMenuKeyEvent(menuKeyEvent); 455 } 456 }); 457 } 458 459 /** 460 * Maps 461 * {@code JMenuItem.processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)} 462 * through queue 463 */ 464 public void processMouseEvent(final MouseEvent mouseEvent, final MenuElement[] menuElement, final MenuSelectionManager menuSelectionManager) { 465 runMapping(new MapVoidAction("processMouseEvent") { 466 @Override 467 public void map() { 468 ((JMenuItem) getSource()).processMouseEvent(mouseEvent, menuElement, menuSelectionManager); 469 } 470 }); 471 } 472 473 /** 474 * Maps 475 * {@code JMenuItem.removeMenuDragMouseListener(MenuDragMouseListener)} 476 * through queue 477 */ 478 public void removeMenuDragMouseListener(final MenuDragMouseListener menuDragMouseListener) { 479 runMapping(new MapVoidAction("removeMenuDragMouseListener") { 480 @Override 481 public void map() { 482 ((JMenuItem) getSource()).removeMenuDragMouseListener(menuDragMouseListener); 483 } 484 }); 485 } 486 487 /** 488 * Maps {@code JMenuItem.removeMenuKeyListener(MenuKeyListener)} 489 * through queue 490 */ 491 public void removeMenuKeyListener(final MenuKeyListener menuKeyListener) { 492 runMapping(new MapVoidAction("removeMenuKeyListener") { 493 @Override 494 public void map() { 495 ((JMenuItem) getSource()).removeMenuKeyListener(menuKeyListener); 496 } 497 }); 498 } 499 500 /** 501 * Maps {@code JMenuItem.setAccelerator(KeyStroke)} through queue 502 */ 503 public void setAccelerator(final KeyStroke keyStroke) { 504 runMapping(new MapVoidAction("setAccelerator") { 505 @Override 506 public void map() { 507 ((JMenuItem) getSource()).setAccelerator(keyStroke); 508 } 509 }); 510 } 511 512 /** 513 * Maps {@code JMenuItem.setArmed(boolean)} through queue 514 */ 515 public void setArmed(final boolean b) { 516 runMapping(new MapVoidAction("setArmed") { 517 @Override 518 public void map() { 519 ((JMenuItem) getSource()).setArmed(b); 520 } 521 }); 522 } 523 524 /** 525 * Maps {@code JMenuItem.setUI(MenuItemUI)} through queue 526 */ 527 public void setUI(final MenuItemUI menuItemUI) { 528 runMapping(new MapVoidAction("setUI") { 529 @Override 530 public void map() { 531 ((JMenuItem) getSource()).setUI(menuItemUI); 532 } 533 }); 534 } 535 536 //End of mapping // 537 //////////////////////////////////////////////////////// 538 /** 539 * Prepares the button to click. 540 */ 541 protected void prepareToClick() { 542 output.printLine("Push menu item\n :" + toStringSource()); 543 output.printGolden("Push menu item"); 544 Timeouts times = timeouts.cloneThis(); 545 times.setTimeout("AbstractButtonOperator.PushButtonTimeout", 546 timeouts.getTimeout("JMenuItemOperator.PushMenuTimeout")); 547 super.setTimeouts(times); 548 super.setOutput(output.createErrorOutput()); 549 } 550 551 static JMenuItemOperator[] getMenuItems(Object[] elements, Operator env) { 552 int size = 0; 553 for (Object element1 : elements) { 554 if (element1 instanceof JMenuItem) { 555 size++; 556 } 557 } 558 JMenuItemOperator[] result = new JMenuItemOperator[size]; 559 int index = 0; 560 for (Object element : elements) { 561 if (element instanceof JMenuItem) { 562 result[index] = new JMenuItemOperator((JMenuItem) element); 563 result[index].copyEnvironment(env); 564 index++; 565 } 566 } 567 return result; 568 } 569 570 static JMenuItemOperator[] getMenuItems(MenuElement parent, Operator env) { 571 return getMenuItems(parent.getSubElements(), env); 572 } 573 574 static JMenuItemOperator[] getMenuItems(JMenu parent, Operator env) { 575 return getMenuItems(parent.getMenuComponents(), env); 576 } 577 578 static ComponentChooser[] createChoosers(String[] names, StringComparator comparator) { 579 ComponentChooser[] choosers = new ComponentChooser[names.length]; 580 for (int i = 0; i < choosers.length; i++) { 581 choosers[i] = new JMenuItemOperator.JMenuItemByLabelFinder(names[i], comparator); 582 } 583 return choosers; 584 } 585 586 /** 587 * Allows to find component by text. 588 */ 589 public static class JMenuItemByLabelFinder implements ComponentChooser { 590 591 String label; 592 StringComparator comparator; 593 594 /** 595 * Constructs JMenuItemByLabelFinder. 596 * 597 * @param lb a text pattern 598 * @param comparator specifies string comparision algorithm. 599 */ 600 public JMenuItemByLabelFinder(String lb, StringComparator comparator) { 601 label = lb; 602 this.comparator = comparator; 603 } 604 605 /** 606 * Constructs JMenuItemByLabelFinder. 607 * 608 * @param lb a text pattern 609 */ 610 public JMenuItemByLabelFinder(String lb) { 611 this(lb, Operator.getDefaultStringComparator()); 612 } 613 614 @Override 615 public boolean checkComponent(Component comp) { 616 if (comp instanceof JMenuItem) { 617 if (((JMenuItem) comp).getText() != null) { 618 return (comparator.equals(((JMenuItem) comp).getText(), 619 label)); 620 } 621 } 622 return false; 623 } 624 625 @Override 626 public String getDescription() { 627 return "JMenuItem with text \"" + label + "\""; 628 } 629 630 @Override 631 public String toString() { 632 return "JMenuItemByLabelFinder{" + "label=" + label + ", comparator=" + comparator + '}'; 633 } 634 } 635 636 /** 637 * Checks component type. 638 */ 639 public static class JMenuItemFinder extends Finder { 640 641 /** 642 * Constructs JMenuItemFinder. 643 * 644 * @param sf other searching criteria. 645 */ 646 public JMenuItemFinder(ComponentChooser sf) { 647 super(JMenuItem.class, sf); 648 } 649 650 /** 651 * Constructs JMenuItemFinder. 652 */ 653 public JMenuItemFinder() { 654 super(JMenuItem.class); 655 } 656 } 657} 658