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 import sun.awt.SunToolkit;
  40 
  41 public class bug4908142 {
  42 
  43     private static JTree tree;
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  48         Robot robot = new Robot();
  49         robot.setAutoDelay(50);
  50 
  51         SwingUtilities.invokeAndWait(new Runnable() {
  52 
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57 
  58         toolkit.realSync();
  59 
  60         SwingUtilities.invokeAndWait(new Runnable() {
  61 
  62             public void run() {
  63                 tree.requestFocus();
  64                 tree.setSelectionRow(0);
  65             }
  66         });
  67 
  68         toolkit.realSync();
  69 
  70 
  71         robot.keyPress(KeyEvent.VK_A);
  72         robot.keyRelease(KeyEvent.VK_A);
  73         robot.keyPress(KeyEvent.VK_A);
  74         robot.keyRelease(KeyEvent.VK_A);
  75         robot.keyPress(KeyEvent.VK_D);
  76         robot.keyRelease(KeyEvent.VK_D);
  77         toolkit.realSync();
  78 
  79 
  80         String sel = Util.invokeOnEDT(new Callable<String>() {
  81 
  82             @Override
  83             public String call() throws Exception {
  84                 return tree.getLastSelectedPathComponent().toString();
  85             }
  86         });
  87 
  88         if (!"aad".equals(sel)) {
  89             throw new Error("The selected index should be \"aad\", but not " + sel);
  90         }
  91     }
  92 
  93     private static void createAndShowGUI() {
  94         JFrame fr = new JFrame("Test");
  95         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96 
  97         String[] data = {"aaa", "aab", "aac", "aad", "ade", "bba"};
  98         final DefaultMutableTreeNode root = new DefaultMutableTreeNode(data[0]);
  99         for (int i = 1; i < data.length; i++) {
 100             DefaultMutableTreeNode node = new DefaultMutableTreeNode(data[i]);
 101             root.add(node);
 102         }
 103 
 104         tree = new JTree(root);
 105 
 106         JScrollPane sp = new JScrollPane(tree);
 107         fr.getContentPane().add(sp);
 108         fr.setSize(200, 200);
 109         fr.setVisible(true);
 110     }
 111 }