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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package search_replace;
029:
030: import java.awt.Color;
031: import java.awt.Component;
032: import java.awt.Container;
033: import java.awt.event.KeyEvent;
034: import javax.swing.JPanel;
035: import javax.swing.JToolBar;
036: import junit.textui.TestRunner;
037: import lib.EditorTestCase;
038: import org.netbeans.jellytools.EditorOperator;
039: import org.netbeans.jemmy.EventTool;
040: import org.netbeans.jemmy.operators.ContainerOperator;
041: import org.netbeans.jemmy.operators.JButtonOperator;
042: import org.netbeans.jemmy.operators.JCheckBoxOperator;
043: import org.netbeans.jemmy.operators.JEditorPaneOperator;
044: import org.netbeans.jemmy.operators.JLabelOperator;
045: import org.netbeans.jemmy.operators.JTextFieldOperator;
046:
047: /**
048: *
049: * @author Jiri Prox
050: */
051: public class IncrementalSearchTest extends EditorTestCase {
052:
053: public IncrementalSearchTest(String name) {
054: super (name);
055: }
056:
057: JPanel searchBar;
058:
059: public boolean getSearchBar(Container comp) {
060: if (comp.getClass().getName().equals(
061: "org.netbeans.modules.editor.impl.SearchBar")) {
062: searchBar = (JPanel) comp;
063: return true;
064: }
065: Component[] coms = comp.getComponents();
066: for (Component component : coms) {
067: if (Container.class.isAssignableFrom(component.getClass())) {
068: if (getSearchBar((Container) component))
069: return true;
070: }
071: }
072: return false;
073: }
074:
075: public void testSearchForward() {
076: openDefaultProject();
077: openDefaultSampleFile();
078: EditorOperator editor = getDefaultSampleEditorOperator();
079: editor.setCaretPosition(4, 1);
080: openSearchBar(editor);
081: ContainerOperator c = new ContainerOperator(searchBar);
082: JTextFieldOperator t = new JTextFieldOperator(c);
083: t.clearText();
084: new EventTool().waitNoEvent(100);
085: t.typeText("p");
086: t.pushKey(KeyEvent.VK_ENTER);
087: assertSelection(editor, 97, 98);
088: t.typeText("u");
089: t.pushKey(KeyEvent.VK_ENTER);
090: assertSelection(editor, 113, 115);
091: t.typeText("b");
092: t.pushKey(KeyEvent.VK_ENTER);
093: assertSelection(editor, 126, 129);
094: t.pushKey(KeyEvent.VK_ENTER);
095: assertSelection(editor, 170, 173);
096: JLabelOperator status = new JLabelOperator(editor, 3);
097: t.pushKey(KeyEvent.VK_ENTER);
098: assertSelection(editor, 47, 50);
099: assertEquals(
100: "'pub' found at 3:1; End of the document reached. Continuing search from beginning.",
101: status.getText());
102: t.pushKey(KeyEvent.VK_ESCAPE);
103: new EventTool().waitNoEvent(100);
104: assertFalse("ToolBar not closed", searchBar.isVisible());
105: closeFileWithDiscard();
106: }
107:
108: public void testSearchBackwards() {
109: openDefaultProject();
110: openFile(
111: "Source Packages|search_replace.IncrementalSearchTest",
112: "testSearchForward");
113: EditorOperator editor = new EditorOperator("testSearchForward");
114: editor.setCaretPosition(11, 1);
115: openSearchBar(editor);
116: ContainerOperator c = new ContainerOperator(searchBar);
117: JTextFieldOperator t = new JTextFieldOperator(c);
118: t.clearText();
119: new EventTool().waitNoEvent(100);
120: t.typeText("pub");
121: t.pushKey(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
122: assertSelection(editor, 126, 129);
123: t.pushKey(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
124: assertSelection(editor, 47, 50);
125: t.pushKey(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
126: assertSelection(editor, 170, 173);
127: JLabelOperator status = new JLabelOperator(editor, 3);
128: assertEquals(
129: "'pub' found at 12:5; Beginning of the document reached. Continuing search from end.",
130: status.getText());
131: t.pushKey(KeyEvent.VK_ESCAPE);
132: new EventTool().waitNoEvent(100);
133: assertFalse("ToolBar not closed", searchBar.isVisible());
134: editor.closeDiscard();
135: }
136:
137: public void testMatchCase() {
138: openDefaultProject();
139: openFile(
140: "Source Packages|search_replace.IncrementalSearchTest",
141: "match.txt");
142: EditorOperator editor = new EditorOperator("match.txt");
143: editor.setCaretPosition(1, 1);
144: new EventTool().waitNoEvent(500);
145: openSearchBar(editor);
146: ContainerOperator c = new ContainerOperator(searchBar);
147: JCheckBoxOperator jcbo = new JCheckBoxOperator(c);
148: jcbo.setSelected(true);
149: JTextFieldOperator t = new JTextFieldOperator(c);
150: t.clearText();
151: new EventTool().waitNoEvent(100);
152: t.typeText("Abc");
153: t.pushKey(KeyEvent.VK_ENTER);
154: assertSelection(editor, 8, 11);
155: t.pushKey(KeyEvent.VK_ESCAPE);
156: new EventTool().waitNoEvent(100);
157: assertFalse("ToolBar not closed", searchBar.isVisible());
158: openSearchBar(editor);
159: assertTrue("Checkbox state is not persisten", jcbo.isSelected());
160: assertEquals("Last searched text not persisten", t.getText(),
161: "Abc");
162: jcbo.setSelected(false);
163: t.pushKey(KeyEvent.VK_ESCAPE);
164: new EventTool().waitNoEvent(100);
165: assertFalse("ToolBar not closed", searchBar.isVisible());
166: editor.closeDiscard();
167: }
168:
169: public void testNextButton() {
170: openDefaultProject();
171: openFile(
172: "Source Packages|search_replace.IncrementalSearchTest",
173: "match.txt");
174: EditorOperator editor = new EditorOperator("match.txt");
175: editor.setCaretPosition(1, 1);
176: openSearchBar(editor);
177: ContainerOperator c = new ContainerOperator(searchBar);
178: JTextFieldOperator t = new JTextFieldOperator(c);
179: t.clearText();
180: new EventTool().waitNoEvent(100);
181: t.typeText("abc");
182: JButtonOperator b = new JButtonOperator(c, 1); //Newxt button
183: b.push();
184: assertSelection(editor, 12, 15);
185: b.push();
186: assertSelection(editor, 8, 11);
187: t.pushKey(KeyEvent.VK_ESCAPE);
188: new EventTool().waitNoEvent(100);
189: assertFalse("ToolBar not closed", searchBar.isVisible());
190: editor.closeDiscard();
191: }
192:
193: public void testPrevButton() {
194: openDefaultProject();
195: openFile(
196: "Source Packages|search_replace.IncrementalSearchTest",
197: "match.txt");
198: EditorOperator editor = new EditorOperator("match.txt");
199: editor.setCaretPosition(3, 1);
200: openSearchBar(editor);
201: ContainerOperator c = new ContainerOperator(searchBar);
202: JTextFieldOperator t = new JTextFieldOperator(c);
203: t.clearText();
204: new EventTool().waitNoEvent(100);
205: t.typeText("abc");
206: JButtonOperator b = new JButtonOperator(c, 2); // Previous button
207: b.push();
208: assertSelection(editor, 8, 11);
209: b.push();
210: assertSelection(editor, 12, 15);
211: t.pushKey(KeyEvent.VK_ESCAPE);
212: new EventTool().waitNoEvent(100);
213: assertFalse("ToolBar not closed", searchBar.isVisible());
214: editor.closeDiscard();
215: }
216:
217: public void testCloseButton() {
218: openDefaultProject();
219: openFile(
220: "Source Packages|search_replace.IncrementalSearchTest",
221: "match.txt");
222: EditorOperator editor = new EditorOperator("match.txt");
223: editor.setCaretPosition(3, 1);
224: openSearchBar(editor);
225: ContainerOperator c = new ContainerOperator(searchBar);
226: JTextFieldOperator t = new JTextFieldOperator(c);
227: JButtonOperator b = new JButtonOperator(c, 3); // close
228: b.push();
229: new EventTool().waitNoEvent(250);
230: if (searchBar.isVisible()) { //2nd try
231: b = new JButtonOperator(c, 4); // close
232: b.push();
233: new EventTool().waitNoEvent(250);
234: }
235: assertFalse("ToolBar not closed", searchBar.isVisible());
236: editor.closeDiscard();
237: }
238:
239: public void testNotFound() {
240: openDefaultProject();
241: openFile(
242: "Source Packages|search_replace.IncrementalSearchTest",
243: "match.txt");
244: EditorOperator editor = new EditorOperator("match.txt");
245: editor.setCaretPosition(3, 1);
246: openSearchBar(editor);
247: ContainerOperator c = new ContainerOperator(searchBar);
248: JTextFieldOperator t = new JTextFieldOperator(c);
249: t.clearText();
250: new EventTool().waitNoEvent(100);
251: t.typeText("XYZ");
252: assertEquals(new Color(178, 0, 0), t.getForeground());
253: t.pushKey(KeyEvent.VK_ENTER);
254: JLabelOperator status = new JLabelOperator(editor, 3);
255: assertEquals("'XYZ' not found", status.getText());
256: t.pushKey(KeyEvent.VK_ESCAPE);
257: new EventTool().waitNoEvent(100);
258: assertFalse("ToolBar not closed", searchBar.isVisible());
259: editor.closeDiscard();
260: }
261:
262: public void testSearchForwardBackward() {
263: openDefaultProject();
264: openFile(
265: "Source Packages|search_replace.IncrementalSearchTest",
266: "match.txt");
267: EditorOperator editor = new EditorOperator("match.txt");
268: editor.setCaretPosition(2, 1);
269: openSearchBar(editor);
270: ContainerOperator c = new ContainerOperator(searchBar);
271: JTextFieldOperator t = new JTextFieldOperator(c);
272: t.clearText();
273: new EventTool().waitNoEvent(100);
274: t.typeText("abc");
275: t.pushKey(KeyEvent.VK_ENTER);
276: assertSelection(editor, 4, 7);
277: t.pushKey(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK);
278: assertSelection(editor, 0, 3);
279: editor.txtEditorPane().requestFocus();
280: new EventTool().waitNoEvent(100);
281: assertFalse("ToolBar not closed by focus transher ", searchBar
282: .isVisible());
283: editor.closeDiscard();
284: }
285:
286: public void testWholeWords() {
287: openDefaultProject();
288: openFile(
289: "Source Packages|search_replace.IncrementalSearchTest",
290: "match2.txt");
291: EditorOperator editor = new EditorOperator("match2.txt");
292: editor.setCaretPosition(1, 1);
293: openSearchBar(editor);
294: ContainerOperator c = new ContainerOperator(searchBar);
295: JTextFieldOperator t = new JTextFieldOperator(c);
296: JCheckBoxOperator ch1 = new JCheckBoxOperator(c, 1);
297: ch1.setSelected(true);
298: t.clearText();
299: t.typeText("whole");
300: t.pushKey(KeyEvent.VK_ENTER);
301: assertSelection(editor, 12, 17);
302: t.pushKey(KeyEvent.VK_ESCAPE);
303: new EventTool().waitNoEvent(100);
304: assertFalse("ToolBar not closed", searchBar.isVisible());
305: closeFileWithDiscard();
306: }
307:
308: public void testRegularExpression() {
309: openDefaultProject();
310: openFile(
311: "Source Packages|search_replace.IncrementalSearchTest",
312: "match2.txt");
313: EditorOperator editor = new EditorOperator("match2.txt");
314: editor.setCaretPosition(1, 1);
315: openSearchBar(editor);
316: ContainerOperator c = new ContainerOperator(searchBar);
317: JTextFieldOperator t = new JTextFieldOperator(c);
318: JCheckBoxOperator ch1 = new JCheckBoxOperator(c, 1);
319: ch1.setSelected(false);
320: JCheckBoxOperator ch2 = new JCheckBoxOperator(c, 2);
321: ch2.setSelected(true);
322: t.clearText();
323: t.typeText("[aA]b*cd?");
324: t.pushKey(KeyEvent.VK_ENTER);
325: assertSelection(editor, 19, 23);
326: t.pushKey(KeyEvent.VK_ENTER);
327: assertSelection(editor, 36, 39);
328: t.pushKey(KeyEvent.VK_ESCAPE);
329: new EventTool().waitNoEvent(100);
330: assertFalse("ToolBar not closed", searchBar.isVisible());
331: closeFileWithDiscard();
332: }
333:
334: public void testFindNext() {
335: openDefaultProject();
336: openFile(
337: "Source Packages|search_replace.IncrementalSearchTest",
338: "match2.txt");
339: EditorOperator editor = new EditorOperator("match2.txt");
340: editor.setCaretPosition(1, 1);
341: openSearchBar(editor);
342: ContainerOperator c = new ContainerOperator(searchBar);
343: JTextFieldOperator t = new JTextFieldOperator(c);
344: JCheckBoxOperator ch1 = new JCheckBoxOperator(c, 1);
345: ch1.setSelected(false);
346: JCheckBoxOperator ch2 = new JCheckBoxOperator(c, 2);
347: ch2.setSelected(false);
348: t.clearText();
349: t.typeText("ab");
350: t.pushKey(KeyEvent.VK_ENTER);
351: assertSelection(editor, 19, 21);
352: t.pushKey(KeyEvent.VK_ESCAPE);
353: new EventTool().waitNoEvent(100);
354: assertFalse("ToolBar not closed", searchBar.isVisible());
355: t.pushKey(KeyEvent.VK_F3);
356: assertSelection(editor, 24, 26);
357: closeFileWithDiscard();
358: }
359:
360: public void testFindPrev() {
361: openDefaultProject();
362: openFile(
363: "Source Packages|search_replace.IncrementalSearchTest",
364: "match2.txt");
365: EditorOperator editor = new EditorOperator("match2.txt");
366: editor.setCaretPosition(6, 1);
367: openSearchBar(editor);
368: ContainerOperator c = new ContainerOperator(searchBar);
369: JTextFieldOperator t = new JTextFieldOperator(c);
370: JCheckBoxOperator ch1 = new JCheckBoxOperator(c, 1);
371: ch1.setSelected(false);
372: JCheckBoxOperator ch2 = new JCheckBoxOperator(c, 2);
373: ch2.setSelected(false);
374: t.clearText();
375: t.typeText("ab");
376: t.pushKey(KeyEvent.VK_ENTER);
377: assertSelection(editor, 36, 38);
378: t.pushKey(KeyEvent.VK_ESCAPE);
379: new EventTool().waitNoEvent(100);
380: assertFalse("ToolBar not closed", searchBar.isVisible());
381: t.pushKey(KeyEvent.VK_F3, KeyEvent.SHIFT_DOWN_MASK);
382: assertSelection(editor, 36, 38);
383: t.pushKey(KeyEvent.VK_F3, KeyEvent.SHIFT_DOWN_MASK);
384: assertSelection(editor, 24, 26);
385: closeFileWithDiscard();
386: }
387:
388: public void assertSelection(EditorOperator editor, int start,
389: int end) {
390: JEditorPaneOperator txtEditorPane = editor.txtEditorPane();
391: int actStart = txtEditorPane.getSelectionStart();
392: int actEnd = txtEditorPane.getSelectionEnd();
393: if (actStart != start || actEnd != end)
394: fail("Wrong text selected in editor, actual selection <"
395: + actStart + "," + actEnd + ">, expected <" + start
396: + "," + end + ">");
397: }
398:
399: public static void main(String[] args) {
400: TestRunner.run(IncrementalSearchTest.class);
401: }
402:
403: private void openSearchBar(EditorOperator editor) {
404: editor.pressKey(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK);
405: new EventTool().waitNoEvent(250);
406: getSearchBar((Container) editor.getSource());
407: assertTrue("ToolBar not opened", searchBar.isVisible());
408: }
409:
410: }
|