1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.awt.event.KeyEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.logging.Handler ; 28 import java.util.logging.Level ; 29 import java.util.logging.LogRecord ; 30 import java.util.logging.Logger ; 31 import javax.swing.Action ; 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.JMenuItem ; 35 import javax.swing.JPanel ; 36 import javax.swing.JToolBar ; 37 import javax.swing.KeyStroke ; 38 import org.netbeans.junit.NbTestCase; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.loaders.DataObject; 42 import org.openide.util.Lookup; 43 import org.openide.util.Utilities; 44 45 public class LookupSensitiveActionUILogTest extends NbTestCase { 46 47 public LookupSensitiveActionUILogTest(String name) { 48 super( name ); 49 } 50 51 private FileObject dir, f1, f2, f3, f4; 52 private DataObject d1, d2, d3, d4; 53 54 private MyHandler my; 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 clearWorkDir(); 59 dir = FileUtil.toFileObject(getWorkDir()); 60 f1 = dir.createData("f1.java"); 61 f2 = dir.createData("f2.java"); 62 f3 = dir.createData("f3.properties"); 63 f4 = dir.createData("f4.xml"); 64 d1 = DataObject.find(f1); 65 d2 = DataObject.find(f2); 66 d3 = DataObject.find(f3); 67 d4 = DataObject.find(f4); 68 69 my = new MyHandler(); 70 Logger.getLogger("org.netbeans.ui.actions").addHandler(my); 71 Logger.getLogger("org.netbeans.ui.actions").setLevel(Level.FINE); 72 } 73 74 protected void tearDown() throws Exception { 75 clearWorkDir(); 76 super.tearDown(); 77 } 78 79 public boolean runInEQ () { 80 return true; 81 } 82 83 public void testMenuPushIsNotified() throws Exception { 84 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 85 TestLSA tlsa = new TestLSA( lookup ); 86 assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ()); 87 tlsa.refreshCounter = 0; 88 TestPropertyChangeListener tpcl = new TestPropertyChangeListener(); 89 tlsa.addPropertyChangeListener( tpcl ); 90 lookup.change(d2); 91 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 92 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 93 assertTrue("Action is enabled", tlsa.isEnabled()); 94 95 tlsa.setDisplayName("Jarda"); 96 97 98 JMenuItem item = new JMenuItem (tlsa); 99 item.doClick(); 100 101 assertEquals("One record logged:\n" + my.recs, 1, my.recs.size()); 102 LogRecord r = my.recs.get(0); 103 assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage()); 104 assertEquals("four args", 5, r.getParameters().length); 105 assertEquals("first is the menu item", item, r.getParameters()[0]); 106 assertEquals("second is its class", JMenuItem .class.getName(), r.getParameters()[1]); 107 assertEquals("3rd is action", tlsa, r.getParameters()[2]); 108 assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]); 109 assertEquals("5th name", "Jarda", r.getParameters()[4]); 110 111 tlsa.clear(); 112 tpcl.clear(); 113 lookup.change(d3); 114 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 115 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 116 } 117 118 public void testToolbarPushIsNotified() throws Exception { 119 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 120 TestLSA tlsa = new TestLSA( lookup ); 121 assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ()); 122 tlsa.refreshCounter = 0; 123 TestPropertyChangeListener tpcl = new TestPropertyChangeListener(); 124 tlsa.addPropertyChangeListener( tpcl ); 125 lookup.change(d2); 126 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 127 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 128 assertTrue("Action is enabled", tlsa.isEnabled()); 129 130 tlsa.setDisplayName("Jarda"); 131 132 JToolBar bar = new JToolBar (); 133 JButton item = bar.add(tlsa); 134 item.doClick(); 135 136 assertEquals("One record logged:\n" + my.recs, 1, my.recs.size()); 137 LogRecord r = my.recs.get(0); 138 assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage()); 139 assertEquals("four args", 5, r.getParameters().length); 140 assertEquals("first is the menu item", item, r.getParameters()[0]); 141 assertEquals("second is its class", item.getClass().getName(), r.getParameters()[1]); 142 assertEquals("3rd is action", tlsa, r.getParameters()[2]); 143 assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]); 144 assertEquals("5th name", "Jarda", r.getParameters()[4]); 145 146 tlsa.clear(); 147 tpcl.clear(); 148 lookup.change(d3); 149 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 150 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 151 } 152 153 public void testKeyEventIsNotified() throws Exception { 154 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 155 TestLSA tlsa = new TestLSA( lookup ); 156 assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ()); 157 tlsa.refreshCounter = 0; 158 TestPropertyChangeListener tpcl = new TestPropertyChangeListener(); 159 tlsa.addPropertyChangeListener( tpcl ); 160 lookup.change(d2); 161 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 162 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 163 assertTrue("Action is enabled", tlsa.isEnabled()); 164 165 tlsa.setDisplayName("Jarda"); 166 167 KeyStroke ks = Utilities.stringToKey("C-S"); 168 169 class MyPanel extends JPanel { 170 public void doEvent(KeyEvent ev, KeyStroke ks) { 171 172 super.processKeyBinding(ks, ev, JPanel.WHEN_FOCUSED, true); 173 } 174 } 175 176 MyPanel p = new MyPanel(); 177 p.getInputMap(JComponent.WHEN_FOCUSED).put(ks, "save"); 178 p.getActionMap().put("save", tlsa); 179 KeyEvent ev = new KeyEvent (p, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.CTRL_DOWN_MASK, KeyEvent.VK_UNDEFINED, 'S'); 180 p.doEvent(ev, ks); 181 182 assertEquals("No record logged:\n" + my.recs, 0, my.recs.size()); 183 199 } 200 201 private static class TestLSA extends LookupSensitiveAction { 202 203 private int performCounter; 204 private int refreshCounter; 205 206 public TestLSA( Lookup lookup ) { 207 super( null, lookup, new Class [] { DataObject.class } ); 208 } 209 210 protected void actionPerformed( Lookup context ) { 211 performCounter++; 212 } 213 214 protected void refresh( Lookup context ) { 215 refreshCounter++; 216 217 DataObject dobj = context.lookup(DataObject.class); 218 219 if (dobj != null) { 220 putValue( Action.NAME, dobj.getName() ); 221 } 222 223 } 224 225 public void clear() { 226 performCounter = refreshCounter = 0; 227 } 228 229 230 } 231 232 233 private static class TestPropertyChangeListener implements PropertyChangeListener { 234 235 List <PropertyChangeEvent > events = new ArrayList <PropertyChangeEvent >(); 236 237 public void propertyChange( PropertyChangeEvent e ) { 238 events.add( e ); 239 } 240 241 void clear() { 242 events.clear(); 243 } 244 245 List <PropertyChangeEvent > getEvents() { 246 return events; 247 } 248 249 } 250 251 252 private static final class MyHandler extends Handler { 253 public final List <LogRecord > recs = new ArrayList <LogRecord >(); 254 255 public void publish(LogRecord record) { 256 recs.add(record); 257 } 258 259 public void flush() { 260 } 261 262 public void close() throws SecurityException { 263 } 264 } } 266 | Popular Tags |