1 19 20 23 package org.openide.explorer.view; 24 25 import java.awt.AWTException ; 26 import java.awt.BorderLayout ; 27 import java.awt.Robot ; 28 import java.awt.event.InputEvent ; 29 import java.awt.event.KeyEvent ; 30 import java.beans.PropertyVetoException ; 31 import java.io.IOException ; 32 import java.lang.reflect.InvocationTargetException ; 33 34 import javax.swing.JList ; 35 import javax.swing.SwingUtilities ; 36 37 import junit.textui.TestRunner; 38 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.junit.NbTestSuite; 41 42 import org.openide.explorer.ExplorerPanel; 43 import org.openide.nodes.AbstractNode; 44 import org.openide.nodes.Children; 45 import org.openide.nodes.Node; 46 import org.openide.nodes.Children.Array; 47 48 51 public class ListViewTest extends NbTestCase { 52 53 private static final int NO_OF_NODES = 3; 54 55 56 public ListViewTest(String name) { 57 super(name); 58 } 59 60 public static void main(String args[]) { 61 TestRunner.run(new NbTestSuite(ListViewTest.class)); 62 } 63 64 69 public void testNodeSelectionByRobot() { 70 final Children c = new Array(); 71 Node n = new AbstractNode (c); 72 final PListView lv = new PListView(); 73 final ExplorerPanel p = new ExplorerPanel(); 74 p.add(lv, BorderLayout.CENTER); 75 p.getExplorerManager().setRootContext(n); 76 p.open(); 77 Node[] children = new Node[NO_OF_NODES]; 78 79 for (int i = 0; i < NO_OF_NODES; i++) { 80 children[i] = new AbstractNode(Children.LEAF); 81 children[i].setDisplayName(Integer.toString(i)); 82 children[i].setName(Integer.toString(i)); 83 c.add(new Node[] { children[i] } ); 84 } 85 87 for (int i = NO_OF_NODES-1; i >= 0; i--) { 88 90 try { 93 SwingUtilities.invokeAndWait( new EmptyRunnable() ); 94 } catch (InterruptedException ie) { 95 fail ("Caught InterruptedException:" + ie.getMessage ()); 96 } catch (InvocationTargetException ite) { 97 fail ("Caught InvocationTargetException: " + ite.getMessage ()); 98 } 99 100 try { 101 p.getExplorerManager().setSelectedNodes(new Node[] {children[i]} ); 102 } catch (PropertyVetoException pve) { 103 fail ("Caught the PropertyVetoException when set selected node " + children[i].getName ()+ "."); 104 } 105 106 c.remove(new Node[] { children[i] }); 108 109 try { 110 Thread.sleep(500); 111 } catch (InterruptedException ie) { 112 fail ("Caught InterruptedException:" + ie.getMessage ()); 113 } 114 115 if (lv.isShowing()) { 116 Robot r = null; 117 118 try { 119 r = new Robot (); 120 } catch (AWTException ae) { 121 fail ("Caught AWTException: " + ae.getMessage ()); 122 } 123 124 r.keyPress(KeyEvent.VK_SHIFT); 125 r.mouseMove(lv.getLocationOnScreen().x + 10,lv.getLocationOnScreen().y + 10); 126 r.mousePress(InputEvent.BUTTON1_MASK); 127 r.keyRelease(KeyEvent.VK_SHIFT); 128 r.mouseRelease(InputEvent.BUTTON1_MASK); 129 } else { 130 fail(); 131 } 132 } 133 } 134 135 138 public void testDestroySelectedNodes() { 139 final Children c = new Array(); 140 Node n = new AbstractNode (c); 141 final PListView lv = new PListView(); 142 final ExplorerPanel p = new ExplorerPanel(); 143 p.add(lv, BorderLayout.CENTER); 144 p.getExplorerManager().setRootContext(n); 145 p.open(); 146 Node[] children = new Node[NO_OF_NODES]; 147 148 for (int i = 0; i < NO_OF_NODES; i++) { 149 children[i] = new AbstractNode(Children.LEAF); 150 children[i].setDisplayName(Integer.toString(i)); 151 children[i].setName(Integer.toString(i)); 152 c.add(new Node[] { children[i] } ); 153 } 154 156 for (int i = NO_OF_NODES-1; i >= 0; i--) { 157 try { 160 SwingUtilities.invokeAndWait( new EmptyRunnable() ); 161 } catch (InterruptedException ie) { 162 fail ("Caught InterruptedException:" + ie.getMessage ()); 163 } catch (InvocationTargetException ite) { 164 fail ("Caught InvocationTargetException: " + ite.getMessage ()); 165 } 166 try { 167 p.getExplorerManager().setSelectedNodes(new Node[] {children[i]} ); 168 } catch (PropertyVetoException pve) { 169 fail ("Caught the PropertyVetoException when set selected node " + children[i].getName ()+ "."); 170 } 171 try { 173 children[i].destroy(); 174 } catch (IOException ioe) { 175 fail ("Caught the IOException when destroy the node " + children[i].getName ()+ "."); 176 } 177 } 178 } 179 180 183 public void testRemoveAndAddNodes() { 184 final Children c = new Array(); 185 Node n = new AbstractNode (c); 186 final PListView lv = new PListView(); 187 final ExplorerPanel p = new ExplorerPanel(); 188 p.add(lv, BorderLayout.CENTER); 189 p.getExplorerManager().setRootContext(n); 190 p.open(); 191 Node[] children = new Node[NO_OF_NODES]; 192 193 for (int i = 0; i < NO_OF_NODES; i++) { 194 children[i] = new AbstractNode(Children.LEAF); 195 children[i].setDisplayName(Integer.toString(i)); 196 children[i].setName(Integer.toString(i)); 197 c.add(new Node[] { children[i] } ); 198 } 199 201 try { 202 try { 205 SwingUtilities.invokeAndWait( new EmptyRunnable() ); 206 } catch (InterruptedException ie) { 207 fail ("Caught InterruptedException:" + ie.getMessage ()); 208 } catch (InvocationTargetException ite) { 209 fail ("Caught InvocationTargetException: " + ite.getMessage ()); 210 } 211 p.getExplorerManager().setSelectedNodes(new Node[] {children[0]} ); 212 } catch (PropertyVetoException pve) { 213 fail ("Caught the PropertyVetoException when set selected node " + children[0].getName ()+ "."); 214 } 215 216 for (int i = 0; i < NO_OF_NODES; i++) { 217 c.remove(new Node [] { children[i] } ); 218 children[i] = new AbstractNode(Children.LEAF); 219 children[i].setDisplayName(Integer.toString(i)); 220 children[i].setName(Integer.toString(i)); 221 c.add(new Node[] { children[i] } ); 222 } 224 assertEquals(NO_OF_NODES, c.getNodesCount()); 225 } 226 227 231 public void testNodeAddingAndRemoving() { 232 final Children c = new Array(); 233 Node n = new AbstractNode (c); 234 final PListView lv = new PListView(); 235 final ExplorerPanel p = new ExplorerPanel(); 236 p.add(lv, BorderLayout.CENTER); 237 p.getExplorerManager().setRootContext(n); 238 p.open(); 239 240 final Node c1 = new AbstractNode(Children.LEAF); 241 c1.setDisplayName("First"); 242 c1.setName("First"); 243 c.add(new Node[] { c1 }); 244 Node c2 = new AbstractNode(Children.LEAF); 245 c2.setDisplayName("Second"); 246 c2.setName("Second"); 247 249 for (int i = 0; i < 5; i++) { 250 c.remove(new Node[] { c1 }); 251 c.add(new Node[] { c2 }); 252 253 try { 256 SwingUtilities.invokeAndWait( new EmptyRunnable() ); 257 } catch (InterruptedException ie) { 258 fail ("Caught InterruptedException:" + ie.getMessage ()); 259 } catch (InvocationTargetException ite) { 260 fail ("Caught InvocationTargetException: " + ite.getMessage ()); 261 } 262 263 try { 264 p.getExplorerManager().setSelectedNodes(new Node[] {c2} ); 265 } catch (PropertyVetoException pve) { 266 fail ("Caught the PropertyVetoException when set selected node " + c2.getName ()+ "."); 267 } 268 269 c.remove(new Node[] { c2 }); 270 c.add(new Node[] { c1 }); 271 272 } 274 } 275 276 private static class PListView extends ListView { 277 JList getJList() { 278 return list; 279 } 280 } 281 282 283 private class EmptyRunnable extends Object implements Runnable { 284 285 public void run() { 286 } 287 288 } 289 290 291 } 292 | Popular Tags |