Path: blob/master/test/jdk/javax/swing/JComboBox/4743225/bug4743225.java
41153 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/*24* @test25* @key headful26* @bug 474322527* @summary Size of JComboBox list is wrong when list is populated via PopupMenuListener28* @author Alexander Potochkin29*/3031import javax.accessibility.AccessibleContext;32import javax.swing.JComboBox;33import javax.swing.JFrame;34import javax.swing.SwingUtilities;35import javax.swing.event.PopupMenuEvent;36import javax.swing.event.PopupMenuListener;37import javax.swing.plaf.basic.BasicComboPopup;38import java.awt.FlowLayout;39import java.awt.Point;40import java.awt.Robot;41import java.awt.event.InputEvent;4243public class bug4743225 extends JFrame {4445private static JComboBox cb;46private static volatile boolean flag;4748public bug4743225() {49setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);50setLayout(new FlowLayout());51cb = new JComboBox(new Object[] {"one", "two", "three"});52cb.addPopupMenuListener(new PopupMenuListener() {53public void popupMenuWillBecomeVisible(PopupMenuEvent e) {54cb.addItem("Test");55}5657public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {58}5960public void popupMenuCanceled(PopupMenuEvent e) {61}62});63add(cb);64pack();65setLocationRelativeTo(null);66}6768public static BasicComboPopup getPopup() {69AccessibleContext c = cb.getAccessibleContext();70for(int i = 0; i < c.getAccessibleChildrenCount(); i ++) {71if (c.getAccessibleChild(i) instanceof BasicComboPopup) {72return (BasicComboPopup) c.getAccessibleChild(i);73}74}75throw new AssertionError("No BasicComboPopup found");76}7778public static void main(String... args) throws Exception {7980Robot robot = new Robot();81robot.setAutoDelay(100);8283SwingUtilities.invokeAndWait(new Runnable() {84public void run() {85new bug4743225().setVisible(true);86}87});88robot.waitForIdle();8990// calling this method from main thread is ok91Point point = cb.getLocationOnScreen();92robot.mouseMove(point.x + 10, point.y + 10);93robot.mousePress(InputEvent.BUTTON1_MASK);94robot.mouseRelease(InputEvent.BUTTON1_MASK);95robot.waitForIdle();9697SwingUtilities.invokeAndWait(new Runnable() {98public void run() {99if(getPopup().getList().getLastVisibleIndex() == 3) {100flag = true;101}102}103});104105if (!flag) {106throw new RuntimeException("The ComboBox popup wasn't correctly updated");107}108}109}110111112