1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 8182031
  26  * @summary  Verifies if ComboBox Popup opens and closes immediately
  27  * @run main ComboPopupTest
  28  */
  29 import java.awt.Container;
  30 import java.awt.Dimension;
  31 import java.awt.FlowLayout;
  32 import java.awt.Point;
  33 import java.awt.Robot;
  34 import java.awt.event.InputEvent;
  35 import javax.swing.JComboBox;
  36 import javax.swing.JComponent;
  37 import javax.swing.JFrame;
  38 import javax.swing.SwingUtilities;
  39 
  40 public class ComboPopupTest {
  41     JFrame frame = null;
  42     JComboBox<String> comboBox = null;
  43     private volatile Point p = null;
  44     private volatile Dimension d = null;
  45 
  46     void blockTillDisplayed(JComponent comp) throws Exception {
  47         while (p == null) {
  48             try {
  49                 SwingUtilities.invokeAndWait(() -> {
  50                     p = comp.getLocationOnScreen();
  51                     d = comboBox.getSize();
  52                 });
  53             } catch (IllegalStateException e) {
  54                 try {
  55                     Thread.sleep(1000);
  56                 } catch (InterruptedException ie) {
  57                 }
  58             }
  59         }
  60     }
  61 
  62     public static void main(String[] args) throws Exception {
  63         new ComboPopupTest();
  64     }
  65 
  66     public ComboPopupTest() throws Exception {
  67         try {
  68             Robot robot = new Robot();
  69             robot.setAutoDelay(200);
  70             SwingUtilities.invokeAndWait(() -> start());
  71             blockTillDisplayed(comboBox);
  72             robot.waitForIdle();
  73             robot.mouseMove(p.x + d.width-1, p.y + d.height/2);
  74             robot.mousePress(InputEvent.BUTTON1_MASK);
  75             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  76             robot.waitForIdle();
  77 
  78             System.out.println("popmenu visible " + comboBox.isPopupVisible());
  79             if (!comboBox.isPopupVisible()) {
  80                 throw new RuntimeException("combobox popup is not visible");
  81             }
  82         } finally {
  83             SwingUtilities.invokeAndWait(()->frame.dispose());
  84         }
  85     }
  86 
  87     public void start() {
  88         frame = new JFrame();
  89         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90         Container contentPane = frame.getContentPane();
  91         comboBox = new JComboBox<String>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" });
  92         contentPane.setLayout(new FlowLayout());
  93         contentPane.add(comboBox);
  94         frame.setLocationRelativeTo(null);
  95         frame.pack();
  96         frame.setVisible(true);
  97     }
  98 }
  99