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