Path: blob/master/test/jdk/java/awt/Choice/ChoicePopupLocation/ChoicePopupLocation.java
41152 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Choice;24import java.awt.FlowLayout;25import java.awt.Frame;26import java.awt.GraphicsConfiguration;27import java.awt.GraphicsDevice;28import java.awt.GraphicsEnvironment;29import java.awt.Insets;30import java.awt.Point;31import java.awt.Rectangle;32import java.awt.Robot;33import java.awt.Toolkit;34import java.awt.event.InputEvent;3536/**37* @test38* @key headful39* @bug 817644840* @run main/timeout=300 ChoicePopupLocation41*/42public final class ChoicePopupLocation {4344private static final int SIZE = 350;45private static int frameWidth;4647public static void main(final String[] args) throws Exception {48GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();49GraphicsDevice[] sds = ge.getScreenDevices();50Point left = null;51Point right = null;52for (GraphicsDevice sd : sds) {53GraphicsConfiguration gc = sd.getDefaultConfiguration();54Rectangle bounds = gc.getBounds();55if (left == null || left.x > bounds.x) {56left = new Point(bounds.x, bounds.y + bounds.height / 2);57}58if (right == null || right.x < bounds.x + bounds.width) {59right = new Point(bounds.x + bounds.width,60bounds.y + bounds.height / 2);61}6263Point point = new Point(bounds.x, bounds.y);64Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);65while (point.y < bounds.y + bounds.height - insets.bottom - SIZE ) {66while (point.x < bounds.x + bounds.width - insets.right - SIZE) {67test(point);68point.translate(bounds.width / 5, 0);69}70point.setLocation(bounds.x, point.y + bounds.height / 5);71}7273}74if (left != null) {75left.translate(-50, 0);76test(left);77}78if (right != null) {79right.translate(-frameWidth + 50, 0);80test(right);81}82}8384private static void test(final Point tmp) throws Exception {85Choice choice = new Choice();86for (int i = 1; i < 7; i++) {87choice.add("Long-long-long-long-long text in the item-" + i);88}89Frame frame = new Frame();90try {91frame.setAlwaysOnTop(true);92frame.setLayout(new FlowLayout());93frame.add(choice);94frame.pack();95frameWidth = frame.getWidth();96frame.setSize(frameWidth, SIZE);97frame.setVisible(true);98frame.setLocation(tmp.x, tmp.y);99openPopup(choice);100} finally {101frame.dispose();102}103}104105private static void openPopup(final Choice choice) throws Exception {106Robot robot = new Robot();107robot.setAutoDelay(100);108robot.setAutoWaitForIdle(true);109robot.waitForIdle();110Point pt = choice.getLocationOnScreen();111robot.mouseMove(pt.x + choice.getWidth() / 2,112pt.y + choice.getHeight() / 2);113robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);114robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);115int x = pt.x + choice.getWidth() / 2;116int y = pt.y + choice.getHeight() / 2 + 70;117robot.mouseMove(x, y);118robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);119robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);120robot.waitForIdle();121if (choice.getSelectedIndex() == 0) {122throw new RuntimeException();123}124}125}126127128