1 19 20 package org.openide.util.actions; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.image.BufferedImage ; 26 import java.awt.image.ImageObserver ; 27 import java.awt.image.PixelGrabber ; 28 import java.util.Date ; 29 import java.util.logging.Level ; 30 import javax.swing.Icon ; 31 import javax.swing.JButton ; 32 import junit.textui.TestRunner; 33 import org.netbeans.junit.Log; 34 import org.netbeans.junit.NbTestCase; 35 import org.openide.ErrorManager; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.Lookup; 38 import org.openide.util.actions.CallableSystemAction; 39 import org.openide.util.lookup.AbstractLookup; 40 import org.openide.util.lookup.InstanceContent; 41 42 46 public class AsynchronousTest extends NbTestCase { 47 48 private CharSequence err; 49 50 public AsynchronousTest(String name) { 51 super(name); 52 } 53 54 protected boolean runInEQ() { 55 return true; 56 } 57 58 protected void setUp() { 59 err = Log.enable("", Level.ALL); 60 } 61 62 public void testExecutionOfActionsThatDoesNotOverrideAsynchronousIsAsynchronousButWarningIsPrinted() throws Exception { 63 DoesNotOverride action = (DoesNotOverride)DoesNotOverride.get(DoesNotOverride.class); 64 65 synchronized (action) { 66 action.actionPerformed(new ActionEvent (this, 0, "")); 67 Thread.sleep(500); 68 assertFalse("Not yet finished", action.finished); 69 action.wait(); 70 assertTrue("The asynchronous action is finished", action.finished); 71 } 72 73 if (err.toString().indexOf(DoesNotOverride.class.getName() + " should override") < 0) { 74 fail("There should be warning about not overriding asynchronous: " + err); 75 } 76 } 77 78 public void testExecutionCanBeAsynchronous() throws Exception { 79 DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get(DoesOverrideAndReturnsTrue.class); 80 81 synchronized (action) { 82 action.actionPerformed(new ActionEvent (this, 0, "")); 83 Thread.sleep(500); 84 assertFalse("Not yet finished", action.finished); 85 action.wait(); 86 assertTrue("The asynchronous action is finished", action.finished); 87 } 88 89 if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) { 90 fail("No warning about the class: " + err); 91 } 92 } 93 94 public void testExecutionCanBeSynchronous() throws Exception { 95 DoesOverrideAndReturnsFalse action = (DoesOverrideAndReturnsFalse)DoesOverrideAndReturnsFalse.get(DoesOverrideAndReturnsFalse.class); 96 97 synchronized (action) { 98 action.actionPerformed(new ActionEvent (this, 0, "")); 99 assertTrue("The synchronous action is finished immediatelly", action.finished); 100 } 101 102 if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) { 103 fail("No warning about the class: " + err); 104 } 105 } 106 107 public void testExecutionCanBeForcedToBeSynchronous() throws Exception { 108 DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get(DoesOverrideAndReturnsTrue.class); 109 110 synchronized (action) { 111 action.actionPerformed(new ActionEvent (this, 0, "waitFinished")); 112 assertTrue("When asked for synchronous the action is finished immediatelly", action.finished); 113 } 114 115 if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) { 116 fail("No warning about the class: " + err); 117 } 118 } 119 120 public static class DoesNotOverride extends CallableSystemAction { 121 boolean finished; 122 123 public HelpCtx getHelpCtx() { 124 return HelpCtx.DEFAULT_HELP; 125 } 126 127 public String getName() { 128 return "Should warn action"; 129 } 130 131 public synchronized void performAction() { 132 notifyAll(); 133 finished = true; 134 } 135 136 } 137 138 public static class DoesOverrideAndReturnsTrue extends DoesNotOverride { 139 public boolean asynchronous() { 140 return true; 141 } 142 } 143 144 public static final class DoesOverrideAndReturnsFalse extends DoesOverrideAndReturnsTrue { 145 public boolean asynchronous() { 146 return false; 147 } 148 } 149 } 150 | Popular Tags |