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