1 19 20 package org.netbeans.spi.looks; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.lang.reflect.*; 26 import java.util.*; 27 import org.netbeans.api.nodes2looks.Nodes; 28 import org.netbeans.api.nodes2looks.TestUtil; 29 import org.netbeans.spi.looks.*; 30 31 import org.netbeans.junit.*; 32 import org.netbeans.modules.looks.LookListener; 33 import org.openide.nodes.Node; 34 import org.openide.nodes.NodeListener; 35 36 40 public class EventDeliveryTest extends NbTestCase { 41 42 44 public EventDeliveryTest(String testName) { 45 super(testName); 46 } 47 48 public static void main( String [] args ) { 49 junit.textui.TestRunner.run(suite()); 50 } 51 52 public static NbTest suite() { 53 NbTestSuite suite = new NbTestSuite(EventDeliveryTest.class); 54 return suite; 55 } 56 57 protected void setUp() throws Exception { 58 super.setUp(); 59 TestUtil.setUpRegistryToDefault(); 60 } 61 62 protected void tearDown() throws Exception { 63 super.tearDown(); 64 } 65 66 68 70 public void testMultipleViewsDetach() { 71 72 BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" ); 73 BadGuy ro = new BadGuy( "First name", new HashSet() ); 74 75 doTest( look, ro ); 76 77 } 78 79 80 82 83 public void testMultipleViewsEventsCount() { 84 85 BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" ); 86 BadGuy ro = new BadGuy( "First name", new ArrayList() ); 87 88 doTest( look, ro ); 89 90 } 91 92 93 private void doTest( BadGuyLook look, BadGuy ro ) { 94 Node n1 = Nodes.node( ro, look ); 96 Node n2 = Nodes.node( ro, look ); 97 98 BadGuyNodeListener bgnl1 = new BadGuyNodeListener(); 99 BadGuyNodeListener bgnl2 = new BadGuyNodeListener(); 100 101 n1.addNodeListener( bgnl1 ); 102 n2.addNodeListener( bgnl2 ); 103 104 ro.setName( "Second name" ); 105 106 assertEquals( "There should be one prop change", 1, bgnl1.names.size() ); 107 assertEquals( "There should be one prop change", 1, bgnl2.names.size() ); 108 109 assertEquals( "Attach should be called once", 1, look.attachTo ); 110 111 WeakReference ref = new WeakReference ( n1 ); 112 n1 = null; assertGC ("The node must disapear", ref); 114 115 ro.setName( "Third name" ); 116 117 assertEquals( "There should be one prop change", 1, bgnl1.names.size() ); 118 assertEquals( "There should be one prop change", 2, bgnl2.names.size() ); 119 } 120 121 122 124 public void testEventAfterDetach() { 125 126 BadGuyLook look = new BadGuyLook( "BAD_GUY_LOOK" ); 127 BadGuy ro = new BadGuy( "First name", new ArrayList() ); 128 BadGuyLookListener ll = new BadGuyLookListener(); 129 130 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( look, ro, ll ); 131 ro.setName( "Second name" ); 132 133 assertEquals( "There should be one name change", 1, ll.names.size() ); 134 135 org.netbeans.modules.looks.Accessor.DEFAULT.removeLookListener( look, ro, ll ); 136 137 ro.setName( "Third name" ); 138 139 assertEquals( "There should be still only one name change", 1, ll.names.size() ); 140 } 141 142 143 145 148 149 public static class BadGuy { 150 151 private Collection listeners; 152 153 private String name; 154 155 public BadGuy( String name, Collection listeners ) { 156 this.name = name; 157 this.listeners = listeners; 158 } 159 160 public String getName() { 161 return name; 162 } 163 164 public void setName( String name ) { 165 String oldName = this.name; 166 this.name = name; 167 168 for( Iterator it = listeners.iterator(); it.hasNext(); ) { 169 ((PropertyChangeListener )it.next()).propertyChange( new PropertyChangeEvent ( this, "NAME", oldName, name ) ); 170 } 171 } 172 173 public void addPropertyChangeListener( PropertyChangeListener listener ) { 174 listeners.add( listener ); 175 } 176 177 public void removePropertyChangeListener( PropertyChangeListener listener ) { 178 listeners.remove( listener ); 179 } 180 181 } 182 183 184 public static class BadGuyLook extends Look implements PropertyChangeListener { 185 186 private int attachTo; 187 private int detachFrom; 188 189 private Class exClass; 190 191 public BadGuyLook( String name ) { 192 super( name ); 193 } 194 195 public void attachTo( Object representedObject ) { 196 ((BadGuy)representedObject).addPropertyChangeListener( this ); 197 attachTo++; 198 } 199 200 public void detachFrom( Object representedObject ) { 201 ((BadGuy)representedObject).removePropertyChangeListener( this ); 202 detachFrom++; 203 } 204 205 public int getAttachToCount() { 206 return attachTo; 207 } 208 209 public int getDetachFromCount() { 210 return detachFrom; 211 } 212 213 public void propertyChange( PropertyChangeEvent evt ) { 214 fireChange( evt.getSource(), Look.GET_NAME ); 215 } 216 217 } 218 219 public static class BadGuyNodeListener implements NodeListener { 220 221 private List names = new ArrayList(); 222 223 public void propertyChange(PropertyChangeEvent evt) { 224 names.add( evt.getNewValue() ); 225 } 226 227 public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) { 228 } 229 230 public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) { 231 } 232 233 public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) { 234 } 235 236 public void nodeDestroyed(org.openide.nodes.NodeEvent ev) { 237 } 238 239 } 240 241 public static class BadGuyLookListener implements LookListener { 242 243 private List names = new ArrayList(); 244 245 public void change(org.netbeans.modules.looks.LookEvent evt) { 246 long mask = evt.getMask(); 247 248 if ( ( mask & Look.GET_NAME ) > 0 ) { 249 names.add( new Object () ); 250 } 251 } 252 253 254 public void propertyChange(org.netbeans.modules.looks.LookEvent evt) { 255 } 256 257 } 258 259 } 260 | Popular Tags |