Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.openide.util.actions; |
15 |
|
16 |
import java.awt.Image; |
17 |
import java.awt.Toolkit; |
18 |
import java.awt.event.ActionEvent; |
19 |
import java.awt.image.BufferedImage; |
20 |
import java.awt.image.ImageObserver; |
21 |
import java.awt.image.PixelGrabber; |
22 |
import javax.swing.Icon; |
23 |
import javax.swing.JButton; |
24 |
import org.netbeans.junit.*; |
25 |
import junit.textui.TestRunner; |
26 |
import org.openide.util.HelpCtx; |
27 |
import org.openide.util.lookup.AbstractLookup; |
28 |
|
29 |
/** Test general aspects of system actions. |
30 |
* Currently, just the icon. |
31 |
* @author Jesse Glick |
32 |
*/ |
33 |
public class AsynchronousTest extends NbTestCase { |
34 |
|
35 |
public AsynchronousTest (String name) { |
36 |
super(name); |
37 |
} |
38 |
|
39 |
public static void main(String[] args) { |
40 |
System.setProperty("org.openide.util.Lookup", Lkp.class.getName ()); |
41 |
TestRunner.run(new NbTestSuite(AsynchronousTest.class)); |
42 |
} |
43 |
|
44 |
protected void setUp () { |
45 |
ErrManager.messages.delete (0, ErrManager.messages.length ()); |
46 |
} |
47 |
|
48 |
public void testExecutionOfActionsThatDoesNotOverrideAsynchronousIsAsynchronousButWarningIsPrinted () throws Exception { |
49 |
DoesNotOverride action = (DoesNotOverride)DoesNotOverride.get (DoesNotOverride.class); |
50 |
|
51 |
synchronized (action) { |
52 |
action.actionPerformed (new ActionEvent(this, 0, "")); |
53 |
Thread.sleep (500); |
54 |
assertFalse ("Not yet finished", action.finished); |
55 |
action.wait (); |
56 |
assertTrue ("The asynchronous action is finished", action.finished); |
57 |
} |
58 |
|
59 |
if (ErrManager.messages.toString ().indexOf (DoesNotOverride.class.getName () + " should override") < 0) { |
60 |
fail ("There should be warning about not overriding asynchronous: " + ErrManager.messages); |
61 |
} |
62 |
} |
63 |
|
64 |
public void testExecutionCanBeAsynchronous () throws Exception { |
65 |
DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get (DoesOverrideAndReturnsTrue.class); |
66 |
|
67 |
synchronized (action) { |
68 |
action.actionPerformed (new ActionEvent(this, 0, "")); |
69 |
Thread.sleep (500); |
70 |
assertFalse ("Not yet finished", action.finished); |
71 |
action.wait (); |
72 |
assertTrue ("The asynchronous action is finished", action.finished); |
73 |
} |
74 |
|
75 |
if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) { |
76 |
fail ("No warning about the class: " + ErrManager.messages); |
77 |
} |
78 |
} |
79 |
|
80 |
public void testExecutionCanBeSynchronous () throws Exception { |
81 |
DoesOverrideAndReturnsFalse action = (DoesOverrideAndReturnsFalse)DoesOverrideAndReturnsFalse.get (DoesOverrideAndReturnsFalse.class); |
82 |
|
83 |
synchronized (action) { |
84 |
action.actionPerformed (new ActionEvent(this, 0, "")); |
85 |
assertTrue ("The synchronous action is finished immediatelly", action.finished); |
86 |
} |
87 |
|
88 |
if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) { |
89 |
fail ("No warning about the class: " + ErrManager.messages); |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
public static class DoesNotOverride extends CallableSystemAction { |
95 |
boolean finished; |
96 |
|
97 |
public HelpCtx getHelpCtx () { |
98 |
return HelpCtx.DEFAULT_HELP; |
99 |
} |
100 |
|
101 |
public String getName () { |
102 |
return "Should warn action"; |
103 |
} |
104 |
|
105 |
public synchronized void performAction () { |
106 |
notifyAll (); |
107 |
finished = true; |
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
public static class DoesOverrideAndReturnsTrue extends DoesNotOverride { |
113 |
public boolean asynchronous () { |
114 |
return true; |
115 |
} |
116 |
} |
117 |
|
118 |
public static final class DoesOverrideAndReturnsFalse extends DoesOverrideAndReturnsTrue { |
119 |
public boolean asynchronous () { |
120 |
return false; |
121 |
} |
122 |
} |
123 |
|
124 |
|
125 |
public static final class Lkp extends AbstractLookup { |
126 |
public Lkp () { |
127 |
this (new org.openide.util.lookup.InstanceContent ()); |
128 |
} |
129 |
|
130 |
private Lkp (org.openide.util.lookup.InstanceContent ic) { |
131 |
super (ic); |
132 |
ic.add (new ErrManager ()); |
133 |
} |
134 |
} |
135 |
|
136 |
private static final class ErrManager extends org.openide.ErrorManager { |
137 |
public static final StringBuffer messages = new StringBuffer (); |
138 |
|
139 |
public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, java.util.Date date) { |
140 |
return t; |
141 |
} |
142 |
|
143 |
public Throwable attachAnnotations (Throwable t, org.openide.ErrorManager.Annotation[] arr) { |
144 |
return t; |
145 |
} |
146 |
|
147 |
public org.openide.ErrorManager.Annotation[] findAnnotations (Throwable t) { |
148 |
return null; |
149 |
} |
150 |
|
151 |
public org.openide.ErrorManager getInstance (String name) { |
152 |
return this; |
153 |
} |
154 |
|
155 |
public void log (int severity, String s) { |
156 |
messages.append (s); |
157 |
messages.append ('\n'); |
158 |
} |
159 |
|
160 |
public void notify (int severity, Throwable t) { |
161 |
} |
162 |
|
163 |
} |
164 |
} |