1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import javax.swing.Action ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.loaders.DataObject; 32 import org.openide.util.Lookup; 33 34 public class LookupSensitiveActionTest extends NbTestCase { 35 36 public LookupSensitiveActionTest(String name) { 37 super( name ); 38 } 39 40 private FileObject dir, f1, f2, f3, f4; 41 private DataObject d1, d2, d3, d4; 42 43 protected void setUp() throws Exception { 44 super.setUp(); 45 clearWorkDir(); 46 dir = FileUtil.toFileObject(getWorkDir()); 47 f1 = dir.createData("f1.java"); 48 f2 = dir.createData("f2.java"); 49 f3 = dir.createData("f3.properties"); 50 f4 = dir.createData("f4.xml"); 51 d1 = DataObject.find(f1); 52 d2 = DataObject.find(f2); 53 d3 = DataObject.find(f3); 54 d4 = DataObject.find(f4); 55 } 56 57 protected void tearDown() throws Exception { 58 clearWorkDir(); 59 super.tearDown(); 60 } 61 62 public boolean runInEQ () { 63 return true; 64 } 65 66 public void testListening() throws Exception { 67 68 71 72 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 73 TestLSA tlsa = new TestLSA( lookup ); 74 assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ()); 75 tlsa.refreshCounter = 0; 76 77 lookup.change(d1); 78 assertEquals( "No refresh should be called ", 0, tlsa.refreshCounter ); 79 lookup.change(d2); 80 lookup.change(d1); 81 assertEquals( "No refresh should be called ", 0, tlsa.refreshCounter ); 82 83 84 TestPropertyChangeListener tpcl = new TestPropertyChangeListener(); 85 tlsa.addPropertyChangeListener( tpcl ); 86 lookup.change(d2); 87 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 88 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 89 90 91 tlsa.clear(); 92 tpcl.clear(); 93 lookup.change(d3); 94 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 95 assertEquals( "One event should be fired", 1, tpcl.getEvents().size() ); 96 97 } 98 99 public void testCorrectValuesWithoutListener() throws Exception { 100 101 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 102 TestLSA tlsa = new TestLSA( lookup ); 103 104 lookup.change(d1); 105 assertEquals( "Action should return correct name ", d1.getName(), tlsa.getValue( Action.NAME ) ); 106 107 assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter ); 108 109 assertEquals( "Action should return correct name ", d1.getName(), tlsa.getValue( Action.NAME ) ); 110 assertEquals( "Refresh should still be called only once", 1, tlsa.refreshCounter ); 111 112 } 113 114 public void testActionGC() throws Exception { 115 116 TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); 117 TestLSA tlsa = new TestLSA( lookup ); 118 119 WeakReference <?> reference = new WeakReference <Object >(tlsa); 120 tlsa = null; 121 122 assertGC( "Action should be GCed", reference ); 123 124 } 125 126 127 private static class TestLSA extends LookupSensitiveAction { 128 129 private int performCounter; 130 private int refreshCounter; 131 132 public TestLSA( Lookup lookup ) { 133 super( null, lookup, new Class [] { DataObject.class } ); 134 } 135 136 protected void actionPerformed( Lookup context ) { 137 performCounter++; 138 } 139 140 protected void refresh( Lookup context ) { 141 refreshCounter++; 142 143 DataObject dobj = context.lookup(DataObject.class); 144 145 if (dobj != null) { 146 putValue( Action.NAME, dobj.getName() ); 147 } 148 149 } 150 151 public void clear() { 152 performCounter = refreshCounter = 0; 153 } 154 155 156 } 157 158 159 private static class TestPropertyChangeListener implements PropertyChangeListener { 160 161 List <PropertyChangeEvent > events = new ArrayList <PropertyChangeEvent >(); 162 163 public void propertyChange( PropertyChangeEvent e ) { 164 events.add( e ); 165 } 166 167 void clear() { 168 events.clear(); 169 } 170 171 List <PropertyChangeEvent > getEvents() { 172 return events; 173 } 174 175 } 176 177 178 } 179 | Popular Tags |