Line 0
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.netbeans.modules.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.awt.ActionRegistration; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.filesystems.FileSystem; |
54 |
import org.openide.filesystems.Repository; |
55 |
import org.openide.util.ContextAwareAction; |
56 |
import org.openide.util.Lookup; |
57 |
|
58 |
/** |
59 |
* |
60 |
* @author Jaroslav Tulach |
61 |
*/ |
62 |
public class ActionRegistrationProcessorTest extends NbTestCase implements PropertyChangeListener { |
63 |
private FileObject folder; |
64 |
private int changeCounter; |
65 |
|
66 |
public ActionRegistrationProcessorTest(String testName) { |
67 |
super(testName); |
68 |
} |
69 |
|
70 |
@Override |
71 |
protected void setUp() throws Exception { |
72 |
FileSystem fs = Repository.getDefault().getDefaultFileSystem(); |
73 |
folder = fs.findResource("Toolbar/Edit/"); |
74 |
assertNotNull("testing layer is loaded: ", folder); |
75 |
|
76 |
myListenerCalled = 0; |
77 |
myListenerCounter = 0; |
78 |
MyAction.last = null; |
79 |
} |
80 |
|
81 |
@Override |
82 |
protected boolean runInEQ() { |
83 |
return true; |
84 |
} |
85 |
|
86 |
public void testIconIsCorrect() throws Exception { |
87 |
// XXX test in no way matches what is being actually registered, rewrite... |
88 |
myListenerCounter = 0; |
89 |
Action a = readAction("testIconIsCorrect.instance"); |
90 |
|
91 |
assertNotNull("Action created", a); |
92 |
assertEquals("No myListener called", 0, myListenerCounter); |
93 |
|
94 |
Object name = a.getValue(a.NAME); |
95 |
Object mnem = a.getValue(a.MNEMONIC_KEY); |
96 |
Object smallIcon = a.getValue(a.SMALL_ICON); |
97 |
//Object icon = a.getValue(a.ICON) |
98 |
|
99 |
assertEquals("Right localized name", "Icon &Name Action", name); |
100 |
assertEquals("Mnemonic is N", Character.valueOf('N'), mnem); |
101 |
assertNotNull("small icon present", smallIcon); |
102 |
|
103 |
|
104 |
assertTrue("Always enabled", a.isEnabled()); |
105 |
a.setEnabled(false); |
106 |
assertTrue("Still Always enabled", a.isEnabled()); |
107 |
|
108 |
a.actionPerformed(new ActionEvent(this, 0, "kuk")); |
109 |
|
110 |
assertEquals("Listener invoked", 1, myListenerCounter); |
111 |
|
112 |
|
113 |
assertEquals("No icon in menu", Boolean.TRUE, a.getValue("noIconInMenu")); |
114 |
} |
115 |
|
116 |
|
117 |
private static int myListenerCounter; |
118 |
private static int myListenerCalled; |
119 |
@ActionRegistration(category="Testing", displayName="#Always", |
120 |
menus=@ActionRegistration.Menu(path="Edit", position=30), |
121 |
toolbars=@ActionRegistration.Toolbar(path="Edit", position=30), |
122 |
shortcuts=@ActionRegistration.Shortcut(key="D-X")) |
123 |
private static Action myAction() { |
124 |
myListenerCounter++; |
125 |
return new MyAction(); |
126 |
} |
127 |
|
128 |
private Action readAction(String fileName) throws Exception { |
129 |
FileObject fo = this.folder.getFileObject(fileName); |
130 |
assertNotNull("file " + fileName, fo); |
131 |
|
132 |
Object obj = fo.getAttribute("instanceCreate"); |
133 |
assertNotNull("File object has not null instanceCreate attribute", obj); |
134 |
|
135 |
if (!(obj instanceof Action)) { |
136 |
fail("Object needs to be action: " + obj); |
137 |
} |
138 |
|
139 |
return (Action)obj; |
140 |
} |
141 |
|
142 |
public void propertyChange(PropertyChangeEvent evt) { |
143 |
changeCounter++; |
144 |
} |
145 |
|
146 |
private static class MyListener implements ActionListener { |
147 |
public void actionPerformed(ActionEvent e) { |
148 |
myListenerCalled++; |
149 |
} |
150 |
} |
151 |
private static class MyAction extends AbstractAction { |
152 |
static MyAction last; |
153 |
|
154 |
MyAction() { |
155 |
last = this; |
156 |
setEnabled(false); |
157 |
} |
158 |
|
159 |
public void actionPerformed(ActionEvent e) { |
160 |
myListenerCalled++; |
161 |
} |
162 |
} |
163 |
private static class MyContextAction extends MyAction |
164 |
implements ContextAwareAction { |
165 |
static int clones; |
166 |
static Lookup lkp; |
167 |
|
168 |
public Action createContextAwareInstance(Lookup actionContext) { |
169 |
clones++; |
170 |
lkp = actionContext; |
171 |
return new MyContextAction(); |
172 |
} |
173 |
} |
174 |
|
175 |
} |