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.options.keymap;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import junit.framework.TestCase;
053: import org.netbeans.core.options.keymap.api.ShortcutAction;
054: import org.netbeans.core.options.keymap.spi.KeymapManager;
055: import org.openide.util.lookup.Lookups;
056: import org.openide.util.lookup.ProxyLookup;
057:
058: /**
059: *
060: * @author David Strupl
061: */
062: public class KeymapModelTest extends TestCase {
063:
064: static {
065: System.setProperty("org.openide.util.Lookup",
066: KeymapModelTest.GlobalLookup.class.getName());
067: }
068:
069: /**
070: *
071: * @param testName
072: */
073:
074: /**
075: *
076: * @param testName
077: */
078: public KeymapModelTest(String testName) {
079: super (testName);
080: }
081:
082: /**
083: *
084: * @throws java.lang.Exception
085: */
086: protected void setUp() throws Exception {
087: }
088:
089: /**
090: *
091: */
092: public void testGetActionCategories() {
093: KeymapModel instance = new KeymapModel();
094: Set<String> result = instance.getActionCategories();
095: assertTrue("Category C1 should be there", result.contains("C1"));
096: }
097:
098: /**
099: *
100: */
101: public void testGetActions() {
102: String category = "C1";
103: KeymapModel instance = new KeymapModel();
104: Set expResult = null;
105: Set<ShortcutAction> result = instance.getActions(category);
106: assertEquals(result.size(), 1);
107: Iterator<ShortcutAction> it = result.iterator();
108: ShortcutAction sa = it.next();
109: ShortcutAction sa1 = sa.getKeymapManagerInstance("One");
110: assertNotNull(sa1);
111: ShortcutAction sa2 = sa.getKeymapManagerInstance("Two");
112: assertNotNull(sa2);
113: }
114:
115: /**
116: *
117: */
118: public void testRefreshActions() {
119: KeymapModel instance = new KeymapModel();
120: instance.refreshActions();
121: assertTrue(GlobalLookup.km1.refreshActionsCalled);
122: assertTrue(GlobalLookup.km2.refreshActionsCalled);
123: }
124:
125: /**
126: *
127: */
128: public void testGetCurrentProfile() {
129: KeymapModel instance = new KeymapModel();
130: String expResult = "test";
131: String result = instance.getCurrentProfile();
132: assertEquals(expResult, result);
133: }
134:
135: /**
136: *
137: */
138: public void testSetCurrentProfile() {
139: String profile = "test";
140: KeymapModel instance = new KeymapModel();
141:
142: instance.setCurrentProfile(profile);
143: assertTrue(GlobalLookup.km1.setProfileCalled);
144: assertTrue(GlobalLookup.km2.setProfileCalled);
145: }
146:
147: /**
148: *
149: */
150: public void testGetProfiles() {
151: KeymapModel instance = new KeymapModel();
152: List expResult = new ArrayList();
153: expResult.add("test");
154: List result = instance.getProfiles();
155: assertEquals(expResult, result);
156: }
157:
158: /**
159: *
160: */
161: public void testGetKeymap() {
162: String profile = "test";
163: KeymapModel instance = new KeymapModel();
164: Map<ShortcutAction, Set<String>> result = instance
165: .getKeymap(profile);
166: Set<String> s = result.values().iterator().next();
167: assertNotNull(s);
168: assertTrue(s.contains("CS-A"));
169: }
170:
171: /**
172: *
173: */
174: public void testGetKeymapDefaults() {
175: String profile = "test";
176: KeymapModel instance = new KeymapModel();
177: Map expResult = instance.getKeymap(profile);
178: Map result = instance.getKeymapDefaults(profile);
179: assertEquals(expResult, result);
180: }
181:
182: /**
183: *
184: */
185: public void testDeleteProfile() {
186: String profile = "test";
187: KeymapModel instance = new KeymapModel();
188: instance.deleteProfile(profile);
189: assertTrue(GlobalLookup.km1.deleteProfileCalled);
190: assertTrue(GlobalLookup.km2.deleteProfileCalled);
191: }
192:
193: /**
194: *
195: */
196: public void testChangeKeymap() {
197: String profile = "test";
198: Map<ShortcutAction, Set<String>> actionToShortcuts = new HashMap<ShortcutAction, Set<String>>();
199: HashSet<String> hs = new HashSet<String>();
200: hs.add("CS-B");
201: actionToShortcuts.put(GlobalLookup.km1.sa1, hs);
202: KeymapModel instance = new KeymapModel();
203: instance.changeKeymap(profile, actionToShortcuts);
204: Map<ShortcutAction, Set<String>> result = instance
205: .getKeymap(profile);
206: for (Set<String> s : result.values()) {
207: if (s.contains("CS-B")) {
208: return;
209: }
210: }
211: fail("CS-B should have been found");
212: }
213:
214: /**
215: *
216: */
217: public void testMergeActions() {
218: Collection<ShortcutAction> res = new ArrayList<ShortcutAction>();
219: res.add(GlobalLookup.km1.sa1);
220: Collection<ShortcutAction> adding = new ArrayList<ShortcutAction>();
221: adding.add(GlobalLookup.km2.sa2);
222: String name = "Two";
223: KeymapModel instance = new KeymapModel();
224: Set result = instance.mergeActions(res, adding, name);
225: assertEquals(1, result.size());
226: Iterator<ShortcutAction> it = result.iterator();
227: ShortcutAction sa = it.next();
228: ShortcutAction sa2 = sa.getKeymapManagerInstance("Two");
229: assertNotNull(sa2);
230: }
231:
232: public static class GlobalLookup extends ProxyLookup {
233: public static KeymapManager1 km1 = new KeymapManager1();
234: public static KeymapManager2 km2 = new KeymapManager2();
235:
236: public GlobalLookup() {
237: super (Lookups.fixed(km1, km2));
238: }
239: }
240:
241: static class KeymapManager1 extends KeymapManager {
242: public ShortcutAction sa1 = new ShortcutAction() {
243: public String getDisplayName() {
244: return "Action 1";
245: }
246:
247: public String getId() {
248: return "A1";
249: }
250:
251: public String getDelegatingActionId() {
252: return null;
253: }
254:
255: public ShortcutAction getKeymapManagerInstance(
256: String keymapManagerName) {
257: if ("One".equals(keymapManagerName)) {
258: return this ;
259: }
260: return null;
261: }
262: };
263: public boolean deleteProfileCalled = false;
264: public boolean setProfileCalled = false;
265: public boolean refreshActionsCalled = false;
266:
267: public KeymapManager1() {
268: super ("One");
269: }
270:
271: public Map<String, Set<ShortcutAction>> getActions() {
272: Map<String, Set<ShortcutAction>> m = new HashMap<String, Set<ShortcutAction>>();
273: HashSet<ShortcutAction> h = new HashSet<ShortcutAction>();
274: h.add(sa1);
275: m.put("C1", h);
276: return m;
277: }
278:
279: public void refreshActions() {
280: refreshActionsCalled = true;
281: }
282:
283: public Map<ShortcutAction, Set<String>> getKeymap(
284: String profileName) {
285: Map<ShortcutAction, Set<String>> m = new HashMap<ShortcutAction, Set<String>>();
286: Set<String> ss = new HashSet<String>();
287: ss.add("CS-A");
288: m.put(sa1, ss);
289: return m;
290: }
291:
292: public void saveKeymap(String profileName,
293: Map<ShortcutAction, Set<String>> actionToShortcuts) {
294: }
295:
296: public List<String> getProfiles() {
297: List<String> al = new ArrayList<String>();
298: al.add("test");
299: return al;
300: }
301:
302: public String getCurrentProfile() {
303: return "test";
304: }
305:
306: public void setCurrentProfile(String profileName) {
307: setProfileCalled = true;
308: }
309:
310: public void deleteProfile(String profileName) {
311: deleteProfileCalled = true;
312: }
313:
314: public boolean isCustomProfile(String profileName) {
315: return false;
316: }
317:
318: public Map<ShortcutAction, Set<String>> getDefaultKeymap(
319: String profileName) {
320: return getKeymap(profileName);
321: }
322: }
323:
324: private static class KeymapManager2 extends KeymapManager {
325: public boolean deleteProfileCalled = false;
326: public boolean setProfileCalled = false;
327: public boolean refreshActionsCalled = false;
328: public ShortcutAction sa2 = new ShortcutAction() {
329: public String getDisplayName() {
330: return "Action 2";
331: }
332:
333: public String getId() {
334: return "A2";
335: }
336:
337: public String getDelegatingActionId() {
338: return "A1";
339: }
340:
341: public ShortcutAction getKeymapManagerInstance(
342: String keymapManagerName) {
343: if ("Two".equals(keymapManagerName)) {
344: return this ;
345: }
346: return null;
347: }
348: };
349:
350: public KeymapManager2() {
351: super ("Two");
352: }
353:
354: public Map<String, Set<ShortcutAction>> getActions() {
355: Map<String, Set<ShortcutAction>> m = new HashMap<String, Set<ShortcutAction>>();
356: HashSet<ShortcutAction> h = new HashSet<ShortcutAction>();
357: h.add(sa2);
358: m.put("C1", h);
359: return m;
360: }
361:
362: public void refreshActions() {
363: refreshActionsCalled = true;
364: }
365:
366: public Map<ShortcutAction, Set<String>> getKeymap(
367: String profileName) {
368: Map<ShortcutAction, Set<String>> m = new HashMap<ShortcutAction, Set<String>>();
369: return m;
370: }
371:
372: public void saveKeymap(String profileName,
373: Map<ShortcutAction, Set<String>> actionToShortcuts) {
374: }
375:
376: public List<String> getProfiles() {
377: return null;
378: }
379:
380: public String getCurrentProfile() {
381: return null;
382: }
383:
384: public void setCurrentProfile(String profileName) {
385: setProfileCalled = true;
386: }
387:
388: public void deleteProfile(String profileName) {
389: deleteProfileCalled = true;
390: }
391:
392: public boolean isCustomProfile(String profileName) {
393: return false;
394: }
395:
396: public Map<ShortcutAction, Set<String>> getDefaultKeymap(
397: String profileName) {
398: return getKeymap(profileName);
399: }
400: }
401: }
|