KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > suites > focus > FocusTestPerformer


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.editor.suites.focus;
21
22 import java.awt.*;
23 import java.awt.Robot JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.io.*;
26 import java.util.*;
27 import org.netbeans.jellytools.*;
28 import org.netbeans.jellytools.modules.editor.Abbreviations;
29 import org.netbeans.jellytools.modules.editor.Find;
30 import org.netbeans.jellytools.modules.editor.GoToLine;
31 import org.netbeans.jellytools.modules.editor.KeyBindings;
32 import org.netbeans.jemmy.EventTool;
33 import org.netbeans.jemmy.JemmyProperties;
34 import org.netbeans.test.editor.LineDiff;
35 import org.openide.cookies.EditorCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.Repository;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataObjectNotFoundException;
40
41 /**This is test in very development stage. I put it into the CVS mainly because
42  * it simplifies testing on different platforms. This test may or may not
43  * be reliable and may or may not work at all.
44  *
45  * @author eh103527
46  */

47 public class FocusTestPerformer extends JellyTestCase {
48     
49     private EditorOperator editor = null;
50     private EditorOperator editor2 = null;
51     
52     String JavaDoc TEXT=" INSERTING TEXT ";
53     
54     int[] lines=new int[] {10,26,50,42,12,18,32,128,64,256,333,401,54,7,450,220,125,444};//max 459
55

56     String JavaDoc[] texts=new String JavaDoc[] {"a1","a2","a3","a4","a5","a6","a7","a8","a9"};
57     
58     public FocusTestPerformer(String JavaDoc name) {
59         super(name);
60     }
61     
62     public synchronized EditorOperator getEditor() {
63         return editor;
64     }
65     
66     public EditorOperator getEditor2() {
67         return editor2;
68     }
69     
70     public synchronized void createEditor(String JavaDoc fileName) {
71         FileObject fo = Repository.getDefault().findResource("org/netbeans/test/editor/suites/focus/data/testfiles/FocusTestPerformer/"+fileName+".java");
72         try {
73             DataObject od = DataObject.find(fo);
74             EditorCookie ec = (EditorCookie) od.getCookie(EditorCookie.class);
75             ec.open();
76             
77             if (editor == null) {
78                 editor = new EditorOperator(fileName);
79                 editor.pushKey(KeyEvent.VK_HOME, KeyEvent.CTRL_MASK);
80             } else if (editor2 == null) {
81                 editor2 = new EditorOperator(fileName);
82                 editor2.pushKey(KeyEvent.VK_HOME, KeyEvent.CTRL_MASK);
83             }
84         } catch (DataObjectNotFoundException e) {
85             assertTrue(false);
86         }
87     }
88     
89     public void prepareLongFileEditor() {
90         try {
91             EditorOperator.closeDiscardAll();
92             log("Closed Welcome screen.");
93         } catch (Exception JavaDoc ex) {
94         }
95         createEditor("Test");
96     }
97     
98     public void prepareShortFileEditor() {
99         try {
100             EditorOperator.closeDiscardAll();
101             log("Closed Welcome screen.");
102         } catch (Exception JavaDoc ex) {
103         }
104         createEditor("Test2");
105     }
106     
107     public void prepare2FilesEditors() {
108         try {
109             EditorOperator.closeDiscardAll();
110             log("Closed Welcome screen.");
111         } catch (Exception JavaDoc ex) {
112         }
113         //open 2 empty files
114
createEditor("Test3");
115         createEditor("Test4");
116     }
117     
118     
119     protected void flushResult() {
120         getRef().print(getEditor().getText());
121         getRef().flush();
122         try {
123             assertFile("Output does not match golden file.", getGoldenFile(), new File(getWorkDir(), this.getName() + ".ref"),
124             new File(getWorkDir(), this.getName() + ".diff"), new LineDiff(false));
125         } catch (IOException e) {
126             assertTrue("IOException: " + e.getMessage(), false);
127         }
128     }
129     
130     public void finishEditor() {
131         if (editor != null) {
132             getEditor().closeDiscard();
133         }
134         if (editor2 != null) {
135             getEditor2().closeDiscard();
136         }
137     }
138     
139     
140     public void setUp() {
141         log("Starting Focus test.");
142         System.setOut(getLog());
143     }
144     
145     public void tearDown() throws Exception JavaDoc {
146         log("Ending Focus test.");
147     }
148     
149     private void robotWrite(String JavaDoc s,Robot JavaDoc robot) {
150         int c;
151         for (int i=0;i < s.length();i++) {
152             c=(int)(s.charAt(i));
153             if (c == '\n') {
154                 c=(int)(KeyEvent.VK_ENTER);
155             }
156             if (Character.isUpperCase((char)c)) {
157                 robot.keyPress(KeyEvent.VK_SHIFT);
158             }
159             robot.keyPress(c);
160             robot.delay(5);
161             robot.keyRelease(c);
162             robot.delay(5);
163             if (Character.isUpperCase((char)c)) {
164                 robot.keyRelease(KeyEvent.VK_SHIFT);
165             }
166         }
167     }
168     
169     public void robotPushKey(int keycode, int mask, Robot JavaDoc robot) {
170         if ((mask & KeyEvent.CTRL_MASK) > 0) {
171             robot.keyPress(KeyEvent.VK_CONTROL);
172             robot.delay(1);
173         }
174         if ((mask & KeyEvent.SHIFT_MASK) > 0) {
175             robot.keyPress(KeyEvent.VK_SHIFT);
176             robot.delay(1);
177         }
178         if ((mask & KeyEvent.ALT_MASK) > 0) {
179             robot.keyPress(KeyEvent.VK_ALT);
180             robot.delay(1);
181         }
182         robot.keyPress(keycode);
183         robot.delay(50);
184         robot.keyRelease(keycode);
185         robot.delay(1);
186         if ((mask & KeyEvent.ALT_MASK) > 0) {
187             robot.keyRelease(KeyEvent.VK_ALT);
188             robot.delay(1);
189         }
190         if ((mask & KeyEvent.SHIFT_MASK) > 0) {
191             robot.keyRelease(KeyEvent.VK_SHIFT);
192             robot.delay(1);
193         }
194         if ((mask & KeyEvent.CTRL_MASK) > 0) {
195             robot.keyRelease(KeyEvent.VK_CONTROL);
196             robot.delay(1);
197         }
198         robot.waitForIdle();
199     }
200     
201     public void testGoToLine() {
202         try {
203             log("testGoToLine starting");
204             prepareLongFileEditor();
205             log("testGoToLine editor prepared");
206             Robot JavaDoc robot=new Robot JavaDoc();
207             log("testGoToLine Robot created");
208             getEditor().grabFocus();
209             robot.waitForIdle();
210             log("Idle occured.");
211             for (int i=0;i < lines.length;i++) {
212                 robot.keyPress(KeyEvent.VK_CONTROL);
213                 robot.delay(1);
214                 robot.keyPress(KeyEvent.VK_G);
215                 robot.delay(50);
216                 robot.keyRelease(KeyEvent.VK_G);
217                 robot.delay(1);
218                 robot.keyRelease(KeyEvent.VK_CONTROL);
219                 robot.waitForIdle();
220                 robot.delay(50);
221                 GoToLine.goToLine(lines[i],robot);
222                 robot.delay(1000);
223                 robotWrite(TEXT,robot);
224                 robot.delay(500);
225             }
226             log("testGoToLine flush results:");
227             flushResult();
228         } catch (AWTException ex) {
229             assertTrue("Robot couldn't be created: AWTException "+ex.getClass().getName()+": "+ex.getMessage(),false);
230         } finally {
231             log("testGoToLine closing editor:");
232             finishEditor();
233             log("testGoToLine finished");
234         }
235     }
236     
237     public void testFind() {
238         try {
239             log("testFind starting");
240             prepareShortFileEditor();
241             log("testFind editor prepared");
242             Robot JavaDoc robot=new Robot JavaDoc();
243             log("testFind Robot created");
244             getEditor().grabFocus();
245             robot.waitForIdle();
246             for (int i=0;i < texts.length;i++) {
247                 robot.keyPress(KeyEvent.VK_CONTROL);
248                 robot.delay(1);
249                 robot.keyPress(KeyEvent.VK_F);
250                 robot.delay(20);
251                 robot.keyRelease(KeyEvent.VK_F);
252                 robot.delay(1);
253                 robot.keyRelease(KeyEvent.VK_CONTROL);
254                 robot.waitForIdle();
255                 Find.find(texts[i],robot);
256                 robot.delay(500);
257                 robotWrite(TEXT,robot);
258                 robot.delay(500);
259             }
260             log("testFind flush results:");
261             flushResult();
262         } catch (AWTException ex) {
263             assertTrue("Robot couldn't be created: AWTException "+ex.getClass().getName()+": "+ex.getMessage(),false);
264         } finally {
265             log("testFind closing editor:");
266             finishEditor();
267             log("testFind finished");
268         }
269     }
270     
271     /**
272      * This test case should not be run with solaris operation system.
273      * Alt+Left/Right switch the X workspaces there.
274      *
275      */

276     public void testTabsSwitching() {
277         try {
278             log("TabsSwitching starting");
279             
280             String JavaDoc osname=System.getProperty("os.name").toLowerCase();
281             
282             if (osname.indexOf("solaris") > -1 || osname.indexOf("sunos") > -1) {
283                 log("This test case couldn't be run in Solaris platform.");
284                 this.assertTrue("Solaris system.",true);
285                 
286                 return;
287             }
288             Robot JavaDoc robot=new Robot JavaDoc();
289             log("testFind Robot created");
290             prepare2FilesEditors();
291             getEditor().grabFocus();
292             for (int i=0;i < 10;i++) {
293                 robotPushKey(KeyEvent.VK_RIGHT,KeyEvent.ALT_MASK,robot);
294                 robot.waitForIdle();
295                 robotPushKey(KeyEvent.VK_LEFT,KeyEvent.ALT_MASK,robot);
296                 robot.waitForIdle();
297                 robotWrite(TEXT+"\n",robot);
298                 robot.waitForIdle();
299                 robot.delay(500);
300             }
301             flushResult();
302         } catch (AWTException ex) {
303             assertTrue("Robot couldn't be created: AWTException "+ex.getClass().getName()+": "+ex.getMessage(),false);
304         } finally {
305             log("TabsSwitching closing editor:");
306             finishEditor();
307             log("TabsSwitching finished");
308         }
309     }
310     
311     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
312         Object JavaDoc o=org.openide.options.SystemOption.findObject(org.openide.actions.NextTabAction.class);
313         
314
315         if (o != null) {
316             org.openide.actions.NextTabAction act=(org.openide.actions.NextTabAction)o;
317             Object JavaDoc o2=act.getActionMapKey();
318             System.out.println(o2);
319         }
320         //new CheckActionsListPerformer("testCheckActions").testCheckActions();
321
}
322     
323 }
324
Popular Tags