1 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 ; 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 39 public class RobotTest { 40 41 42 public RobotTest() { 43 } 44 45 48 public static void main(String [] args) throws Exception { 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(); } finally { 66 reg.removePropertyChangeListener(waitPCL); 67 } 68 69 Document doc = editor.openDocument(); 70 JEditorPane [] panes = editor.getOpenedPanes(); 71 if (panes == null) throw new IllegalStateException ("Null panes " + editor.getClass()); 72 if (panes.length == 0) throw new IllegalStateException ("No pane " + editor.getClass()); 73 String string = doc.getText(0, doc.getLength()-1); 74 int dot = string.indexOf("//java.awt.Robot"); 75 if (dot < 0) throw new IllegalStateException ("Mark not found: " + string); 76 Caret caret = panes[0].getCaret(); 77 caret.setDot(dot); 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 { 162 while (null == opened) wait(); 163 return opened; 164 } 165 } 166 167 } 168 | Popular Tags |