1 /*
   2  * Copyright (c) 2013, 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 /* @test
  24    @bug 6236162
  25    @summary Checks that there is no an inconsistence in combo box
  26             behavior when user points an item in combo popup
  27             by mouse and then uses UP/DOWN keys.
  28    @library ../../regtesthelpers
  29    @build Util
  30    @author Mikhail Lapshin
  31    @run main bug6236162
  32 */
  33 
  34 import javax.swing.*;
  35 import javax.swing.plaf.basic.*;
  36 import javax.swing.plaf.metal.MetalComboBoxUI;
  37 import java.awt.*;
  38 import java.awt.event.KeyEvent;
  39 
  40 public class bug6236162 {
  41     private static JFrame frame;
  42     private static JComboBox combo;
  43     private static MyComboUI comboUI;
  44 
  45     public static void main(String[] args) throws Exception {
  46         UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  47         SwingUtilities.invokeAndWait(new Runnable() {
  48             public void run() {
  49                 createAndShowGUI();
  50             }
  51         });
  52         test();
  53         System.out.println("Test passed");
  54     }
  55 
  56     private static void createAndShowGUI() {
  57         frame = new JFrame("bug6236162");
  58 
  59         combo = new JComboBox(new String[]{"one", "two", "three", "four", "five"});
  60         combo.setEditable(true);
  61         comboUI = new MyComboUI();
  62         combo.setUI(comboUI);
  63         combo.setSelectedIndex(3);
  64         frame.getContentPane().add(combo);
  65 
  66         frame.pack();
  67         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68         frame.setLocationRelativeTo(null);
  69         frame.setVisible(true);
  70     }
  71 
  72     private static void test() throws AWTException {
  73         Robot robot = new Robot();
  74         robot.setAutoDelay(50);
  75 
  76         // Open popup menu
  77         robot.waitForIdle();
  78         Util.hitKeys(robot, KeyEvent.VK_DOWN);
  79 
  80         // Move mouse to the first popup menu item
  81         robot.waitForIdle();
  82         Point p = combo.getLocationOnScreen();
  83         Dimension size = combo.getSize();
  84         p.x += size.width / 2;
  85         p.y += size.height;
  86         float dy = 1;
  87         robot.mouseMove(p.x, p.y - 5);
  88         for (int i=1; i <= 10; i++) {
  89             robot.mouseMove((int)(p.x), (int)(p.y - 5 + dy*i));
  90         }
  91 
  92         // Select the second popup menu item
  93         robot.waitForIdle();
  94         Util.hitKeys(robot, KeyEvent.VK_DOWN);
  95 
  96         robot.waitForIdle();
  97         JList list = comboUI.getComboPopup().getList();
  98         if (list.getSelectedIndex() != 1) {
  99             throw new RuntimeException("There is an inconsistence in combo box " +
 100                     "behavior when user points an item in combo popup " +
 101                     "by mouse and then uses UP/DOWN keys.");
 102         }
 103     }
 104 
 105 
 106     // Gives access to BasicComboBoxUI.popup field
 107     private static class MyComboUI extends MetalComboBoxUI {
 108         public ComboPopup getComboPopup() {
 109             return popup;
 110         }
 111     }
 112 }