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.Choice;
  25 import java.awt.FlowLayout;
  26 import java.awt.Frame;
  27 import java.awt.GraphicsConfiguration;
  28 import java.awt.GraphicsDevice;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.Insets;
  31 import java.awt.Point;
  32 import java.awt.Rectangle;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.event.InputEvent;
  36 
  37 /**
  38  * @test
  39  * @bug 8176448
  40  * @run main/timeout=300 ChoicePopupLocation
  41  */
  42 public final class ChoicePopupLocation {
  43 
  44     private static final int SIZE = 350;
  45 
  46     public static void main(final String[] args) throws Exception {
  47         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  48         GraphicsDevice[] sds = ge.getScreenDevices();
  49         Point left = null;
  50         for (GraphicsDevice sd : sds) {
  51             GraphicsConfiguration gc = sd.getDefaultConfiguration();
  52             Rectangle bounds = gc.getBounds();
  53             if (left == null || left.x > bounds.x) {
  54                 left = new Point(bounds.x, bounds.y + bounds.height / 2);
  55             }
  56 
  57             Point point = new Point(bounds.x, bounds.y);
  58             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  59             while (point.y < bounds.y + bounds.height - insets.bottom - SIZE ) {
  60                 while (point.x < bounds.x + bounds.width - insets.right - SIZE) {
  61                     test(point);
  62                     point.translate(bounds.width / 5, 0);
  63                 }
  64                 point.setLocation(bounds.x, point.y + bounds.height / 5);
  65             }
  66 
  67         }
  68         if (left != null) {
  69             left.translate(-50, 0);
  70             test(left);
  71         }
  72     }
  73 
  74     private static void test(final Point tmp) throws Exception {
  75         Choice choice = new Choice();
  76         for (int i = 1; i < 7; i++) {
  77             choice.add("Long-long-long-long-long text in the item-" + i);
  78         }
  79         Frame frame = new Frame();
  80         try {
  81             frame.setAlwaysOnTop(true);
  82             frame.setLayout(new FlowLayout());
  83             frame.add(choice);
  84             frame.pack();
  85             frame.setSize(frame.getWidth(), SIZE);
  86             frame.setVisible(true);
  87             frame.setLocation(tmp.x, tmp.y);
  88             openPopup(choice);
  89         } finally {
  90             frame.dispose();
  91         }
  92     }
  93 
  94     private static void openPopup(final Choice choice) throws Exception {
  95         Robot robot = new Robot();
  96         robot.setAutoDelay(100);
  97         robot.setAutoWaitForIdle(true);
  98         robot.waitForIdle();
  99         Point pt = choice.getLocationOnScreen();
 100         robot.mouseMove(pt.x + choice.getWidth() / 2,
 101                         pt.y + choice.getHeight() / 2);
 102         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 103         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 104         int x = pt.x + choice.getWidth() / 2;
 105         int y = pt.y + choice.getHeight() / 2 + 70;
 106         robot.mouseMove(x, y);
 107         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 108         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 109         robot.waitForIdle();
 110         if (choice.getSelectedIndex() == 0) {
 111             throw new RuntimeException();
 112         }
 113     }
 114 }