KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bookmarks > EditorTestCase


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * EditorTestCase.java
22  *
23  * Created on 24. srpen 2004, 12:32
24  */

25
26 package bookmarks;
27
28 import java.awt.datatransfer.Transferable JavaDoc;
29 import java.io.File JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import org.netbeans.jellytools.EditorOperator;
34 import org.netbeans.jellytools.JellyTestCase;
35 import org.netbeans.jellytools.NbDialogOperator;
36 import org.netbeans.jellytools.ProjectsTabOperator;
37 import org.netbeans.jellytools.nodes.Node;
38 import org.netbeans.jellytools.nodes.ProjectRootNode;
39 import org.netbeans.jemmy.JemmyException;
40 import org.netbeans.jemmy.JemmyProperties;
41 import org.netbeans.jemmy.TimeoutExpiredException;
42 import org.netbeans.jemmy.Waitable;
43 import org.netbeans.jemmy.Waiter;
44 import org.netbeans.jemmy.operators.ComponentOperator;
45 import org.netbeans.jemmy.operators.JEditorPaneOperator;
46 import org.netbeans.jemmy.operators.JTextFieldOperator;
47 import org.netbeans.junit.ide.ProjectSupport;
48
49 /**
50  *
51  * @author Petr Felenda, Martin Roskanin
52  */

53 public class EditorTestCase extends JellyTestCase {
54     
55     private static final int OPENED_PROJECT_ACCESS_TIMEOUT = 1000;
56     
57     /** Default name of project is used if not specified in openProject method. */
58     private String JavaDoc defaultProjectName = "editor_test";
59     private String JavaDoc defaultSamplePackage = "dummy";
60     private String JavaDoc defaultSampleName = "sample1";
61
62     private static final char treeSeparator = '|';
63     private final String JavaDoc defaultPackageNameTreePath = "Source packages"+treeSeparator+"dummy";
64     private final String JavaDoc defaultFileName = "sample1";
65     private String JavaDoc projectName = null;
66     private String JavaDoc treeSubPackagePathToFile = null;
67     private String JavaDoc fileName = null;
68     private final String JavaDoc dialogSaveTitle = "Save"; // I18N
69
public static final int WAIT_MAX_MILIS_FOR_CLIPBOARD = 4000;
70     
71     /**
72      * Creates a new instance of EditorTestCase.
73      *
74      * <p>
75      * Initializes default sample file name (package and name)
76      * so that {@link #openDefaultSampleFile()} can be used.
77      * <br>
78      * The rule for naming is the same like for golden files
79      * i.e. package corresponds to the class name and the file
80      * name corresponds to test method name.
81      *
82      * @param testMethodName name of the test method
83      * that should be executed.
84      */

85     public EditorTestCase(String JavaDoc testMethodName) {
86         super(testMethodName);
87         
88         defaultSamplePackage = getClass().getName();
89         defaultSampleName = getName();
90     }
91         
92     /** Open project. Before opening the project is checked opened projects.
93      * @param projectName is name of the project stored in .../editor/test/qa-functional/data/ directory.
94      */

95     public void openProject(String JavaDoc projectName) {
96         this.projectName = projectName;
97         File JavaDoc projectPath = new File JavaDoc(this.getDataDir() + "/projects", projectName);
98         log("data dir = "+this.getDataDir().toString());
99         
100         /* 1. check if project is open */
101         ProjectsTabOperator pto = new ProjectsTabOperator();
102         pto.invoke();
103         boolean isOpen = true;
104         try {
105             JemmyProperties.setCurrentTimeout("JTreeOperator.WaitNextNodeTimeout", OPENED_PROJECT_ACCESS_TIMEOUT);
106             ProjectRootNode prn = pto.getProjectRootNode(projectName);
107         } catch (TimeoutExpiredException ex) {
108             // This excpeiton is ok, project is not open;
109
//ex.printStackTrace();
110
isOpen = false;
111         }
112         
113         if ( isOpen ) {
114             log("Project is open!");
115             return;
116         }
117                 
118         /* 2. open project */
119         Object JavaDoc prj= ProjectSupport.openProject(projectPath);
120     }
121    
122     /**
123      * Get the default project name to be used
124      * in {@link openDefaultProject()}.
125      * <br>
126      * The default value is "editor_test".
127      *
128      * @return default project name
129      */

130     protected String JavaDoc getDefaultProjectName() {
131         return defaultProjectName;
132     }
133             
134     /**
135      * Open default project determined
136      * by {@link #getDefaultProjectName()}.
137      */

138     protected void openDefaultProject() {
139         openProject(getDefaultProjectName());
140     }
141     
142     /**
143      * Close the default project.
144      */

145     protected void closeDefaultProject() {
146         closeProject(getDefaultProjectName());
147     }
148     
149     protected void closeProject(String JavaDoc projectName) {
150         ProjectSupport.closeProject(projectName);
151     }
152     
153     
154     /** Open file in open project
155      * @param treeSubPath e.g. "Source Packages|test","sample1" */

156     public void openFile(String JavaDoc treeSubPackagePathToFile, String JavaDoc fileName) {
157         // debug info, to be removed
158
this.treeSubPackagePathToFile = treeSubPackagePathToFile;
159         ProjectsTabOperator pto = new ProjectsTabOperator();
160         pto.invoke();
161         ProjectRootNode prn = pto.getProjectRootNode(projectName);
162         prn.select();
163         
164         // fix of issue #51191
165
// each of nodes is checked by calling method waitForChildNode
166
// before they are actually opened
167
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(treeSubPackagePathToFile,
168                 treeSeparator+"");
169         String JavaDoc token = "";
170         String JavaDoc oldtoken = "";
171         // if there are more then one tokens process each of them
172
if (st.countTokens()>1) {
173             token = st.nextToken();
174             String JavaDoc fullpath = token;
175             while (st.hasMoreTokens()) {
176                 token = st.nextToken();
177                 waitForChildNode(fullpath, token);
178                 fullpath += treeSeparator+token;
179             }
180         }
181         // last node
182
waitForChildNode(treeSubPackagePathToFile, fileName);
183         // end of fix of issue #51191
184

185         Node node = new Node(prn,treeSubPackagePathToFile+treeSeparator+fileName);
186         node.performPopupAction("Open");
187     }
188     
189     /**
190      * Waits for a child node to be shown in the IDE. Needed for test
191      * stabilization on slow machines.
192      * @param parentPath full path for parent, | used as a delimiter
193      * @param childName name of the child node
194      */

195     public void waitForChildNode(String JavaDoc parentPath, String JavaDoc childName) {
196         ProjectsTabOperator pto = new ProjectsTabOperator();
197         ProjectRootNode prn = pto.getProjectRootNode(projectName);
198         prn.select();
199         Node parent = new Node(prn, parentPath);
200         final String JavaDoc finalFileName = childName;
201         try {
202             // wait for max. 30 seconds for the file node to appear
203
JemmyProperties.setCurrentTimeout("Waiter.WaitingTime", 30000);
204             new Waiter(new Waitable() {
205                 public Object JavaDoc actionProduced(Object JavaDoc parent) {
206                     return ((Node)parent).isChildPresent(finalFileName) ?
207                             Boolean.TRUE: null;
208                 }
209                 public String JavaDoc getDescription() {
210                     return("Waiting for the tree to load.");
211                 }
212             }).waitAction(parent);
213         } catch (InterruptedException JavaDoc e) {
214             throw new JemmyException("Interrupted.", e);
215         }
216     }
217     
218     /** Open the default file in open project */
219     public void openFile() {
220         openFile(defaultPackageNameTreePath,defaultFileName);
221     }
222     
223     /** Close file in open project.
224      */

225     public void closeFile() {
226         try {
227             new EditorOperator(fileName).close();
228         } catch ( TimeoutExpiredException ex) {
229             log(ex.getMessage());
230             log("Can't close the file");
231         }
232     }
233     
234     /** Close file in open project.
235      */

236     public void closeFileWithSave() {
237         try {
238            new EditorOperator(fileName).close(true);
239         } catch ( TimeoutExpiredException ex) {
240             log(ex.getMessage());
241             log("Can't close the file");
242         }
243     }
244     
245     
246     /** Close file in open project.
247      */

248     public void closeFileWithDiscard() {
249         try {
250            new EditorOperator(fileName).closeDiscard();
251         } catch ( TimeoutExpiredException ex) {
252             log(ex.getMessage());
253             log("Can't close the file");
254         }
255     }
256     
257     /** Close dialog with added title
258      * @param title dialog title */

259     public void closeDialog(String JavaDoc title) {
260         NbDialogOperator dialog = new NbDialogOperator(title);
261         dialog.closeByButton();
262     }
263     
264     /**
265      * Write the text of the passed document to the ref file
266      * and compare the created .ref file with the golden file.
267      * <br>
268      * If the two files differ the test fails and generates the diff file.
269      *
270      * @param testDoc document to be written to the .ref file and compared.
271      */

272     protected void compareReferenceFiles(Document JavaDoc testDoc) {
273         try {
274             ref(testDoc.getText(0, testDoc.getLength()));
275             compareReferenceFiles();
276         } catch (BadLocationException JavaDoc e) {
277             e.printStackTrace(getLog());
278             fail();
279         }
280     }
281
282     /**
283      * Open a source file located in the "Source packages" in the editor.
284      *
285      * @param dir directory path with "|" separator.
286      * @param srcName source name without suffix.
287      */

288     protected void openSourceFile(String JavaDoc dir, String JavaDoc srcName) {
289         openFile(org.netbeans.jellytools.Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle", "NAME_src.dir")+treeSeparator+dir, srcName);
290     }
291     
292     protected final String JavaDoc getDefaultSamplePackage() {
293         return defaultSamplePackage;
294     }
295     
296     protected final String JavaDoc getDefaultSampleName() {
297         return defaultSampleName;
298     }
299
300     protected void openDefaultSampleFile() {
301         openSourceFile(defaultSamplePackage, defaultSampleName);
302     }
303
304     protected EditorOperator getDefaultSampleEditorOperator() {
305         return new EditorOperator(defaultSampleName);
306     }
307
308     /** Method will wait max. <code> maxMiliSeconds </code> miliseconds for the <code> requiredValue </code>
309      * gathered by <code> resolver </code>.
310      *
311      * @param maxMiliSeconds maximum time to wait for requiredValue
312      * @param resolver resolver, which is gathering an actual value
313      * @param requiredValue if resolver value equals requiredValue the wait cycle is finished
314      *
315      * @return false if the given maxMiliSeconds time elapsed and the requiredValue wasn't obtained
316      */

317     protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object JavaDoc requiredValue){
318         int time = (int) maxMiliSeconds / 100;
319         while (time > 0) {
320             Object JavaDoc resolvedValue = resolver.getValue();
321             if (requiredValue == null && resolvedValue == null){
322                 return true;
323             }
324             if (requiredValue != null && requiredValue.equals(resolvedValue)){
325                 return true;
326             }
327             try {
328                 Thread.currentThread().sleep(100);
329             } catch (InterruptedException JavaDoc ex) {
330                 time=0;
331             }
332             time--;
333         }
334         return false;
335     }
336     
337     /** Interface for value resolver needed for i.e. waitMaxMilisForValue method.
338      * For more details, please look at {@link #waitMaxMilisForValue()}.
339      */

340     public static interface ValueResolver{
341         /** Returns checked value */
342         Object JavaDoc getValue();
343     }
344     
345 }
346
Popular Tags