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 import java.awt.EventQueue;
  25 import java.awt.FlowLayout;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Insets;
  30 import java.awt.Point;
  31 import java.awt.Rectangle;
  32 import java.awt.Robot;
  33 import java.awt.Toolkit;
  34 import java.awt.event.InputEvent;
  35 
  36 import javax.swing.JComboBox;
  37 import javax.swing.JFrame;
  38 import javax.swing.UIManager;
  39 
  40 /**
  41  * @test
  42  * @bug 8176448
  43  * @run main/timeout=600 JComboBoxPopupLocation
  44  */
  45 public final class JComboBoxPopupLocation {
  46 
  47     private static final int SIZE = 300;
  48     public static final String PROPERTY_NAME = "JComboBox.isPopDown";
  49     private static volatile Robot robot;
  50     private static volatile JComboBox<String> comboBox;
  51     private static volatile JFrame frame;
  52 
  53     public static void main(final String[] args) throws Exception {
  54         robot = new Robot();
  55         robot.setAutoDelay(100);
  56         robot.setAutoWaitForIdle(true);
  57         GraphicsEnvironment ge =
  58                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  59         GraphicsDevice[] sds = ge.getScreenDevices();
  60         UIManager.LookAndFeelInfo[] lookAndFeelArray =
  61                 UIManager.getInstalledLookAndFeels();
  62         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  63             System.setProperty(PROPERTY_NAME, "true");
  64             step(sds, lookAndFeelItem);
  65             if (lookAndFeelItem.getClassName().contains("Aqua")) {
  66                 System.setProperty(PROPERTY_NAME, "false");
  67                 step(sds, lookAndFeelItem);
  68             }
  69         }
  70     }
  71 
  72     private static void step(GraphicsDevice[] sds,
  73                              UIManager.LookAndFeelInfo lookAndFeelItem)
  74             throws Exception {
  75         UIManager.setLookAndFeel(lookAndFeelItem.getClassName());
  76         Point left = null;
  77         for (final GraphicsDevice sd : sds) {
  78             GraphicsConfiguration gc = sd.getDefaultConfiguration();
  79             Rectangle bounds = gc.getBounds();
  80             if (left == null || left.x > bounds.x) {
  81                 left = new Point(bounds.x, bounds.y + bounds.height / 2);
  82             }
  83             Point point = new Point(bounds.x, bounds.y);
  84             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  85             while (point.y < bounds.y + bounds.height - insets.bottom - SIZE ) {
  86                 while (point.x < bounds.x + bounds.width - insets.right - SIZE) {
  87                     try {
  88                         EventQueue.invokeAndWait(() -> {
  89                             setup(point);
  90                         });
  91                         robot.waitForIdle();
  92                         test(comboBox);
  93                         robot.waitForIdle();
  94                         validate(comboBox);
  95                         robot.waitForIdle();
  96                         point.translate(bounds.width / 5, 0);
  97                     } finally {
  98                         dispose();
  99                     }
 100                 }
 101                 point.setLocation(bounds.x, point.y + bounds.height / 5);
 102             }
 103         }
 104         if (left != null) {
 105             final Point finalLeft = left;
 106             finalLeft.translate(-50, 0);
 107             try {
 108                 EventQueue.invokeAndWait(() -> {
 109                     setup(finalLeft);
 110                 });
 111                 robot.waitForIdle();
 112                 test(comboBox);
 113                 robot.waitForIdle();
 114                 validate(comboBox);
 115             } finally {
 116                 dispose();
 117             }
 118         }
 119     }
 120 
 121     private static void dispose() throws Exception {
 122         EventQueue.invokeAndWait(() -> {
 123             if (frame != null) {
 124                 frame.dispose();
 125             }
 126         });
 127     }
 128 
 129     private static void setup(final Point tmp) {
 130         comboBox = new JComboBox<>();
 131         for (int i = 1; i < 7; i++) {
 132             comboBox.addItem("Long-long-long-long-long text in the item-" + i);
 133         }
 134         String property = System.getProperty(PROPERTY_NAME);
 135         comboBox.putClientProperty(PROPERTY_NAME, Boolean.valueOf(property));
 136         frame = new JFrame();
 137         frame.setAlwaysOnTop(true);
 138         frame.setLayout(new FlowLayout());
 139         frame.add(comboBox);
 140         frame.pack();
 141         frame.setSize(frame.getWidth(), SIZE);
 142         frame.setVisible(true);
 143         frame.setLocation(tmp.x, tmp.y);
 144     }
 145 
 146     private static void test(final JComboBox comboBox) throws Exception {
 147         Point pt = comboBox.getLocationOnScreen();
 148         robot.mouseMove(pt.x + comboBox.getWidth() / 2,
 149                         pt.y + comboBox.getHeight() / 2);
 150         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 151         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 152         int x = pt.x + comboBox.getWidth() / 2;
 153         int y = pt.y + comboBox.getHeight() / 2 + 70;
 154         robot.mouseMove(x, y);
 155         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 156         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 157     }
 158 
 159     private static void validate(final JComboBox comboBox) throws Exception {
 160         EventQueue.invokeAndWait(() -> {
 161             if (comboBox.getSelectedIndex() == 0) {
 162                 throw new RuntimeException();
 163             }
 164         });
 165     }
 166 }