1 /*
   2  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @bug 4908142
  28  * @summary JList doesn't handle search function appropriately
  29  * @author Andrey Pikalev
  30  * @library ../../regtesthelpers
  31  * @build Util
  32  * @run main bug4908142
  33  */
  34 import javax.swing.*;
  35 import javax.swing.tree.*;
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import java.util.concurrent.Callable;
  39 
  40 public class bug4908142 {
  41 
  42     private static JTree tree;
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         Robot robot = new Robot();
  47         robot.setAutoDelay(50);
  48 
  49         SwingUtilities.invokeAndWait(new Runnable() {
  50 
  51             public void run() {
  52                 createAndShowGUI();
  53             }
  54         });
  55 
  56         robot.waitForIdle();
  57 
  58         SwingUtilities.invokeAndWait(new Runnable() {
  59 
  60             public void run() {
  61                 tree.requestFocus();
  62                 tree.setSelectionRow(0);
  63             }
  64         });
  65 
  66         robot.waitForIdle();
  67 
  68 
  69         robot.keyPress(KeyEvent.VK_A);
  70         robot.keyRelease(KeyEvent.VK_A);
  71         robot.keyPress(KeyEvent.VK_A);
  72         robot.keyRelease(KeyEvent.VK_A);
  73         robot.keyPress(KeyEvent.VK_D);
  74         robot.keyRelease(KeyEvent.VK_D);
  75         robot.waitForIdle();
  76 
  77 
  78         String sel = Util.invokeOnEDT(new Callable<String>() {
  79 
  80             @Override
  81             public String call() throws Exception {
  82                 return tree.getLastSelectedPathComponent().toString();
  83             }
  84         });
  85 
  86         if (!"aad".equals(sel)) {
  87             throw new Error("The selected index should be \"aad\", but not " + sel);
  88         }
  89     }
  90 
  91     private static void createAndShowGUI() {
  92         JFrame fr = new JFrame("Test");
  93         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94 
  95         String[] data = {"aaa", "aab", "aac", "aad", "ade", "bba"};
  96         final DefaultMutableTreeNode root = new DefaultMutableTreeNode(data[0]);
  97         for (int i = 1; i < data.length; i++) {
  98             DefaultMutableTreeNode node = new DefaultMutableTreeNode(data[i]);
  99             root.add(node);
 100         }
 101 
 102         tree = new JTree(root);
 103 
 104         JScrollPane sp = new JScrollPane(tree);
 105         fr.getContentPane().add(sp);
 106         fr.setSize(200, 200);
 107         fr.setVisible(true);
 108     }
 109 }