1 19 20 package org.openide.util; 21 22 import java.lang.ref.*; 23 import javax.swing.Action ; 24 import javax.swing.JMenuItem ; 25 26 import junit.framework.*; 27 28 import org.netbeans.junit.*; 29 30 32 public class UtilitiesActionsTest extends NbTestCase { 33 34 public UtilitiesActionsTest(java.lang.String testName) { 35 super(testName); 36 } 37 38 public static void main(java.lang.String [] args) { 39 junit.textui.TestRunner.run(new NbTestSuite(UtilitiesActionsTest.class)); 40 } 41 42 public void testActionClone () throws Exception { 43 CloneAction original = new CloneAction (); 44 45 javax.swing.Action clone = original.createContextAwareInstance(Lookup.EMPTY); 46 47 assertTrue ("Clone is instance of desired class", clone instanceof CloneAction); 48 assertNull ("Original has empty lookup", original.lookup); 49 assertEquals ("Clone has lookup assigned", Lookup.EMPTY, ((CloneAction)clone).lookup); 50 } 51 52 53 public void testActionToPopupForLookup () throws Exception { 54 class MyAction extends CloneAction 55 implements org.openide.util.actions.Presenter.Popup { 56 public JMenuItem item = new JMenuItem ("Ahoj"); 57 public int called; 58 59 protected CloneAction cloneAction () { 60 return new MyAction (); 61 } 62 63 64 public JMenuItem getPopupPresenter () { 65 called++; 66 return item; 67 } 68 } 69 70 final CloneAction[] arr = { 71 new CloneAction (), new MyAction () 72 }; 73 74 final javax.swing.JPopupMenu menu = org.openide.util.Utilities.actionsToPopup (arr, Lookup.EMPTY); 75 76 assertEquals ("Presenter called", 1, ((MyAction)arr[1].clone).called); 77 assertEquals ("Lookup is fine", ((CloneAction)arr[0].clone).lookup, Lookup.EMPTY); 78 assertEquals ("The other as well", ((CloneAction)arr[1].clone).lookup, Lookup.EMPTY); 79 80 final AssertionFailedError[] err = new AssertionFailedError[1]; 82 Mutex.EVENT.readAccess(new Runnable () { 83 public void run() { 84 try { 85 86 javax.swing.MenuElement [] elem = menu.getSubElements (); 87 assertEquals ("Two subitems", 2, elem.length); 88 89 elem[0].getComponent().addNotify(); 91 92 arr[0].clone.setEnabled(false); 93 assertTrue ("Menu item is disabled", !((JMenuItem )elem[0].getComponent()).isEnabled ()); 94 arr[0].clone.setEnabled(true); 95 assertTrue ("Menu item is enabled", ((JMenuItem )elem[0].getComponent()).isEnabled ()); 96 97 assertEquals ("The presenter is really used", ((MyAction)arr[1].clone).item, elem[1].getComponent ()); 98 99 } catch (AssertionFailedError e) { 100 err[0] = e; 101 } 102 } 103 }); 104 if (err[0] != null) throw err[0]; 105 } 106 107 public void testActionsToPopupFromComponent () throws Exception { 108 Lookup lookup = org.openide.util.lookup.Lookups.singleton (this); 109 final Lookup[] returnedLookup = {lookup}; 110 111 class Provider extends javax.swing.JComponent implements Lookup.Provider { 112 public Lookup getLookup () { 113 return returnedLookup[0]; 114 } 115 } 116 117 CloneAction sample = new CloneAction (); 118 119 120 javax.swing.JComponent outerMost = new javax.swing.JPanel (); 121 outerMost.getActionMap().put ("1", sample); 122 123 javax.swing.JComponent provider = new Provider (); 124 outerMost.add (provider); 125 provider.getActionMap().put ("2", sample); 126 127 javax.swing.JComponent child = new javax.swing.JPanel (); 128 provider.add (child); 129 child.getActionMap().put ("3", sample); 130 javax.swing.JComponent menuOwner = new javax.swing.JPanel (); 131 child.add (menuOwner); 132 menuOwner.getActionMap().put ("4", sample); 133 134 javax.swing.JComponent sibling = new javax.swing.JPanel (); 135 provider.add (child); 136 sibling.getActionMap().put ("5", sample); 137 138 139 CloneAction[] arr = { new CloneAction () }; 140 141 javax.swing.JPopupMenu menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner); 142 143 Lookup actionLookup = arr[0].clone.lookup; 144 assertNotNull("Clone lookup is not null", actionLookup); 145 assertEquals ("The lookup returned by 'provider' is assigned to the clonned actions", 146 this, actionLookup.lookup (this.getClass ()) 147 ); 148 149 returnedLookup[0] = null; 151 arr[0].clone = null; 152 menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner); 153 154 actionLookup = arr[0].clone.lookup; 155 assertNotNull("Clone lookup is not null", actionLookup); 156 157 javax.swing.ActionMap map = (javax.swing.ActionMap )actionLookup.lookup (javax.swing.ActionMap .class); 158 assertNotNull ("Action map is in the lookup", map); 159 assertNull ("ActionMap of parent of Lookup.Provider is not included", map.get ("1")); 160 assertNotNull ("ActionMap of Lookup.Provider is included", map.get ("2")); 161 assertNotNull ("So is the child's one", map.get ("3")); 162 assertNotNull ("And also of the menuOwner", map.get ("4")); 163 assertNull ("But of course the sibling's one is not", map.get ("5")); 164 } 165 166 class CloneAction extends javax.swing.AbstractAction implements ContextAwareAction { 167 public Lookup lookup; 168 public CloneAction clone; 169 170 public void actionPerformed (java.awt.event.ActionEvent ev) { 171 } 172 173 protected CloneAction cloneAction () { 174 return new CloneAction (); 175 } 176 177 public Action createContextAwareInstance(Lookup actionContext) { 178 assertNull ("We support just one clone right now", clone); 179 clone = cloneAction (); 180 clone.lookup = actionContext; 181 return clone; 182 } 183 184 } 186 } 187 | Popular Tags |