001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui.actions;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import javax.swing.Action;
047: import javax.swing.KeyStroke;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.junit.MockServices;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.spi.project.ActionProvider;
053: import org.netbeans.spi.project.ui.support.ProjectActionPerformer;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.loaders.DataObject;
057: import org.openide.util.Lookup;
058: import org.openide.util.lookup.Lookups;
059:
060: public class ProjectActionTest extends NbTestCase {
061:
062: public ProjectActionTest(String name) {
063: super (name);
064: }
065:
066: private FileObject p1;
067: private FileObject p2;
068: private FileObject f1_1;
069: private FileObject f1_2;
070: private FileObject f2_1;
071: private FileObject f2_2;
072: private DataObject d1_1;
073: private DataObject d1_2;
074: private DataObject d2_1;
075: private DataObject d2_2;
076: private TestSupport.TestProject project1;
077: private TestSupport.TestProject project2;
078:
079: protected void setUp() throws Exception {
080: super .setUp();
081: MockServices.setServices(TestSupport.TestProjectFactory.class);
082: clearWorkDir();
083: FileObject workDir = FileUtil.toFileObject(getWorkDir());
084:
085: p1 = TestSupport.createTestProject(workDir, "project1");
086: f1_1 = p1.createData("f1_1.java");
087: f1_2 = p1.createData("f1_2.krava");
088: d1_1 = DataObject.find(f1_1);
089: d1_2 = DataObject.find(f1_2);
090:
091: project1 = (TestSupport.TestProject) ProjectManager
092: .getDefault().findProject(p1);
093: project1.setLookup(Lookups
094: .fixed(new Object[] { new TestActionProvider() }));
095:
096: p2 = TestSupport.createTestProject(workDir, "project2");
097: f2_1 = p2.createData("f2_1.java");
098: f2_2 = p2.createData("f2_2.krava");
099: d2_1 = DataObject.find(f2_1);
100: d2_2 = DataObject.find(f2_2);
101:
102: project2 = (TestSupport.TestProject) ProjectManager
103: .getDefault().findProject(p2);
104: }
105:
106: protected void tearDown() throws Exception {
107: clearWorkDir();
108: super .tearDown();
109: }
110:
111: public boolean runInEQ() {
112: return true;
113: }
114:
115: public void testCommandEnablement() {
116: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
117: ProjectAction action = new ProjectAction("COMMAND",
118: "TestProjectAction", null, lookup);
119:
120: assertFalse("Action should NOT be enabled", action.isEnabled());
121:
122: lookup.change(d1_1);
123: assertTrue("Action should be enabled", action.isEnabled());
124:
125: lookup.change(d1_1, d1_2);
126: assertFalse("Action should NOT be enabled", action.isEnabled());
127:
128: lookup.change(d1_1, d2_1);
129: assertFalse("Action should NOT be enabled", action.isEnabled());
130:
131: }
132:
133: public void testProviderEnablement() {
134: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
135: TestActionPerformer tap = new TestActionPerformer();
136: ProjectAction action = new ProjectAction(tap,
137: "TestProjectAction", null, lookup);
138:
139: assertFalse("Action should NOT be enabled", action.isEnabled());
140: assertEquals("enable() should NOT be called: ", 0,
141: tap.enableCount);
142:
143: tap.clear();
144: tap.on(true);
145: assertFalse("Action should NOT be enabled", action.isEnabled());
146: assertEquals("enable() should NOT be called: ", 0,
147: tap.enableCount);
148:
149: tap.clear();
150: tap.on(false);
151: lookup.change(d1_1);
152: assertFalse("Action should NOT be enabled", action.isEnabled());
153: assertEquals("enable() should be called once: ", 1,
154: tap.enableCount);
155: assertEquals("enable() should be called on right project: ",
156: project1.toString(), tap.project.toString());
157:
158: tap.clear();
159: tap.on(true);
160: lookup.change(d1_2);
161: assertTrue("Action should be enabled", action.isEnabled());
162: assertEquals("enable() should be called once: ", 1,
163: tap.enableCount);
164: assertEquals("enable() should be called on right project: ",
165: project1.toString(), tap.project.toString());
166:
167: tap.clear();
168: tap.on(false);
169: lookup.change(d1_1, d2_1);
170: assertFalse("Action should NOT be enabled", action.isEnabled());
171: assertEquals("enable() should NOT be called: ", 0,
172: tap.enableCount);
173:
174: }
175:
176: public void testAcceleratorsPropagated() {
177: doTestAcceleratorsPropagated(new ActionCreator() {
178: public ProjectAction create(Lookup l) {
179: return new ProjectAction("command",
180: "TestProjectAction", null, l);
181: }
182: }, true);
183: }
184:
185: public static void doTestAcceleratorsPropagated(
186: ActionCreator creator, boolean testMenus) {
187: Lookup l1 = Lookups.fixed(new Object[] { "1" });
188: Lookup l2 = Lookups.fixed(new Object[] { "2" });
189:
190: ProjectAction a1 = creator.create(l1);
191:
192: KeyStroke k1 = KeyStroke.getKeyStroke("shift pressed A");
193: KeyStroke k2 = KeyStroke.getKeyStroke("shift pressed A");
194:
195: assertNotNull(k1);
196: assertNotNull(k2);
197:
198: a1.putValue(Action.ACCELERATOR_KEY, k1);
199:
200: ProjectAction a2 = creator.create(l2);
201:
202: assertEquals(k1, a2.getValue(Action.ACCELERATOR_KEY));
203:
204: a2.putValue(Action.ACCELERATOR_KEY, k2);
205:
206: assertEquals(k2, a1.getValue(Action.ACCELERATOR_KEY));
207:
208: if (testMenus) {
209: assertEquals(k2, a2.getMenuPresenter().getAccelerator());
210: }
211:
212: a1.putValue(Action.ACCELERATOR_KEY, k1);
213: assertEquals(k1, a2.getValue(Action.ACCELERATOR_KEY));
214:
215: if (testMenus) {
216: assertEquals(k1, a2.getMenuPresenter().getAccelerator());
217: }
218: }
219:
220: public static interface ActionCreator {
221: public ProjectAction create(Lookup l);
222: }
223:
224: private static class TestActionProvider implements ActionProvider {
225:
226: public String COMMAND = "COMMAND";
227:
228: private String[] ACTIONS = new String[] { COMMAND };
229:
230: private List<String> invocations = new ArrayList<String>();
231:
232: public String[] getSupportedActions() {
233: return ACTIONS;
234: }
235:
236: public void invokeAction(String command, Lookup context)
237: throws IllegalArgumentException {
238:
239: if (COMMAND.equals(command)) {
240: invocations.add(command);
241: } else {
242: throw new IllegalArgumentException();
243: }
244:
245: }
246:
247: public boolean isActionEnabled(String command, Lookup context)
248: throws IllegalArgumentException {
249:
250: if (COMMAND.equals(command)) {
251: for (DataObject dobj : context
252: .lookupAll(DataObject.class)) {
253: if (!dobj.getPrimaryFile().getNameExt().endsWith(
254: ".java")) {
255: return false;
256: }
257: }
258: return true;
259: } else {
260: throw new IllegalArgumentException();
261: }
262:
263: }
264:
265: }
266:
267: private static class TestActionPerformer implements
268: ProjectActionPerformer {
269:
270: private int enableCount;
271: private Project project;
272: private boolean on;
273:
274: public boolean enable(Project project) {
275: enableCount++;
276: this .project = project;
277: return on;
278: }
279:
280: public void perform(Project project) {
281: this .project = project;
282: }
283:
284: public void on(boolean on) {
285: this .on = on;
286: }
287:
288: public void clear() {
289: this .project = null;
290: enableCount = 0;
291: }
292:
293: }
294:
295: }
|