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.AWTException;
045: import java.awt.BorderLayout;
046: import java.awt.Point;
047: import java.awt.Robot;
048: import java.awt.event.KeyEvent;
049: import java.beans.PropertyVetoException;
050: import java.lang.reflect.InvocationTargetException;
051: import javax.swing.JFrame;
052: import javax.swing.JPanel;
053: import javax.swing.JTree;
054: import javax.swing.SwingUtilities;
055: import javax.swing.tree.TreePath;
056: import org.netbeans.junit.NbTestCase;
057: import org.openide.explorer.ExplorerManager;
058: import org.openide.nodes.AbstractNode;
059: import org.openide.nodes.Children;
060: import org.openide.nodes.Node;
061:
062: /**
063: * Tests for the quick search feature in the treeview.
064: */
065: public class TreeViewQuickSearchTest extends NbTestCase {
066:
067: private static final int NO_OF_NODES = 3;
068:
069: public TreeViewQuickSearchTest(String name) {
070: super (name);
071: }
072:
073: public void testSubstringQuickSearch() throws Throwable {
074: doQuickSearchTest(true);
075: }
076:
077: public void testPrefixQuickSearch() throws Throwable {
078: doQuickSearchTest(false);
079: }
080:
081: private void doQuickSearchTest(final boolean substringSearch)
082: throws Throwable {
083: final AbstractNode root = new AbstractNode(new Children.Array());
084: root.setName("test root");
085:
086: final Node[] children = { createLeaf("foo1"),
087: createLeaf("foo2"), createLeaf("bar1"),
088: createLeaf("bar2"), createLeaf("alpha"), };
089:
090: root.getChildren().add(children);
091:
092: final Panel p = new Panel();
093: p.getExplorerManager().setRootContext(root);
094:
095: final BeanTreeView btv = new BeanTreeView();
096: p.add(BorderLayout.CENTER, btv);
097: btv.setUseSubstringInQuickSearch(substringSearch);
098:
099: JFrame f = new JFrame();
100: f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
101: f.getContentPane().add(BorderLayout.CENTER, p);
102: f.pack();
103: f.setVisible(true);
104:
105: final JTree tree = btv.tree;
106: final Exception[] problem = new Exception[1];
107: final Integer[] phase = new Integer[1];
108: phase[0] = 0;
109: class AWTTst implements Runnable {
110: public void run() {
111: try {
112: if (phase[0] == 0) {
113: btv.tree.requestFocus();
114: try {
115: p.getExplorerManager().setSelectedNodes(
116: new Node[] { root });
117: } catch (PropertyVetoException e) {
118: fail("Unexpected PropertyVetoException from ExplorerManager.setSelectedNodes()");
119: }
120: }
121: if (phase[0] == 1) {
122: Robot robot = new Robot();
123: Point p = btv.tree.getLocationOnScreen();
124: robot.mouseMove(p.x + 10, p.y + 10);
125: robot.mousePress(0);
126: robot.mouseRelease(0);
127: robot.keyPress(KeyEvent.VK_A);
128: robot.keyRelease(KeyEvent.VK_A);
129: }
130:
131: if (phase[0] == 2) {
132: Node operateOn = substringSearch ? children[2]
133: : children[4];
134: TreePath[] paths = tree.getSelectionPaths();
135: assertNotNull(
136: "One node should be selected, but there are none.",
137: paths);
138: assertEquals(
139: "One node should be selected, but there are none.",
140: 1, paths.length);
141: assertEquals("Wrong node selected.", operateOn,
142: Visualizer.findNode(paths[0]
143: .getLastPathComponent()));
144: }
145: } catch (AWTException ex) {
146: problem[0] = ex;
147: }
148: }
149: }
150: AWTTst awt = new AWTTst();
151: try {
152: SwingUtilities.invokeAndWait(awt);
153: } catch (InvocationTargetException ex) {
154: throw ex.getTargetException();
155: }
156: if (problem[0] != null) {
157: throw problem[0];
158: }
159: Thread.sleep(1000);
160: phase[0] = 1;
161: try {
162: SwingUtilities.invokeAndWait(awt);
163: } catch (InvocationTargetException ex) {
164: throw ex.getTargetException();
165: }
166: if (problem[0] != null) {
167: throw problem[0];
168: }
169: phase[0] = 2;
170: Thread.sleep(1000);
171: try {
172: SwingUtilities.invokeAndWait(awt);
173: } catch (InvocationTargetException ex) {
174: throw ex.getTargetException();
175: }
176: if (problem[0] != null) {
177: throw problem[0];
178: }
179: }
180:
181: private static Node createLeaf(String name) {
182: AbstractNode n = new AbstractNode(Children.LEAF);
183: n.setName(name);
184: return n;
185: }
186:
187: private static class Panel extends JPanel implements
188: ExplorerManager.Provider {
189: private ExplorerManager em = new ExplorerManager();
190:
191: public ExplorerManager getExplorerManager() {
192: return em;
193: }
194: }
195: }
|