Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JComboBox/4743225/bug4743225.java
38853 views
/*1* Copyright (c) 2010, 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/* @test24* @bug 474322525* @summary Size of JComboBox list is wrong when list is populated via PopupMenuListener26* @author Alexander Potochkin27*/2829import javax.accessibility.AccessibleContext;30import javax.swing.JComboBox;31import javax.swing.JFrame;32import javax.swing.SwingUtilities;33import javax.swing.event.PopupMenuEvent;34import javax.swing.event.PopupMenuListener;35import javax.swing.plaf.basic.BasicComboPopup;36import java.awt.FlowLayout;37import java.awt.Point;38import java.awt.Robot;39import java.awt.event.InputEvent;4041public class bug4743225 extends JFrame {4243private static JComboBox cb;44private static volatile boolean flag;4546public bug4743225() {47setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);48setLayout(new FlowLayout());49cb = new JComboBox(new Object[] {"one", "two", "three"});50cb.addPopupMenuListener(new PopupMenuListener() {51public void popupMenuWillBecomeVisible(PopupMenuEvent e) {52cb.addItem("Test");53}5455public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {56}5758public void popupMenuCanceled(PopupMenuEvent e) {59}60});61add(cb);62pack();63}6465public static BasicComboPopup getPopup() {66AccessibleContext c = cb.getAccessibleContext();67for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) {68if (c.getAccessibleChild(i) instanceof BasicComboPopup) {69return (BasicComboPopup) c.getAccessibleChild(i);70}71}72throw new AssertionError("No BasicComboPopup found");73}7475public static void main(String... args) throws Exception {7677Robot robot = new Robot();78robot.setAutoDelay(20);7980SwingUtilities.invokeAndWait(new Runnable() {81public void run() {82new bug4743225().setVisible(true);83}84});85robot.waitForIdle();8687// calling this method from main thread is ok88Point point = cb.getLocationOnScreen();89robot.mouseMove(point.x + 10, point.y + 10);90robot.mousePress(InputEvent.BUTTON1_MASK);91robot.mouseRelease(InputEvent.BUTTON1_MASK);92robot.waitForIdle();9394SwingUtilities.invokeAndWait(new Runnable() {95public void run() {96if(getPopup().getList().getLastVisibleIndex() == 3) {97flag = true;98}99}100});101102if (!flag) {103throw new RuntimeException("The ComboBox popup wasn't correctly updated");104}105}106}107108109