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 org.netbeans.jellytools.actions.AttachWindowAction;
044: import org.netbeans.jellytools.actions.CopyAction;
045: import org.netbeans.jellytools.actions.DeleteAction;
046: import org.netbeans.jellytools.actions.OpenAction;
047: import org.netbeans.jellytools.actions.PasteActionNoBlock;
048: import org.netbeans.jellytools.nodes.Node;
049: import org.netbeans.jellytools.nodes.SourcePackagesNode;
050: import org.netbeans.jemmy.operators.JButtonOperator;
051: import org.netbeans.junit.NbTest;
052: import org.netbeans.junit.NbTestSuite;
053:
054: /** Test TopComponentOperator.
055: *
056: * @author Jiri.Skrivanek@sun.com
057: */
058: public class TopComponentOperatorTest extends JellyTestCase {
059:
060: public TopComponentOperatorTest(java.lang.String testName) {
061: super (testName);
062: }
063:
064: public static void main(java.lang.String[] args) {
065: junit.textui.TestRunner.run(suite());
066: }
067:
068: public static NbTest suite() {
069: NbTestSuite suite = new NbTestSuite();
070: // test cases have to be in particular order
071: suite.addTest(new TopComponentOperatorTest("testConstructors"));
072: suite.addTest(new TopComponentOperatorTest(
073: "testMakeComponentVisible"));
074: suite.addTest(new TopComponentOperatorTest("testAttachTo"));
075: suite.addTest(new TopComponentOperatorTest("testMaximize"));
076: suite.addTest(new TopComponentOperatorTest("testRestore"));
077: suite
078: .addTest(new TopComponentOperatorTest(
079: "testCloneDocument"));
080: suite
081: .addTest(new TopComponentOperatorTest(
082: "testPushMenuOnTab"));
083: suite.addTest(new TopComponentOperatorTest("testSaveDocument"));
084: suite.addTest(new TopComponentOperatorTest("testSave"));
085: suite.addTest(new TopComponentOperatorTest("testCloseDiscard"));
086: suite.addTest(new TopComponentOperatorTest("testCloseWindow"));
087: suite.addTest(new TopComponentOperatorTest("testClose"));
088: suite.addTest(new TopComponentOperatorTest(
089: "testCloseAllDocuments"));
090: return suite;
091: }
092:
093: /** Print out test name. */
094: public void setUp() {
095: System.out.println("### " + getName() + " ###");
096: }
097:
098: private static Node editableSourceNode;
099: private static TopComponentOperator tco1;
100: private static TopComponentOperator tco2;
101:
102: /** Opens two sources to test. */
103: private static void initSources() {
104: EditorOperator.closeDiscardAll();
105: Node sample1 = new Node(
106: new SourcePackagesNode("SampleProject"), "sample1"); // NOI18N
107: Node sample2 = new Node(
108: new SourcePackagesNode("SampleProject"),
109: "sample1.sample2"); // NOI18N
110: Node node = new Node(sample1, "SampleClass1.java");// NOI18N
111: new OpenAction().performAPI(node);
112: node = new Node(sample2, "SampleClass2.java");// NOI18N
113: new OpenAction().performAPI(node);
114: if (editableSourceNode == null) {
115: // copy node to be able to write in
116: new CopyAction().performAPI(node);
117: new PasteActionNoBlock().performAPI(sample2);
118: String copyClassTitle = Bundle.getString(
119: "org.netbeans.modules.refactoring.java.ui.Bundle",
120: "LBL_CopyClass");
121: NbDialogOperator copyClassOper = new NbDialogOperator(
122: copyClassTitle);
123: // "Refactor"
124: String refactorLabel = Bundle.getStringTrimmed(
125: "org.netbeans.modules.refactoring.spi.impl.Bundle",
126: "CTL_Finish");
127: new JButtonOperator(copyClassOper, refactorLabel).push();
128: copyClassOper.waitClosed();
129: editableSourceNode = new Node(sample2, "SampleClass21.java");// NOI18N
130: }
131: // "Navigator"
132: String navigatorLabel = Bundle.getString(
133: "org.netbeans.modules.navigator.Bundle",
134: "LBL_Navigator");
135: if (TopComponentOperator.findTopComponent(navigatorLabel, 0) != null) {
136: // close navigator because it can be mixed with editor
137: new TopComponentOperator(navigatorLabel).close();
138: }
139: tco1 = new TopComponentOperator("SampleClass1.java"); //NOI18N
140: tco2 = new TopComponentOperator("SampleClass2.java"); //NOI18N
141: }
142:
143: /** Test constructors. */
144: public void testConstructors() {
145: initSources();
146: // test constructors
147: TopComponentOperator tco = new TopComponentOperator(
148: "SampleClass1.java"); //NOI18N
149: assertEquals(
150: "Constructor new TopComponentOperator(String) failed.",
151: "SampleClass1.java", tco.getName()); //NOI18N
152: tco = new TopComponentOperator("SampleClass", 0); //NOI18N
153: TopComponentOperator tco1 = new TopComponentOperator(
154: "SampleClass", 1); //NOI18N
155: assertFalse(
156: "Constructor new TopComponentOperator(String, int) failed.",
157: tco.getName().equals(tco1.getName())); //NOI18N
158: tco = new TopComponentOperator(MainWindowOperator.getDefault(),
159: "SampleClass1.java"); //NOI18N
160: assertEquals(
161: "Constructor new TopComponentOperator(ContainerOperator, String) failed.",
162: "SampleClass1.java", tco.getName()); //NOI18N
163: tco = new TopComponentOperator(MainWindowOperator.getDefault(),
164: "SampleClass", 0); //NOI18N
165: tco1 = new TopComponentOperator(
166: MainWindowOperator.getDefault(), "SampleClass", 1); //NOI18N
167: assertFalse(
168: "Constructor new TopComponentOperator(ContainerOperator, String, int) failed.",
169: tco.getName().equals(tco1.getName())); //NOI18N
170: new TopComponentOperator(MainWindowOperator.getDefault()); //NOI18N
171: new TopComponentOperator(MainWindowOperator.getDefault(), 1); //NOI18N
172: }
173:
174: /**
175: * Test of makeComponentVisible method.
176: */
177: public void testMakeComponentVisible() {
178: tco1.makeComponentVisible();
179: assertTrue("makeComponentVisible doesn't work.", tco1
180: .isShowing());
181: }
182:
183: /**
184: * Test of attachTo method.
185: */
186: public void testAttachTo() {
187: tco1.attachTo("SampleClass2.java", AttachWindowAction.RIGHT);
188: tco1.attachTo(tco2, AttachWindowAction.AS_LAST_TAB);
189: }
190:
191: /**
192: * Test of maximize method.
193: */
194: public void testMaximize() {
195: tco1.maximize();
196: }
197:
198: /**
199: * Test of restore method.
200: */
201: public void testRestore() {
202: tco1.restore();
203: }
204:
205: /**
206: * Test of cloneDocument method.
207: */
208: public void testCloneDocument() {
209: tco1.cloneDocument();
210: // try to find and close cloned document
211: new TopComponentOperator("SampleClass1.java", 1).close();
212: }
213:
214: /**
215: * Test of pushMenuOnTab method.
216: */
217: public void testPushMenuOnTab() {
218: // need to find again tco1 because clone test can close it instead of cloned version
219: tco1 = new TopComponentOperator("SampleClass1.java"); //NOI18N
220: /** "Clone Document" popup menu item. */
221: String popupPath = Bundle.getStringTrimmed(
222: "org.netbeans.core.windows.actions.Bundle",
223: "LBL_CloneDocumentAction");
224: tco1.pushMenuOnTab(popupPath);
225: // try to find and close cloned document
226: new TopComponentOperator("SampleClass1.java", 1).close();
227: // need to find again tco1 because clone test can close it instead of cloned version
228: tco1 = new TopComponentOperator("SampleClass1.java"); //NOI18N
229: }
230:
231: /**
232: * Test of saveDocument method.
233: */
234: public void testSaveDocument() {
235: ProjectsTabOperator.invoke();
236: new OpenAction().performAPI(editableSourceNode);
237: EditorOperator eo = new EditorOperator(editableSourceNode
238: .getText());
239: eo.insert("//dummy\n", 1, 1); // NOI18N
240: eo.saveDocument();
241: boolean modified = eo.isModified();
242: assertFalse("Document is not saved.", modified);//NOI18N
243: }
244:
245: /**
246: * Test of save method.
247: */
248: public void testSave() {
249: EditorOperator eo = new EditorOperator(editableSourceNode
250: .getText());
251: eo.insert("//dummy\n", 1, 1); // NOI18N
252: eo.save();
253: boolean modified = eo.isModified();
254: assertFalse("Document is not saved.", modified);//NOI18N
255: }
256:
257: /**
258: * Test of closeDiscard method.
259: */
260: public void testCloseDiscard() {
261: EditorOperator eo = new EditorOperator(editableSourceNode
262: .getText());
263: eo.insert("//NOT THERE\n", 1, 1); // NOI18N
264: eo.closeDiscard();
265: new OpenAction().performAPI(editableSourceNode);
266: eo = new EditorOperator(editableSourceNode.getText());
267: boolean saved = eo.contains("NOT THERE");
268: eo.closeDiscard();
269: // clean up - delete editable source
270: new DeleteAction().performAPI(editableSourceNode);
271: // "Safe Delete"
272: String safeDeleteTitle = Bundle.getString(
273: "org.netbeans.modules.refactoring.spi.impl.Bundle",
274: "LBL_SafeDel"); // NOI18N
275: new NbDialogOperator(safeDeleteTitle).ok();
276: assertFalse("Document is not discarded.", saved);//NOI18N
277: }
278:
279: /**
280: * Test of closeWindow method.
281: */
282: public void testCloseWindow() {
283: tco1.closeWindow();
284: assertFalse("closeWindow doesn't work", tco1.isShowing());
285: }
286:
287: /**
288: * Test of close method.
289: */
290: public void testClose() {
291: tco2.close();
292: assertFalse("close doesn't work", tco2.isShowing());
293: }
294:
295: /**
296: * Test of closeAllDocuments method.
297: */
298: public void testCloseAllDocuments() {
299: initSources();
300: tco1.closeAllDocuments();
301: assertFalse("closeAllDocuments doesn't work", tco1.isShowing());
302: }
303: }
|