1 19 20 package org.netbeans.modules.options.keymap; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.net.URLStreamHandler ; 26 import java.net.URLStreamHandlerFactory ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import javax.swing.Action ; 37 import javax.swing.JComponent ; 38 import javax.swing.text.TextAction ; 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 59 public class KeymapViewModelTest extends NbTestCase { 60 61 static { 62 IDEInitializer.setup ( 63 new String [] { 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 [] {} 69 ); 70 } 71 72 public KeymapViewModelTest (String testName) { 73 super (testName); 74 } 75 76 public void testCancelCurrentProfile () { 77 IDEInitializer.cleanWorkDir (); 78 KeymapViewModel model = new KeymapViewModel (); 79 String 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 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 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 [] 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 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 shortcuts = getShortcuts (model); 144 Map 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 156 private Map setRandomShortcuts (final KeymapViewModel model) { 157 final int[] ii = {1}; 158 final Map result = new HashMap (); 159 System.out.println("set random shortcuts"); 160 forAllActions (model, new R () { 161 public void run (KeymapViewModel model, ActionImpl action) { 162 String shortcut = Integer.toString (ii [0], 36).toUpperCase (); 163 StringBuffer sb = new StringBuffer (); 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 s = Collections.singleton (shortcut); 169 model.setShortcuts (action, s); 170 result.put (s, action); 171 ii [0] ++; 173 } 174 }); 175 return result; 176 } 177 178 182 private Map getShortcuts (final KeymapViewModel model) { 183 final Map result = new HashMap (); 184 System.out.println("get shortcuts"); 185 forAllActions (model, new R () { 186 public void run (KeymapViewModel model, ActionImpl action) { 187 String [] sh = model.getShortcuts (action); 188 if (sh.length == 0) return; 189 Set shortcuts = new HashSet (Arrays.asList (sh)); 190 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 getName (Object action) { 199 if (action instanceof TextAction ) 200 return (String ) ((TextAction ) action).getValue (Action.SHORT_DESCRIPTION); 201 if (action instanceof Action ) 202 return (String ) ((Action ) action).getValue (Action.NAME); 203 return action.toString (); 204 } 205 206 private void checkShortcuts (final KeymapViewModel model, final Map shortcuts, final boolean print) { 207 System.out.println("check shortcuts"); 208 final Map localCopy = new HashMap (shortcuts); 209 forAllActions (model, new R () { 210 public void run (KeymapViewModel model, ActionImpl action) { 211 String [] sh = model.getShortcuts (action); 212 if (sh.length == 0) return; 213 Set s = new HashSet (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 255 private void forAllActions (KeymapViewModel model, R r) { 256 forAllActions (model, r, ""); 257 } 258 259 private void forAllActions (KeymapViewModel model, R r, String folder) { 260 Iterator it = model.getItems (folder).iterator (); 261 while (it.hasNext ()) { 262 Object o = it.next (); 263 if (o instanceof String ) 264 forAllActions (model, r, (String ) 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 |