KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > RobotTest


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.modules.editor;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.beans.*;
25 import java.util.*;
26 import javax.swing.JEditorPane JavaDoc;
27 import javax.swing.text.*;
28 import org.openide.cookies.*;
29 import org.openide.filesystems.*;
30 import org.openide.loaders.*;
31 import org.openide.windows.*;
32
33 /**
34  * Test benchmarking code completion in editor. It must be executed using
35  * internal execution.
36  *
37  * @author Petr Kuzel
38  */

39 public class RobotTest {
40
41     /** Creates a new instance of RobotTest */
42     public RobotTest() {
43     }
44
45     /**
46      * @param args the command line arguments
47      */

48     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
49
50         Robot robot = new Robot();
51         
52         Repository repo = Repository.getDefault();
53         FileObject fo = repo.findResource("org/netbeans/modules/editor/data/Robot.java");
54         
55         DataObject dobj = DataObject.find(fo);
56         EditorCookie editor = (EditorCookie) dobj.getCookie(EditorCookie.class);
57         TopComponent.Registry reg = WindowManager.getDefault().getRegistry();
58         WaitPCL waitPCL = new WaitPCL();
59         reg.addPropertyChangeListener(waitPCL);
60         try {
61             editor.openDocument();
62             editor.open();
63             waitPCL.waitUntilOpened().requestFocus();
64             robot.waitForIdle(); // try to eliminate null panes bellow
65
} finally {
66             reg.removePropertyChangeListener(waitPCL);
67         }
68                 
69         Document doc = editor.openDocument();
70         JEditorPane JavaDoc[] panes = editor.getOpenedPanes();
71         if (panes == null) throw new IllegalStateException JavaDoc("Null panes " + editor.getClass());
72         if (panes.length == 0) throw new IllegalStateException JavaDoc("No pane " + editor.getClass());
73         String JavaDoc string = doc.getText(0, doc.getLength()-1);
74         int dot = string.indexOf("//java.awt.Robot");
75         if (dot < 0) throw new IllegalStateException JavaDoc("Mark not found: " + string);
76         Caret caret = panes[0].getCaret();
77         caret.setDot(dot); // CCE
78

79         System.gc();
80         robot.waitForIdle();
81         
82         long start = System.currentTimeMillis();
83
84         robot.keyPress(KeyEvent.VK_SHIFT);
85         robot.keyPress(KeyEvent.VK_S);
86         robot.keyRelease(KeyEvent.VK_S);
87         robot.keyRelease(KeyEvent.VK_SHIFT);
88         robot.delay(100);
89         robot.keyPress(KeyEvent.VK_Y);
90         robot.delay(100);
91         robot.keyPress(KeyEvent.VK_S);
92         robot.delay(100);
93         robot.keyPress(KeyEvent.VK_T);
94         robot.delay(100);
95         robot.keyPress(KeyEvent.VK_E);
96         robot.delay(100);
97         robot.keyPress(KeyEvent.VK_M);
98         robot.delay(100);
99         robot.keyPress(KeyEvent.VK_DECIMAL);
100         robot.delay(100);
101         robot.keyPress(KeyEvent.VK_G);
102         robot.delay(100);
103         robot.keyPress(KeyEvent.VK_CONTROL);
104         robot.keyPress(KeyEvent.VK_SPACE);
105         robot.keyRelease(KeyEvent.VK_SPACE);
106         robot.keyRelease(KeyEvent.VK_CONTROL);
107         
108         robot.waitForIdle();
109         
110         AWTMonitor monitor = new AWTMonitor();
111         
112         try {
113             Toolkit.getDefaultToolkit().addAWTEventListener(monitor, AWTEvent.PAINT_EVENT_MASK);
114             robot.delay(2000);
115         } finally {
116             Toolkit.getDefaultToolkit().removeAWTEventListener(monitor);
117         }
118         
119         long end = monitor.getLastTime();
120         
121         
122         System.out.println("Total time " + (end - start) + "ms");
123     }
124     
125     private static class AWTMonitor implements AWTEventListener {
126         
127         long last = System.currentTimeMillis();
128         
129         public synchronized void eventDispatched(AWTEvent e) {
130             System.err.println("AWT event " + e);
131             last = System.currentTimeMillis();
132         }
133         
134         public synchronized long getLastTime() {
135             return last;
136         }
137         
138     }
139     
140     private static class WaitPCL implements PropertyChangeListener {
141             
142         private TopComponent opened = null;
143         
144         public WaitPCL() {
145         }
146         
147         public void propertyChange(PropertyChangeEvent e) {
148             if (TopComponent.Registry.PROP_OPENED.equals(e.getPropertyName())) {
149                 TopComponent.Registry reg = WindowManager.getDefault().getRegistry();
150                 Set opened = new HashSet((Set)e.getNewValue());
151                 opened.removeAll((Set)e.getOldValue());
152                 notifyOpened((TopComponent)opened.iterator().next());
153             }
154         }
155
156         private synchronized void notifyOpened(TopComponent comp) {
157             opened = comp;
158             notify();
159         }
160
161         public synchronized TopComponent waitUntilOpened() throws InterruptedException JavaDoc{
162             while (null == opened) wait();
163             return opened;
164         }
165     }
166     
167 }
168
Popular Tags