1 19 package org.netbeans.core; 20 21 import java.awt.Toolkit ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.KeyEvent ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.logging.Level ; 29 import javax.swing.AbstractAction ; 30 import javax.swing.Action ; 31 import javax.swing.KeyStroke ; 32 import javax.swing.text.Keymap ; 33 import org.netbeans.core.startup.Main; 34 import org.netbeans.junit.*; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileLock; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.Repository; 41 import org.openide.loaders.DataObject; 42 import org.openide.util.Lookup; 43 44 public class ShortcutsFolderTest extends NbTestCase { 45 private ErrorManager err; 46 private Keymap keymap; 47 48 51 public ShortcutsFolderTest(String s) { 52 super(s); 53 } 54 55 protected Level logLevel() { 56 return Level.ALL; 57 } 58 59 protected void setUp() throws Exception { 60 MockServices.setServices(ENV.class); 61 62 Main.initializeURLFactory (); 63 keymap = Lookup.getDefault().lookup(Keymap .class); 64 65 assertNotNull("There is a keymap", keymap); 66 ShortcutsFolder.initShortcuts (); 67 68 err = ErrorManager.getDefault().getInstance("TEST-" + getName()); 69 } 70 71 public void testApplyChangeToFactoryActionIssue49597 () throws Exception { 72 final FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 73 final FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts"); 74 FileObject inst = FileUtil.createData (fs.getRoot (), "/Actions/Tools/TestAction.instance"); 75 TestAction action = new TestAction (); 76 inst.setAttribute ("instanceCreate", action); 77 78 WeakReference ref = new WeakReference (inst); 79 inst = null; 80 assertGC ("File can disappear", ref); 81 82 84 assertEquals ("Nothing registered", Collections.EMPTY_LIST, Arrays.asList (keymap.getBoundActions ())); 85 86 final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F9, KeyEvent.ALT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); 87 88 class R implements FileSystem.AtomicAction { 89 FileObject inst2; 90 91 public void run() throws IOException { 92 inst2 = FileUtil.createData (fs.getRoot (), "/Shortcuts/CA-F9.shadow"); 93 inst2.setAttribute ("originalFile", "/Actions/Tools/TestAction.instance"); 94 } 95 } 96 R run = new R(); 97 fs.runAtomicAction(run); 98 99 ShortcutsFolder.waitFinished (); 100 err.log("ShortcutsFolder.waitFinished"); 101 102 FileObject[] arr = shortcuts.getChildren (); 103 err.log("children are here"); 104 105 assertEquals ("One element is there", 1, arr.length); 106 org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (arr[0]); 107 err.log("Object is here" + obj); 108 109 assertEquals ("It is DataShadow", org.openide.loaders.DataShadow.class, obj.getClass ()); 110 111 Object a = keymap.getAction (stroke); 112 assertNotNull ("There is an action", a); 113 assertEquals ("It is test action", TestAction.class, a.getClass ()); 114 } 115 116 public void testShortcutsForDifferentFilesThanInstanceOrShadows () throws Exception { 117 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 118 FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts"); 119 FileObject inst = FileUtil.createData (fs.getRoot (), "/Shortcuts/C-F11.xml"); 120 121 FileLock lock = inst.lock (); 122 java.io.PrintStream ps = new java.io.PrintStream (inst.getOutputStream (lock)); 123 ps.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 124 ps.println ("<project default=\"run\" name=\"Build everything.\">"); 125 ps.println ("<target name=\"run\">"); 126 ps.println ("<ant antfile=\"SampleProject.xml\" inheritall=\"false\" target=\"all\"/>"); 127 ps.println ("</target>"); 128 ps.println ("</project>"); 129 ps.close(); 130 lock.releaseLock (); 131 132 DataObject obj = DataObject.find (inst); 133 assertEquals ("XML Data object", org.openide.loaders.XMLDataObject.class, obj.getClass()); 134 org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie)obj.getCookie(org.openide.cookies.InstanceCookie.class); 135 assertNotNull ("Has cookie", ic); 136 137 final KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F11, KeyEvent.CTRL_MASK); 138 139 ShortcutsFolder.waitFinished (); 140 141 Action action = keymap.getAction(stroke); 142 if (action == null) { 143 fail("There should be some action for " + stroke + " in:\n" + keymap); 144 } 145 146 inst.delete (); 147 ShortcutsFolder.waitFinished (); 148 action = keymap.getAction (stroke); 149 assertNull ("Action removed", action); 150 } 151 152 public static class TestAction extends AbstractAction { 153 public void actionPerformed (ActionEvent ae) {} 154 } 155 156 public static class ENV extends Object implements org.openide.loaders.Environment.Provider { 157 public Lookup getEnvironment(DataObject obj) { 158 if (obj instanceof org.openide.loaders.XMLDataObject) { 159 try { 160 org.w3c.dom.Document doc = ((org.openide.loaders.XMLDataObject)obj).getDocument(); 161 if (doc.getDocumentElement().getNodeName().equals ("project")) { 162 return org.openide.util.lookup.Lookups.singleton ( 163 new org.openide.loaders.InstanceSupport.Instance ( 164 new TestAction () 165 ) 166 ); 167 } 168 } catch (Exception ex) { 169 ex.printStackTrace(); 170 fail ("No exception: " + ex.getMessage()); 171 } 172 } 173 return org.openide.util.Lookup.EMPTY; 174 } 175 } 176 177 } 178 | Popular Tags |