1 19 20 package org.openide.actions; 21 22 23 import java.util.Arrays ; 24 import javax.swing.Action ; 25 import javax.swing.ActionMap ; 26 27 import junit.textui.TestRunner; 28 29 import org.netbeans.junit.*; 30 import org.openide.actions.*; 31 import org.openide.util.Lookup; 32 import org.openide.util.actions.CallbackSystemAction; 33 34 35 37 public abstract class AbstractCallbackActionTestHidden extends NbTestCase { 38 public AbstractCallbackActionTestHidden(String name) { 39 super(name); 40 } 41 42 43 44 45 46 protected org.openide.util.actions.CallbackSystemAction global; 47 48 49 protected OurAction action = new OurAction (); 50 51 52 protected ActionMap map; 53 54 protected Action clone; 55 56 57 protected CntListener listener; 58 59 60 private Lookup lookup; 61 62 64 protected abstract Class actionClass (); 65 66 68 protected abstract String actionKey (); 69 70 71 72 protected boolean runInEQ () { 73 return true; 74 } 75 76 protected void setUp() throws Exception { 77 global = (CallbackSystemAction)CallbackSystemAction.get (actionClass ()); 78 map = new ActionMap (); 79 map.put (actionKey (), action); 80 lookup = org.openide.util.lookup.Lookups.singleton(map); 81 clone = global.createContextAwareInstance(lookup); 83 84 listener = new CntListener (); 85 clone.addPropertyChangeListener(listener); 86 } 87 88 public void testThatDefaultEditorKitPasteActionIsTheCorrectKeyOfPasteAction () { 89 clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); 90 action.assertCnt ("Clone correctly delegates to OurAction", 1); 91 } 92 93 public void testChangesAreCorrectlyPropagatedToTheDelegate () { 94 action.setEnabled (true); 95 96 assertTrue ("Clone is correctly enabled", clone.isEnabled ()); 97 98 action.setEnabled (false); 99 assertTrue ("Clone is disabled", !clone.isEnabled()); 100 listener.assertCnt ("Change notified", 1); 101 102 action.setEnabled (true); 103 listener.assertCnt ("Change notified again", 1); 104 assertTrue ("Clone is correctly enabled", clone.isEnabled ()); 105 } 106 107 protected static final class OurAction extends javax.swing.AbstractAction { 108 private int cnt; 109 private java.util.HashSet listeners = new java.util.HashSet (); 110 111 public void actionPerformed(java.awt.event.ActionEvent e) { 112 cnt++; 113 } 114 115 public void assertCnt (String msg, int count) { 116 assertEquals (msg, count, this.cnt); 117 this.cnt = 0; 118 } 119 120 public void assertListeners (String msg, int count) throws Exception { 121 if (count == 0) { 122 synchronized (this) { 123 int c = 5; 124 while (this.listeners.size () != 0 && c-- > 0) { 125 System.gc (); 126 wait (500); 127 } 128 } 129 } 130 131 if (count != this.listeners.size ()) { 132 fail (msg + " listeners expected: " + count + " but are " + this.listeners); 133 } 134 } 135 136 public void addPropertyChangeListener (java.beans.PropertyChangeListener listener) { 137 super.addPropertyChangeListener (listener); 138 listeners.add (listener); 139 } 140 141 public synchronized void removePropertyChangeListener (java.beans.PropertyChangeListener listener) { 142 super.removePropertyChangeListener (listener); 143 listeners.remove (listener); 144 notifyAll (); 145 } 146 } 148 protected static final class CntListener extends Object 149 implements java.beans.PropertyChangeListener { 150 private int cnt; 151 152 public void propertyChange(java.beans.PropertyChangeEvent evt) { 153 cnt++; 154 } 155 156 public void assertCnt (String msg, int count) { 157 assertEquals (msg, count, this.cnt); 158 this.cnt = 0; 159 } 160 } } 162 | Popular Tags |