1 19 20 package org.netbeans.modules.enode; 21 22 import java.util.Arrays ; 23 import javax.swing.Action ; 24 import javax.swing.JMenuItem ; 25 import javax.swing.JPopupMenu ; 26 27 import junit.textui.TestRunner; 28 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 32 import org.openide.ErrorManager; 33 import org.openide.modules.ModuleInfo; 34 import org.openide.nodes.*; 35 import org.openide.util.Lookup; 36 import org.openide.util.Utilities; 37 import org.openide.util.actions.SystemAction; 38 39 import org.netbeans.api.enode.ExtensibleNode; 40 import org.netbeans.api.registry.*; 41 42 47 public class ExtensibleActionsTest extends NbTestCase { 48 49 private Context root; 50 51 private Context rootLookup; 52 53 private Context submenu; 54 55 private int i = 0; 56 57 public ExtensibleActionsTest(String name) { 58 super(name); 59 } 60 61 public static void main(String [] args) { 62 TestRunner.run(new NbTestSuite(ExtensibleActionsTest.class)); 63 } 64 65 69 protected void setUp () throws Exception { 70 Lookup.getDefault().lookup(ModuleInfo.class); 71 String baseFolder = ExtensibleNode.E_NODE_ACTIONS.substring(1, ExtensibleNode.E_NODE_ACTIONS.length()-1); 72 root = Context.getDefault().createSubcontext(baseFolder); 73 String baseFolderLookup = ExtensibleNode.E_NODE_LOOKUP.substring(1, ExtensibleNode.E_NODE_LOOKUP.length()-1); 74 rootLookup = Context.getDefault().createSubcontext(baseFolderLookup); 75 String submenuFolder = ExtensibleNode.E_NODE_SUBMENUS.substring(1, ExtensibleNode.E_NODE_SUBMENUS.length()-1); 76 submenu = Context.getDefault().createSubcontext(submenuFolder); 77 } 78 79 82 protected void tearDown() throws Exception { 83 } 84 85 99 public void testCreateAndDeleteAction() throws Exception { 100 try { 101 ExtensibleNode en1 = new ExtensibleNode("test", false); 102 assertEquals("No actions at the start " + i, 0, en1.getActions(false).length); 103 Context test = root.createSubcontext("test"); 104 105 SystemAction sa = SystemAction.get(org.openide.actions.PropertiesAction.class); 106 test.putObject("ttt", sa); 107 Action [] res = en1.getActions(false); 108 assertEquals("There should be exactly one action. " + i , 1, res.length); 109 test.putObject("ttt", null); 110 assertEquals("No actions after deleting " + i, 0, en1.getActions(false).length); 111 } finally { 112 root.destroySubcontext("test"); 113 } 114 } 115 116 136 public void testHierarchicalBehaviour() throws Exception { 137 try { 138 ExtensibleNode en1 = new ExtensibleNode("test/t1", true); 139 assertEquals("No actions at the start", 0, en1.getActions(false).length); 140 Context test = root.createSubcontext("test"); 141 142 SystemAction sa = SystemAction.get(org.openide.actions.PropertiesAction.class); 143 test.putObject("ttt", sa); 144 Action [] res = en1.getActions(false); 145 assertEquals("There should be exactly one action.", 1, res.length); 146 Context t1 = test.createSubcontext("t1"); 147 SystemAction a2 = SystemAction.get(org.openide.actions.CutAction.class); 148 t1.putObject("t2", sa); 149 assertEquals("There should 2 actions.", 2, en1.getActions(false).length); 150 151 test.putObject("ttt", null); 152 assertEquals("There should be one after first delete.", 1, en1.getActions(false).length); 153 154 t1.putObject("t2", null); 155 assertEquals("No actions after deleting both", 0, en1.getActions(false).length); 156 } finally { 157 root.destroySubcontext("test"); 158 } 159 } 160 161 165 public void testRepetitiveDeleting() throws Exception { 166 for (i = 0; i < 100; i++) { 167 testCreateAndDeleteAction(); 168 Thread.sleep(100); 169 } 170 } 171 172 177 public void testWrongActionObjectInConfig() throws Exception { 178 try { 179 ExtensibleNode en1 = new ExtensibleNode("test", false); 180 assertEquals("No actions at the start", 0, en1.getActions(false).length); 181 Context test = root.createSubcontext("test"); 182 test.putObject("ttt", "foobar"); 183 Action [] res = en1.getActions(false); 184 assertEquals("There should be zero actions.", 0, res.length); 185 } finally { 186 root.destroySubcontext("test"); 187 } 188 } 189 190 203 public void testAddingSeparators() throws Exception { 204 try { 205 ExtensibleNode en1 = new ExtensibleNode("test", false); 206 assertEquals("No actions at the start", 0, en1.getActions(false).length); 207 Context test = root.createSubcontext("test"); 208 209 SystemAction a1 = SystemAction.get(org.openide.actions.PropertiesAction.class); 210 Object sep = new javax.swing.JSeparator (); 211 SystemAction a2 = SystemAction.get(org.openide.actions.CutAction.class); 212 test.putObject("a1", a1); 213 test.putObject("sep", sep); 214 test.putObject("a2", a2); 215 test.orderContext(Arrays.asList(new String [] { "a1", "sep", "a2" } )); 216 217 javax.swing.Action [] actions = en1.getActions(false); 218 assertEquals("Actions array should contain 3 elements", 3, actions.length); 219 assertNull("separator should create null element in the array but created 1." + actions[0] + " 2. " + actions[1] + " 3. " + actions[2], actions[1]); 220 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 221 assertEquals("Popup should contain 3 components", 3, jp.getComponentCount()); 222 assertTrue("Separator should be second", jp.getComponent(1) instanceof javax.swing.JSeparator ); 223 } finally { 224 root.destroySubcontext("test"); 225 } 226 } 227 228 243 public void testSubMenuBehaviour() throws Exception { 244 ExtensibleNode en1 = new ExtensibleNode("test1", false); 245 Action [] actions = en1.getActions(false); 246 if (actions.length == 1) { 247 fail("actions only contains " + actions[0]); 248 } 249 assertEquals("Actions array should contain 2 elements ", 2, actions.length); 250 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 251 assertEquals("Popup should contain 2 components", 2, jp.getComponentCount()); 252 assertTrue("The first component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu ); 253 javax.swing.JMenu jm = (javax.swing.JMenu )jp.getComponent(0); 254 assertEquals("Submenu should contain two elements", 2, jm.getMenuComponentCount()); 255 assertEquals("Submenu should have correct name", "Sub Menu1", jm.getText()); 256 } 257 258 271 public void testAddingSeparatorsToSubMenu() throws Exception { 272 ExtensibleNode en1 = new ExtensibleNode("test2", false); 273 javax.swing.Action [] actions = en1.getActions(false); 274 assertEquals("Actions array should contain 1 element", 1, actions.length); 275 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 276 assertEquals("Popup should contain 1 component", 1, jp.getComponentCount()); 277 assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu ); 278 javax.swing.JMenu jm = (javax.swing.JMenu )jp.getComponent(0); 279 assertEquals("Submenu should contain 3 elements", 3, jm.getMenuComponentCount()); 280 assertTrue("Separator should be second", jm.getMenuComponent(1) instanceof javax.swing.JSeparator ); 281 } 282 283 284 299 public void testCreateAndDeleteActionForCookie() throws Exception { 300 try { 301 ExtensibleNode en1 = new ExtensibleNode("test", false); 302 assertEquals("No actions at the start", 0, en1.getActions(false).length); 303 Context test = rootLookup.createSubcontext("test"); 304 Context cFolder = root.createSubcontext("Cookie"); 305 306 SystemAction a1 = SystemAction.get(org.openide.actions.PropertiesAction.class); 307 cFolder.putObject("a1", a1); 308 test.putObject("Cookie", "brumbrum"); 309 310 Action [] res = en1.getActions(false); 311 assertEquals("There should be exactly one action.", 1, res.length); 312 test.putObject("Cookie", null); 313 assertEquals("No actions after deleting cookie", 0, en1.getActions(false).length); 314 } finally { 315 rootLookup.destroySubcontext("test"); 316 root.destroySubcontext("Cookie"); 317 } 318 } 319 320 334 public void testSubMenuOnMoreSelectedNodes() throws Exception { 335 ExtensibleNode en1 = new ExtensibleNode("test2", false); 336 ExtensibleNode en2 = new ExtensibleNode("test2", false); 337 338 Action [] actions = NodeOp.findActions(new Node[] { en1, en2 }); 339 assertEquals("Actions array should contain 1 element", 1, actions.length); 340 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 341 assertEquals("Popup should contain 1 component", 1, jp.getComponentCount()); 342 assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu ); 343 javax.swing.JMenu jm = (javax.swing.JMenu )jp.getComponent(0); 344 assertEquals("Submenu should contain 3 elements", 3, jm.getMenuComponentCount()); 345 } 346 347 358 public void testOrderOfActions() throws Exception { 359 ExtensibleNode en1 = new ExtensibleNode(Children.LEAF,"TPFVWC_View/View", true); 360 javax.swing.Action [] actions = NodeOp.findActions(new Node[] { en1 }); 361 assertEquals("Actions array should contain 2 elements ", 2, actions.length); 362 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 363 assertEquals("Popup should contain 2 components", 2, jp.getComponentCount()); 364 assertEquals("The second component should be Delete", "Delete", ((JMenuItem )jp.getComponent(1)).getText()); 365 366 ExtensibleNode en2 = new ExtensibleNode(Children.LEAF,"TPFVWC_View/ViewFolder", true); 367 javax.swing.Action [] actions2 = NodeOp.findActions(new Node[] { en2 }); 368 assertEquals("Actions array should contain 2 elements ", 2, actions2.length); 369 JPopupMenu jp2 = Utilities.actionsToPopup(actions2, org.openide.util.lookup.Lookups.singleton(en2)); 370 assertEquals("Popup should contain 2 components", 2, jp2.getComponentCount()); 371 assertEquals("The second component should be Delete", "Delete", ((JMenuItem )jp2.getComponent(1)).getText()); 372 } 373 384 public void testSubSubMenu() throws Exception { 385 ExtensibleNode en1 = new ExtensibleNode("test3", false); 386 javax.swing.Action [] actions = en1.getActions(false); 387 assertEquals("Actions array should contain 1 element", 1, actions.length); 388 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 389 assertEquals("Popup should contain 1 component", 1, jp.getComponentCount()); 390 assertTrue("The component should be menu", jp.getComponent(0) instanceof javax.swing.JMenu ); 391 javax.swing.JMenu jm = (javax.swing.JMenu )jp.getComponent(0); 392 assertEquals("Menu should have display name Nice", "Nice", jm.getText()); 393 394 assertEquals("Menu should contain 1 component", 1, jm.getMenuComponentCount()); 395 assertTrue("The component should be menu", jm.getMenuComponent(0) instanceof javax.swing.JMenu ); 396 javax.swing.JMenu jm2 = (javax.swing.JMenu )jm.getMenuComponent(0); 397 assertEquals("Menu should have display name Localized", "Localized", jm2.getText()); 398 399 assertEquals("Menu2 should contain 1 component", 1, jm2.getMenuComponentCount()); 400 assertTrue("The component should be menu", jm2.getMenuComponent(0) instanceof javax.swing.JMenu ); 401 javax.swing.JMenu jm3 = (javax.swing.JMenu )jm2.getMenuComponent(0); 402 assertEquals("Menu should have display name Menu", "Menu", jm3.getText()); 403 404 assertEquals("Submenu should contain 3 elements", 3, jm3.getMenuComponentCount()); 405 assertTrue("Separator should be second", jm3.getMenuComponent(1) instanceof javax.swing.JSeparator ); 406 } 407 408 419 public void testMoreSeparators() throws Exception { 420 ExtensibleNode en1 = new ExtensibleNode("test4", false); 421 javax.swing.Action [] actions = en1.getActions(false); 422 assertEquals("Actions array should contain 6 elements", 6, actions.length); 423 JPopupMenu jp = Utilities.actionsToPopup(actions, org.openide.util.lookup.Lookups.singleton(en1)); 424 assertEquals("Popup should contain 6 components", 6, jp.getComponentCount()); 425 assertTrue("The 2nd component should be separator", jp.getComponent(1) instanceof javax.swing.JSeparator ); 426 assertTrue("The 5nd component should be separator", jp.getComponent(4) instanceof javax.swing.JSeparator ); 427 428 assertTrue("The 3rd component should be menu", jp.getComponent(2) instanceof javax.swing.JMenu ); 429 javax.swing.JMenu jm = (javax.swing.JMenu )jp.getComponent(2); 430 431 assertEquals("Menu should contain 6 components", 6, jm.getMenuComponentCount()); 432 assertTrue("The 2nd component should be separator", jm.getMenuComponent(1) instanceof javax.swing.JSeparator ); 433 assertTrue("The 5nd component should be separator", jm.getMenuComponent(4) instanceof javax.swing.JSeparator ); 434 assertTrue("The 3rd component should be menu", jm.getMenuComponent(2) instanceof javax.swing.JMenu ); 435 javax.swing.JMenu jm2 = (javax.swing.JMenu )jm.getMenuComponent(2); 436 437 assertEquals("Menu2 should contain 5 components", 5, jm2.getMenuComponentCount()); 438 assertTrue("The 2nd component should be separator", jm2.getMenuComponent(1) instanceof javax.swing.JSeparator ); 439 assertTrue("The 4nd component should be separator", jm2.getMenuComponent(3) instanceof javax.swing.JSeparator ); 440 } 441 442 445 public void testfindExistingContext() throws Exception { 446 try { 447 String baseFolder = ExtensibleNode.E_NODE_ACTIONS.substring(1, ExtensibleNode.E_NODE_ACTIONS.length()-1); 448 Context test = root.createSubcontext("test"); 449 Context res = ExtensibleLookupImpl.findExistingContext(baseFolder + "/test/ahoj"); 450 String s = res.getAbsoluteContextName(); 451 assertEquals("Should find the test folder ", ExtensibleNode.E_NODE_ACTIONS+"test", s); 452 Context res2 = ExtensibleLookupImpl.findExistingContext(baseFolder + "/test/ahoj/booooom/ba"); 453 String s2 = res2.getAbsoluteContextName(); 454 assertEquals("Should find the test folder ", ExtensibleNode.E_NODE_ACTIONS+"test", s2); 455 } finally { 456 root.destroySubcontext("test"); 457 } 458 } 459 } 460 461 462 | Popular Tags |