Path: blob/master/test/jdk/java/awt/Choice/PopupPosTest/PopupPosTest.java
41152 views
/*1* Copyright (c) 2004, 2018, 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*/2223/*24@test25@key headful26@bug 504415027@summary Tests that pupup doesn't popdown if no space to display under28@requires (os.family == "linux")29@run main PopupPosTest30*/3132import java.awt.*;33import java.awt.event.*;3435public class PopupPosTest {3637public static void main(final String[] args) {38Frame frame = new TestFrame();39}40}4142class TestFrame extends Frame implements ItemListener {43Robot robot;44Toolkit tk = Toolkit.getDefaultToolkit();45Choice choice = new Choice();46boolean indexChanged = false;47final static int INITIAL_ITEM = 99;48volatile boolean stateChanged;4950public TestFrame() {51for (int i = 0; i < 100; i++) {52choice.addItem("Item Item Item " + i);53}54choice.addItemListener(this);5556choice.select(INITIAL_ITEM);57choice.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 100));5859add(choice, BorderLayout.CENTER);60Dimension screen = tk.getScreenSize();61setSize(screen.width - 10, screen.height - 70);62setVisible(true);63toFront();64try {65robot = new Robot();66robot.setAutoDelay(50);67robot.waitForIdle();68// fix for 6175418. When we take "choice.getHeight()/2"69// divider 2 is not sufficiently big to hit into the70// small box Choice. We should use bigger divider to get71// smaller value choice.getHeight()/i. 4 is sufficient.72Point pt = choice.getLocationOnScreen();73// click on 1/4 of Choice's height74mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,75pt.y + choice.getHeight()/4);7677// click on center of Choice's height78mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,79pt.y + choice.getHeight()/2);8081// click on 3/4 of Choice's height82mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,83pt.y + choice.getHeight()*3/4);84// testing that ItemEvent doesn't generated on a simple85// mouse click when the dropdown appears under mouse : 642506786stateChanged = false;87openChoice();88closeChoice();89} catch (Throwable e) {90throw new RuntimeException("The test was not completed.\n\n" + e);91}9293if (!indexChanged){94throw new RuntimeException("Test failed. Another item wasn't selected.");95}9697if(stateChanged){98throw new RuntimeException("Test failed. ItemEvent was generated on a simple mouse click when the dropdown appears under mouse");99}100}// start()101102public void itemStateChanged(ItemEvent ie) {103System.out.println("choice.stateChanged = "+ ie);104stateChanged = true;105}106107public void mouseMoveAndPressOnChoice(int x, int y){108openChoice();109robot.mouseMove(x, y);110robot.mousePress(InputEvent.BUTTON1_MASK);111robot.delay(30);112robot.mouseRelease(InputEvent.BUTTON1_MASK);113robot.waitForIdle();114//should close choice after each test stage115closeChoice();116checkSelectedIndex();117}118119public void openChoice(){120Point pt = choice.getLocationOnScreen();121robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight()/4,122pt.y + choice.getHeight()/2);123robot.mousePress(InputEvent.BUTTON1_MASK);124robot.delay(30);125robot.mouseRelease(InputEvent.BUTTON1_MASK);126robot.waitForIdle();127}128public void closeChoice(){129robot.keyPress(KeyEvent.VK_ESCAPE);130robot.keyRelease(KeyEvent.VK_ESCAPE);131robot.waitForIdle();132}133134public void checkSelectedIndex(){135if (choice.getSelectedIndex() != INITIAL_ITEM) {136System.out.println("choice.getSelectedIndex = "+ choice.getSelectedIndex());137indexChanged = true;138}139}140}// class TestFrame141142143