1 19 20 package org.openide.actions; 21 22 23 import javax.swing.Action ; 24 import javax.swing.JPopupMenu ; 25 import junit.textui.TestRunner; 26 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 30 import org.openide.loaders.TemplateWizard; 31 import org.openide.nodes.AbstractNode; 32 import org.openide.nodes.Children; 33 import org.openide.nodes.Node; 34 import org.openide.util.Lookup; 35 import org.openide.util.Utilities; 36 import org.openide.util.lookup.*; 37 40 public class NewTemplateActionTest extends NbTestCase { 41 public NewTemplateActionTest(String name) { 42 super(name); 43 } 44 45 public void testContextAware () { 46 NewTemplateAction global = (NewTemplateAction)NewTemplateAction.get (NewTemplateAction.class); 47 48 CookieNode node = new CookieNode (); 49 JPopupMenu popup = Utilities.actionsToPopup (new Action [] { 50 global 51 }, node.getLookup ()); 52 53 assertTrue ("NewTemplateAction's cookie must be called.", node.counter > 0); 54 55 global.getPopupPresenter (); 56 57 assertTrue ("When calling wizard on global action, the CookieNode's cookie is not " + 58 "as it is not selected", node.counter > 0 59 ); 60 } 61 62 public void testContextAwareWithChanges () { 63 doContextAwareWithChanges (false); 64 } 65 public void testContextAwareWithChangesWithDeepGC () { 66 doContextAwareWithChanges (true); 67 } 68 69 private void doContextAwareWithChanges (boolean withGC) { 70 class P implements Lookup.Provider { 71 private Lookup lookup = Lookup.EMPTY; 72 73 public Lookup getLookup () { 74 return lookup; 75 } 76 } 77 P provider = new P (); 78 Lookup lookup = Lookups.proxy (provider); 79 80 NewTemplateAction global = (NewTemplateAction)NewTemplateAction.get (NewTemplateAction.class); 81 Action clone = global.createContextAwareInstance (lookup); 82 CookieNode node = new CookieNode (); 83 84 assertFalse ("Local is not enabled if no nodes provided", clone.isEnabled ()); 86 87 JPopupMenu popup = Utilities.actionsToPopup (new Action [] { 88 global 89 }, lookup); 90 91 if (withGC) { 92 try { 93 assertGC ("Will fail", new java.lang.ref.WeakReference (this)); 94 } catch (Throwable t) { 95 } 96 } 97 98 assertFalse ("No node selected, no query", node.counter > 0); 99 100 provider.lookup = node.getLookup (); 101 lookup.lookup (Object .class); 103 assertTrue ("After change of Lookup the CookieNode is queried for cookie", node.counter > 0); 104 assertTrue ("Local is enabled if a node is provided", clone.isEnabled ()); 105 } 106 107 private static class CookieNode extends AbstractNode implements NewTemplateAction.Cookie { 108 public CookieNode () { 109 super (Children.LEAF); 110 getCookieSet ().add (this); 111 } 112 113 int counter = 0; 114 public TemplateWizard getTemplateWizard () { 115 counter ++; 116 return new TemplateWizard (); 117 } 118 } 119 } 120 | Popular Tags |