1 19 20 package org.netbeans.modules.j2ee.common; 21 22 import java.awt.event.KeyEvent ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.JRootPane ; 26 import javax.swing.SwingUtilities ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.util.Mutex; 29 import org.openide.util.RequestProcessor; 30 31 35 public class EventRequestProcessorTest extends NbTestCase { 36 37 41 public EventRequestProcessorTest(String testName) { 42 super(testName); 43 } 44 45 protected boolean runInEQ() { 46 return true; 47 } 48 49 public void testInvoke() { 50 final EventRequestProcessor erp = new EventRequestProcessor(); 51 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 52 53 final int[] invokeCount = new int[1]; 55 final int[] correctThreadCount = new int[1]; 58 final int[] correctPanelStateCount = new int[1]; 61 62 actions.add(new EventRequestProcessor.SynchronousAction() { 63 public void run(final EventRequestProcessor.Context actionContext) { 64 correctThreadCount[0] += isEventDispatchThread(); 65 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 66 public Object run() { 67 correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel()); 68 return null; 69 } 70 }); 71 invokeCount[0]++; 72 } 73 }); 74 75 actions.add(new EventRequestProcessor.SynchronousAction() { 76 public void run(final EventRequestProcessor.Context actionContext) { 77 correctThreadCount[0] += isEventDispatchThread(); 78 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 79 public Object run() { 80 correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel()); 81 return null; 82 } 83 }); 84 invokeCount[0]++; 85 } 86 }); 87 88 actions.add(new EventRequestProcessor.AsynchronousAction() { 89 public void run(final EventRequestProcessor.Context actionContext) { 90 actionContext.getProgress().progress("Testing an asynchronous action."); 91 92 correctThreadCount[0] += isNotEventDispatchThread(); 93 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 94 public Object run() { 95 correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel()); 96 return null; 97 } 98 }); 99 invokeCount[0]++; 100 try { 101 Thread.sleep(300); 102 } catch (InterruptedException e) { 103 } 104 } 105 }); 106 107 actions.add(new EventRequestProcessor.AsynchronousAction() { 108 public void run(final EventRequestProcessor.Context actionContext) { 109 actionContext.getProgress().progress("Testing the second asynchronous action."); 110 111 correctThreadCount[0] += isNotEventDispatchThread(); 112 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 113 public Object run() { 114 correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel()); 115 return null; 116 } 117 }); 118 invokeCount[0]++; 119 try { 120 Thread.sleep(300); 121 } catch (InterruptedException e) { 122 } 123 } 124 }); 125 126 actions.add(new EventRequestProcessor.SynchronousAction() { 127 public void run(final EventRequestProcessor.Context actionContext) { 128 correctThreadCount[0] += isEventDispatchThread(); 129 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 130 public Object run() { 131 correctPanelStateCount[0] += isClosed(actionContext.getProgress().getPanel()); 132 return null; 133 } 134 }); 135 invokeCount[0]++; 136 } 137 }); 138 139 actions.add(new EventRequestProcessor.AsynchronousAction() { 140 public void run(final EventRequestProcessor.Context actionContext) { 141 actionContext.getProgress().progress("Testing the third asynchronous action."); 142 143 correctThreadCount[0] += isNotEventDispatchThread(); 144 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 145 public Object run() { 146 correctPanelStateCount[0] += isOpen(actionContext.getProgress().getPanel()); 147 return null; 148 } 149 }); 150 invokeCount[0]++; 151 try { 152 Thread.sleep(300); 153 } catch (InterruptedException e) { 154 } 155 } 156 }); 157 158 erp.invoke(actions); 159 assertEquals(6, invokeCount[0]); 160 assertEquals(6, correctThreadCount[0]); 161 assertEquals(6, correctPanelStateCount[0]); 162 163 erp.invoke(actions); 164 assertEquals(12, invokeCount[0]); 165 assertEquals(12, correctThreadCount[0]); 166 assertEquals(12, correctPanelStateCount[0]); 167 } 168 169 private int isEventDispatchThread() { 170 return SwingUtilities.isEventDispatchThread() ? 1 : 0; 171 } 172 173 private int isNotEventDispatchThread() { 174 return SwingUtilities.isEventDispatchThread() ? 0 : 1; 175 } 176 177 private int isOpen(ProgressPanel panel) { 178 return panel.isOpen() ? 1 : 0; 179 } 180 181 private int isClosed(ProgressPanel panel) { 182 return panel.isOpen() ? 0 : 1; 183 } 184 185 public void testProgressMessage() { 186 EventRequestProcessor erp = new EventRequestProcessor(); 187 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 188 189 final String [] progressMessage = new String [1]; 190 191 actions.add(new EventRequestProcessor.AsynchronousAction() { 192 public void run(final EventRequestProcessor.Context actionContext) { 193 actionContext.getProgress().progress("Progress message"); 194 195 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 196 public Object run() { 197 ProgressPanel progressPanel = actionContext.getProgress().getPanel(); 198 progressMessage[0] = progressPanel.getText(); 199 return null; 200 } 201 }); 202 } 203 }); 204 205 erp.invoke(actions); 206 assertEquals("Progress message", progressMessage[0]); 207 } 208 209 public void testDisabledActionDoesNotCauseAnInfiniteLoop() { 210 EventRequestProcessor erp = new EventRequestProcessor(); 211 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 212 213 final Object sync = new Object (); 214 final boolean[] ran = new boolean[1]; 215 216 actions.add(new EventRequestProcessor.SynchronousAction() { 217 public void run(EventRequestProcessor.Context actionContext) { 218 synchronized (sync) { 219 ran[0] = true; 220 sync.notifyAll(); 221 } 222 } 223 224 public boolean isEnabled() { 225 return false; 226 } 227 }); 228 229 RequestProcessor.getDefault().post(new Runnable () { 230 public void run() { 231 long startTime = System.currentTimeMillis(); 232 synchronized (sync) { 233 while (!ran[0]) { 234 if (System.currentTimeMillis() - startTime >= 5 * 1000) { 235 System.exit(1); 237 } 238 try { 239 sync.wait(500); 240 } catch (InterruptedException e) { } 241 } 242 } 243 } 244 }); 245 246 erp.invoke(actions); 247 } 248 249 public void testExceptionInSyncActionPropagates() { 250 EventRequestProcessor erp = new EventRequestProcessor(); 251 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 252 253 actions.add(new EventRequestProcessor.SynchronousAction() { 254 public void run(final EventRequestProcessor.Context actionContext) { 255 throw new RuntimeException ("Error"); 256 } 257 }); 258 259 try { 260 erp.invoke(actions); 261 fail("Should throw RuntimeException"); 262 } catch (RuntimeException e) { 263 } 264 265 assertNull(erp.actionInvoker); 266 } 267 268 public void testExceptionInAsyncActionPropagatesAndProgressPanelCloses() { 269 final EventRequestProcessor erp = new EventRequestProcessor(); 270 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 271 272 final ProgressPanel progressPanel[] = new ProgressPanel[1]; 273 final Boolean panelOpen[] = new Boolean [1]; 274 275 actions.add(new EventRequestProcessor.AsynchronousAction() { 276 public void run(final EventRequestProcessor.Context actionContext) { 277 actionContext.getProgress().progress("Asynchronous"); 278 279 progressPanel[0] = actionContext.getProgress().getPanel(); 281 282 throw new AssertionError ("Error"); 284 } 285 }); 286 287 try { 288 erp.invoke(actions); 289 fail("Should throw RuntimeException"); 290 } catch (RuntimeException e) { 291 assertTrue(e.getCause() instanceof AssertionError ); 292 } 293 294 assertFalse(progressPanel[0].isOpen()); 295 296 assertNull(erp.actionInvoker); 297 } 298 299 public void testNoCancelButtonWhenNonCancellableInvocation() { 300 EventRequestProcessor erp = new EventRequestProcessor(); 301 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 302 303 final boolean[] cancelVisible = new boolean[1]; 304 305 actions.add(new EventRequestProcessor.AsynchronousAction() { 306 public void run(final EventRequestProcessor.Context actionContext) { 307 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 308 public Object run() { 309 ProgressPanel progressPanel = actionContext.getProgress().getPanel(); 310 cancelVisible[0] = progressPanel.isCancelVisible(); 311 return null; 312 } 313 }); 314 } 315 }); 316 317 erp.invoke(actions); 318 assertFalse(cancelVisible[0]); 319 } 320 321 public void testCancelButtonWhenCancellableInvocation() { 322 EventRequestProcessor erp = new EventRequestProcessor(); 323 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 324 325 final boolean[] cancelVisible = new boolean[2]; 326 final boolean[] cancelEnabled = new boolean[2]; 327 328 actions.add(new EventRequestProcessor.AsynchronousAction() { 329 public void run(final EventRequestProcessor.Context actionContext) { 330 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 331 public Object run() { 332 ProgressPanel progressPanel = actionContext.getProgress().getPanel(); 333 cancelVisible[0] = progressPanel.isCancelVisible(); 334 cancelEnabled[0] = progressPanel.isCancelEnabled(); 335 return null; 336 } 337 }); 338 } 339 }); 340 341 actions.add(new EventRequestProcessor.CancellableAction() { 342 public void run(final EventRequestProcessor.Context actionContext) { 343 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 344 public Object run() { 345 ProgressPanel progressPanel = actionContext.getProgress().getPanel(); 346 cancelVisible[1] = progressPanel.isCancelVisible(); 347 cancelEnabled[1] = progressPanel.isCancelEnabled(); 348 return null; 349 } 350 }); 351 } 352 353 public boolean isEnabled() { 354 return true; 355 } 356 357 public boolean getRunInEventThread() { 358 return false; 359 } 360 361 public boolean cancel() { 362 return true; 363 } 364 }); 365 366 assertTrue(erp.invoke(actions, true)); 369 370 assertTrue(cancelVisible[0]); 372 assertFalse(cancelEnabled[0]); 373 374 assertTrue(cancelVisible[1]); 376 assertTrue(cancelEnabled[1]); 377 } 378 379 public void testCancelWorks() { 380 EventRequestProcessor erp = new EventRequestProcessor(); 381 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 382 383 final boolean[] secondActionInvoked = new boolean[1]; 384 final boolean[] cancelInvoked = new boolean[1]; 385 final boolean[] cancelInvokedInEDT = new boolean[1]; 386 387 actions.add(new EventRequestProcessor.CancellableAction() { 388 public void run(final EventRequestProcessor.Context actionContext) { 389 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 390 public Object run() { 391 actionContext.getProgress().getPanel().getCancelButton().doClick(); 392 return null; 393 } 394 }); 395 } 397 398 public boolean isEnabled() { 399 return true; 400 } 401 402 public boolean getRunInEventThread() { 403 return false; 404 } 405 406 public boolean cancel() { 407 cancelInvokedInEDT[0] = SwingUtilities.isEventDispatchThread(); 408 cancelInvoked[0] = true; 409 return true; 410 } 411 }); 412 413 actions.add(new EventRequestProcessor.AsynchronousAction() { 414 public void run(EventRequestProcessor.Context actionContext) { 415 secondActionInvoked[0] = true; 416 } 417 }); 418 419 assertFalse(erp.invoke(actions, true)); 420 assertFalse(secondActionInvoked[0]); 421 assertTrue(cancelInvokedInEDT[0]); 422 assertTrue(cancelInvoked[0]); 423 } 424 425 public void testNoCancelWhenTheCancelMethodReturnsFalse() { 426 EventRequestProcessor erp = new EventRequestProcessor(); 427 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 428 429 final boolean[] secondActionInvoked = new boolean[1]; 430 431 actions.add(new EventRequestProcessor.CancellableAction() { 432 public void run(final EventRequestProcessor.Context actionContext) { 433 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 434 public Object run() { 435 actionContext.getProgress().getPanel().getCancelButton().doClick(); 436 return null; 437 } 438 }); 439 } 441 442 public boolean isEnabled() { 443 return true; 444 } 445 446 public boolean getRunInEventThread() { 447 return false; 448 } 449 450 public boolean cancel() { 451 return false; 452 } 453 }); 454 455 actions.add(new EventRequestProcessor.AsynchronousAction() { 456 public void run(EventRequestProcessor.Context actionContext) { 457 secondActionInvoked[0] = true; 458 } 459 }); 460 461 assertTrue(erp.invoke(actions, true)); 462 assertTrue(secondActionInvoked[0]); 463 } 464 465 public void testEscapeDoesNotCloseDialogForAsyncNonCancellableActions() { 466 EventRequestProcessor erp = new EventRequestProcessor(); 467 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 468 469 final boolean[] panelOpen = new boolean[1]; 470 471 actions.add(new EventRequestProcessor.AsynchronousAction() { 472 public void run(final EventRequestProcessor.Context actionContext) { 473 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 474 public Object run() { 475 JRootPane rootPane = actionContext.getProgress().getPanel().getRootPane(); 477 KeyEvent event = new KeyEvent (rootPane, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, KeyEvent.CHAR_UNDEFINED); 478 rootPane.dispatchEvent(event); 479 480 panelOpen[0] = actionContext.getProgress().getPanel().isOpen(); 481 return null; 482 } 483 }); 484 } 485 }); 486 487 erp.invoke(actions); 488 assertTrue(panelOpen[0]); 489 } 490 491 public void testEscapeCancelsCancellableActions() { 492 EventRequestProcessor erp = new EventRequestProcessor(); 493 List <EventRequestProcessor.Action> actions = new ArrayList <EventRequestProcessor.Action>(); 494 495 final boolean[] panelOpen = new boolean[1]; 496 final boolean[] cancelEnabled = new boolean[1]; 497 498 actions.add(new EventRequestProcessor.CancellableAction() { 499 public void run(final EventRequestProcessor.Context actionContext) { 500 Mutex.EVENT.readAccess(new Mutex.Action<Object >() { 501 public Object run() { 502 JRootPane rootPane = actionContext.getProgress().getPanel().getRootPane(); 504 KeyEvent event = new KeyEvent (rootPane, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, KeyEvent.CHAR_UNDEFINED); 505 rootPane.dispatchEvent(event); 506 507 panelOpen[0] = actionContext.getProgress().getPanel().isOpen(); 508 cancelEnabled[0] = actionContext.getProgress().getPanel().getCancelButton().isEnabled(); 509 return null; 510 } 511 }); 512 } 513 514 public boolean isEnabled() { 515 return true; 516 } 517 518 public boolean getRunInEventThread() { 519 return false; 520 } 521 522 public boolean cancel() { 523 return true; 524 } 525 }); 526 527 erp.invoke(actions, true); 528 assertTrue(panelOpen[0]); 529 assertFalse(cancelEnabled[0]); 530 } 531 } 532 | Popular Tags |