1 19 20 package org.netbeans.core.execution; 21 22 import java.awt.event.ActionEvent ; 23 import javax.swing.AbstractAction ; 24 import junit.framework.*; 25 import org.netbeans.api.progress.ProgressHandle; 26 import org.netbeans.api.progress.ProgressHandleFactory; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.progress.module.Controller; 29 import org.netbeans.progress.spi.ProgressEvent; 30 import org.netbeans.progress.spi.ProgressUIWorker; 31 import org.openide.actions.ActionManager; 32 import org.openide.util.RequestProcessor; 33 34 38 public class PendingTaskTest extends NbTestCase { 39 40 public PendingTaskTest(String testName) { 41 super(testName); 42 } 43 44 protected void setUp() throws Exception { 45 } 46 47 protected void tearDown() throws Exception { 48 } 49 50 51 public void testActionManagersInvokeAction() throws InterruptedException { 52 class BlockingAction extends AbstractAction implements Runnable { 53 public synchronized void actionPerformed(ActionEvent e) { 54 notifyAll(); 55 try { 56 wait(); 57 } catch (InterruptedException ex) { 58 ex.printStackTrace(); 59 fail("No InterruptedException please"); 60 } 61 } 62 63 64 public void run() { 65 ActionManager.getDefault().invokeAction(this, new ActionEvent (this, 0, "")); 66 } 67 } 68 69 BlockingAction b = new BlockingAction(); 70 71 72 assertEquals("No tasks now", Install.getPendingTasks().size(), 0); 73 74 RequestProcessor.Task t; 75 synchronized (b) { 76 t = RequestProcessor.getDefault().post(b); 77 b.wait(); 78 } 79 80 assertEquals("One action in progress", 1, Install.getPendingTasks().size()); 81 82 synchronized (b) { 83 b.notifyAll(); 84 } 85 t.waitFinished(); 86 87 assertEquals("Action finished", Install.getPendingTasks().size(), 0); 88 } 89 90 91 public void testProgressTasks() throws InterruptedException { 92 class MyWorker implements ProgressUIWorker { 93 int cnt; 94 95 public synchronized void processProgressEvent(ProgressEvent event) { 96 cnt++; 97 getLog().println("processProgressEvent: " + event); 98 notifyAll(); 99 } 100 public void processSelectedProgressEvent(ProgressEvent event) { 101 getLog().println("processSelectedProgressEvent: " + event); 102 } 103 104 public synchronized void waitForEvent() throws InterruptedException { 105 int prev = cnt; 106 getLog().println("waitForEvent before wait"); 107 wait(5000); 108 getLog().println("waitForEvent after wait"); 109 if (prev == cnt) { 110 fail("Time out - no event delivered"); 111 } 112 } 113 } 114 115 MyWorker worker = new MyWorker(); 116 Controller.defaultInstance = new Controller(worker); 117 118 ProgressHandle proghandle = ProgressHandleFactory.createHandle("a1"); 119 proghandle.setInitialDelay(0); 120 121 assertEquals("None before", 0, Install.getPendingTasks().size()); 122 123 synchronized (worker) { 124 getLog().println("proghandle - start"); 125 proghandle.start(); 126 worker.waitForEvent(); 127 } 128 129 assertEquals("One now", 1, Install.getPendingTasks().size()); 130 131 Thread.sleep(1000); 134 135 synchronized (worker) { 136 getLog().println("proghandle - finish"); 137 proghandle.finish(); 138 worker.waitForEvent(); 139 } 140 141 assertEquals("None after", 0, Install.getPendingTasks().size()); 142 } 143 144 } 145 | Popular Tags |