Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mouse/MouseComboBoxTest/MouseComboBoxTest.java
47791 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 803287226* @summary Tests JComboBox selection via the mouse27* @author Dmitry Markov28*/29import javax.swing.*;30import javax.swing.plaf.basic.BasicComboPopup;31import javax.swing.plaf.basic.ComboPopup;32import javax.swing.plaf.metal.MetalComboBoxUI;33import javax.swing.plaf.metal.MetalLookAndFeel;34import java.awt.*;35import java.awt.event.InputEvent;36import java.awt.event.KeyEvent;3738public class MouseComboBoxTest {39private static final String[] items = {"One", "Two", "Three", "Four", "Five"};4041private static Robot robot = null;42private static JFrame frame = null;43private static JComboBox comboBox = null;44private static MyComboBoxUI comboBoxUI = null;4546public static void main(String[] args) throws Exception {47robot = new Robot();48robot.setAutoDelay(50);4950UIManager.setLookAndFeel(new MetalLookAndFeel());51SwingUtilities.invokeAndWait(new Runnable() {52@Override53public void run() {54createAndShowGUI();55}56});57robot.waitForIdle();5859for (int i = 0; i < items.length; i++) {60// Open popup61robot.keyPress(KeyEvent.VK_DOWN);62robot.keyRelease(KeyEvent.VK_DOWN);63robot.waitForIdle();6465Point point = getItemPointToClick(i);66robot.mouseMove(point.x, point.y);67robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);68robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);69robot.waitForIdle();7071if (i != getSelectedIndex()) {72throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() +73", expected value = " + i);74}75}76}7778private static Point getItemPointToClick(final int item) throws Exception {79final Point[] result = new Point[1];8081SwingUtilities.invokeAndWait(new Runnable() {82@Override83public void run() {84BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();85Point point = popup.getLocationOnScreen();86Dimension size = popup.getSize();8788int step = size.height / items.length;89point.x += size.width / 2;90point.y += step / 2 + step * item;91result[0] = point;92}93});94return result[0];95}9697private static int getSelectedIndex() throws Exception {98final int[] result = new int[1];99100SwingUtilities.invokeAndWait(new Runnable() {101@Override102public void run() {103result[0] = comboBox.getSelectedIndex();104}105});106return result[0];107}108109private static void createAndShowGUI() {110frame = new JFrame("MouseComboBoxTest");111112comboBox = new JComboBox(items);113comboBox.setEditable(true);114comboBoxUI = new MyComboBoxUI();115comboBox.setUI(comboBoxUI);116117frame.pack();118frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);119frame.setVisible(true);120121JWindow window = new JWindow(frame);122window.add(comboBox);123window.pack();124window.setVisible(true);125}126127private static class MyComboBoxUI extends MetalComboBoxUI {128public ComboPopup getComboPopup() {129return popup;130}131}132}133134135136