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     private static int frameWidth;
  46 
  47     public static void main(final String[] args) throws Exception {
  48         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  49         GraphicsDevice[] sds = ge.getScreenDevices();
  50         Point left = null;
  51         Point right = null;
  52         for (GraphicsDevice sd : sds) {
  53             GraphicsConfiguration gc = sd.getDefaultConfiguration();
  54             Rectangle bounds = gc.getBounds();
  55             if (left == null || left.x > bounds.x) {
  56                 left = new Point(bounds.x, bounds.y + bounds.height / 2);
  57             }
  58             if (right == null || right.x < bounds.x + bounds.width) {
  59                 right = new Point(bounds.x + bounds.width,
  60                                   bounds.y + bounds.height / 2);
  61             }
  62 
  63             Point point = new Point(bounds.x, bounds.y);
  64             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
  65             while (point.y < bounds.y + bounds.height - insets.bottom - SIZE ) {
  66                 while (point.x < bounds.x + bounds.width - insets.right - SIZE) {
  67                     test(point);
  68                     point.translate(bounds.width / 5, 0);
  69                 }
  70                 point.setLocation(bounds.x, point.y + bounds.height / 5);
  71             }
  72 
  73         }
  74         if (left != null) {
  75             left.translate(-50, 0);
  76             test(left);
  77         }
  78         if (right != null) {
  79             right.translate(-frameWidth + 50, 0);
  80             test(right);
  81         }
  82     }
  83 
  84     private static void test(final Point tmp) throws Exception {
  85         Choice choice = new Choice();
  86         for (int i = 1; i < 7; i++) {
  87             choice.add("Long-long-long-long-long text in the item-" + i);
  88         }
  89         Frame frame = new Frame();
  90         try {
  91             frame.setAlwaysOnTop(true);
  92             frame.setLayout(new FlowLayout());
  93             frame.add(choice);
  94             frame.pack();
  95             frameWidth = frame.getWidth();
  96             frame.setSize(frameWidth, SIZE);
  97             frame.setVisible(true);
  98             frame.setLocation(tmp.x, tmp.y);
  99             openPopup(choice);
 100         } finally {
 101             frame.dispose();
 102         }
 103     }
 104 
 105     private static void openPopup(final Choice choice) throws Exception {
 106         Robot robot = new Robot();
 107         robot.setAutoDelay(100);
 108         robot.setAutoWaitForIdle(true);
 109         robot.waitForIdle();
 110         Point pt = choice.getLocationOnScreen();
 111         robot.mouseMove(pt.x + choice.getWidth() / 2,
 112                         pt.y + choice.getHeight() / 2);
 113         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 114         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 115         int x = pt.x + choice.getWidth() / 2;
 116         int y = pt.y + choice.getHeight() / 2 + 70;
 117         robot.mouseMove(x, y);
 118         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 119         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 120         robot.waitForIdle();
 121         if (choice.getSelectedIndex() == 0) {
 122             throw new RuntimeException();
 123         }
 124     }
 125 }