1 19 20 package org.openide.util; 21 22 import java.awt.AWTEvent ; 23 import java.awt.Dialog ; 24 import java.awt.Frame ; 25 import java.awt.Graphics ; 26 import java.awt.Toolkit ; 27 import java.awt.event.AWTEventListener ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.util.ArrayList ; 31 import java.util.EventListener ; 32 import javax.swing.JPanel ; 33 import javax.swing.SwingUtilities ; 34 import javax.swing.Timer ; 35 import org.netbeans.junit.NbTestCase; 36 37 41 public class InitJobTest extends NbTestCase { 42 43 Dialog dlg; 44 45 ArrayList constructCalls, finishCalls, cancelCalls; 46 47 Thread edThread; 48 49 SimpleInitComp comp; 50 51 private Frame frame; 52 53 54 public InitJobTest(String testName) { 55 super(testName); 56 } 57 58 59 protected boolean runInEQ() { 60 return true; 61 } 62 63 66 public void testInitJob() throws Exception { 67 System.out.println("Testing simple init job run"); 68 initializeSimple(); 69 comp = new SimpleInitComp(); 70 Utilities.attachInitJob(comp, comp); 71 frame = new Frame (); 72 frame.setSize(100, 100); 73 frame.setVisible(true); 74 dlg = new Dialog (frame, true); 75 dlg.setSize(50, 50); 76 dlg.add(comp); 77 dlg.setVisible(true); 78 } 79 80 public void testCancelAbility() throws Exception { 81 System.out.println("Testing cancel ability of async init job"); 82 initializeSimple(); 83 initCancelResults(); 84 CancelInitComp comp = new CancelInitComp(); 85 Utilities.attachInitJob(comp, comp); 86 frame = new Frame (); 87 frame.setSize(100, 100); 88 frame.setVisible(true); 89 dlg = new Dialog (frame, true); 90 dlg.setSize(50, 50); 91 dlg.add(comp); 92 dlg.setVisible(true); 93 } 94 95 96 97 98 private void constructCalled(Thread thread, long time) { 99 constructCalls.add(new CallData(thread, time)); 100 } 101 102 private void finishCalled(Thread thread, long time) { 103 finishCalls.add(new CallData(thread, time)); 104 } 105 106 private void cancelCalled() { 107 cancelCalls.add(new CallData(Thread.currentThread(), System.currentTimeMillis())); 108 } 109 110 private void checkSimpleResults() { 111 if (constructCalls.size() != 1) { 112 fail("AsyncGUIJob.construct was called " + constructCalls.size() + 113 " times intead of just once."); 114 } 115 if (finishCalls.size() != 1) { 116 fail("AsyncGUIJob.finish was called " + finishCalls.size() + 117 " times intead of just once."); 118 } 119 CallData constructCall = (CallData)constructCalls.get(0); 120 CallData finishCall = (CallData)finishCalls.get(0); 121 if (constructCall.thread.equals(edThread)) { 122 fail("AsyncGUIJob.construct *was* called from event dispatch thread, " + 123 "which is wrong."); 124 } 125 if (!finishCall.thread.equals(edThread)) { 126 fail("AsyncGUIJob.finish *was not* called from event dispatch thread, " + 127 "which is wrong."); 128 } 129 if (constructCall.time > finishCall.time) { 130 fail("AsyncGUIJob.finish was called before AsyncGUIJob.construct, " + 131 "which is wrong."); 132 } 133 AWTEventListener [] awtListeners = 134 Toolkit.getDefaultToolkit().getAWTEventListeners(AWTEvent.PAINT_EVENT_MASK); 135 for (int i = 0; i < awtListeners.length; i++) { 136 if (awtListeners[i].getClass().equals(AsyncInitSupport.class)) { 137 fail("Probable memory leak: AsyncInitSupport didn't detached " + 138 "from AWT toolkit."); 139 } 140 } 141 EventListener [] listeners = comp.getListeners(AsyncInitSupport.class); 142 if (listeners.length != 0) { 143 fail("Probable memory leak: AsyncInitSupport didn't detached " + 144 "from component being inited " + comp); 145 } 146 } 147 148 private void checkCancelResults() { 149 if (cancelCalls.size() != 1) { 150 fail("Cancellable.cancel was called " + cancelCalls.size() + 151 " times intead of just once."); 152 } 153 if (finishCalls.size() != 0) { 154 fail("AsyncGUIJob.finish should not been called at all, but was called " 155 + finishCalls.size() + " times."); 156 } 157 } 158 159 private void initializeSimple() throws Exception { 160 constructCalls = new ArrayList (); 161 finishCalls = new ArrayList (); 162 if (SwingUtilities.isEventDispatchThread()) { 163 edThread = Thread.currentThread(); 164 } else { 165 SwingUtilities.invokeAndWait(new Runnable () { 166 public void run() { 167 edThread = Thread.currentThread(); 168 } 169 }); 170 } 171 } 172 173 private void initCancelResults() { 174 cancelCalls = new ArrayList (); 175 } 176 177 178 private final static class CallData { 179 Thread thread; 180 long time; 181 182 public CallData(Thread thread, long time) { 183 this.thread = thread; 184 this.time = time; 185 } 186 } 187 188 189 private final class TimerListener implements ActionListener { 190 191 private boolean cancel; 192 public TimerListener(boolean cancel) { 193 this.cancel = cancel; 194 } 195 public void actionPerformed(ActionEvent e) { 196 dlg.dispose(); 197 frame.dispose(); 198 if (cancel) { 199 checkCancelResults(); 200 } else { 201 checkSimpleResults(); 202 } 203 } 204 } 205 206 208 private final class SimpleInitComp extends JPanel implements AsyncGUIJob { 209 210 215 public void construct() { 216 constructCalled(Thread.currentThread(), System.currentTimeMillis()); 217 } 218 219 224 public void finished() { 225 finishCalled(Thread.currentThread(), System.currentTimeMillis()); 226 } 227 228 public void paint(Graphics g) { 229 super.paint(g); 230 Timer timer = new Timer (1000, new TimerListener(false)); 231 timer.setRepeats(false); 232 timer.start(); 233 } 234 235 } 237 239 private final class CancelInitComp extends JPanel implements AsyncGUIJob, Cancellable { 240 241 246 public void construct() { 247 try { 249 Thread.sleep(2000); 250 } catch (InterruptedException exc) { 251 } 253 } 254 255 260 public void finished() { 261 finishCalled(Thread.currentThread(), System.currentTimeMillis()); 262 } 263 264 public void paint(Graphics g) { 265 super.paint(g); 266 Timer timer = new Timer (1000, new TimerListener(true)); 267 timer.setRepeats(false); 268 timer.start(); 269 } 270 271 274 public boolean cancel() { 275 cancelCalled(); 276 return true; 277 } 278 279 } 281 } 282 | Popular Tags |