KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > KeymapViewModelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.keymap;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLStreamHandler JavaDoc;
26 import java.net.URLStreamHandlerFactory JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import javax.swing.Action JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.text.TextAction JavaDoc;
39 import org.netbeans.junit.Manager;
40 import org.netbeans.junit.NbTestCase;
41 import org.netbeans.modules.options.editor.IDEInitializer;
42 import org.netbeans.spi.options.OptionsCategory;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.filesystems.Repository;
46 import org.openide.loaders.DataFolder;
47 import org.openide.loaders.FolderLookup;
48 import org.openide.util.Lookup;
49
50 import org.netbeans.modules.options.macros.MacrosPanelController;
51 import org.openide.util.lookup.Lookups;
52 import org.openide.util.lookup.ProxyLookup;
53
54
55 /**
56  *
57  * @author Jan Jancura
58  */

59 public class KeymapViewModelTest extends NbTestCase {
60     
61     static {
62         IDEInitializer.setup (
63             new String JavaDoc[] {
64                 "org/netbeans/modules/options/editor/mf-layer.xml",
65                 "org/netbeans/modules/defaults/mf-layer.xml",
66                 "org/netbeans/core/ui/resources/layer.xml"
67             },
68             new Object JavaDoc[] {}
69         );
70     }
71     
72     public KeymapViewModelTest (String JavaDoc testName) {
73         super (testName);
74     }
75     
76     public void testCancelCurrentProfile () {
77         IDEInitializer.cleanWorkDir ();
78         KeymapViewModel model = new KeymapViewModel ();
79         String JavaDoc currentProfile = model.getCurrentProfile ();
80         model.setCurrentProfile ("XXX");
81         assertEquals ("XXX", model.getCurrentProfile ());
82         model.cancel ();
83         assertEquals (currentProfile, model.getCurrentProfile ());
84         assertEquals (currentProfile, new KeymapViewModel ().getCurrentProfile ());
85     }
86     
87     public void testOkCurrentProfile () {
88         IDEInitializer.cleanWorkDir ();
89         KeymapViewModel model = new KeymapViewModel ();
90         String JavaDoc currentProfile = model.getCurrentProfile ();
91         model.setCurrentProfile ("XXX");
92         assertEquals ("XXX", model.getCurrentProfile ());
93         assertEquals (currentProfile, new KeymapViewModel ().getCurrentProfile ());
94         model.apply ();
95         assertEquals ("XXX", model.getCurrentProfile ());
96         assertEquals ("XXX", new KeymapViewModel ().getCurrentProfile ());
97     }
98     
99     public void testChangeShortcuts () {
100         IDEInitializer.cleanWorkDir ();
101         KeymapViewModel model = new KeymapViewModel ();
102         forAllActions (model, new R () {
103             public void run (KeymapViewModel model, ActionImpl action) {
104                 model.setShortcuts (action, Collections.EMPTY_SET);
105             }
106         });
107         forAllActions (model, new R () {
108             public void run (KeymapViewModel model, ActionImpl action) {
109                 assertEquals (0, model.getShortcuts (action).length);
110             }
111         });
112         final Set JavaDoc set = Collections.singleton ("Alt+K");
113         forAllActions (model, new R () {
114             public void run (KeymapViewModel model, ActionImpl action) {
115                 model.setShortcuts (action, set);
116             }
117         });
118         forAllActions (model, new R () {
119             public void run (KeymapViewModel model, ActionImpl action) {
120                 String JavaDoc[] s = model.getShortcuts (action);
121                 assertEquals (1, s.length);
122                 assertEquals ("Alt+K", s [0]);
123             }
124         });
125     }
126     
127     public void testChangeShortcutsOk () {
128         IDEInitializer.cleanWorkDir ();
129         KeymapViewModel model = new KeymapViewModel ();
130         Map JavaDoc shortcuts = setRandomShortcuts (model);
131         System.out.println ("apply changes");
132         model.apply ();
133         System.gc ();
134         model.apply ();
135         System.gc ();
136         checkShortcuts (model, shortcuts, true);
137         checkShortcuts (new KeymapViewModel (), shortcuts, true);
138     }
139     
140     public void testChangeShortcutsCancel () {
141         IDEInitializer.cleanWorkDir ();
142         KeymapViewModel model = new KeymapViewModel ();
143         Map JavaDoc shortcuts = getShortcuts (model);
144         Map JavaDoc shortcuts2 = setRandomShortcuts (model);
145         checkShortcuts (model, shortcuts2, false);
146         System.out.println ("cancel changes");
147         model.cancel ();
148         checkShortcuts (model, shortcuts, false);
149         checkShortcuts (new KeymapViewModel (), shortcuts, false);
150     }
151     
152     /**
153      * Sets random shortcuts and returns them in
154      * Map (Set (String (shortcut)) > String (action name)).
155      */

156     private Map JavaDoc setRandomShortcuts (final KeymapViewModel model) {
157         final int[] ii = {1};
158         final Map JavaDoc result = new HashMap JavaDoc ();
159         System.out.println("set random shortcuts");
160         forAllActions (model, new R () {
161             public void run (KeymapViewModel model, ActionImpl action) {
162                 String JavaDoc shortcut = Integer.toString (ii [0], 36).toUpperCase ();
163                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
164                 int i, k = shortcut.length ();
165                 for (i = 0; i < k; i++)
166                     sb.append (shortcut.charAt (i)).append (' ');
167                 shortcut = sb.toString ().trim ();
168                 Set JavaDoc s = Collections.singleton (shortcut);
169                 model.setShortcuts (action, s);
170                 result.put (s, action);
171                 //System.out.println (s + " : " + action);
172
ii [0] ++;
173             }
174         });
175         return result;
176     }
177     
178     /**
179      * Returns Map (Set (String (shortcut)) > String (action name)) containing
180      * all current shortcuts.
181      */

182     private Map JavaDoc getShortcuts (final KeymapViewModel model) {
183         final Map JavaDoc result = new HashMap JavaDoc ();
184         System.out.println("get shortcuts");
185         forAllActions (model, new R () {
186             public void run (KeymapViewModel model, ActionImpl action) {
187                 String JavaDoc[] sh = model.getShortcuts (action);
188                 if (sh.length == 0) return;
189                 Set JavaDoc shortcuts = new HashSet JavaDoc (Arrays.asList (sh));
190                 //System.out.println("sh: " + shortcuts + " : " + action);
191
assertFalse ("Same shortcuts assigned to two actions ", result.containsKey (shortcuts));
192                 result.put (shortcuts, action);
193             }
194         });
195         return result;
196     }
197     
198     private static String JavaDoc getName (Object JavaDoc action) {
199         if (action instanceof TextAction JavaDoc)
200             return (String JavaDoc) ((TextAction JavaDoc) action).getValue (Action.SHORT_DESCRIPTION);
201         if (action instanceof Action JavaDoc)
202             return (String JavaDoc) ((Action JavaDoc) action).getValue (Action.NAME);
203         return action.toString ();
204     }
205     
206     private void checkShortcuts (final KeymapViewModel model, final Map JavaDoc shortcuts, final boolean print) {
207         System.out.println("check shortcuts");
208         final Map JavaDoc localCopy = new HashMap JavaDoc (shortcuts);
209         forAllActions (model, new R () {
210             public void run (KeymapViewModel model, ActionImpl action) {
211                 String JavaDoc[] sh = model.getShortcuts (action);
212                 if (sh.length == 0) return;
213                 Set JavaDoc s = new HashSet JavaDoc (Arrays.asList (sh));
214                 if (print)
215                     System.out.println (s + " : " + action + " : " + localCopy.get (s));
216                 assertEquals ("Shortcut changed: " + s + " : " + action, localCopy.get (s), action);
217                 localCopy.remove (s);
218             }
219         });
220         assertTrue ("Some shortcuts found: " + localCopy, localCopy.isEmpty ());
221     }
222     
223 // public void testChangeShortcutsCancel () {
224
// KeymapViewModel model = new KeymapViewModel ();
225
// final Set set = Collections.singleton ("Alt+K");
226
// forAllActions (model, new R () {
227
// public void run (KeymapViewModel model, Object action) {
228
// model.setShortcuts (action, set);
229
// }
230
// });
231
// model.apply ();
232
// final Set set1 = Collections.singleton ("Ctrl+L");
233
// forAllActions (model, new R () {
234
// public void run (KeymapViewModel model, Object action) {
235
// model.setShortcuts (action, set);
236
// }
237
// });
238
// forAllActions (model, new R () {
239
// public void run (KeymapViewModel model, Object action) {
240
// String[] s = model.getShortcuts (action);
241
// assertEquals (1, s.length);
242
// assertEquals ("Alt+L", s [0]);
243
// }
244
// });
245
// model.cancel ();
246
// forAllActions (model, new R () {
247
// public void run (KeymapViewModel model, Object action) {
248
// String[] s = model.getShortcuts (action);
249
// assertEquals (1, s.length);
250
// assertEquals ("Alt+K", s [0]);
251
// }
252
// });
253
// }
254

255     private void forAllActions (KeymapViewModel model, R r) {
256         forAllActions (model, r, "");
257     }
258     
259     private void forAllActions (KeymapViewModel model, R r, String JavaDoc folder) {
260         Iterator JavaDoc it = model.getItems (folder).iterator ();
261         while (it.hasNext ()) {
262             Object JavaDoc o = it.next ();
263             if (o instanceof String JavaDoc)
264                 forAllActions (model, r, (String JavaDoc) o);
265             else
266                 r.run (model, (ActionImpl) o);
267         }
268     }
269     
270     interface R {
271         void run (KeymapViewModel model, ActionImpl action);
272     }
273 }
274
275
276
Popular Tags