1 19 20 package org.openide.explorer.view; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Image ; 24 import java.beans.PropertyVetoException ; 25 import java.lang.reflect.InvocationTargetException ; 26 import javax.swing.JFrame ; 27 import javax.swing.JPanel ; 28 import javax.swing.SwingUtilities ; 29 import junit.framework.AssertionFailedError; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.explorer.ExplorerManager; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 36 39 public class TreeView48993Test extends NbTestCase { 40 41 private static final int NO_OF_NODES = 3; 42 43 private static Throwable exc; 44 45 JFrame f; 46 47 static { 48 System.setProperty("sun.awt.exception.handler", TreeView48993Test.class.getName()); 49 } 50 51 53 public TreeView48993Test(){ 54 super(""); 55 } 56 57 public TreeView48993Test(String name) { 58 super(name); 59 } 60 61 protected void setUp() { 62 exc = null; 63 } 64 65 protected void tearDown() { 66 if (f != null) { 67 f.setVisible(false); 68 f.dispose(); 69 } 70 71 if (exc != null) { 72 AssertionFailedError ass = new AssertionFailedError("There should be no exception in AWT"); 73 ass.initCause(exc); 74 throw ass; 75 } 76 } 77 78 protected int timeOut() { 79 return 15000; } 81 82 public void testNotReentrantWhenDoingUpdate() throws Throwable { 83 f = new JFrame (); 84 final MyChildren ch = new MyChildren(); 85 final AbstractNode root = new AbstractNode(ch); 86 root.setName("test root"); 87 88 ch.keys(new String [] {"A", "B", "C", "D" } ); 89 90 final Node[] all = ch.getNodes(); 91 final UglyNode u = (UglyNode)all[2]; 92 93 SwingUtilities.invokeLater(new Runnable () { public void run() { 94 Panel p = new Panel (); 95 p.getExplorerManager().setRootContext(root); 96 try { 97 p.getExplorerManager().setSelectedNodes(new Node[] {all[0]}); 98 } catch (PropertyVetoException pve) { 99 fail(pve.toString()); 100 } 101 102 BeanTreeView btv = new BeanTreeView(); 103 p.add(BorderLayout.CENTER, btv); 104 105 f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 106 f.getContentPane().add(BorderLayout.CENTER, p); 107 f.setBounds(300, 300, 200, 200); 108 f.setVisible(true); 109 }}); 110 111 u.waitOn(10000); 112 113 waitForAWT(); 115 assertTrue("Somebody asked for the icon", u.flag); 116 assertEquals("Still four children", 4, ch.getNodesCount()); 117 118 ((VisualizerNode)Visualizer.findVisualizer(u)).getIcon(true, true); 120 synchronized (u) { 121 u.flag = false; 122 u.keys = new String [] {"C" }; 123 ch.keys(new String [] { "B", "C", "D", "A" }); 124 u.fire(); 125 u.waitOn(10000); 126 } 127 Thread.sleep(1000); 128 129 assertTrue("Somebody asked for the display name again", u.flag); 130 assertEquals("Just one child", 1, ch.getNodesCount()); 131 } 132 133 public void testSomeChildrenDisappearDuringFirstPaint() throws Throwable { 134 assertFalse("Won't work from AWT thread", SwingUtilities.isEventDispatchThread()); 135 136 f = new JFrame (); 137 final MyChildren ch = new MyChildren(); 138 final AbstractNode root = new AbstractNode(ch); 139 root.setName("test root"); 140 141 ch.keys(new String [] {"A", "B", "C", "D" } ); 142 143 Node[] all = ch.getNodes(); 144 final UglyNode u = (UglyNode)all[1]; 145 146 synchronized (u) { 147 Panel p = new Panel (); 148 p.getExplorerManager().setRootContext(root); 149 p.getExplorerManager().setSelectedNodes(new Node[] {all[0]}); 150 151 BeanTreeView btv = new BeanTreeView(); 152 p.add(BorderLayout.CENTER, btv); 153 154 155 f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 156 f.getContentPane().add(BorderLayout.CENTER, p); 157 f.pack(); 158 f.setVisible(true); 159 u.waitOn(10000000); 160 161 Thread.sleep(2000); 162 ((VisualizerNode)Visualizer.findVisualizer(u)).getIcon(true, true); 164 u.flag = false; 165 u.keys = new String [] { "B", "C", "D" }; 166 } 167 168 f.setBounds(300, 300, 200, 200); 169 f.setVisible(false); 170 f.invalidate(); 171 f.validate(); 172 f.repaint(); 173 f.setVisible(true); 174 175 u.waitOn(10000000); 177 waitForAWT(); 178 179 assertTrue("Somebody asked for the icon", u.flag); 180 assertEquals("And now we 3 ", 3, ch.getNodesCount()); 181 } 182 183 186 private static void waitForAWT() throws InterruptedException , InvocationTargetException { 187 for (int i=0; i<5; i++) { 188 SwingUtilities.invokeAndWait(new Runnable () {public void run() {}});; 189 } 190 } 191 192 private static class UglyNode extends AbstractNode { 193 private boolean flag; 194 private Object [] keys; 195 boolean ticked; 196 197 public UglyNode(String name) { 198 super(Children.LEAF); 199 setName(name); 200 } 201 202 public String toString() { 203 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 204 205 } 206 207 public Image getIcon(int type) { 208 synchronized(this) { 209 tick(); 210 flag = true; 211 if (keys != null) { 212 ((MyChildren)getParentNode().getChildren()).keys(keys); 213 } 214 return super.getIcon(type); 215 } 216 } 217 218 public void fire() { 219 fireIconChange(); 220 } 221 222 225 public boolean waitOn(int timeout) { 226 synchronized(this) { 227 boolean tck; 228 if(!ticked) { try { 230 wait(timeout); 231 } catch (InterruptedException e) { 232 throw new InternalError (); 233 } 234 } 235 tck = ticked; 236 ticked = false; return !tck; 238 } 239 } 240 241 public void tick() { 242 synchronized(this) { 243 ticked = true; 244 notifyAll(); 245 } 246 } 247 248 } 249 250 public void testRemoveOneOfEqualsChildren() throws Throwable { 251 class EqualsNode extends AbstractNode { 252 private Object token; 253 254 public EqualsNode(String name, Object token) { 255 super(Children.LEAF); 256 this.token = token; 257 setName(name); 258 } 259 260 public boolean equals(Object obj) { 261 if (obj instanceof EqualsNode) { 262 return ((EqualsNode)obj).token.equals(token); 263 } 264 return super.equals(obj); 265 } 266 } 267 268 class EqualsChildren extends Children.Keys { 269 Object token; 270 EqualsChildren(Object token) { 271 this.token = token; 272 } 273 274 protected Node[] createNodes(Object key) { 275 return new Node[] {new EqualsNode(key.toString(), token)}; 276 } 277 278 public void keys(Object [] keys) { 279 setKeys(keys); 280 } 281 } 282 283 Object token = new Object (); 284 assertFalse("Won't work from AWT thread", SwingUtilities.isEventDispatchThread()); 285 286 EqualsChildren ch = new EqualsChildren(token); 287 AbstractNode root = new AbstractNode(ch); 288 root.setName("test root"); 289 290 Panel p = new Panel (); 291 p.getExplorerManager().setRootContext(root); 292 BeanTreeView btv = new BeanTreeView(); 293 p.add(BorderLayout.CENTER, btv); 294 295 f = new JFrame (); 296 f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 297 f.getContentPane().add(BorderLayout.CENTER, p); 298 f.pack(); 299 f.setVisible(true); 300 waitForAWT(); 301 302 ch.keys(new String [] {"A", "B"} ); 303 304 waitForAWT(); 305 306 ch.keys(new String [] {"a", "B"} ); 307 308 waitForAWT(); 309 } 310 311 312 313 314 private static class MyChildren extends Children.Keys { 315 protected Node[] createNodes(Object key) { 316 return new Node[] {new UglyNode(key.toString())}; 317 } 318 319 public void keys(Object [] keys) { 320 setKeys(keys); 321 } 322 } 323 324 325 public static void handle(Throwable t) { 326 exc = t; 327 } 328 329 private static class Panel extends JPanel 330 implements ExplorerManager.Provider { 331 private ExplorerManager em = new ExplorerManager(); 332 333 public ExplorerManager getExplorerManager() { 334 return em; 335 } 336 } 337 } 338 | Popular Tags |