Path: blob/master/test/jdk/javax/swing/JComboBox/JComboBoxPopupMenuEventTest.java
66644 views
/*1* Copyright (c) 2001, 2022, 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*/2223import java.awt.Point;24import java.awt.Robot;25import java.awt.event.InputEvent;26import java.awt.event.KeyEvent;27import java.util.Arrays;28import java.util.List;29import java.util.concurrent.CountDownLatch;30import java.util.concurrent.TimeUnit;31import java.util.concurrent.atomic.AtomicBoolean;32import java.util.concurrent.atomic.AtomicReference;33import java.util.stream.Collectors;34import javax.swing.JComboBox;35import javax.swing.JComponent;36import javax.swing.JFrame;37import javax.swing.JPanel;38import javax.swing.JTextField;39import javax.swing.SwingUtilities;40import javax.swing.UIManager;41import javax.swing.UIManager.LookAndFeelInfo;42import javax.swing.UnsupportedLookAndFeelException;43import javax.swing.event.PopupMenuEvent;44import javax.swing.event.PopupMenuListener;4546import static javax.swing.UIManager.getInstalledLookAndFeels;4748/*49* @test50* @key headful51* @bug 4287690 433105852* @summary This testcase tests RFE-4287690 and RFE-4331058 requests,53* JComboBox should send drop down visible as well as invisible events.54* @run main JComboBoxPopupMenuEventTest55*/56public class JComboBoxPopupMenuEventTest {5758private static final String[] compStrs =59{"Apple", "Citibank", "Cisco", "Cienna", "Oracle", "IBM"};60private static Robot robot;61private static JComboBox comboBox;62private static JTextField searchTextField;63private static CountDownLatch popupMenuVisibleLatch;64private static CountDownLatch popupMenuInvisibleLatch;65private static JFrame frame;6667public static void main(String[] args) throws Exception {68robot = new Robot();69robot.setAutoWaitForIdle(true);70robot.setAutoDelay(200);71List<String> lafs = Arrays.stream(getInstalledLookAndFeels())72.map(LookAndFeelInfo::getClassName)73.collect(Collectors.toList());74for (final String laf : lafs) {75try {76popupMenuVisibleLatch = new CountDownLatch(1);77popupMenuInvisibleLatch = new CountDownLatch(1);78AtomicBoolean lafSetSuccess = new AtomicBoolean(false);79SwingUtilities.invokeAndWait(() -> {80lafSetSuccess.set(setLookAndFeel(laf));81if (lafSetSuccess.get()) {82createUI();83}84});85if (!lafSetSuccess.get()) {86continue;87}88robot.waitForIdle();8990mouseClick(searchTextField);91hitKeys(KeyEvent.VK_C, KeyEvent.VK_I);92mouseClick(comboBox);9394// Verifying whether popupMenuWillBecomeVisible method of95// PopupMenuListener gets called when popup menu appears.96if (!popupMenuVisibleLatch.await(3, TimeUnit.SECONDS)) {97throw new RuntimeException(98"Waited too long, but popupMenuWillBecomeVisible " +99"not yet got called for " + laf);100}101102hitKeys(KeyEvent.VK_ENTER);103104// Verifying whether popupMenuWillBecomeInvisible method of105// PopupMenuListener gets called when popup menu disappears.106if (!popupMenuInvisibleLatch.await(3, TimeUnit.SECONDS)) {107throw new RuntimeException(108"Waited too long, but popupMenuWillBecomeInvisible " +109"not yet got called for " + laf);110}111112System.out.println("Test passed for " + laf);113} finally {114SwingUtilities.invokeAndWait(115JComboBoxPopupMenuEventTest::disposeFrame);116}117}118}119120private static void mouseClick(JComponent jComponent) throws Exception {121final Point location = getLocationOnScreen(jComponent);122robot.mouseMove(location.x + 8, location.y + 8);123robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);124robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);125}126127private static Point getLocationOnScreen(JComponent jComponent)128throws Exception {129final AtomicReference<Point> loc = new AtomicReference<>();130SwingUtilities.invokeAndWait(131() -> loc.set(jComponent.getLocationOnScreen()));132return loc.get();133}134135private static void hitKeys(int... keys) {136for (int key : keys) {137robot.keyPress(key);138}139140for (int i = keys.length - 1; i >= 0; i--) {141robot.keyRelease(keys[i]);142}143}144145public static void createUI() {146frame = new JFrame();147JPanel panel = new JPanel();148searchTextField = new JTextField(6);149panel.add(searchTextField);150comboBox = new JComboBox(compStrs);151panel.add(comboBox);152comboBox.addPopupMenuListener(new PopupMenuListener() {153public void popupMenuWillBecomeVisible(PopupMenuEvent e) {154System.out.println("popupMenuWillBecomeVisible() got called");155popupMenuVisibleLatch.countDown();156comboBox.removeAllItems();157String text = searchTextField.getText().trim();158Arrays.stream(compStrs)159.filter(str -> str.toLowerCase().startsWith(text))160.forEach(str -> comboBox.addItem(str));161}162163public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {164System.out.println("popupMenuWillBecomeInvisible() got called");165popupMenuInvisibleLatch.countDown();166}167168public void popupMenuCanceled(PopupMenuEvent e) {169}170});171172frame.setContentPane(panel);173frame.setSize(250, 100);174frame.setLocationRelativeTo(null);175frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);176frame.setVisible(true);177}178179private static boolean setLookAndFeel(String lafName) {180try {181UIManager.setLookAndFeel(lafName);182} catch (UnsupportedLookAndFeelException ignored) {183System.out.println("Ignoring Unsupported L&F: " + lafName);184return false;185} catch (ClassNotFoundException | InstantiationException186| IllegalAccessException e) {187throw new RuntimeException(e);188}189return true;190}191192private static void disposeFrame() {193if (frame != null) {194frame.dispose();195frame = null;196}197}198199}200201202