KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > GuiUtilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.java.gui;
21
22 import org.netbeans.jellytools.*;
23 import org.netbeans.jellytools.nodes.*;
24 import org.netbeans.jemmy.operators.*;
25
26 import java.io.File JavaDoc;
27 import javax.swing.tree.TreeModel JavaDoc;
28 import org.netbeans.jemmy.JemmyException;
29 import org.netbeans.jemmy.JemmyProperties;
30 import org.netbeans.jemmy.Waitable;
31 import org.netbeans.jemmy.Waiter;
32 import org.netbeans.test.java.Utilities;
33
34 //import org.netbeans.junit.ide.ProjectSupport;
35

36 /**
37  * Common utilities for GUI tests.
38  * @author Roman Strobl
39  */

40 public class GuiUtilities {
41     
42     private static final String JavaDoc JAVA_BUNDLE_PATH = "org.netbeans.modules.java.project.Bundle";
43
44     /**
45      * Creates a testing project.
46      * @param projectName name of project
47      * @param workDir working directory for project
48      * @return path to project directory
49      */

50     public static String JavaDoc createProject(String JavaDoc projectName, String JavaDoc workDir) {
51         NewProjectWizardOperator opc = NewProjectWizardOperator.invoke();
52         
53         // close project if it exits (it was not closed during last test)
54
ProjectsTabOperator pto = new ProjectsTabOperator();
55         JTreeOperator tree = pto.tree();
56     
57         TreeModel JavaDoc tm = tree.getModel();
58         Object JavaDoc root = tm.getRoot();
59         for (int i=0; i<tm.getChildCount(root); i++) {
60             // if project is opened, close it, it may be opened several times
61
// by mistake, so go through all cases
62
if ((tm.getChild(root, i).toString().equals(projectName)) ||
63             (tm.getChild(root, i).toString().equals(projectName+" [Main]"))) {
64                     Node pn = new ProjectsTabOperator().getProjectRootNode(
65                             tm.getChild(root, i).toString());
66                     pn.select();
67                     pn.performPopupAction(
68                             org.netbeans.jellytools.Bundle.getString(
69                             "org.openide.nodes.Bundle", "Button_close")+" "
70                             +org.netbeans.jellytools.Bundle.getString(
71                             "org.netbeans.modules.project.ui.Bundle",
72                             "ACSN_jLabel1"));
73                     i--;
74                     Utilities.takeANap(1000);
75             }
76         }
77         
78         // delete workdir if it exists (it was not deleted during last test)
79
File JavaDoc f = new File JavaDoc(workDir);
80         if (f.exists()) {
81             Utilities.deleteDirectory(f);
82         }
83         
84         // wait till all fields are loaded
85
JDialogOperator jdo = new JDialogOperator(
86                 org.netbeans.jellytools.Bundle.getString(
87                 "org.netbeans.modules.project.ui.Bundle",
88                 "LBL_NewProjectWizard_Title"));
89         JTreeOperator jto = new JTreeOperator(jdo, 0);
90         
91         boolean exitLoop = false;
92         System.out.println("Waiting for 'General'");
93         for (int i=0; i<20; i++) {
94             System.out.println("Round "+i);
95             Utilities.takeANap(2000);
96             for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) {
97                 if (jto.getChild(jto.getRoot(), j).toString() == Bundle.getString(
98                         "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
99                         "Templates/Project/Standard")) {
100                     exitLoop = true;
101                     System.out.println("General found");
102                     break;
103                 }
104             }
105             if (exitLoop) break;
106         }
107         
108         new Node(jto, Bundle.getString(
109                 "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
110                 "Templates/Project/Standard")).select();
111         // java project
112
opc.selectProject(Bundle.getString(
113                 "org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
114                 "Templates/Project/Standard/emptyJ2SE.xml"));
115         opc.next();
116         
117         // set project name, no main class, created in workdir
118
NewProjectNameLocationStepOperator npnlso = new
119                 NewProjectNameLocationStepOperator();
120         npnlso.txtProjectName().setText(projectName);
121         npnlso.cbCreateMainClass().setSelected(false);
122         npnlso.txtProjectLocation().setText(workDir);
123         npnlso.finish();
124         
125         String JavaDoc projectDir = workDir+"/"+projectName;
126         
127         //ProjectSupport.waitScanFinished();
128

129         // "Scanning Project Classpaths" - NO MORE IN 4.2
130
/* String titleScanning = Bundle.getString(
131                 "org.netbeans.modules.javacore.Bundle",
132                 "TXT_ApplyingPathsTitle");
133         NbDialogOperator scanningDialogOper = new NbDialogOperator(titleScanning);
134         
135         // scanning can last for a long time => wait max. 5 minutes
136         scanningDialogOper.getTimeouts().setTimeout(
137                 "ComponentOperator.WaitStateTimeout", 300000);
138         scanningDialogOper.waitClosed(); */

139         
140         // wait project appear in projects view
141
new ProjectsTabOperator().getProjectRootNode(projectName);
142         
143         return projectDir;
144     }
145     
146     
147     /**
148      * Creates a testing package.
149      * @param projectName name of project
150      * @param packageName name of created package
151      */

152     public static void createPackage(String JavaDoc projectName, String JavaDoc packageName) {
153         // create a new package
154
Node pn = new ProjectsTabOperator().getProjectRootNode(
155                 projectName);
156         pn.select();
157         
158         NewFileWizardOperator op = NewFileWizardOperator.invoke();
159         
160         // wait till all fields are loaded
161
JDialogOperator jdo = new JDialogOperator(
162                 org.netbeans.jellytools.Bundle.getString(
163                 "org.netbeans.modules.project.ui.Bundle",
164                 "LBL_NewFileWizard_Title"));
165         JTreeOperator jto = new JTreeOperator(jdo, 0);
166         boolean exitLoop = false;
167         for (int i=0; i<10; i++) {
168             for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) {
169                 if (jto.getChild(jto.getRoot(), j).toString()==
170                         Bundle.getString("org.netbeans.modules.java.project.Bundle",
171                         "Templates/Classes")) {
172                     exitLoop = true;
173                     break;
174                 }
175             }
176             if (exitLoop) break;
177             Utilities.takeANap(1000);
178         }
179         
180         op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
181                 "Templates/Classes"));
182         op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
183                 "Templates/Classes/Package"));
184         op.next();
185         
186         JTextFieldOperator tfp = new JTextFieldOperator(op, 0);
187         tfp.setText(packageName);
188         
189         // set package name
190
NewFileNameLocationStepOperator nfnlso =
191                 new NewFileNameLocationStepOperator();
192         nfnlso.txtObjectName().setText(packageName);
193         for (int i=0; i<10; i++) {
194             if (nfnlso.btFinish().isEnabled()) break;
195             Utilities.takeANap(1000);
196         }
197         nfnlso.finish();
198         
199         // wait for package to appear
200
Node sample1Node = new Node(new SourcePackagesNode(projectName),
201                 packageName);
202     }
203     
204     /**
205      * Creates a testing class.
206      * @param projectName name of project
207      * @param packageName name of package
208      * @param className name of created class
209      */

210     public static void createClass(String JavaDoc projectName, String JavaDoc packageName,
211             String JavaDoc className) {
212         Node pn2 = new ProjectsTabOperator().getProjectRootNode(
213                 projectName);
214         pn2.select();
215         
216         // create testing class
217
NewFileWizardOperator op2 = NewFileWizardOperator.invoke();
218         
219         // wait till all fields are loaded
220
JDialogOperator jdo = new JDialogOperator(
221                 org.netbeans.jellytools.Bundle.getString(
222                 "org.netbeans.modules.project.ui.Bundle",
223                 "LBL_NewFileWizard_Title"));
224         JTreeOperator jto = new JTreeOperator(jdo, 0);
225         boolean exitLoop = false;
226         for (int i=0; i<10; i++) {
227             for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) {
228                 if (jto.getChild(jto.getRoot(), j).toString()==
229                         Bundle.getString(JAVA_BUNDLE_PATH,
230                         "Templates/Classes")) {
231                     exitLoop = true;
232                     break;
233                 }
234             }
235             if (exitLoop) break;
236             Utilities.takeANap(1000);
237         }
238         
239         op2.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
240                 "Templates/Classes"));
241         op2.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
242                 "Templates/Classes/Class.java"));
243         op2.next();
244         
245         JTextFieldOperator tf = new JTextFieldOperator(op2);
246         tf.setText(className);
247         
248         op2.finish();
249         
250         // wait for class to appear
251
Node sample2Node = new Node(new Node(new SourcePackagesNode(
252                 projectName), packageName), className);
253     }
254
255     /**
256      * Creates a testing interface.
257      * @param projectName name of project
258      * @param packageName name of package
259      * @param ifaceName name of interface
260      */

261     public static void createInterface(String JavaDoc projectName, String JavaDoc packageName,
262             String JavaDoc ifaceName) {
263         Node pn2 = new ProjectsTabOperator().getProjectRootNode(
264                 projectName);
265         pn2.select();
266         
267         // create testing class
268
NewFileWizardOperator op2 = NewFileWizardOperator.invoke();
269         
270         // wait till all fields are loaded
271
JDialogOperator jdo = new JDialogOperator(
272                 org.netbeans.jellytools.Bundle.getString(
273                 "org.netbeans.modules.project.ui.Bundle",
274                 "LBL_NewFileWizard_Title"));
275         JTreeOperator jto = new JTreeOperator(jdo, 0);
276         boolean exitLoop = false;
277         for (int i=0; i<10; i++) {
278             for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) {
279                 if (jto.getChild(jto.getRoot(), j).toString()==
280                         Bundle.getString(JAVA_BUNDLE_PATH,
281                         "Templates/Classes")) {
282                     exitLoop = true;
283                     break;
284                 }
285             }
286             if (exitLoop) break;
287             Utilities.takeANap(1000);
288         }
289         
290         op2.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
291                 "Templates/Classes"));
292         op2.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
293                 "Templates/Classes/Interface.java"));
294         op2.next();
295         
296         JTextFieldOperator tf = new JTextFieldOperator(op2);
297         tf.setText(ifaceName);
298         
299         op2.finish();
300         
301         // wait for class to appear
302
Node sample2Node = new Node(new Node(new SourcePackagesNode(
303                 projectName), packageName), ifaceName);
304     }
305     
306     /**
307      * Creates a testing project, package and class.
308      * @param projectName name of created project
309      * @param packageName name of created package
310      * @param className name of created class
311      * @param workDir working directory for project
312      * @return path to project directory
313      */

314     public static String JavaDoc createProjectAndPackageAndClass(String JavaDoc projectName,
315             String JavaDoc packageName, String JavaDoc className, String JavaDoc workDir) {
316         String JavaDoc projectDir = createProject(projectName, workDir);
317         createPackage(projectName, packageName);
318         createClass(projectName, packageName, className);
319         return projectDir;
320     }
321     
322     /**
323      * Deletes testing project including files on hard drive.
324      * @param projectName name of project to delete
325      * @param className name of class to close
326      * @param workDir working directory of project
327      * @param closeFile should opened class be closed
328      */

329     public static void deleteProject(String JavaDoc projectName, String JavaDoc className,
330             String JavaDoc workDir, boolean closeFiles) {
331         
332         if (closeFiles) {
333             EditorOperator.closeDiscardAll();
334         }
335                 
336         Node pn = new ProjectsTabOperator().getProjectRootNode(
337                 projectName);
338         pn.select();
339         
340         pn.performPopupAction(org.netbeans.jellytools.Bundle.getString(
341                 "org.openide.nodes.Bundle", "Button_close")+" "
342                 +org.netbeans.jellytools.Bundle.getString(
343                 "org.netbeans.modules.project.ui.Bundle", "ACSN_jLabel1"));
344         
345         /* delete project - commented out because it causes exceptions
346         File f = new File(workDir);
347         Utilities.deleteDirectory(f);*/

348     }
349     
350     /**
351      * Waits for a child node to be shown in the IDE. Needed for test
352      * stabilization on slow machines.
353      * @param parentPath full path for parent, | used as a delimiter
354      * @param childName name of the child node
355      * @param projectName name of project
356      */

357     public static void waitForChildNode(String JavaDoc projectName, String JavaDoc parentPath,
358             String JavaDoc childName) {
359         ProjectsTabOperator pto = new ProjectsTabOperator();
360         ProjectRootNode prn = pto.getProjectRootNode(projectName);
361         prn.select();
362         Node parent = new Node(prn, parentPath);
363         final String JavaDoc finalFileName = childName;
364         try {
365             // wait for max. 30 seconds for the file node to appear
366
JemmyProperties.setCurrentTimeout("Waiter.WaitingTime", 30000);
367             new Waiter(new Waitable() {
368                 public Object JavaDoc actionProduced(Object JavaDoc parent) {
369                     return ((Node)parent).isChildPresent(finalFileName) ?
370                         Boolean.TRUE: null;
371                 }
372                 public String JavaDoc getDescription() {
373                     return("Waiting for the tree to load.");
374                 }
375             }).waitAction(parent);
376         } catch (InterruptedException JavaDoc e) {
377             throw new JemmyException("Interrupted.", e);
378         }
379     }
380     
381 }
382
Popular Tags