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 lib.EditorTestCase; 26 import org.netbeans.jellytools.EditorOperator; 27 import org.netbeans.jellytools.HelpOperator; 28 import org.netbeans.jellytools.actions.ReplaceAction; 29 import org.netbeans.jellytools.modules.editor.Replace; 30 import org.netbeans.jemmy.ComponentChooser; 31 import org.netbeans.jemmy.EventTool; 32 import org.netbeans.jemmy.TestOut; 33 import org.netbeans.jemmy.operators.JEditorPaneOperator; 34 import org.netbeans.jemmy.operators.WindowOperator; 35 36 40 public class ReplaceTest extends EditorTestCase { 41 42 private static int REPLACE_TIMEOUT = 1000; 43 44 45 public ReplaceTest(String testMethodName) { 46 super(testMethodName); 47 } 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 closeReplaceDialogIfOpened(); 53 } 54 55 56 57 60 public void testReplaceDialogOpenClose() { 61 openDefaultProject(); 62 openDefaultSampleFile(); 63 try { 64 EditorOperator editor = getDefaultSampleEditorOperator(); 65 JEditorPaneOperator txtOper = editor.txtEditorPane(); 66 67 new ReplaceAction().perform(); 69 txtOper.pushKey(KeyEvent.VK_ESCAPE); 70 71 txtOper.pushKey(KeyEvent.VK_H, KeyEvent.CTRL_MASK); 73 Replace replace = new Replace(); 74 replace.btHelp().doClick(); 75 76 HelpOperator help = new HelpOperator(); 78 help.close(); 79 80 replace.btClose().doClick(); 82 83 } finally { 84 closeReplaceDialogIfOpened(); 85 closeFileWithDiscard(); 86 } 87 } 88 89 92 public void testReplaceSelectionRepeated() { 93 openDefaultProject(); 94 openDefaultSampleFile(); 95 try { 96 EditorOperator editor = getDefaultSampleEditorOperator(); 97 98 editor.select(13, 14, 41); 100 new EventTool().waitNoEvent(REPLACE_TIMEOUT); 101 new ReplaceAction().perform(); 102 Replace replace = new Replace(); 103 104 uncheckAll(); 106 replace.cbHighlightSearch().doClick(); 107 replace.cbIncrementalSearch().doClick(); 108 replace.cbWrapSearch().doClick(); 109 110 String text = replace.cboFindWhat().getTextField().getText(); 111 assertEquals(text, "testReplaceSelectionRepeated"); 113 replace.cboReplaceWith().clearText(); 114 replace.cboReplaceWith().typeText("testReplaceSelectionRepeated2"); 115 replace.replace(); 116 new EventTool().waitNoEvent(REPLACE_TIMEOUT); 117 replace.close(); 118 log(editor.lblStatusBar().getText()); 120 waitForLabel("'testReplaceSelectionRepeated' found at 16:12"); 121 122 editor.select(15, 35, 62); 124 new ReplaceAction().perform(); 125 Replace replace2 = new Replace(); 126 text = replace2.cboFindWhat().getTextField().getText(); 127 assertEquals(text, "testReplaceSelectionRepeated"); 129 replace2.replace(); 130 new EventTool().waitNoEvent(REPLACE_TIMEOUT); 131 replace2.close(); 132 waitForLabel("'testReplaceSelectionRepeated' found at 2:4; End of document reached. " 134 +"Continuing search from beginning."); 135 136 ref(editor.getText()); 137 compareReferenceFiles(); 138 139 } finally { 140 closeReplaceDialogIfOpened(); 141 closeFileWithDiscard(); 142 } 143 } 144 145 148 public void testReplaceDialogComboBox() { 149 openDefaultProject(); 150 openDefaultSampleFile(); 151 try { 152 EditorOperator editor = getDefaultSampleEditorOperator(); 153 154 editor.setCaretPosition(1,1); 155 editor.txtEditorPane().pushKey(KeyEvent.VK_H, KeyEvent.CTRL_MASK); 156 Replace replace = new Replace(); 157 replace.cboFindWhat().removeAllItems(); 158 replace.cboReplaceWith().removeAllItems(); 159 160 uncheckAll(); 162 replace.cbHighlightSearch().doClick(); 163 replace.cbIncrementalSearch().doClick(); 164 replace.cbWrapSearch().doClick(); 165 166 replace.cboFindWhat().clearText(); 167 replace.cboFindWhat().typeText("package"); 168 replace.cboReplaceWith().clearText(); 169 replace.cboReplaceWith().typeText("pakaz"); 170 replace.replace(); 171 waitForLabel("'package' not found"); 173 174 replace.cboFindWhat().clearText(); 175 replace.cboFindWhat().typeText("class"); 176 replace.cboReplaceWith().clearText(); 177 replace.cboReplaceWith().typeText("klasa"); 178 replace.replace(); 179 waitForLabel("'class' not found"); 181 182 replace.cboFindWhat().clearText(); 183 replace.cboFindWhat().typeText("testReplaceDialogComboBox"); 184 replace.cboReplaceWith().clearText(); 185 replace.cboReplaceWith().typeText("testReplaceDialogComboBox2"); 186 replace.replace(); 187 waitForLabel("'testReplaceDialogComboBox' found at 13:35"); 189 190 boolean found1 = false; 191 boolean found2 = false; 192 boolean found3 = false; 193 for (int i = 0; i<replace.cboFindWhat().getItemCount(); i++) { 194 if (((String )replace.cboFindWhat().getItemAt(i)).equals( 195 "testReplaceDialogComboBox")) found1 = true; 196 if (((String )replace.cboFindWhat().getItemAt(i)).equals( 197 "class")) found2 = true; 198 if (((String )replace.cboFindWhat().getItemAt(i)).equals( 199 "package")) found3 = true; 200 } 201 assertEquals(found1, true); 202 assertEquals(found2, true); 203 assertEquals(found3, true); 204 205 found1 = false; 206 found2 = false; 207 found3 = false; 208 for (int i = 0; i<replace.cboReplaceWith().getItemCount(); i++) { 209 if (((String )replace.cboReplaceWith().getItemAt(i)).equals( 210 "testReplaceDialogComboBox2")) found1 = true; 211 if (((String )replace.cboReplaceWith().getItemAt(i)).equals( 212 "klasa")) found2 = true; 213 if (((String )replace.cboReplaceWith().getItemAt(i)).equals( 214 "pakaz")) found3 = true; 215 } 216 assertEquals(found1, true); 217 assertEquals(found2, true); 218 assertEquals(found3, true); 219 220 new EventTool().waitNoEvent(REPLACE_TIMEOUT); 221 replace.close(); 222 223 ref(editor.getText()); 224 compareReferenceFiles(); 225 226 } finally { 227 closeReplaceDialogIfOpened(); 228 closeFileWithDiscard(); 229 } 230 } 231 232 235 public void testReplaceMatchCase() { 236 openDefaultProject(); 237 openDefaultSampleFile(); 238 try { 239 EditorOperator editor = getDefaultSampleEditorOperator(); 240 241 editor.setCaretPosition(1,1); 242 new ReplaceAction().perform(); 243 Replace replace = new Replace(); 244 245 uncheckAll(); 247 replace.cbMatchCase().doClick(); 248 249 replace.cboFindWhat().clearText(); 250 replace.cboFindWhat().typeText("testCase"); 251 replace.cboReplaceWith().clearText(); 252 replace.cboReplaceWith().typeText("xxxxXxxx"); 253 replace.replace(); 254 255 replace.close(); 256 257 ref(editor.getText()); 258 compareReferenceFiles(); 259 260 } finally { 261 closeReplaceDialogIfOpened(); 262 closeFileWithDiscard(); 263 } 264 } 265 266 269 public void testReplaceAll() { 270 openDefaultProject(); 271 openDefaultSampleFile(); 272 try { 273 EditorOperator editor = getDefaultSampleEditorOperator(); 274 275 editor.setCaretPosition(1,1); 276 new ReplaceAction().perform(); 277 Replace replace = new Replace(); 278 279 uncheckAll(); 281 282 replace.cboFindWhat().clearText(); 283 replace.cboFindWhat().typeText("testWord"); 284 replace.cboReplaceWith().clearText(); 285 replace.cboReplaceWith().typeText("xxxxXxxx"); 286 replace.replaceAll(); 287 waitForLabel("14 of 14 items replaced"); 289 290 replace.close(); 291 292 ref(editor.getText()); 293 compareReferenceFiles(); 294 295 } finally { 296 closeReplaceDialogIfOpened(); 297 closeFileWithDiscard(); 298 } 299 } 300 301 304 public void testReplaceInSelectionOnly() { 305 openDefaultProject(); 306 openDefaultSampleFile(); 307 try { 308 EditorOperator editor = getDefaultSampleEditorOperator(); 309 310 editor.select(20, 24); 311 new ReplaceAction().perform(); 312 Replace replace = new Replace(); 313 314 uncheckAll(); 316 replace.cbSearchSelection().doClick(); 317 replace.cboFindWhat().clearText(); 318 replace.cboFindWhat().typeText("testWord"); 319 replace.cboReplaceWith().clearText(); 320 replace.cboReplaceWith().typeText("xxxxXxxx"); 321 replace.replaceAll(); 322 waitForLabel("5 of 5 items replaced"); 324 325 replace.close(); 326 327 ref(editor.getText()); 328 compareReferenceFiles(); 329 330 } finally { 331 closeReplaceDialogIfOpened(); 332 closeFileWithDiscard(); 333 } 334 } 335 336 339 public void uncheckAll() { 340 Replace replace = new Replace(); 341 replace.cbBackwardSearch().setSelected(false); 342 replace.cbSearchSelection().setSelected(false); 343 replace.cbHighlightSearch().setSelected(false); 344 replace.cbIncrementalSearch().setSelected(false); 345 replace.cbMatchCase().setSelected(false); 346 replace.cbMatchWholeWordsOnly().setSelected(false); 347 replace.cbRegularExpressions().setSelected(false); 348 replace.cbWrapSearch().setSelected(false); 350 } 351 352 357 public void waitForLabel(String label) { 358 EditorOperator editor = getDefaultSampleEditorOperator(); 359 for (int i = 0; i<10; i++) { 360 if (editor.lblStatusBar().getText().equals(label)) break; 361 new EventTool().waitNoEvent(REPLACE_TIMEOUT); 362 } 363 assertEquals(editor.lblStatusBar().getText(), label); 364 } 365 366 369 public void closeReplaceDialogIfOpened() { 370 Window replaceWindow = WindowOperator.findWindow( 371 new ComponentChooser() { 372 public boolean checkComponent(Component comp) { 373 WindowOperator winOper = new WindowOperator((Window )comp); 374 winOper.setOutput(TestOut.getNullOutput()); 375 return null != winOper.findSubComponent( 376 new ComponentChooser() { 377 public boolean checkComponent(Component comp) { 378 return comp.getClass().getName().startsWith( 379 "org.netbeans.editor.ext.Find"); } 381 public String getDescription() { 382 return("any replace dialog"); } 384 }); 385 } 386 public String getDescription() { 387 return "containing any replace dialog"; } 389 }); 390 if(replaceWindow != null) { 391 new Replace().close(); 392 } 393 } 394 395 } 396 | Popular Tags |