Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.openide.awt; |
43 |
|
44 |
import java.awt.event.ActionEvent; |
45 |
import java.awt.event.ActionListener; |
46 |
import java.beans.PropertyChangeEvent; |
47 |
import java.beans.PropertyChangeListener; |
48 |
import javax.swing.AbstractAction; |
49 |
import javax.swing.Action; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.filesystems.FileObject; |
52 |
import org.openide.filesystems.FileSystem; |
53 |
import org.openide.filesystems.Repository; |
54 |
import org.openide.util.ContextAwareAction; |
55 |
import org.openide.util.Lookup; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Jaroslav Tulach |
60 |
*/ |
61 |
public class AnnotationActionTest extends NbTestCase implements PropertyChangeListener { |
62 |
private FileObject folder; |
63 |
private int changeCounter; |
64 |
|
65 |
public AnnotationActionTest(String testName) { |
66 |
super(testName); |
67 |
} |
68 |
|
69 |
@Override |
70 |
protected void setUp() throws Exception { |
71 |
FileSystem fs = Repository.getDefault().getDefaultFileSystem(); |
72 |
folder = fs.findResource("Toolbar/Edit/"); |
73 |
assertNotNull("testing layer is loaded: ", folder); |
74 |
|
75 |
myListenerCalled = 0; |
76 |
myListenerCounter = 0; |
77 |
MyAction.last = null; |
78 |
} |
79 |
|
80 |
@Override |
81 |
protected boolean runInEQ() { |
82 |
return true; |
83 |
} |
84 |
|
85 |
public void testIconIsCorrect() throws Exception { |
86 |
myListenerCounter = 0; |
87 |
Action a = readAction("testIconIsCorrect.instance"); |
88 |
|
89 |
assertNotNull("Action created", a); |
90 |
assertEquals("No myListener called", 0, myListenerCounter); |
91 |
|
92 |
Object name = a.getValue(a.NAME); |
93 |
Object mnem = a.getValue(a.MNEMONIC_KEY); |
94 |
Object smallIcon = a.getValue(a.SMALL_ICON); |
95 |
//Object icon = a.getValue(a.ICON) |
96 |
|
97 |
assertEquals("Right localized name", "Icon &Name Action", name); |
98 |
assertEquals("Mnemonic is N", Character.valueOf('N'), mnem); |
99 |
assertNotNull("small icon present", smallIcon); |
100 |
|
101 |
|
102 |
assertTrue("Always enabled", a.isEnabled()); |
103 |
a.setEnabled(false); |
104 |
assertTrue("Still Always enabled", a.isEnabled()); |
105 |
|
106 |
a.actionPerformed(new ActionEvent(this, 0, "kuk")); |
107 |
|
108 |
assertEquals("Listener invoked", 1, myListenerCounter); |
109 |
|
110 |
|
111 |
assertEquals("No icon in menu", Boolean.TRUE, a.getValue("noIconInMenu")); |
112 |
} |
113 |
|
114 |
|
115 |
private static int myListenerCounter; |
116 |
private static int myListenerCalled; |
117 |
@ActionRegistration(category="Testing", displayName="#Always") |
118 |
@ActionRegistration.Menu(path="Edit", position=30) |
119 |
@ActionRegistration.Toolbar(path="Edit", position=30) |
120 |
@ActionRegistration.Shortcut(key="D-X") |
121 |
private static ActionListener myListener() { |
122 |
myListenerCounter++; |
123 |
return new MyListener(); |
124 |
} |
125 |
|
126 |
private Action readAction(String fileName) throws Exception { |
127 |
FileObject fo = this.folder.getFileObject(fileName); |
128 |
assertNotNull("file " + fileName, fo); |
129 |
|
130 |
Object obj = fo.getAttribute("instanceCreate"); |
131 |
assertNotNull("File object has not null instanceCreate attribute", obj); |
132 |
|
133 |
if (!(obj instanceof Action)) { |
134 |
fail("Object needs to be action: " + obj); |
135 |
} |
136 |
|
137 |
return (Action)obj; |
138 |
} |
139 |
|
140 |
public void propertyChange(PropertyChangeEvent evt) { |
141 |
changeCounter++; |
142 |
} |
143 |
|
144 |
private static class MyListener implements ActionListener { |
145 |
public void actionPerformed(ActionEvent e) { |
146 |
myListenerCalled++; |
147 |
} |
148 |
} |
149 |
private static class MyAction extends AbstractAction { |
150 |
static MyAction last; |
151 |
|
152 |
MyAction() { |
153 |
last = this; |
154 |
setEnabled(false); |
155 |
} |
156 |
|
157 |
public void actionPerformed(ActionEvent e) { |
158 |
myListenerCalled++; |
159 |
} |
160 |
} |
161 |
private static class MyContextAction extends MyAction |
162 |
implements ContextAwareAction { |
163 |
static int clones; |
164 |
static Lookup lkp; |
165 |
|
166 |
public Action createContextAwareInstance(Lookup actionContext) { |
167 |
clones++; |
168 |
lkp = actionContext; |
169 |
return new MyContextAction(); |
170 |
} |
171 |
} |
172 |
|
173 |
} |