KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > ListViewTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  *
22  */

23 package org.openide.explorer.view;
24
25 import java.awt.AWTException JavaDoc;
26 import java.awt.BorderLayout JavaDoc;
27 import java.awt.Robot JavaDoc;
28 import java.awt.event.InputEvent JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.beans.PropertyVetoException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33
34 import javax.swing.JList JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
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 /**
49  * Tests for class ListView
50  */

51 public class ListViewTest extends NbTestCase {
52     
53     private static final int NO_OF_NODES = 3;
54
55     
56     public ListViewTest(String JavaDoc name) {
57         super(name);
58     }
59    
60     public static void main(String JavaDoc args[]) {
61          TestRunner.run(new NbTestSuite(ListViewTest.class));
62     }
63     
64     /**
65      * 1. selects a node in a ListView
66      * 2. removes the node
67      * 3. Shift-Click another node by java.awt.Robot
68      */

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         //Thread.sleep(2000);
86

87         for (int i = NO_OF_NODES-1; i >= 0; i--) {
88             //Thread.sleep(500);
89

90             // Waiting for until the view is updated.
91
// This should not be necessary [HREBEJK]
92
try {
93                 SwingUtilities.invokeAndWait( new EmptyRunnable() );
94             } catch (InterruptedException JavaDoc ie) {
95                 fail ("Caught InterruptedException:" + ie.getMessage ());
96             } catch (InvocationTargetException JavaDoc ite) {
97                 fail ("Caught InvocationTargetException: " + ite.getMessage ());
98             }
99        
100             try {
101                 p.getExplorerManager().setSelectedNodes(new Node[] {children[i]} );
102             } catch (PropertyVetoException JavaDoc pve) {
103                 fail ("Caught the PropertyVetoException when set selected node " + children[i].getName ()+ ".");
104             }
105             
106             //Thread.sleep(500);
107
c.remove(new Node[] { children[i] });
108             
109             try {
110                 Thread.sleep(500);
111             } catch (InterruptedException JavaDoc ie) {
112                 fail ("Caught InterruptedException:" + ie.getMessage ());
113             }
114             
115             if (lv.isShowing()) {
116                 Robot JavaDoc r = null;
117                 
118                 try {
119                     r = new Robot JavaDoc();
120                 } catch (AWTException JavaDoc 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     /**
136      * Removes selected node by calling destroy
137      */

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         //Thread.sleep(2000);
155

156         for (int i = NO_OF_NODES-1; i >= 0; i--) {
157             // Waiting for until the view is updated.
158
// This should not be necessary
159
try {
160                 SwingUtilities.invokeAndWait( new EmptyRunnable() );
161             } catch (InterruptedException JavaDoc ie) {
162                 fail ("Caught InterruptedException:" + ie.getMessage ());
163             } catch (InvocationTargetException JavaDoc ite) {
164                 fail ("Caught InvocationTargetException: " + ite.getMessage ());
165             }
166             try {
167                 p.getExplorerManager().setSelectedNodes(new Node[] {children[i]} );
168             } catch (PropertyVetoException JavaDoc pve) {
169                 fail ("Caught the PropertyVetoException when set selected node " + children[i].getName ()+ ".");
170             }
171             //Thread.sleep(500);
172
try {
173                 children[i].destroy();
174             } catch (IOException JavaDoc ioe) {
175                 fail ("Caught the IOException when destroy the node " + children[i].getName ()+ ".");
176             }
177         }
178     }
179     
180     /**
181      * Removes selected node by calling Children.Array.remove
182      */

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         //Thread.sleep(2000);
200

201         try {
202             // Waiting for until the view is updated.
203
// This should not be necessary
204
try {
205                 SwingUtilities.invokeAndWait( new EmptyRunnable() );
206             } catch (InterruptedException JavaDoc ie) {
207                 fail ("Caught InterruptedException:" + ie.getMessage ());
208             } catch (InvocationTargetException JavaDoc ite) {
209                 fail ("Caught InvocationTargetException: " + ite.getMessage ());
210             }
211             p.getExplorerManager().setSelectedNodes(new Node[] {children[0]} );
212         } catch (PropertyVetoException JavaDoc 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             //Thread.sleep(350);
223
}
224         assertEquals(NO_OF_NODES, c.getNodesCount());
225     }
226     
227     /**
228      * Creates two nodes. Selects one and tries to remove it
229      * and replace with the other one (several times).
230      */

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         //Thread.sleep(500);
248

249         for (int i = 0; i < 5; i++) {
250             c.remove(new Node[] { c1 });
251             c.add(new Node[] { c2 });
252             
253             // Waiting for until the view is updated.
254
// This should not be necessary
255
try {
256                 SwingUtilities.invokeAndWait( new EmptyRunnable() );
257             } catch (InterruptedException JavaDoc ie) {
258                 fail ("Caught InterruptedException:" + ie.getMessage ());
259             } catch (InvocationTargetException JavaDoc ite) {
260                 fail ("Caught InvocationTargetException: " + ite.getMessage ());
261             }
262             
263             try {
264                 p.getExplorerManager().setSelectedNodes(new Node[] {c2} );
265             } catch (PropertyVetoException JavaDoc 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             //Thread.sleep(350);
273
}
274     }
275     
276     private static class PListView extends ListView {
277         JList JavaDoc getJList() {
278             return list;
279         }
280     }
281
282     
283     private class EmptyRunnable extends Object JavaDoc implements Runnable JavaDoc {
284
285     public void run() {
286     }
287
288     }
289     
290
291 }
292
Popular Tags