1 19 20 package org.openide.actions; 21 22 23 import java.awt.event.ActionEvent ; 24 import java.util.Arrays ; 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 import javax.swing.ActionMap ; 28 import javax.swing.JComponent ; 29 import javax.swing.JMenuItem ; 30 31 import junit.textui.TestRunner; 32 33 import org.netbeans.junit.*; 34 import org.openide.actions.*; 35 import org.openide.actions.ActionsInfraHid.UsefulThings; 36 import org.openide.awt.DynamicMenuContent; 37 import org.openide.util.Lookup; 38 import org.openide.util.datatransfer.PasteType; 39 import org.openide.windows.TopComponent; 40 41 42 44 public class PasteActionTest extends AbstractCallbackActionTestHidden { 45 static { 46 ActionsInfraHid.UT.setActivated (null); 47 } 48 49 public PasteActionTest(String name) { 50 super(name); 51 } 52 53 public static void main(String [] args) { 54 TestRunner.run(new NbTestSuite(PasteActionTest.class)); 55 } 56 57 protected Class actionClass () { 58 return PasteAction.class; 59 } 60 61 protected String actionKey () { 62 return javax.swing.text.DefaultEditorKit.pasteAction; 63 } 64 65 public void testListenersAreUnregisteredBug32073 () throws Exception { 66 action.assertListeners ("When we created clone, we added a listener", 1); 67 68 java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (clone); 69 clone = null; 70 assertGC ("Clone can disappear", ref); 71 action.assertListeners ("No listeners, as the clone has been GCed", 0); 72 } 73 74 public void testPresenterCanBeGCedIssue47314 () throws Exception { 75 javax.swing.JMenuItem item = ((org.openide.util.actions.Presenter.Popup)clone).getPopupPresenter (); 76 77 java.lang.ref.WeakReference itemref = new java.lang.ref.WeakReference (item); 78 item = null; 79 java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (clone); 80 clone = null; 81 assertGC ("Item can disappear", itemref); 82 assertGC ("Clone can disappear", ref); 83 } 84 85 86 public void testDelegatesAsOneAction() { 87 OurAction[] arr = { 88 new OurAction () 89 }; 90 91 action.setEnabled (true); 92 assertTrue ("Now is the action enabled", clone.isEnabled()); 93 listener.assertCnt ("No change fired as it was enabled by default", 0); 94 95 arr[0].setEnabled (false); 96 action.putValue ("delegates", arr); 97 98 assertTrue ("Clone should be disabled as the only action we delegate is", !clone.isEnabled ()); 99 listener.assertCnt ("That means one change should be fired", 1); 100 101 action.setEnabled (false); 102 assertTrue ("No influence on enabled state in delegate mode", !clone.isEnabled ()); 103 listener.assertCnt ("No changes fired", 0); 104 105 arr[0].setEnabled (true); 106 assertTrue ("State of clone changed to be enabled", clone.isEnabled ()); 107 listener.assertCnt ("Change fired", 1); 108 109 arr[0].setEnabled (false); 110 assertTrue ("Disabled again", !clone.isEnabled ()); 111 listener.assertCnt ("Changed delivered", 1); 112 113 action.putValue ("delegates", null); 114 assertTrue ("Still disabled, because action itself is disabled", !clone.isEnabled ()); 115 listener.assertCnt ("No changes due to that", 0); 116 117 action.setEnabled (true); 118 assertTrue ("Now we are listening to just the one action", clone.isEnabled ()); 119 listener.assertCnt ("And that is why we should be enabled with one event change", 1); 120 121 arr[0].setEnabled (false); 122 action.putValue ("delegates", arr); 123 assertTrue ("Now we have delegates again, thus we are disabled", !clone.isEnabled ()); 124 listener.assertCnt ("One change delivered", 1); 125 } 126 127 public void testDelegatesAsArrayOfAction () throws Exception { 128 OurAction[] arr = { 129 new OurAction () 130 }; 131 action.putValue ("delegates", arr); 132 134 TopComponent tc = new TopComponent(); 135 tc.getActionMap ().put(javax.swing.text.DefaultEditorKit.pasteAction, action); 136 ActionsInfraHid.UT.setActivated (tc); 137 global.actionPerformed (new ActionEvent (this, 0, "waitFinished")); 138 139 arr[0].assertCnt ("Performed on delegate", 1); 140 action.assertCnt ("Not performed on action", 0); 141 } 142 143 public void testDelegatesAsArrayOfPasteType () throws Exception { 144 OurPasteType [] arr = { 145 new OurPasteType () 146 }; 147 action.putValue ("delegates", arr); 148 150 TopComponent tc = new TopComponent(); 151 tc.getActionMap ().put(javax.swing.text.DefaultEditorKit.pasteAction, action); 152 ActionsInfraHid.UT.setActivated (tc); 153 global.actionPerformed (new ActionEvent (this, 0, "waitFinished")); 154 155 action.assertCnt ("Not performed on action", 0); 156 arr[0].assertCnt ("Performed on delegate", 1); 157 } 158 159 public void testDelegatesAsMoreActions () throws Exception { 160 action.setEnabled (false); 161 listener.assertCnt ("One changed now", 1); 162 163 OurAction[] arr = { 164 new OurAction (), 165 new OurAction () 166 }; 167 168 169 action.putValue ("delegates", arr); 170 assertTrue ("Enabled because it has more than one action", clone.isEnabled ()); 171 listener.assertCnt ("One changes since that", 1); 172 173 action.putValue ("delegates", new Object [0]); 174 assertTrue ("Disabled as no delegates", !clone.isEnabled ()); 175 listener.assertCnt ("One changes since that", 1); 176 177 action.putValue ("delegates", arr); 178 assertTrue ("Enabled again", clone.isEnabled ()); 179 180 181 clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); 182 arr[0].assertCnt ("First delegate invoked", 1); 183 } 184 185 public void testDelegatesAsMorePasteTypes () throws Exception { 186 action.setEnabled (false); 187 listener.assertCnt ("One changed now", 1); 188 189 OurPasteType[] arr = { 190 new OurPasteType(), 191 new OurPasteType() 192 }; 193 194 195 action.putValue ("delegates", arr); 196 assertTrue ("Enabled because it has more than one action", clone.isEnabled ()); 197 listener.assertCnt ("One changes since that", 1); 198 199 action.putValue ("delegates", new Object [0]); 200 assertTrue ("Disabled as no delegates", !clone.isEnabled ()); 201 listener.assertCnt ("One changes since that", 1); 202 203 action.putValue ("delegates", arr); 204 assertTrue ("Enabled again", clone.isEnabled ()); 205 206 clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); 207 arr[0].assertCnt ("First delegate invoked", 1); 208 209 arr = new OurPasteType[] { new OurPasteType () }; 210 211 action.putValue ("delegates", arr); 212 assertTrue ("Enabled still", clone.isEnabled ()); 213 214 clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); 215 arr[0].assertCnt ("First delegate invoked", 1); 216 } 217 218 public void testDisableIsOk() throws Exception { 219 PasteAction p = (PasteAction)PasteAction.get(PasteAction.class); 220 221 class A extends AbstractAction { 222 public void actionPerformed(ActionEvent e) { 223 } 224 } 225 A action = new A(); 226 action.setEnabled(false); 227 229 TopComponent td = new TopComponent(); 230 td.getActionMap().put(javax.swing.text.DefaultEditorKit.pasteAction, action); 231 232 ActionsInfraHid.UT.setActivated(td); 233 234 assertFalse("Disabled", p.isEnabled()); 235 JMenuItem item = p.getMenuPresenter(); 236 assertTrue("Dynamic one: " + item, item instanceof DynamicMenuContent); 237 DynamicMenuContent d = (DynamicMenuContent)item; 238 JComponent [] items = d.getMenuPresenters(); 239 items = d.synchMenuPresenters(items); 240 assertEquals("One item", 1, items.length); 241 assertTrue("One jmenu item", items[0] instanceof JMenuItem ); 242 JMenuItem one = (JMenuItem )items[0]; 243 assertFalse("And is disabled", one.getModel().isEnabled()); 244 } 245 246 private static final class OurPasteType extends org.openide.util.datatransfer.PasteType { 247 private int cnt; 248 249 public java.awt.datatransfer.Transferable paste() throws java.io.IOException { 250 cnt++; 251 return null; 252 } 253 254 public void assertCnt (String msg, int count) { 255 assertEquals (msg, count, this.cnt); 256 this.cnt = 0; 257 } 258 } 260 } 261 | Popular Tags |