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-2007 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: package org.netbeans.jellytools;
042:
043: import java.lang.reflect.Method;
044: import javax.swing.text.JTextComponent;
045: import junit.framework.Test;
046: import junit.framework.TestSuite;
047: import junit.textui.TestRunner;
048: import org.netbeans.jellytools.nodes.JavaNode;
049: import org.netbeans.jellytools.nodes.Node;
050: import org.netbeans.jellytools.nodes.SourcePackagesNode;
051: import org.netbeans.jemmy.JemmyException;
052: import org.netbeans.jemmy.Waitable;
053: import org.netbeans.jemmy.Waiter;
054: import org.netbeans.jemmy.operators.AbstractButtonOperator;
055: import org.netbeans.junit.NbTestSuite;
056:
057: /**
058: * Test of org.netbeans.jellytools.EditorOperator.
059: * Order of tests is important.
060: * @author Jiri.Skrivanek@sun.com
061: */
062: public class EditorOperatorTest extends JellyTestCase {
063:
064: /** Constructor required by JUnit.
065: * @param testName method name to be used as testcase
066: */
067: public EditorOperatorTest(java.lang.String testName) {
068: super (testName);
069: }
070:
071: /** Use for internal test execution inside IDE
072: * @param args command line arguments
073: */
074: public static void main(java.lang.String[] args) {
075: TestRunner.run(suite());
076: }
077:
078: /** Method used for explicit testsuite definition
079: * @return created suite
080: */
081: public static Test suite() {
082: TestSuite suite = new NbTestSuite();
083: suite.addTest(new EditorOperatorTest("testTxtEditorPane"));
084: suite.addTest(new EditorOperatorTest("testLblRowColumn"));
085: suite.addTest(new EditorOperatorTest("testLblStatusBar"));
086: suite.addTest(new EditorOperatorTest("testLblInputMode"));
087: suite.addTest(new EditorOperatorTest("testGetText"));
088: suite.addTest(new EditorOperatorTest("testContains"));
089: suite.addTest(new EditorOperatorTest("testSelect"));
090: suite.addTest(new EditorOperatorTest("testGetLineNumber"));
091: suite.addTest(new EditorOperatorTest("testPushHomeKey"));
092: suite.addTest(new EditorOperatorTest("testPushEndKey"));
093: suite.addTest(new EditorOperatorTest("testPushDownArrowKey"));
094: suite.addTest(new EditorOperatorTest("testPushUpArrowKey"));
095: suite.addTest(new EditorOperatorTest("testFolding"));
096: suite.addTest(new EditorOperatorTest(
097: "testSetCaretPositionRelative"));
098: suite.addTest(new EditorOperatorTest(
099: "testSetCaretPositionToLine"));
100: suite.addTest(new EditorOperatorTest("testSetCaretPosition"));
101: suite.addTest(new EditorOperatorTest("testGetToolbarButton"));
102: suite.addTest(new EditorOperatorTest("testReplace"));
103: suite.addTest(new EditorOperatorTest("testInsert"));
104: // annotations have to be tested after testInsert because of parser annotations
105: suite.addTest(new EditorOperatorTest("testGetAnnotations"));
106: suite.addTest(new EditorOperatorTest("testGetAnnotationType"));
107: suite.addTest(new EditorOperatorTest(
108: "testGetAnnotationShortDescription"));
109: suite.addTest(new EditorOperatorTest("testDelete"));
110: suite.addTest(new EditorOperatorTest("testPushTabKey"));
111: suite.addTest(new EditorOperatorTest("testCloseDiscard"));
112: return suite;
113: }
114:
115: private static EditorOperator eo;
116:
117: /** Opens sample class and finds EditorOperator instance */
118: protected void setUp() {
119: System.out.println("### " + getName() + " ###");
120: if (eo == null) {
121: Node sample1 = new Node(new SourcePackagesNode(
122: "SampleProject"), "sample1"); // NOI18N
123: JavaNode sampleClass1 = new JavaNode(sample1,
124: SAMPLE_CLASS_1);
125: sampleClass1.open();
126: eo = new EditorOperator(SAMPLE_CLASS_1);
127: }
128: }
129:
130: private static final String SAMPLE_CLASS_1 = "SampleClass1";
131:
132: /** Test of txtEditorPane method. */
133: public void testTxtEditorPane() {
134: String text = eo.txtEditorPane().getText();
135: assertTrue("Wrong editor pane found.", text
136: .indexOf(SAMPLE_CLASS_1) != -1);
137: }
138:
139: /** Test of lblRowColumn method. */
140: public void testLblRowColumn() {
141: assertEquals("1:1", eo.lblRowColumn().getText());
142: }
143:
144: /** Test of lblInputMode method. */
145: public void testLblInputMode() {
146: String expected = Bundle.getString(
147: "org.netbeans.editor.Bundle", "status-bar-insert");
148: assertEquals(expected, eo.lblInputMode().getText());
149: }
150:
151: /** Test of lblStatusBar method. */
152: public void testLblStatusBar() {
153: String expected = "Status bar text";
154: // set text to status bar
155: try {
156: String className = "org.netbeans.editor.Utilities";
157: Class clazz = Class.forName(className);
158: Method setStatusTextMethod = clazz.getDeclaredMethod(
159: "setStatusText", new Class[] {
160: JTextComponent.class, String.class });
161: setStatusTextMethod.invoke(null, new Object[] {
162: (JTextComponent) eo.txtEditorPane().getSource(),
163: expected });
164: } catch (Exception e) {
165: e.printStackTrace(getLog());
166: fail("Error in reflection operations: " + e.getMessage());
167: }
168: assertEquals("Wrong label found.", expected, eo.lblStatusBar()
169: .getText());
170: }
171:
172: /** Test of getText method. */
173: public void testGetText() {
174: String text = eo.getText();
175: String expected = SAMPLE_CLASS_1;
176: assertTrue("Found \"" + text + "\" but expected \"" + expected
177: + "\".", text.indexOf(expected) != -1);
178: expected = "public static void main";
179: eo.setCaretPosition(expected, true);
180: text = eo.getText(eo.getLineNumber());
181: assertTrue("Found \"" + text + "\" but expected \"" + expected
182: + "\".", text.indexOf(expected) != -1);
183: }
184:
185: /** Test of contains method. */
186: public void testContains() {
187: assertTrue("Editor should contain \"" + SAMPLE_CLASS_1 + "\".",
188: eo.contains(SAMPLE_CLASS_1));
189: String dummy = "Dummy string @#$%^&";
190: assertTrue("Editor should not contain \"" + dummy + "\".", !eo
191: .contains(dummy));
192: }
193:
194: /** Test of select method. */
195: public void testSelect() {
196: eo.setCaretPosition("public static void main", true);
197: int line = eo.getLineNumber();
198: eo.select(line);
199: String expected = eo.getText(line);
200: String selected = eo.txtEditorPane().getSelectedText() + "\n";
201: assertEquals("Wrong selection.", expected.trim(), selected
202: .trim());
203: eo.select(line, line + 1);
204: expected = eo.getText(line) + eo.getText(line + 1);
205: selected = eo.txtEditorPane().getSelectedText() + "\n";
206: assertEquals("Wrong selection.", expected.trim(), selected
207: .trim());
208: eo.select(line, 5, 10);
209: expected = eo.getText(line).substring(4, 10);
210: selected = eo.txtEditorPane().getSelectedText();
211: assertEquals("Wrong selection.", expected, selected);
212: expected = "public static void main";
213: eo.select(expected);
214: selected = eo.txtEditorPane().getSelectedText();
215: assertEquals("Wrong selection.", expected, selected);
216: expected = "public static void main";
217: eo.select("public", 2);
218: assertEquals("Second occurence of word \"public\" on line "
219: + line + " should be selected.", line, eo
220: .getLineNumber());
221: }
222:
223: /** Test of getLineNumber method. */
224: public void testGetLineNumber() {
225: eo.setCaretPosition(0);
226: assertEquals("Wrong line number.", 1, eo.getLineNumber());
227: }
228:
229: /** Test of pushHomeKey method. */
230: public void testPushHomeKey() {
231: eo.setCaretPosition(eo.getText(1).length() - 1);
232: eo.pushHomeKey();
233: assertEquals("Wrong position after key pushed.", 0, eo
234: .txtEditorPane().getCaretPosition());
235: }
236:
237: /** Test of pushEndKey method. */
238: public void testPushEndKey() {
239: eo.setCaretPosition(0);
240: eo.pushEndKey();
241: assertEquals("Wrong position after key pushed.", eo.getText(1)
242: .length() - 1, eo.txtEditorPane().getCaretPosition());
243: }
244:
245: /** Test of pushDownArrowKey method. */
246: public void testPushDownArrowKey() {
247: eo.setCaretPosition(0);
248: eo.pushDownArrowKey();
249: assertEquals("Wrong line after key pushed.", 2, eo
250: .getLineNumber());
251: }
252:
253: /** Test of pushUpArrowKey method. */
254: public void testPushUpArrowKey() {
255: eo.setCaretPositionToLine(2);
256: eo.pushUpArrowKey();
257: assertEquals("Wrong line after key pushed.", 1, eo
258: .getLineNumber());
259: }
260:
261: /** Test of setCaretPositionRelative method. */
262: public void testSetCaretPositionRelative() {
263: eo.setCaretPosition(0);
264: int expected = 20;
265: eo.setCaretPositionRelative(expected);
266: assertEquals("Wrong caret position.", expected, eo
267: .txtEditorPane().getCaretPosition());
268: }
269:
270: /** Test of setCaretPositionToLine method. */
271: public void testSetCaretPositionToLine() {
272: int expected = 10;
273: eo.setCaretPositionToLine(10);
274: assertEquals("Wrong line.", expected, eo.getLineNumber());
275: }
276:
277: /** Test of setCaretPosition method. */
278: public void testSetCaretPosition() {
279: eo.setCaretPosition(0);
280: assertEquals("Wrong caret position.", 0, eo.txtEditorPane()
281: .getCaretPosition());
282: String expected = "public static void main";
283: eo.setCaretPosition(expected, true);
284: int position = eo.txtEditorPane().getCaretPosition();
285: String text = eo.txtEditorPane().getText(position,
286: expected.length());
287: assertEquals("Wrong caret position before text.", expected,
288: text);
289: eo.setCaretPosition(position);
290: int newPosition = eo.txtEditorPane().getCaretPosition();
291: assertEquals("Wrong caret position.", position, newPosition);
292: eo.setCaretPosition(expected, false);
293: position = eo.txtEditorPane().getCaretPosition();
294: text = eo.txtEditorPane().getText(position - expected.length(),
295: expected.length());
296: assertEquals("Wrong caret position after text.", expected, text);
297: eo.setCaretPosition("public", 2, true);
298: position = eo.txtEditorPane().getCaretPosition();
299: text = eo.txtEditorPane().getText(position, expected.length());
300: assertEquals("Wrong caret position before text.", expected,
301: text);
302: eo.setCaretPosition("public", 2, false);
303: position = eo.txtEditorPane().getCaretPosition();
304: text = eo.txtEditorPane().getText(position - "public".length(),
305: expected.length());
306: assertEquals("Wrong caret position after text.", expected, text);
307: }
308:
309: /** Test of getToolbarButton method. Uses "Toggle bookmark button". */
310: public void testGetToolbarButton() {
311: String tooltip = Bundle.getString(
312: "org.netbeans.lib.editor.bookmarks.actions.Bundle",
313: "bookmark-toggle");
314: AbstractButtonOperator button1 = eo.getToolbarButton(tooltip);
315: button1.push();
316: AbstractButtonOperator button2 = eo.getToolbarButton(9);
317: assertEquals("Toggle Bookmark button should have index 9",
318: button1.getToolTipText(), button2.getToolTipText());
319: }
320:
321: /** Test of replace method. */
322: public void testReplace() {
323: String oldText = "public static void main";
324: String newText = "XXXXXXXXXXXXXXXXXX";
325: eo.replace(oldText, newText);
326: assertTrue("Replace of \"" + oldText + "\" by \"" + newText
327: + "\" failed.", eo.contains(newText));
328: // replace back
329: eo.replace(newText, oldText);
330: }
331:
332: /** Test of insert method. */
333: public void testInsert() {
334: eo.setCaretPosition(0);
335: eo.insert("111 First line\n");
336: eo.insert("222 Second line\n");
337: eo.insert("333 Third line\n");
338: assertEquals("Insertion failed on first line.",
339: "111 First line\n", eo.getText(1));
340: eo.insert(" addendum", 3, 15);
341: assertEquals("Insertion failed on third line.",
342: "333 Third line addendum\n", eo.getText(3));
343: }
344:
345: /** Test of getAnnotations method. Expects bookmark
346: * created in test testGetToolbarButton() and parser annotations on
347: * inserted lines (testInsert).
348: * @throws java.lang.InterruptedException
349: */
350: public void testGetAnnotations() throws InterruptedException {
351: // wait parser annotations
352: new Waiter(new Waitable() {
353: public Object actionProduced(Object oper) {
354: return eo.getAnnotations().length > 1 ? Boolean.TRUE
355: : null;
356: }
357:
358: public String getDescription() {
359: return ("Wait parser annotations."); // NOI18N
360: }
361: }).waitAction(null);
362: }
363:
364: /** Test of getAnnotationType method. */
365: public void testGetAnnotationType() {
366: Object[] an = eo.getAnnotations();
367: String type = eo.getAnnotationType(an[0]);
368: assertNotNull("getAnnotationType return null.", type);
369: assertTrue("getAnnotationType return empty string.", type
370: .length() > 0);
371: }
372:
373: /** Test of getAnnotationShortDescription method. */
374: public void testGetAnnotationShortDescription() {
375: Object[] an = eo.getAnnotations();
376: String desc = eo.getAnnotationShortDescription(an[0]);
377: assertNotNull("getAnnotationShortDescription return null.",
378: desc);
379: assertTrue(
380: "getAnnotationShortDescription return empty string.",
381: desc.length() > 0);
382: }
383:
384: /** Test of folding methods. */
385: public static void testFolding() {
386: eo.waitFolding();
387: assertFalse("Initial comment at line 2 should be expanded.", eo
388: .isCollapsed(2));
389: eo.setCaretPositionToLine(3);
390: assertFalse("Initial comment at line 3 should be expanded.", eo
391: .isCollapsed(3));
392: try {
393: eo.setCaretPosition("package sample1;", true); // NOI18N
394: int line = eo.getLineNumber();
395: eo.isCollapsed(line);
396: fail("JemmyException should be thrown because no fold is at line "
397: + line);
398: } catch (JemmyException e) {
399: // OK.
400: }
401: eo.setCaretPositionToLine(2);
402: eo.collapseFold();
403: assertTrue("Initial comment should be collapsed now.", eo
404: .isCollapsed(2));
405: eo.expandFold();
406: eo.collapseFold(5);
407: eo.expandFold(5);
408: }
409:
410: /** Test of delete method. */
411: public void testDelete() {
412: eo.delete(3, 15, 23);
413: assertEquals("Delete on third line failed.",
414: "333 Third line\n", eo.getText(3));
415: // delete third line
416: eo.deleteLine(3);
417: assertTrue("Delete of third line failed.", !eo
418: .contains("333 Third line"));
419: // delete second line
420: eo.delete("111 First line\n".length(), "222 Second line\n"
421: .length());
422: assertTrue("Delete of second line failed.", !eo
423: .contains("222 Second line"));
424: // delete first line
425: eo.setCaretPosition(0);
426: eo.delete("111 First line\n".length());
427: assertTrue("Delete of first line failed.", !eo
428: .contains("111 First line"));
429: }
430:
431: /** Test of pushTabKey method. */
432: public void testPushTabKey() {
433: int length1 = eo.getText(1).length();
434: eo.setCaretPosition(0);
435: eo.pushTabKey();
436: int length2 = eo.getText(1).length();
437: assertTrue("Tab key not pushed.", length2 > length1);
438: }
439:
440: /** Test of closeDiscard method. */
441: public void testCloseDiscard() {
442: eo.closeDiscard();
443: eo = null;
444: }
445: }
|