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