1 19 20 package org.openide.actions; 21 22 import java.awt.event.ActionEvent ; 23 import org.netbeans.junit.MockServices; 24 import org.netbeans.junit.NbTestCase; 25 import org.openide.LifecycleManager; 26 27 31 public class SaveAllActionTest extends NbTestCase { 32 33 public SaveAllActionTest (String testName) { 34 super (testName); 35 } 36 37 protected boolean runInEQ () { 38 return true; 39 } 40 41 42 protected void setUp () { 43 MockServices.setServices(new Class [] {Life.class}); 44 Life.max = 0; 45 Life.cnt = 0; 46 Life.executed = 0; 47 } 48 49 public void testThatTheActionIsInvokeOutsideOfAWTThreadAndOnlyOnceAtATime () throws Exception { 50 SaveAllAction a = (SaveAllAction)SaveAllAction.get (SaveAllAction.class); 51 a.setEnabled (true); 52 assertTrue ("Is enabled", a.isEnabled ()); 53 54 ActionEvent ev = new ActionEvent (this, 0, ""); 55 a.actionPerformed (ev); 56 a.actionPerformed (ev); 57 a.actionPerformed (ev); 58 59 Object life = org.openide.util.Lookup.getDefault ().lookup (LifecycleManager.class); 60 synchronized (life) { 61 while (Life.executed != 3) { 62 life.wait (); 63 } 64 } 65 66 assertEquals ("Maximum is one invocation of saveAll at one time", 1, Life.max); 67 } 68 69 public static final class Life extends LifecycleManager { 70 static int max; 71 static int cnt; 72 static int executed; 73 74 public synchronized void saveAll () { 75 cnt++; 76 if (cnt > max) { 77 max = cnt; 78 } 79 try { 80 wait (500); 81 } catch (Exception ex) { 82 83 } 84 executed++; 85 notifyAll (); 86 87 cnt--; 88 assertFalse ("No AWT thread: ", javax.swing.SwingUtilities.isEventDispatchThread ()); 89 } 90 91 public void exit () { 92 fail ("Not supported"); 93 } 94 95 } 96 } 97 | Popular Tags |