1 19 20 package search_replace; 21 22 import java.awt.Component ; 23 import java.awt.Window ; 24 import java.awt.event.KeyEvent ; 25 import org.netbeans.jellytools.EditorOperator; 26 import lib.EditorTestCase; 27 import org.netbeans.jellytools.HelpOperator; 28 import org.netbeans.jellytools.MainWindowOperator; 29 import org.netbeans.jellytools.actions.FindAction; 30 import org.netbeans.jellytools.modules.editor.Find; 31 import org.netbeans.jemmy.ComponentChooser; 32 import org.netbeans.jemmy.EventTool; 33 import org.netbeans.jemmy.TestOut; 34 import org.netbeans.jemmy.operators.JComboBoxOperator; 35 import org.netbeans.jemmy.operators.JEditorPaneOperator; 36 import org.netbeans.jemmy.operators.WindowOperator; 37 38 43 public class SearchTest extends EditorTestCase { 44 45 private static int FIND_TIMEOUT = 1000; 46 47 51 public SearchTest(String testMethodName) { 52 super(testMethodName); 53 } 54 55 @Override 56 protected void setUp() throws Exception { 57 super.setUp(); 58 closeFindDialogIfOpened(); 59 } 60 61 62 63 64 67 public void testFindDialogOpenClose() { 68 openDefaultProject(); 69 openDefaultSampleFile(); 70 try { 71 EditorOperator editor = getDefaultSampleEditorOperator(); 72 JEditorPaneOperator txtOper = editor.txtEditorPane(); 73 74 new FindAction().perform(); 76 txtOper.pushKey(KeyEvent.VK_ESCAPE); 77 78 txtOper.pushKey(KeyEvent.VK_F, KeyEvent.CTRL_MASK); 80 Find find = new Find(); 81 find.btHelp().doClick(); 82 83 HelpOperator help = new HelpOperator(); 85 help.close(); 86 87 find.btClose().doClick(); 89 90 } finally { 91 closeFindDialogIfOpened(); 92 closeFileWithDiscard(); 93 } 94 } 95 96 99 public void testFindSelectionRepeated() { 100 openDefaultProject(); 101 openDefaultSampleFile(); 102 try { 103 EditorOperator editor = getDefaultSampleEditorOperator(); 104 105 editor.select(13, 1, 6); 107 new FindAction().perform(); 108 Find find = new Find(); 109 String text = find.cboFindWhat().getTextField().getText(); 110 assertEquals(text, "public"); 112 find.find(); 113 new EventTool().waitNoEvent(FIND_TIMEOUT); 114 find.close(); 115 waitForLabel("'public' found at 16:5"); 117 118 editor.select(13, 14, 38); 120 new FindAction().perform(); 121 Find find2 = new Find(); 122 text = find2.cboFindWhat().getTextField().getText(); 123 assertEquals("testFindSelectionRepeated",text); 125 find2.find(); 126 new EventTool().waitNoEvent(FIND_TIMEOUT); 127 find2.close(); 128 waitForLabel("'testFindSelectionRepeated' found at 15:35"); 130 131 } finally { 132 closeFindDialogIfOpened(); 133 closeFileWithDiscard(); 134 } 135 } 136 137 140 public void testFindDialogComboBox() { 141 openDefaultProject(); 142 openDefaultSampleFile(); 143 try { 144 EditorOperator editor = getDefaultSampleEditorOperator(); 145 146 editor.setCaretPosition(1, 1); 148 new EventTool().waitNoEvent(FIND_TIMEOUT); 149 new FindAction().perform(); 150 Find find = new Find(); 151 find.cboFindWhat().typeText("package"); 152 find.find(); 153 new EventTool().waitNoEvent(FIND_TIMEOUT); 154 find.close(); 155 156 editor.setCaretPosition(1, 1); 158 new EventTool().waitNoEvent(FIND_TIMEOUT); 159 new FindAction().perform(); 160 Find find2 = new Find(); 161 find2.cboFindWhat().typeText("class"); 162 find2.find(); 163 new EventTool().waitNoEvent(FIND_TIMEOUT); 164 find2.close(); 165 166 new FindAction().perform(); 168 Find find3 = new Find(); 169 JComboBoxOperator cbo = find3.cboFindWhat(); 170 cbo.selectItem(1); 171 find3.cbWrapSearch().setSelected(true); 172 find3.find(); 173 new EventTool().waitNoEvent(FIND_TIMEOUT); 174 find3.close(); 175 waitForLabel("'package' found at 7:1; End of document reached. " 177 +"Continuing search from beginning."); 178 179 } finally { 180 closeFindDialogIfOpened(); 181 closeFileWithDiscard(); 182 } 183 } 184 185 188 public void testUnselectedAllOptions() { 189 openDefaultProject(); 190 openDefaultSampleFile(); 191 try { 192 EditorOperator editor = getDefaultSampleEditorOperator(); 193 194 editor.setCaretPosition(1, 1); 196 new EventTool().waitNoEvent(FIND_TIMEOUT); 197 new FindAction().perform(); 198 Find find = new Find(); 199 find.cboFindWhat().typeText("cLaSs"); 200 uncheckAll(); 201 find.find(); 202 new EventTool().waitNoEvent(FIND_TIMEOUT); 203 find.close(); 204 waitForLabel("'cLaSs' found at 13:8"); 206 207 } finally { 208 closeFindDialogIfOpened(); 209 closeFileWithDiscard(); 210 } 211 } 212 213 216 public void testNothingFound() { 217 openDefaultProject(); 218 openDefaultSampleFile(); 219 try { 220 EditorOperator editor = getDefaultSampleEditorOperator(); 221 222 new FindAction().perform(); 224 Find find = new Find(); 225 uncheckAll(); 226 find.cboFindWhat().typeText("foo"); 227 find.find(); 228 new EventTool().waitNoEvent(FIND_TIMEOUT); 229 find.close(); 230 waitForLabel("'foo' not found"); 232 233 } finally { 234 closeFindDialogIfOpened(); 235 closeFileWithDiscard(); 236 } 237 } 238 239 242 public void testMatchCase() { 243 openDefaultProject(); 244 openDefaultSampleFile(); 245 try { 246 EditorOperator editor = getDefaultSampleEditorOperator(); 247 248 new FindAction().perform(); 250 Find find = new Find(); 251 find.cboFindWhat().typeText("Package"); 252 uncheckAll(); 253 find.cbMatchCase().setSelected(true); 254 find.find(); 255 new EventTool().waitNoEvent(FIND_TIMEOUT); 256 find.close(); 257 waitForLabel("'Package' not found"); 259 260 editor.setCaretPosition(1, 1); 262 new EventTool().waitNoEvent(FIND_TIMEOUT); 263 new FindAction().perform(); 264 Find find2 = new Find(); 265 find2.cboFindWhat().typeText("package"); 266 find2.find(); 267 new EventTool().waitNoEvent(FIND_TIMEOUT); 268 find2.close(); 269 waitForLabel("'package' found at 7:1"); 271 272 } finally { 273 closeFindDialogIfOpened(); 274 closeFileWithDiscard(); 275 } 276 } 277 278 281 335 336 339 373 374 377 public void testMatchWholeWordsOnly() { 378 openDefaultProject(); 379 openDefaultSampleFile(); 380 try { 381 EditorOperator editor = getDefaultSampleEditorOperator(); 382 383 new FindAction().perform(); 385 Find find = new Find(); 386 uncheckAll(); 387 find.cbMatchWholeWordsOnly().setSelected(true); 388 find.cboFindWhat().typeText("word"); 389 find.find(); 390 new EventTool().waitNoEvent(FIND_TIMEOUT); 391 find.close(); 392 waitForLabel("'word' found at 18:16"); 394 395 new FindAction().perform(); 397 Find find2 = new Find(); 398 find2.cboFindWhat().typeText("word"); 399 find2.find(); 400 new EventTool().waitNoEvent(FIND_TIMEOUT); 401 find2.close(); 402 waitForLabel("'word' found at 18:24"); 404 405 new FindAction().perform(); 407 Find find3 = new Find(); 408 find3.cboFindWhat().typeText("word"); 409 find3.find(); 410 new EventTool().waitNoEvent(FIND_TIMEOUT); 411 find3.close(); 412 waitForLabel("'word' found at 19:25"); 414 415 new FindAction().perform(); 417 Find find4 = new Find(); 418 find4.cboFindWhat().typeText("word"); 419 find4.find(); 420 new EventTool().waitNoEvent(FIND_TIMEOUT); 421 find4.close(); 422 waitForLabel("'word' not found"); 424 425 } finally { 426 closeFindDialogIfOpened(); 427 closeFileWithDiscard(); 428 } 429 } 430 431 434 public void testHighlightSearch() { 435 openDefaultProject(); 436 openDefaultSampleFile(); 437 try { 438 EditorOperator editor = getDefaultSampleEditorOperator(); 439 440 new FindAction().perform(); 442 Find find = new Find(); 443 uncheckAll(); 444 find.cbHighlightSearch().setSelected(true); 445 find.cboFindWhat().typeText("testHighlightSearch"); 446 find.find(); 447 waitForLabel("'testHighlightSearch' found at 2:4"); 448 find.find(); 449 waitForLabel("'testHighlightSearch' found at 13:14"); 450 find.find(); 451 waitForLabel("'testHighlightSearch' found at 15:35"); 452 find.find(); 453 waitForLabel("'testHighlightSearch' found at 16:12"); 454 find.find(); 455 waitForLabel("'testHighlightSearch' not found"); 456 find.close(); 457 458 } finally { 459 closeFindDialogIfOpened(); 460 closeFileWithDiscard(); 461 } 462 } 463 464 467 public void testIncrementalSearch() { 468 openDefaultProject(); 469 openDefaultSampleFile(); 470 try { 471 EditorOperator editor = getDefaultSampleEditorOperator(); 472 473 new FindAction().perform(); 475 Find find = new Find(); 476 uncheckAll(); 477 find.cbHighlightSearch().setSelected(true); 478 find.cboFindWhat().typeText("searchedWord"); 479 for (int i = 0; i<10; i++) { 480 find.find(); 481 waitForLabel("'searchedWord' found at "+String.valueOf(i+17) 482 +":12"); 483 } 484 find.close(); 485 486 } finally { 487 closeFindDialogIfOpened(); 488 closeFileWithDiscard(); 489 } 490 } 491 492 495 public void testBackwardSearch() { 496 openDefaultProject(); 497 openDefaultSampleFile(); 498 try { 499 EditorOperator editor = getDefaultSampleEditorOperator(); 500 501 new FindAction().perform(); 503 Find find = new Find(); 504 uncheckAll(); 505 find.cboFindWhat().typeText("first"); 506 find.find(); 507 waitForLabel("'first' found at 21:12"); 508 find.cboFindWhat().clearText(); 509 find.cboFindWhat().typeText("second"); 510 find.cbBackwardSearch().setSelected(true); 511 find.find(); 512 waitForLabel("'second' found at 20:12"); 513 find.cboFindWhat().clearText(); 514 find.cboFindWhat().typeText("third"); 515 find.find(); 516 waitForLabel("'third' found at 19:12"); 517 find.cboFindWhat().clearText(); 518 find.cboFindWhat().typeText("fourth"); 519 find.find(); 520 waitForLabel("'fourth' found at 18:12"); 521 find.close(); 522 523 } finally { 524 closeFindDialogIfOpened(); 525 closeFileWithDiscard(); 526 } 527 } 528 529 532 public void testWrapSearch() { 533 openDefaultProject(); 534 openDefaultSampleFile(); 535 try { 536 EditorOperator editor = getDefaultSampleEditorOperator(); 537 538 new FindAction().perform(); 540 Find find = new Find(); 541 uncheckAll(); 542 find.cbWrapSearch().setSelected(true); 543 find.cboFindWhat().typeText("wrapWord"); 544 for (int i = 0; i<4; i++) { 545 find.find(); 546 waitForLabel("'wrapWord' found at "+String.valueOf(i+18) 547 +":12"); 548 } 549 find.find(); 550 waitForLabel("'wrapWord' found at 18:12; End of document reached. " 551 +"Continuing search from beginning."); 552 for (int i = 0; i<3; i++) { 553 find.find(); 554 waitForLabel("'wrapWord' found at "+String.valueOf(i+19) 555 +":12"); 556 } 557 find.close(); 558 559 } finally { 560 closeFindDialogIfOpened(); 561 closeFileWithDiscard(); 562 } 563 } 564 565 568 public void testFindNextPrevious() { 569 openDefaultProject(); 570 openDefaultSampleFile(); 571 try { 572 EditorOperator editor = getDefaultSampleEditorOperator(); 573 574 editor.setCaretPosition(1, 1); 576 new FindAction().perform(); 577 Find find = new Find(); 578 uncheckAll(); 579 find.cboFindWhat().clearText(); 580 find.cboFindWhat().typeText("testWord"); 581 find.find(); 582 waitForLabel("'testWord' found at 18:12"); 583 find.close(); 584 new EventTool().waitNoEvent(FIND_TIMEOUT); 585 586 for (int i = 0; i<7; i++) { 588 MainWindowOperator.getDefault().pushKey(KeyEvent.VK_F3); 589 waitForLabel("'testWord' found at "+String.valueOf(i+19) 590 +":12"); 591 } 592 593 for (int i = 7; i>0; i--) { 595 MainWindowOperator.getDefault().pushKey(KeyEvent.VK_F3, 596 KeyEvent.SHIFT_MASK); 597 waitForLabel("'testWord' found at "+String.valueOf(i+17) 598 +":12"); 599 } 600 } finally { 601 closeFindDialogIfOpened(); 602 closeFileWithDiscard(); 603 } 604 } 605 606 609 public void testFindSelectionWithoutDialog() { 610 openDefaultProject(); 611 openDefaultSampleFile(); 612 try { 613 EditorOperator editor = getDefaultSampleEditorOperator(); 614 615 editor.select(18, 12, 19); 617 for (int i = 0; i<7; i++) { 618 MainWindowOperator.getDefault().pushKey(KeyEvent.VK_F3); 619 waitForLabel("'testWord' found at "+String.valueOf(i+19) 620 +":12"); 621 } 622 } finally { 623 closeFindDialogIfOpened(); 624 closeFileWithDiscard(); 625 } 626 } 627 628 631 public void testSearchSelection() { 632 openDefaultProject(); 633 openDefaultSampleFile(); 634 try { 635 EditorOperator editor = getDefaultSampleEditorOperator(); 636 637 editor.select(24, 20); 639 new EventTool().waitNoEvent(FIND_TIMEOUT); 640 new FindAction().perform(); 641 Find find = new Find(); 642 uncheckAll(); 643 find.cbIncrementalSearch().setSelected(true); 644 find.cbBlockSearch().setSelected(true); 645 find.cboFindWhat().clearText(); 646 find.cboFindWhat().typeText("testWord2"); 647 find.find(); 648 waitForLabel("'testWord2' found at 22:12"); 649 find.close(); 650 651 } finally { 652 closeFindDialogIfOpened(); 653 closeFileWithDiscard(); 654 } 655 } 656 657 660 public void testSearchSelectionNegative() { 661 openDefaultProject(); 662 openDefaultSampleFile(); 663 try { 664 EditorOperator editor = getDefaultSampleEditorOperator(); 665 666 editor.select(25, 22); 668 new EventTool().waitNoEvent(FIND_TIMEOUT); 669 new FindAction().perform(); 670 Find find2 = new Find(); 671 uncheckAll(); 672 find2.cbIncrementalSearch().setSelected(true); 673 find2.cbBlockSearch().setSelected(true); 674 find2.cboFindWhat().clearText(); 675 find2.cboFindWhat().typeText("testWord2"); 676 find2.find(); 677 waitForLabel("'testWord2' not found"); 678 find2.close(); 679 680 } finally { 681 closeFindDialogIfOpened(); 682 closeFileWithDiscard(); 683 } 684 } 685 686 689 public void testRegexpSimple() { 690 openDefaultProject(); 691 openDefaultSampleFile(); 692 try { 693 EditorOperator editor = getDefaultSampleEditorOperator(); 694 695 editor.setCaretPosition(1, 1); 697 new FindAction().perform(); 698 Find find = new Find(); 699 uncheckAll(); 700 find.cbRegularExpressions().setSelected(true); 701 find.cboFindWhat().clearText(); 702 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 703 find.find(); 704 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' found at 18:12"); 705 find.cboFindWhat().clearText(); 706 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 707 find.find(); 708 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' found at 19:12"); 709 find.cboFindWhat().clearText(); 710 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 711 find.find(); 712 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' found at 20:12"); 713 find.cboFindWhat().clearText(); 714 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 715 find.find(); 716 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' found at 21:12"); 717 find.cboFindWhat().clearText(); 718 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 719 find.find(); 720 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' found at 23:12"); 721 find.cboFindWhat().clearText(); 722 find.cboFindWhat().typeText("[aA][hH][oO][jJ][0-9]{1,3}"); 723 find.find(); 724 waitForLabel("'[aA][hH][oO][jJ][0-9]{1,3}' not found"); 725 find.close(); 726 727 } finally { 728 closeFindDialogIfOpened(); 729 closeFileWithDiscard(); 730 } 731 } 732 733 736 public void testRegexpComplex() { 737 openDefaultProject(); 738 openDefaultSampleFile(); 739 try { 740 EditorOperator editor = getDefaultSampleEditorOperator(); 741 742 editor.setCaretPosition(1, 1); 744 new FindAction().perform(); 745 Find find = new Find(); 746 uncheckAll(); 747 find.cbRegularExpressions().setSelected(true); 748 find.cboFindWhat().clearText(); 749 find.cboFindWhat().typeText("a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]"); 750 find.find(); 751 waitForLabel("'a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]' found at 18:12"); 752 find.cboFindWhat().clearText(); 753 find.cboFindWhat().typeText("a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]"); 754 find.find(); 755 waitForLabel("'a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]' found at 19:12"); 756 find.cboFindWhat().clearText(); 757 find.cboFindWhat().typeText("a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]"); 758 find.find(); 759 waitForLabel("'a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]' found at 21:12"); 760 find.cboFindWhat().clearText(); 761 find.cboFindWhat().typeText("a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]"); 762 find.find(); 763 waitForLabel("'a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]' found at 23:12"); 764 find.cboFindWhat().clearText(); 765 find.cboFindWhat().typeText("a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]"); 766 find.find(); 767 waitForLabel("'a?B*c{2}[dD]e{1,}\\.F{1,2}\\s[^g]' not found"); 768 find.close(); 769 770 } finally { 771 closeFindDialogIfOpened(); 772 closeFileWithDiscard(); 773 } 774 } 775 776 779 public void uncheckAll() { 780 Find find = new Find(); 781 find.cbBackwardSearch().setSelected(false); 782 find.cbBlockSearch().setSelected(false); 783 find.cbHighlightSearch().setSelected(false); 784 find.cbIncrementalSearch().setSelected(false); 785 find.cbMatchCase().setSelected(false); 786 find.cbMatchWholeWordsOnly().setSelected(false); 787 find.cbRegularExpressions().setSelected(false); 788 find.cbWrapSearch().setSelected(false); 790 } 791 792 797 public void waitForLabel(String label) { 798 EditorOperator editor = getDefaultSampleEditorOperator(); 799 for (int i = 0; i<10; i++) { 800 if (editor.lblStatusBar().getText().equals(label)) break; 801 new EventTool().waitNoEvent(FIND_TIMEOUT); 802 } 803 assertEquals(label,editor.lblStatusBar().getText()); 804 } 805 806 807 810 public void closeFindDialogIfOpened() { 811 Window findWindow = WindowOperator.findWindow( 812 new ComponentChooser() { 813 public boolean checkComponent(Component comp) { 814 WindowOperator winOper = new WindowOperator((Window )comp); 815 winOper.setOutput(TestOut.getNullOutput()); 816 return null != winOper.findSubComponent( 817 new ComponentChooser() { 818 public boolean checkComponent(Component comp) { 819 return comp.getClass().getName().startsWith( 820 "org.netbeans.editor.ext.Find"); } 822 public String getDescription() { 823 return("any find dialog"); } 825 }); 826 } 827 public String getDescription() { 828 return "containing any find dialog"; } 830 }); 831 if(findWindow != null) { 832 new Find().close(); 833 } 834 } 835 836 } 837 | Popular Tags |