001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package search_replace;
043:
044: import java.awt.Component;
045: import java.awt.Window;
046: import java.awt.event.KeyEvent;
047: import junit.textui.TestRunner;
048: import lib.EditorTestCase;
049: import org.netbeans.jellytools.EditorOperator;
050: import org.netbeans.jellytools.HelpOperator;
051: import org.netbeans.jellytools.MainWindowOperator;
052: import org.netbeans.jellytools.actions.ReplaceAction;
053: import org.netbeans.jellytools.modules.editor.Replace;
054: import org.netbeans.jemmy.ComponentChooser;
055: import org.netbeans.jemmy.EventTool;
056: import org.netbeans.jemmy.TestOut;
057: import org.netbeans.jemmy.operators.JEditorPaneOperator;
058: import org.netbeans.jemmy.operators.WindowOperator;
059:
060: /**
061: *
062: * @author Roman Strobl
063: */
064: public class ReplaceTest extends EditorTestCase {
065:
066: private static int REPLACE_TIMEOUT = 1000;
067:
068: /** Creates a new instance of ReplaceTest */
069: public ReplaceTest(String testMethodName) {
070: super (testMethodName);
071: }
072:
073: @Override
074: protected void setUp() throws Exception {
075: super .setUp();
076: closeReplaceDialogIfOpened();
077: }
078:
079: private void openReplaceDialog() {
080: MainWindowOperator mwo = MainWindowOperator.getDefault();
081: //mwo.pushKey(KeyEvent.VK_E, KeyEvent.ALT_DOWN_MASK);
082: //new EventTool().waitNoEvent(100);
083: //mwo.pushKey(KeyEvent.VK_ESCAPE);
084: //new ReplaceAction().perform();
085: mwo.requestFocus();
086: mwo.pushKey(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK);
087: new EventTool().waitNoEvent(200);
088: }
089:
090: /**
091: * TC1 - open and close replace dialog
092: */
093: public void testReplaceDialogOpenClose() {
094: openDefaultProject();
095: openDefaultSampleFile();
096: try {
097: EditorOperator editor = getDefaultSampleEditorOperator();
098: JEditorPaneOperator txtOper = editor.txtEditorPane();
099:
100: // open replace and close
101: openReplaceDialog();
102: txtOper.pushKey(KeyEvent.VK_ESCAPE);
103: new EventTool().waitNoEvent(200);
104: // open replace and open help
105: txtOper.requestFocus();
106: txtOper.pushKey(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK);
107: new EventTool().waitNoEvent(200);
108: Replace replace = new Replace();
109: replace.btHelp().doClick();
110:
111: // close help
112: HelpOperator help = new HelpOperator();
113: help.close();
114: new EventTool().waitNoEvent(200);
115: // close replace
116: replace.btClose().doClick();
117:
118: } finally {
119: closeReplaceDialogIfOpened();
120: closeFileWithDiscard();
121: }
122: }
123:
124: /**
125: * TC2 - Replace Dialog Open - Selection
126: */
127: public void testReplaceSelectionRepeated() {
128: openDefaultProject();
129: openDefaultSampleFile();
130: try {
131: EditorOperator editor = getDefaultSampleEditorOperator();
132:
133: // choose the "testReplaceSelectionRepeated" word
134: editor.setCaretPosition(12, 1);
135: new EventTool().waitNoEvent(REPLACE_TIMEOUT);
136: openReplaceDialog();
137: Replace replace = new Replace();
138:
139: // check only selected checkboxes
140: uncheckAll();
141: replace.cbHighlightSearch().doClick();
142: replace.cbIncrementalSearch().doClick();
143: replace.cbWrapSearch().doClick();
144: replace.cboFindWhat().getTextField().typeText(
145: "testReplaceSelectionRepeated");
146: String text = replace.cboFindWhat().getTextField()
147: .getText();
148: // compare
149: assertEquals(text, "testReplaceSelectionRepeated");
150: replace.cboReplaceWith().clearText();
151: replace.cboReplaceWith().typeText(
152: "testReplaceSelectionRepeated2");
153: new EventTool().waitNoEvent(250);
154: replace.replace();
155: new EventTool().waitNoEvent(REPLACE_TIMEOUT);
156: replace.close();
157: // check status bar
158: log(editor.lblStatusBar().getText());
159: waitForLabel("'testReplaceSelectionRepeated' found at 15:35");
160: // choose the "testReplaceSelectionRepeated" word
161: editor.setCaretPosition(15, 1);
162: //editor.select(15, 35, 62);
163: openReplaceDialog();
164: replace.cboFindWhat().getTextField().typeText(
165: "testReplaceSelectionRepeated");
166: replace.cboReplaceWith().typeText(
167: "testReplaceSelectionRepeated2");
168: Replace replace2 = new Replace();
169: text = replace2.cboFindWhat().getTextField().getText();
170: // compare
171: assertEquals(text, "testReplaceSelectionRepeated");
172: replace2.replace();
173: new EventTool().waitNoEvent(REPLACE_TIMEOUT);
174: replace2.close();
175: // check status bar
176: waitForLabel("'testReplaceSelectionRepeated' found at 16:12");
177:
178: //waitForLabel("'testReplaceSelectionRepeated' found at 2:4; End of document reached. "
179: // +"Continuing search from beginning.");
180:
181: ref(editor.getText());
182: compareReferenceFiles();
183:
184: } finally {
185: closeReplaceDialogIfOpened();
186: closeFileWithDiscard();
187: }
188: }
189:
190: /**
191: * TC3 - Replace Dialog Combo Box
192: */
193: public void testReplaceDialogComboBox() {
194: openDefaultProject();
195: openDefaultSampleFile();
196: try {
197: EditorOperator editor = getDefaultSampleEditorOperator();
198:
199: editor.setCaretPosition(1, 1);
200: editor.txtEditorPane().pushKey(KeyEvent.VK_H,
201: KeyEvent.CTRL_MASK);
202: Replace replace = new Replace();
203: //replace.cboFindWhat().removeAllItems();
204: //replace.cboReplaceWith().removeAllItems();
205:
206: // check only selected checkboxes
207: uncheckAll();
208: replace.cbHighlightSearch().doClick();
209: replace.cbIncrementalSearch().doClick();
210: replace.cbWrapSearch().doClick();
211:
212: replace.cboFindWhat().clearText();
213: replace.cboFindWhat().typeText("package");
214: replace.cboReplaceWith().clearText();
215: replace.cboReplaceWith().typeText("pakaz");
216: replace.replace();
217: // check status bar
218: waitForLabel("'package' not found");
219:
220: replace.cboFindWhat().clearText();
221: replace.cboFindWhat().typeText("class");
222: replace.cboReplaceWith().clearText();
223: replace.cboReplaceWith().typeText("klasa");
224: replace.replace();
225: // check status bar
226: waitForLabel("'class' not found");
227:
228: replace.cboFindWhat().clearText();
229: replace.cboFindWhat().typeText("testReplaceDialogComboBox");
230: replace.cboReplaceWith().clearText();
231: replace.cboReplaceWith().typeText(
232: "testReplaceDialogComboBox2");
233: replace.replace();
234: // check status bar
235: waitForLabel("'testReplaceDialogComboBox' found at 13:35");
236:
237: boolean found1 = false;
238: boolean found2 = false;
239: boolean found3 = false;
240: for (int i = 0; i < replace.cboFindWhat().getItemCount(); i++) {
241: if (((String) replace.cboFindWhat().getItemAt(i))
242: .equals("testReplaceDialogComboBox"))
243: found1 = true;
244: if (((String) replace.cboFindWhat().getItemAt(i))
245: .equals("class"))
246: found2 = true;
247: if (((String) replace.cboFindWhat().getItemAt(i))
248: .equals("package"))
249: found3 = true;
250: }
251: assertEquals(found1, true);
252: assertEquals(found2, true);
253: assertEquals(found3, true);
254:
255: found1 = false;
256: found2 = false;
257: found3 = false;
258: for (int i = 0; i < replace.cboReplaceWith().getItemCount(); i++) {
259: if (((String) replace.cboReplaceWith().getItemAt(i))
260: .equals("testReplaceDialogComboBox2"))
261: found1 = true;
262: if (((String) replace.cboReplaceWith().getItemAt(i))
263: .equals("klasa"))
264: found2 = true;
265: if (((String) replace.cboReplaceWith().getItemAt(i))
266: .equals("pakaz"))
267: found3 = true;
268: }
269: assertEquals(found1, true);
270: assertEquals(found2, true);
271: assertEquals(found3, true);
272:
273: new EventTool().waitNoEvent(REPLACE_TIMEOUT);
274: replace.close();
275:
276: ref(editor.getText());
277: compareReferenceFiles();
278:
279: } finally {
280: closeReplaceDialogIfOpened();
281: closeFileWithDiscard();
282: }
283: }
284:
285: /**
286: * TC4 - Replace Match Case
287: */
288: public void testReplaceMatchCase() {
289: openDefaultProject();
290: openDefaultSampleFile();
291: try {
292: EditorOperator editor = getDefaultSampleEditorOperator();
293:
294: editor.setCaretPosition(1, 1);
295: openReplaceDialog();
296: Replace replace = new Replace();
297:
298: // check only selected checkboxes
299: uncheckAll();
300: replace.cbMatchCase().doClick();
301:
302: replace.cboFindWhat().clearText();
303: replace.cboFindWhat().typeText("testCase");
304: replace.cboReplaceWith().clearText();
305: replace.cboReplaceWith().typeText("xxxxXxxx");
306: replace.replace();
307:
308: replace.close();
309:
310: ref(editor.getText());
311: compareReferenceFiles();
312:
313: } finally {
314: closeReplaceDialogIfOpened();
315: closeFileWithDiscard();
316: }
317: }
318:
319: /**
320: * TC5 - Replace All
321: */
322: public void testReplaceAll() {
323: openDefaultProject();
324: openDefaultSampleFile();
325: try {
326: EditorOperator editor = getDefaultSampleEditorOperator();
327:
328: editor.setCaretPosition(1, 1);
329: openReplaceDialog();
330: Replace replace = new Replace();
331:
332: // check only selected checkboxes
333: uncheckAll();
334:
335: replace.cboFindWhat().clearText();
336: replace.cboFindWhat().typeText("testWord");
337: replace.cboReplaceWith().clearText();
338: replace.cboReplaceWith().typeText("xxxxXxxx");
339: replace.replaceAll();
340: // check status bar
341: waitForLabel("14 of 14 items replaced");
342:
343: replace.close();
344:
345: ref(editor.getText());
346: compareReferenceFiles();
347:
348: } finally {
349: closeReplaceDialogIfOpened();
350: closeFileWithDiscard();
351: }
352: }
353:
354: /**
355: * TC6 - Replace in Selection Only
356: */
357: public void testReplaceInSelectionOnly() {
358: openDefaultProject();
359: openDefaultSampleFile();
360: try {
361: EditorOperator editor = getDefaultSampleEditorOperator();
362:
363: editor.select(20, 24);
364: openReplaceDialog();
365: Replace replace = new Replace();
366:
367: // check only selected checkboxes
368: uncheckAll();
369: replace.cbSearchSelection().doClick();
370: replace.cboFindWhat().clearText();
371: replace.cboFindWhat().typeText("testWord");
372: replace.cboReplaceWith().clearText();
373: replace.cboReplaceWith().typeText("xxxxXxxx");
374: replace.replaceAll();
375: // check status bar
376: waitForLabel("5 of 5 items replaced");
377:
378: replace.close();
379:
380: ref(editor.getText());
381: compareReferenceFiles();
382:
383: } finally {
384: closeReplaceDialogIfOpened();
385: closeFileWithDiscard();
386: }
387: }
388:
389: /**
390: * Unchecks all checkboxes in find dialog.
391: */
392: public void uncheckAll() {
393: Replace replace = new Replace();
394: replace.cbBackwardSearch().setSelected(false);
395: replace.cbSearchSelection().setSelected(false);
396: replace.cbHighlightSearch().setSelected(false);
397: replace.cbIncrementalSearch().setSelected(false);
398: replace.cbMatchCase().setSelected(false);
399: replace.cbMatchWholeWordsOnly().setSelected(false);
400: replace.cbRegularExpressions().setSelected(false);
401: //replace.cbSmartCase().setSelected(false);
402: replace.cbWrapSearch().setSelected(false);
403: }
404:
405: /**
406: * Waits for label to appear on Status Bar, checks it 10 times before
407: * failing.
408: * @param label label which should be displayed on status bar
409: */
410: public void waitForLabel(String label) {
411: EditorOperator editor = getDefaultSampleEditorOperator();
412: for (int i = 0; i < 10; i++) {
413: if (editor.lblStatusBar().getText().equals(label))
414: break;
415: new EventTool().waitNoEvent(REPLACE_TIMEOUT);
416: }
417: assertEquals(label, editor.lblStatusBar().getText());
418: }
419:
420: /**
421: * Checks if a replace dialog is opened and if yes it closes it.
422: */
423: public void closeReplaceDialogIfOpened() {
424: Window replaceWindow = WindowOperator
425: .findWindow(new ComponentChooser() {
426: public boolean checkComponent(Component comp) {
427: WindowOperator winOper = new WindowOperator(
428: (Window) comp);
429: winOper.setOutput(TestOut.getNullOutput());
430: return null != winOper
431: .findSubComponent(new ComponentChooser() {
432: public boolean checkComponent(
433: Component comp) {
434: return comp
435: .getClass()
436: .getName()
437: .startsWith(
438: "org.netbeans.editor.ext.Find"); //NOI18N
439: }
440:
441: public String getDescription() {
442: return ("any replace dialog"); //NOI18N
443: }
444: });
445: }
446:
447: public String getDescription() {
448: return "containing any replace dialog"; //NOI18N
449: }
450: });
451: if (replaceWindow != null) {
452: new Replace().close();
453: }
454: }
455:
456: public static void main(String[] args) {
457: TestRunner.run(ReplaceTest.class);
458: }
459:
460: }
|