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.awt.Toolkit;
044: import java.awt.datatransfer.DataFlavor;
045: import java.awt.datatransfer.Transferable;
046: import org.netbeans.jellytools.actions.CompileAction;
047: import org.netbeans.jellytools.nodes.Node;
048: import org.netbeans.jellytools.nodes.SourcePackagesNode;
049: import org.netbeans.jemmy.EventTool;
050: import org.netbeans.jemmy.JemmyException;
051: import org.netbeans.jemmy.JemmyProperties;
052: import org.netbeans.jemmy.Waitable;
053: import org.netbeans.jemmy.Waiter;
054: import org.netbeans.jemmy.operators.JButtonOperator;
055: import org.netbeans.jemmy.operators.JTextFieldOperator;
056: import org.netbeans.junit.NbTest;
057: import org.netbeans.junit.NbTestSuite;
058:
059: /** Test of OutputTabOperator.
060: *
061: * @author Jiri.Skrivanek@sun.com
062: */
063: public class OutputTabOperatorTest extends JellyTestCase {
064:
065: public OutputTabOperatorTest(java.lang.String testName) {
066: super (testName);
067: }
068:
069: public static void main(java.lang.String[] args) {
070: junit.textui.TestRunner.run(suite());
071: }
072:
073: public static NbTest suite() {
074: NbTestSuite suite = new NbTestSuite();
075: // suites have to be in particular order
076: suite.addTest(new OutputTabOperatorTest(
077: "testMakeComponentVisible"));
078: suite.addTest(new OutputTabOperatorTest("testFindLine"));
079: suite.addTest(new OutputTabOperatorTest("testGetText"));
080: suite.addTest(new OutputTabOperatorTest("testWaitText"));
081: suite.addTest(new OutputTabOperatorTest("testGetLineCount"));
082: suite.addTest(new OutputTabOperatorTest("testGetLine"));
083: suite.addTest(new OutputTabOperatorTest("testGetLength"));
084: suite.addTest(new OutputTabOperatorTest("testVerify"));
085: suite.addTest(new OutputTabOperatorTest("testSelectAll"));
086: suite.addTest(new OutputTabOperatorTest("testCopy"));
087: suite.addTest(new OutputTabOperatorTest("testFind"));
088: suite.addTest(new OutputTabOperatorTest("testFindNext"));
089: suite.addTest(new OutputTabOperatorTest("testSaveAs"));
090: //suite.addTest(new OutputTabOperatorTest("testNextError"));
091: //suite.addTest(new OutputTabOperatorTest("testPreviousError"));
092: suite.addTest(new OutputTabOperatorTest("testWrapText"));
093: suite.addTest(new OutputTabOperatorTest("testClear"));
094: suite.addTest(new OutputTabOperatorTest("testClose"));
095: return suite;
096: }
097:
098: /** Print out test name. */
099: public void setUp() {
100: System.out.println("### " + getName() + " ###");
101: }
102:
103: private static final String targetName = "compile-single";
104: //private static final String targetName = "compile-test-single";
105: private static OutputTabOperator outputTabOperator;
106:
107: /** Compiles a source which opens output tab to test. */
108: private static void initTab() {
109: Node sample1 = new Node(
110: new SourcePackagesNode("SampleProject"), "sample1"); // NOI18N
111: Node sample2 = new Node(
112: new SourcePackagesNode("SampleProject"),
113: "sample1.sample2"); // NOI18N
114: Node sampleClass1 = new Node(sample1, "SampleClass1.java"); // NOI18N
115: CompileAction compileAction = new CompileAction();
116: compileAction.perform(sampleClass1);
117: outputTabOperator = new OutputTabOperator(targetName);
118: // increase time to wait
119: outputTabOperator.getTimeouts().setTimeout(
120: "ComponentOperator.WaitStateTimeout", 60000);
121: // wait build is finished
122: outputTabOperator.waitText("BUILD SUCCESSFUL");
123: }
124:
125: /**
126: * Test of makeComponentVisible method.
127: */
128: public void testMakeComponentVisible() {
129: initTab();
130: // should be improved to use 2 terms and activate the hidden one
131: outputTabOperator.makeComponentVisible();
132: assertTrue(targetName + " output tab should be visible.",
133: outputTabOperator.isShowing());
134: }
135:
136: /**
137: * Test of findLine method.
138: */
139: public void testFindLine() {
140: assertTrue("Wrong row found.", outputTabOperator
141: .findLine(targetName) > 0); // NOI18N
142: }
143:
144: /**
145: * Test of getText method.
146: */
147: public void testGetText() {
148: String text = outputTabOperator.getText();
149: assertTrue("Text is not from " + targetName + " output tab.",
150: text.indexOf(targetName) > -1);
151: String twoLines = outputTabOperator.getText(0, 1);
152: assertTrue(
153: "Text from first and second lines should contain 2 ':'",
154: twoLines.indexOf(':') != twoLines.lastIndexOf(':'));
155: }
156:
157: /**
158: * Test of waitText method.
159: */
160: public void testWaitText() {
161: outputTabOperator.waitText(targetName);
162: }
163:
164: /**
165: * Test of getLineCount method.
166: */
167: public void testGetLineCount() {
168: assertTrue("Wrong line count.", outputTabOperator
169: .getLineCount() > 0);
170: }
171:
172: /**
173: * Test of getLine() method.
174: */
175: public void testGetLine() {
176: assertTrue("Wrong text found.", outputTabOperator.getLine(
177: outputTabOperator.findLine(targetName)).indexOf(
178: targetName) > -1);
179: }
180:
181: /**
182: * Test of getLength() method.
183: */
184: public void testGetLength() {
185: assertEquals("Wrong length returned.", outputTabOperator
186: .getLength(), outputTabOperator.getText().length());
187: }
188:
189: /**
190: * Test of verify method.
191: */
192: public void testVerify() {
193: outputTabOperator.verify();
194: }
195:
196: /**
197: * Test of selectAll method.
198: */
199: public void testSelectAll() {
200: clearTestStatus();
201: startTest();
202: outputTabOperator.selectAll();
203: endTest();
204: }
205:
206: /**
207: * Test of copy method.
208: */
209: public void testCopy() throws Exception {
210: startTest();
211: clearTestStatus();
212: outputTabOperator.copy();
213: assertTrue("Copy doesn't work.", getClipboardText().indexOf(
214: targetName) > -1);
215: }
216:
217: /**
218: * Test of find method.
219: */
220: public void testFind() {
221: outputTabOperator.find();
222: // "Find"
223: String findTitle = Bundle.getString(
224: "org.netbeans.core.output2.Bundle", "LBL_Find_Title");
225: NbDialogOperator findDialog = new NbDialogOperator(findTitle);
226: // assuming somthing with 'a' is printed in output
227: JTextFieldOperator jtfo = new JTextFieldOperator(findDialog);
228: jtfo.enterText("a"); // NOI18N
229: try {
230: // need to wait find action is finished
231: findDialog.waitClosed();
232: } catch (JemmyException e) {
233: // sometimes it fails on Solaris => try it once more in Robot mode
234: log("Dialog not closed first time. Trying once more.");
235: // "Find"
236: String findButtonLabel = Bundle.getStringTrimmed(
237: "org.netbeans.core.output2.Bundle", "BTN_Find");
238: JButtonOperator findButtonOper = new JButtonOperator(
239: findDialog, findButtonLabel);
240: findButtonOper.getProperties().setDispatchingModel(
241: JemmyProperties.ROBOT_MODEL_MASK);
242: findButtonOper.push();
243: findDialog.waitClosed();
244: }
245: new EventTool().waitNoEvent(1000);
246: }
247:
248: /**
249: * Test of findNext method.
250: */
251: public void testFindNext() {
252: outputTabOperator.findNext();
253: }
254:
255: /**
256: * Test of saveAs method.
257: */
258: public void testSaveAs() {
259: outputTabOperator.saveAs();
260: // "Save As"
261: String saveAsTitle = Bundle.getString(
262: "org.netbeans.core.output2.Bundle", "TITLE_SAVE_DLG");
263: new NbDialogOperator(saveAsTitle).close();
264: }
265:
266: /** Test of nextError method. */
267: public void testNextError() {
268: // TODO add test some day
269: //outputTabOperator.nextError();
270: }
271:
272: /** Test of previousError method. */
273: public void testPreviousError() {
274: // TODO add test some day
275: //outputTabOperator.previousError();
276: }
277:
278: /** Test of wrapText method. */
279: public void testWrapText() {
280: // set
281: outputTabOperator.wrapText();
282: // unset
283: outputTabOperator.wrapText();
284: }
285:
286: /** Test of clear method. */
287: public void testClear() {
288: outputTabOperator.clear();
289: assertTrue("Text was not cleared.", outputTabOperator.getText()
290: .length() == 0);
291: }
292:
293: /**f
294: * Test of close method.
295: */
296: public void testClose() {
297: outputTabOperator.close();
298: assertFalse("Output tab should be closed.", outputTabOperator
299: .isShowing());
300: }
301:
302: /** Wait until clipboard contains string data and returns the text. */
303: private String getClipboardText() throws Exception {
304: Waiter waiter = new Waiter(new Waitable() {
305: public Object actionProduced(Object obj) {
306: Transferable contents = Toolkit.getDefaultToolkit()
307: .getSystemClipboard().getContents(null);
308: if (contents == null) {
309: return null;
310: } else {
311: return contents
312: .isDataFlavorSupported(DataFlavor.stringFlavor) ? Boolean.TRUE
313: : null;
314: }
315: }
316:
317: public String getDescription() {
318: return ("Wait clipboard contains string data");
319: }
320: });
321: waiter.waitAction(null);
322: return Toolkit.getDefaultToolkit().getSystemClipboard()
323: .getContents(null).getTransferData(
324: DataFlavor.stringFlavor).toString();
325: }
326: }
|