Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.openide.explorer.view; |
43 |
|
44 |
import java.awt.EventQueue; |
45 |
import java.util.ArrayList; |
46 |
import java.util.List; |
47 |
import java.util.concurrent.Callable; |
48 |
import java.util.concurrent.atomic.AtomicReference; |
49 |
import javax.swing.JFrame; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.explorer.ExplorerManager; |
52 |
import org.openide.nodes.AbstractNode; |
53 |
import org.openide.nodes.Children; |
54 |
import org.openide.nodes.Node; |
55 |
|
56 |
/** |
57 |
* A test that verifies few aspects of the ListView implementation. |
58 |
* |
59 |
* @author Petr Nejedly |
60 |
*/ |
61 |
public final class ListViewWithUpTest extends NbTestCase { |
62 |
|
63 |
private ListView view; |
64 |
private ExplorerWindow testWindow; |
65 |
|
66 |
public ListViewWithUpTest(String testName) { |
67 |
super(testName); |
68 |
} |
69 |
|
70 |
/** |
71 |
* Tests whether the ListView doesn't try to scroll to a changed node |
72 |
* on a change event. See issue 88209 |
73 |
*/ |
74 |
public void testNoScrollOnIconChange() throws Exception { |
75 |
assert !EventQueue.isDispatchThread(); |
76 |
|
77 |
testWindow = new ExplorerWindow(); |
78 |
testWindow.getContentPane().add(view = new ListView()); |
79 |
view.setShowParentNode(true); |
80 |
|
81 |
Ch20 ch = new Ch20(""); |
82 |
Node root = new AbstractNode(ch); |
83 |
root.setName("Root"); |
84 |
|
85 |
testWindow.getExplorerManager().setRootContext(root); |
86 |
awtRequest(new Callable<Void>() { |
87 |
@Override |
88 |
public Void call() { |
89 |
testWindow.pack(); |
90 |
testWindow.setVisible(true); |
91 |
return null; |
92 |
} |
93 |
}); |
94 |
|
95 |
while (!awtRequest(new Callable<Boolean>() { |
96 |
@Override |
97 |
public Boolean call() throws Exception { |
98 |
return testWindow.isShowing(); |
99 |
} |
100 |
})) { |
101 |
Thread.sleep(100); |
102 |
} |
103 |
|
104 |
assertEquals("Just 20 items for root", 20, view.model.getSize()); |
105 |
testWindow.getExplorerManager().setExploredContext(root.getChildren().getNodeAt(10)); |
106 |
awtRequest(new Callable<Void>() { |
107 |
@Override |
108 |
public Void call() { |
109 |
return null; |
110 |
} |
111 |
}); |
112 |
assertEquals("21 items for non-root", 21, view.model.getSize()); |
113 |
awtRequest(new Callable<Void>() { |
114 |
@Override |
115 |
public Void call() { |
116 |
view.performObjectAt(0, 0); |
117 |
return null; |
118 |
} |
119 |
}); |
120 |
assertEquals("Moved back to root", root, testWindow.getExplorerManager().getExploredContext()); |
121 |
|
122 |
// cleanup |
123 |
awtRequest(new Callable<Void>() { |
124 |
@Override |
125 |
public Void call() { |
126 |
testWindow.setVisible(false); |
127 |
return null; |
128 |
} |
129 |
}); |
130 |
} |
131 |
|
132 |
private static final class Ch20 extends Children.Keys<Integer> { |
133 |
private final String prefix; |
134 |
|
135 |
public Ch20(String prefix) { |
136 |
this.prefix = prefix; |
137 |
} |
138 |
|
139 |
|
140 |
@Override |
141 |
protected void addNotify() { |
142 |
List<Integer> arr = new ArrayList<Integer>(); |
143 |
for (int i = 0; i < 20; i++) { |
144 |
arr.add(i); |
145 |
} |
146 |
setKeys(arr); |
147 |
} |
148 |
|
149 |
|
150 |
@Override |
151 |
protected Node[] createNodes(Integer key) { |
152 |
AbstractNode an = new AbstractNode(new Ch20(prefix + key)); |
153 |
an.setName(prefix + key); |
154 |
return new Node[] { an }; |
155 |
} |
156 |
|
157 |
} |
158 |
|
159 |
|
160 |
private static final class ExplorerWindow extends JFrame |
161 |
implements ExplorerManager.Provider { |
162 |
|
163 |
private final ExplorerManager explManager = new ExplorerManager(); |
164 |
|
165 |
ExplorerWindow() { |
166 |
super("ListView test"); //NOI18N |
167 |
} |
168 |
|
169 |
@Override |
170 |
public ExplorerManager getExplorerManager() { |
171 |
return explManager; |
172 |
} |
173 |
} |
174 |
|
175 |
private static <T> T awtRequest(final Callable<T> call) throws Exception { |
176 |
final AtomicReference<T> value = new AtomicReference<T>(); |
177 |
final Exception[] exc = new Exception[1]; |
178 |
EventQueue.invokeAndWait(new Runnable() { |
179 |
@Override |
180 |
public void run() { |
181 |
try { |
182 |
value.set(call.call()); |
183 |
} catch (Exception ex) { |
184 |
exc[0] = ex; |
185 |
} |
186 |
} |
187 |
}); |
188 |
|
189 |
if (exc[0] != null) throw exc[0]; |
190 |
return value.get(); |
191 |
} |
192 |
|
193 |
} |