Path: blob/master/test/jdk/javax/swing/JComboBox/8182031/ComboPopupTest.java
41153 views
/*1* Copyright (c) 2017, 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 818203126* @summary Verifies if ComboBox Popup opens and closes immediately27* @key headful28* @run main ComboPopupTest29*/3031import java.awt.Container;32import java.awt.Dimension;33import java.awt.FlowLayout;34import java.awt.Point;35import java.awt.Robot;36import java.awt.event.InputEvent;37import javax.swing.JComboBox;38import javax.swing.JComponent;39import javax.swing.JFrame;40import javax.swing.SwingUtilities;4142public class ComboPopupTest {43JFrame frame = null;44JComboBox<String> comboBox = null;45private volatile Point p = null;46private volatile Dimension d = null;4748void blockTillDisplayed(JComponent comp) throws Exception {49while (p == null) {50try {51SwingUtilities.invokeAndWait(() -> {52p = comp.getLocationOnScreen();53d = comboBox.getSize();54});55} catch (IllegalStateException e) {56try {57Thread.sleep(1000);58} catch (InterruptedException ie) {59}60}61}62}6364public static void main(String[] args) throws Exception {65new ComboPopupTest();66}6768public ComboPopupTest() throws Exception {69try {70Robot robot = new Robot();71robot.setAutoDelay(200);72SwingUtilities.invokeAndWait(() -> start());73blockTillDisplayed(comboBox);74robot.waitForIdle();75robot.mouseMove(p.x + d.width-1, p.y + d.height/2);76robot.mousePress(InputEvent.BUTTON1_MASK);77robot.mouseRelease(InputEvent.BUTTON1_MASK);78robot.waitForIdle();7980System.out.println("popmenu visible " + comboBox.isPopupVisible());81if (!comboBox.isPopupVisible()) {82throw new RuntimeException("combobox popup is not visible");83}84} finally {85if (frame != null) { SwingUtilities.invokeAndWait(()->frame.dispose()); }86}87}8889public void start() {90frame = new JFrame();91frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);92Container contentPane = frame.getContentPane();93comboBox = new JComboBox<String>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" });94contentPane.setLayout(new FlowLayout());95contentPane.add(comboBox);96frame.setLocationRelativeTo(null);97frame.pack();98frame.setVisible(true);99}100}101102103104