001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.explorer.view;
043:
044: import java.awt.EventQueue;
045: import java.util.concurrent.Callable;
046: import javax.swing.JFrame;
047: import org.netbeans.junit.NbTestCase;
048: import org.openide.explorer.ExplorerManager;
049: import org.openide.nodes.AbstractNode;
050: import org.openide.nodes.Children;
051: import org.openide.nodes.Node;
052:
053: /**
054: * A test that verifies few aspects of the ListView implementation.
055: *
056: * @author Petr Nejedly
057: */
058: public final class ListViewTest extends NbTestCase {
059:
060: private ListView view;
061: private ExplorerWindow testWindow;
062:
063: public ListViewTest(String testName) {
064: super (testName);
065: }
066:
067: /**
068: * Tests whether the ListView doesn't try to scroll to a changed node
069: * on a change event. See issue 88209
070: */
071: public void testNoScrollOnIconChange() throws Exception {
072: assert !EventQueue.isDispatchThread();
073:
074: testWindow = new ExplorerWindow();
075: testWindow.getContentPane().add(view = new ListView());
076:
077: Children.Array ch = new Children.Array();
078: Node root = new AbstractNode(ch);
079:
080: AbstractNode last = null;
081:
082: for (int i = 1; i < 20; i++) {
083: last = new AbstractNode(Children.LEAF);
084: last.setName("Node #" + i);
085: ch.add(new Node[] { last });
086: }
087:
088: testWindow.getExplorerManager().setRootContext(root);
089:
090: awtRequest(new Callable<Void>() {
091: public Void call() {
092: testWindow.pack();
093: testWindow.setVisible(true);
094: return null;
095: }
096: });
097:
098: //Wait for the AWT thread to actually display the dialog:
099: Thread.sleep(2000);
100:
101: // check the pane is not scrolled yet
102: assertEquals(0, (int) awtRequest(new CallY()));
103:
104: // cause an icon change
105: last.setIconBaseWithExtension("org/openide/nodes/beans.gif");
106: Thread.sleep(1000);
107: // check the pane is still not scrolled
108: assertEquals(0, (int) awtRequest(new CallY()));
109:
110: // cleanup
111: awtRequest(new Callable<Void>() {
112: public Void call() {
113: testWindow.setVisible(false);
114: return null;
115: }
116: });
117: }
118:
119: private static final class ExplorerWindow extends JFrame implements
120: ExplorerManager.Provider {
121:
122: private final ExplorerManager explManager = new ExplorerManager();
123:
124: ExplorerWindow() {
125: super ("ListView test"); //NOI18N
126: }
127:
128: public ExplorerManager getExplorerManager() {
129: return explManager;
130: }
131: }
132:
133: private static <T> T awtRequest(final Callable<T> call)
134: throws Exception {
135: final Object[] value = new Object[1];
136: final Exception[] exc = new Exception[1];
137: EventQueue.invokeAndWait(new Runnable() {
138: public void run() {
139: try {
140: value[0] = call.call();
141: } catch (Exception ex) {
142: exc[0] = ex;
143: }
144: }
145: });
146:
147: if (exc[0] != null)
148: throw exc[0];
149: return (T) value[0];
150: }
151:
152: private class CallY implements Callable<Integer> {
153: public Integer call() {
154: return view.getViewport().getViewPosition().y;
155: }
156: };
157:
158: }
|