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