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.Arrays;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.Set;
051: import javax.swing.Action;
052: import javax.swing.text.TextAction;
053: import org.netbeans.core.options.keymap.api.ShortcutAction;
054: import org.netbeans.junit.NbTestCase;
055:
056: /**
057: *
058: * @author Jan Jancura
059: */
060: public class KeymapViewModelTest extends NbTestCase {
061:
062: /**
063: *
064: * @param testName
065: */
066: public KeymapViewModelTest(String testName) {
067: super (testName);
068: }
069:
070: /**
071: *
072: */
073: public void testCancelCurrentProfile() {
074: KeymapViewModel model = new KeymapViewModel();
075: String currentProfile = model.getCurrentProfile();
076: model.setCurrentProfile("XXX");
077: assertEquals("XXX", model.getCurrentProfile());
078: model.cancel();
079: assertEquals(currentProfile, model.getCurrentProfile());
080: assertEquals(currentProfile, new KeymapViewModel()
081: .getCurrentProfile());
082: }
083:
084: /**
085: *
086: */
087: public void testOkCurrentProfile() {
088: KeymapViewModel model = new KeymapViewModel();
089: String currentProfile = model.getCurrentProfile();
090: model.setCurrentProfile("XXX");
091: assertEquals("XXX", model.getCurrentProfile());
092: assertEquals(currentProfile, new KeymapViewModel()
093: .getCurrentProfile());
094: model.apply();
095: assertEquals("XXX", model.getCurrentProfile());
096: // TODO: this no longer works:
097: // assertEquals ("XXX", new KeymapViewModel ().getCurrentProfile ());
098: }
099:
100: /**
101: *
102: */
103: public void testChangeShortcuts() {
104: KeymapViewModel model = new KeymapViewModel();
105: forAllActions(model, new R() {
106: public void run(KeymapViewModel model, ShortcutAction action) {
107: model.setShortcuts(action, Collections.EMPTY_SET);
108: }
109: });
110: forAllActions(model, new R() {
111: public void run(KeymapViewModel model, ShortcutAction action) {
112: assertEquals(0, model.getShortcuts(action).length);
113: }
114: });
115: final Set set = Collections.singleton("Alt+K");
116: forAllActions(model, new R() {
117: public void run(KeymapViewModel model, ShortcutAction action) {
118: model.setShortcuts(action, set);
119: }
120: });
121: forAllActions(model, new R() {
122: public void run(KeymapViewModel model, ShortcutAction action) {
123: String[] s = model.getShortcuts(action);
124: assertEquals(1, s.length);
125: assertEquals("Alt+K", s[0]);
126: }
127: });
128: }
129:
130: /**
131: *
132: */
133: public void testChangeShortcutsOk() {
134: KeymapViewModel model = new KeymapViewModel();
135: Map shortcuts = setRandomShortcuts(model);
136: System.out.println("apply changes");
137: model.apply();
138: System.gc();
139: model.apply();
140: System.gc();
141: checkShortcuts(model, shortcuts, true);
142: checkShortcuts(new KeymapViewModel(), shortcuts, true);
143: }
144:
145: /**
146: *
147: */
148: public void testChangeShortcutsCancel() {
149: KeymapViewModel model = new KeymapViewModel();
150: Map shortcuts = getShortcuts(model);
151: Map shortcuts2 = setRandomShortcuts(model);
152: checkShortcuts(model, shortcuts2, false);
153: System.out.println("cancel changes");
154: model.cancel();
155: checkShortcuts(model, shortcuts, false);
156: checkShortcuts(new KeymapViewModel(), shortcuts, false);
157: }
158:
159: /**
160: * Sets random shortcuts and returns them in
161: * Map (Set (String (shortcut)) > String (action name)).
162: */
163: private Map setRandomShortcuts(final KeymapViewModel model) {
164: final int[] ii = { 1 };
165: final Map result = new HashMap();
166: System.out.println("set random shortcuts");
167: forAllActions(model, new R() {
168: public void run(KeymapViewModel model, ShortcutAction action) {
169: String shortcut = Integer.toString(ii[0], 36)
170: .toUpperCase();
171: StringBuffer sb = new StringBuffer();
172: int i, k = shortcut.length();
173: for (i = 0; i < k; i++)
174: sb.append(shortcut.charAt(i)).append(' ');
175: shortcut = sb.toString().trim();
176: Set s = Collections.singleton(shortcut);
177: model.setShortcuts(action, s);
178: result.put(s, action);
179: //System.out.println (s + " : " + action);
180: ii[0]++;
181: }
182: });
183: return result;
184: }
185:
186: /**
187: * Returns Map (Set (String (shortcut)) > String (action name)) containing
188: * all current shortcuts.
189: */
190: private Map getShortcuts(final KeymapViewModel model) {
191: final Map result = new HashMap();
192: System.out.println("get shortcuts");
193: forAllActions(model, new R() {
194: public void run(KeymapViewModel model, ShortcutAction action) {
195: String[] sh = model.getShortcuts(action);
196: if (sh.length == 0)
197: return;
198: Set shortcuts = new HashSet(Arrays.asList(sh));
199: //System.out.println("sh: " + shortcuts + " : " + action);
200: assertFalse("Same shortcuts assigned to two actions ",
201: result.containsKey(shortcuts));
202: result.put(shortcuts, action);
203: }
204: });
205: return result;
206: }
207:
208: private static String getName(Object action) {
209: if (action instanceof TextAction)
210: return (String) ((TextAction) action)
211: .getValue(Action.SHORT_DESCRIPTION);
212: if (action instanceof Action)
213: return (String) ((Action) action).getValue(Action.NAME);
214: return action.toString();
215: }
216:
217: private void checkShortcuts(final KeymapViewModel model,
218: final Map shortcuts, final boolean print) {
219: System.out.println("check shortcuts");
220: final Map localCopy = new HashMap(shortcuts);
221: forAllActions(model, new R() {
222: public void run(KeymapViewModel model, ShortcutAction action) {
223: String[] sh = model.getShortcuts(action);
224: if (sh.length == 0)
225: return;
226: Set s = new HashSet(Arrays.asList(sh));
227: if (print)
228: System.out.println(s + " : " + action + " : "
229: + localCopy.get(s));
230: assertEquals("Shortcut changed: " + s + " : " + action,
231: localCopy.get(s), action);
232: localCopy.remove(s);
233: }
234: });
235: assertTrue("Some shortcuts found: " + localCopy, localCopy
236: .isEmpty());
237: }
238:
239: private void forAllActions(KeymapViewModel model, R r) {
240: forAllActions(model, r, "");
241: }
242:
243: private void forAllActions(KeymapViewModel model, R r, String folder) {
244: Iterator it = model.getItems(folder).iterator();
245: while (it.hasNext()) {
246: Object o = it.next();
247: if (o instanceof String)
248: forAllActions(model, r, (String) o);
249: else
250: r.run(model, (ShortcutAction) o);
251: }
252: }
253:
254: interface R {
255: void run(KeymapViewModel model, ShortcutAction action);
256: }
257: }
|