1 7 8 package org.netbeans.modules.options.macros; 9 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Vector ; 15 import junit.framework.TestCase; 16 import org.netbeans.modules.options.editor.IDEInitializer; 17 import org.netbeans.modules.options.keymap.KeymapViewModel; 18 import org.openide.util.lookup.Lookups; 19 20 21 25 public class MacrosModelTest extends TestCase { 26 27 static { 28 IDEInitializer.setup ( 29 new String [] { 30 "org/netbeans/modules/options/editor/mf-layer.xml", 31 "org/netbeans/modules/java/editor/resources/layer.xml", 32 "org/netbeans/modules/defaults/mf-layer.xml", 33 "org/netbeans/core/ui/resources/layer.xml" 34 }, 35 new Object [] {} 36 ); 37 } 38 39 public MacrosModelTest (String testName) { 40 super (testName); 41 } 42 43 public void testAddMacro () { 44 IDEInitializer.cleanWorkDir (); 45 46 MacrosModel model = new MacrosModel ( 48 Lookups.singleton (new KeymapViewModel ()) 49 ); 50 Iterator it = model.getMacroNames ().iterator (); 51 Map macros = readMacros (model); 52 Vector original = clone (model.getShortcutsTableModel ().getDataVector ()); 53 54 model.addMacro ("testName", "testValue"); 56 model.setShortcut (getIndex (model, "testName"), "Alt+Shift+H"); 57 model.addMacro ("testName2", "testValue2"); 58 model.setShortcut (getIndex (model, "testName2"), "Alt+Shift+R"); 59 60 assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ())); 62 assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ()); 63 assertEquals (original.size () + 2, model.getMacroNames ().size ()); 64 assertEquals ("testValue", model.getMacroText ("testName")); 65 assertEquals ("testValue2", model.getMacroText ("testName2")); 66 assertEquals ("Alt+Shift+H", getShortcut (model, "testName")); 67 assertEquals ("Alt+Shift+R", getShortcut (model, "testName2")); 68 69 model.applyChanges (); 70 71 assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ())); 73 assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ()); 74 assertEquals (original.size () + 2, model.getMacroNames ().size ()); 75 assertEquals ("testValue", model.getMacroText ("testName")); 76 assertEquals ("testValue2", model.getMacroText ("testName2")); 77 assertEquals ("Alt+Shift+H", getShortcut (model, "testName")); 78 assertEquals ("Alt+Shift+R", getShortcut (model, "testName2")); 79 80 model = new MacrosModel ( 81 Lookups.singleton (new KeymapViewModel ()) 82 ); 83 84 assertFalse (original.equals (model.getShortcutsTableModel ().getDataVector ())); 86 assertEquals (original.size () + 2, model.getShortcutsTableModel ().getDataVector ().size ()); 87 assertEquals (original.size () + 2, model.getMacroNames ().size ()); 88 assertEquals ("testValue", model.getMacroText ("testName")); 89 assertEquals ("testValue2", model.getMacroText ("testName2")); 90 assertEquals ("Alt+Shift+H", getShortcut (model, "testName")); 91 assertEquals ("Alt+Shift+R", getShortcut (model, "testName2")); 92 } 93 94 private String getShortcut (MacrosModel model, String macroName) { 95 Iterator it = model.getShortcutsTableModel ().getDataVector (). 96 iterator (); 97 while (it.hasNext ()) { 98 Vector line = (Vector ) it.next (); 99 if (line.get (0).equals (macroName)) 100 return (String ) line.get (1); 101 } 102 return null; 103 } 104 105 private Vector clone (Vector v) { 106 Iterator it = v.iterator (); 107 Vector result = new Vector (); 108 while (it.hasNext ()) { 109 Vector line = (Vector ) it.next (); 110 result.add (line.clone ()); 111 } 112 return result; 113 } 114 115 private int getIndex (MacrosModel model, String macroName) { 116 Vector data = model.getShortcutsTableModel ().getDataVector (); 117 int i, k = data.size (); 118 for (i = 0; i < k; i++) { 119 Vector line = (Vector ) data.get (i); 120 if (macroName.equals (line.get (0))) return i; 121 } 122 return -1; 123 } 124 125 private Map readMacros (MacrosModel model) { 126 Iterator it = model.getMacroNames ().iterator (); 127 Map macros = new HashMap (); 128 while (it.hasNext ()) { 129 String macroName = (String ) it.next (); 130 macros.put ( 131 macroName, 132 model.getMacroText (macroName) 133 ); 134 } 135 return macros; 136 } 137 } 138 | Popular Tags |