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 org.netbeans.test.java.gui;
043:
044: import org.netbeans.jellytools.*;
045: import org.netbeans.jellytools.nodes.*;
046: import org.netbeans.jemmy.operators.*;
047:
048: import java.io.File;
049: import javax.swing.tree.TreeModel;
050: import org.netbeans.jemmy.EventTool;
051: import org.netbeans.jemmy.JemmyException;
052: import org.netbeans.jemmy.JemmyProperties;
053: import org.netbeans.jemmy.Waitable;
054: import org.netbeans.jemmy.Waiter;
055: import org.netbeans.test.java.Utilities;
056:
057: //import org.netbeans.junit.ide.ProjectSupport;
058:
059: /**
060: * Common utilities for GUI tests.
061: * @author Roman Strobl
062: */
063: public class GuiUtilities {
064:
065: private static final String JAVA_BUNDLE_PATH = "org.netbeans.modules.java.project.Bundle";
066:
067: /**
068: * Creates a testing project.
069: * @param projectName name of project
070: * @param workDir working directory for project
071: * @return path to project directory
072: */
073: public static String createProject(String projectName,
074: String workDir) {
075: NewProjectWizardOperator opc = NewProjectWizardOperator
076: .invoke();
077:
078: // close project if it exits (it was not closed during last test)
079: ProjectsTabOperator pto = new ProjectsTabOperator();
080: JTreeOperator tree = pto.tree();
081:
082: TreeModel tm = tree.getModel();
083: Object root = tm.getRoot();
084: for (int i = 0; i < tm.getChildCount(root); i++) {
085: // if project is opened, close it, it may be opened several times
086: // by mistake, so go through all cases
087: if ((tm.getChild(root, i).toString().equals(projectName))
088: || (tm.getChild(root, i).toString()
089: .equals(projectName + " [Main]"))) {
090: Node pn = new ProjectsTabOperator()
091: .getProjectRootNode(tm.getChild(root, i)
092: .toString());
093: pn.select();
094: pn.performPopupAction(org.netbeans.jellytools.Bundle
095: .getString("org.openide.nodes.Bundle",
096: "Button_close"));
097: i--;
098: Utilities.takeANap(1000);
099: }
100: }
101:
102: // delete workdir if it exists (it was not deleted during last test)
103: File f = new File(workDir);
104: if (f.exists()) {
105: Utilities.deleteDirectory(f);
106: }
107:
108: // wait till all fields are loaded
109: JDialogOperator jdo = new JDialogOperator(
110: org.netbeans.jellytools.Bundle.getString(
111: "org.netbeans.modules.project.ui.Bundle",
112: "LBL_NewProjectWizard_Title"));
113: JTreeOperator jto = new JTreeOperator(jdo, 0);
114:
115: boolean exitLoop = false;
116: System.out.println("Waiting for 'General'");
117: for (int i = 0; i < 20; i++) {
118: System.out.println("Round " + i);
119: Utilities.takeANap(2000);
120: for (int j = 0; j < jto.getChildCount(jto.getRoot()); j++) {
121: if (jto.getChild(jto.getRoot(), j).toString() == Bundle
122: .getString(
123: "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
124: "Templates/Project/Standard")) {
125: exitLoop = true;
126: System.out.println("General found");
127: break;
128: }
129: }
130: if (exitLoop)
131: break;
132: }
133:
134: new Node(
135: jto,
136: Bundle
137: .getString(
138: "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
139: "Templates/Project/Standard")).select();
140: // java project
141: opc
142: .selectProject(Bundle
143: .getString(
144: "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
145: "Templates/Project/Standard/emptyJ2SE.xml"));
146: opc.next();
147:
148: // set project name, no main class, created in workdir
149: NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
150: npnlso.txtProjectName().setText(projectName);
151: npnlso.cbCreateMainClass().setSelected(false);
152: npnlso.txtProjectLocation().setText(workDir);
153: npnlso.finish();
154:
155: String projectDir = workDir + "/" + projectName;
156:
157: //ProjectSupport.waitScanFinished();
158:
159: // "Scanning Project Classpaths" - NO MORE IN 4.2
160: /* String titleScanning = Bundle.getString(
161: "org.netbeans.modules.javacore.Bundle",
162: "TXT_ApplyingPathsTitle");
163: NbDialogOperator scanningDialogOper = new NbDialogOperator(titleScanning);
164:
165: // scanning can last for a long time => wait max. 5 minutes
166: scanningDialogOper.getTimeouts().setTimeout(
167: "ComponentOperator.WaitStateTimeout", 300000);
168: scanningDialogOper.waitClosed(); */
169:
170: // wait project appear in projects view
171: new EventTool().waitNoEvent(3000);
172: new ProjectsTabOperator().getProjectRootNode(projectName);
173:
174: return projectDir;
175: }
176:
177: /**
178: * Creates a testing package.
179: * @param projectName name of project
180: * @param packageName name of created package
181: */
182: public static void createPackage(String projectName,
183: String packageName) {
184: // create a new package
185: Node pn = new ProjectsTabOperator()
186: .getProjectRootNode(projectName);
187: pn.select();
188:
189: NewFileWizardOperator op = NewFileWizardOperator.invoke();
190:
191: // wait till all fields are loaded
192: JDialogOperator jdo = new JDialogOperator(
193: org.netbeans.jellytools.Bundle.getString(
194: "org.netbeans.modules.project.ui.Bundle",
195: "LBL_NewFileWizard_Title"));
196: JTreeOperator jto = new JTreeOperator(jdo, 0);
197: boolean exitLoop = false;
198: for (int i = 0; i < 10; i++) {
199: for (int j = 0; j < jto.getChildCount(jto.getRoot()); j++) {
200: if (jto.getChild(jto.getRoot(), j).toString() == Bundle
201: .getString(
202: "org.netbeans.modules.java.project.Bundle",
203: "Templates/Classes")) {
204: exitLoop = true;
205: break;
206: }
207: }
208: if (exitLoop)
209: break;
210: Utilities.takeANap(1000);
211: }
212:
213: op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
214: "Templates/Classes"));
215: op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
216: "Templates/Classes/Package"));
217: op.next();
218:
219: JTextFieldOperator tfp = new JTextFieldOperator(op, 0);
220: tfp.setText(packageName);
221:
222: // set package name
223: NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
224: nfnlso.txtObjectName().setText(packageName);
225: for (int i = 0; i < 10; i++) {
226: if (nfnlso.btFinish().isEnabled())
227: break;
228: Utilities.takeANap(1000);
229: }
230: nfnlso.finish();
231:
232: // wait for package to appear
233: Node sample1Node = new Node(
234: new SourcePackagesNode(projectName), packageName);
235: }
236:
237: /**
238: * Creates a testing class.
239: * @param projectName name of project
240: * @param packageName name of package
241: * @param className name of created class
242: */
243: public static void createClass(String projectName,
244: String packageName, String className) {
245: Node pn2 = new ProjectsTabOperator()
246: .getProjectRootNode(projectName);
247: pn2.select();
248:
249: // create testing class
250: NewFileWizardOperator op2 = NewFileWizardOperator.invoke();
251:
252: // wait till all fields are loaded
253: JDialogOperator jdo = new JDialogOperator(
254: org.netbeans.jellytools.Bundle.getString(
255: "org.netbeans.modules.project.ui.Bundle",
256: "LBL_NewFileWizard_Title"));
257: JTreeOperator jto = new JTreeOperator(jdo, 0);
258: boolean exitLoop = false;
259: for (int i = 0; i < 10; i++) {
260: for (int j = 0; j < jto.getChildCount(jto.getRoot()); j++) {
261: if (jto.getChild(jto.getRoot(), j).toString() == Bundle
262: .getString(JAVA_BUNDLE_PATH,
263: "Templates/Classes")) {
264: exitLoop = true;
265: break;
266: }
267: }
268: if (exitLoop)
269: break;
270: Utilities.takeANap(1000);
271: }
272:
273: op2.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
274: "Templates/Classes"));
275: op2.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
276: "Templates/Classes/Class.java"));
277: op2.next();
278:
279: JTextFieldOperator tf = new JTextFieldOperator(op2);
280: tf.setText(className);
281:
282: op2.finish();
283:
284: // wait for class to appear
285: Node sample2Node = new Node(new Node(new SourcePackagesNode(
286: projectName), packageName), className);
287: }
288:
289: /**
290: * Creates a testing interface.
291: * @param projectName name of project
292: * @param packageName name of package
293: * @param ifaceName name of interface
294: */
295: public static void createInterface(String projectName,
296: String packageName, String ifaceName) {
297: Node pn2 = new ProjectsTabOperator()
298: .getProjectRootNode(projectName);
299: pn2.select();
300:
301: // create testing class
302: NewFileWizardOperator op2 = NewFileWizardOperator.invoke();
303:
304: // wait till all fields are loaded
305: JDialogOperator jdo = new JDialogOperator(
306: org.netbeans.jellytools.Bundle.getString(
307: "org.netbeans.modules.project.ui.Bundle",
308: "LBL_NewFileWizard_Title"));
309: JTreeOperator jto = new JTreeOperator(jdo, 0);
310: boolean exitLoop = false;
311: for (int i = 0; i < 10; i++) {
312: for (int j = 0; j < jto.getChildCount(jto.getRoot()); j++) {
313: if (jto.getChild(jto.getRoot(), j).toString() == Bundle
314: .getString(JAVA_BUNDLE_PATH,
315: "Templates/Classes")) {
316: exitLoop = true;
317: break;
318: }
319: }
320: if (exitLoop)
321: break;
322: Utilities.takeANap(1000);
323: }
324:
325: op2.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
326: "Templates/Classes"));
327: op2.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
328: "Templates/Classes/Interface.java"));
329: op2.next();
330:
331: JTextFieldOperator tf = new JTextFieldOperator(op2);
332: tf.setText(ifaceName);
333:
334: op2.finish();
335:
336: // wait for class to appear
337: Node sample2Node = new Node(new Node(new SourcePackagesNode(
338: projectName), packageName), ifaceName);
339: }
340:
341: /**
342: * Creates a testing project, package and class.
343: * @param projectName name of created project
344: * @param packageName name of created package
345: * @param className name of created class
346: * @param workDir working directory for project
347: * @return path to project directory
348: */
349: public static String createProjectAndPackageAndClass(
350: String projectName, String packageName, String className,
351: String workDir) {
352: String projectDir = createProject(projectName, workDir);
353: createPackage(projectName, packageName);
354: createClass(projectName, packageName, className);
355: return projectDir;
356: }
357:
358: /**
359: * Deletes testing project including files on hard drive.
360: * @param projectName name of project to delete
361: * @param className name of class to close
362: * @param workDir working directory of project
363: * @param closeFile should opened class be closed
364: */
365: public static void deleteProject(String projectName,
366: String className, String workDir, boolean closeFiles) {
367:
368: if (closeFiles) {
369: EditorOperator.closeDiscardAll();
370: }
371:
372: Node pn = new ProjectsTabOperator()
373: .getProjectRootNode(projectName);
374: pn.select();
375:
376: pn.performPopupAction(org.netbeans.jellytools.Bundle.getString(
377: "org.openide.nodes.Bundle", "Button_close"));
378:
379: /* delete project - commented out because it causes exceptions
380: File f = new File(workDir);
381: Utilities.deleteDirectory(f);*/
382: }
383:
384: /**
385: * Waits for a child node to be shown in the IDE. Needed for test
386: * stabilization on slow machines.
387: * @param parentPath full path for parent, | used as a delimiter
388: * @param childName name of the child node
389: * @param projectName name of project
390: */
391: public static void waitForChildNode(String projectName,
392: String parentPath, String childName) {
393: ProjectsTabOperator pto = new ProjectsTabOperator();
394: ProjectRootNode prn = pto.getProjectRootNode(projectName);
395: prn.select();
396: Node parent = new Node(prn, parentPath);
397: final String finalFileName = childName;
398: try {
399: // wait for max. 30 seconds for the file node to appear
400: JemmyProperties.setCurrentTimeout("Waiter.WaitingTime",
401: 30000);
402: new Waiter(new Waitable() {
403: public Object actionProduced(Object parent) {
404: return ((Node) parent)
405: .isChildPresent(finalFileName) ? Boolean.TRUE
406: : null;
407: }
408:
409: public String getDescription() {
410: return ("Waiting for the tree to load.");
411: }
412: }).waitAction(parent);
413: } catch (InterruptedException e) {
414: throw new JemmyException("Interrupted.", e);
415: }
416: }
417:
418: }
|