Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/PopupPosTest/PopupPosTest.java
47490 views
/*1* Copyright (c) 2004, 2014, 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/*24test25@bug 504415026@summary Tests that pupup doesn't popdown if no space to display under27@author andrei.dmitriev area=awt.choice28@library ../../../../lib/testlibrary29@build jdk.testlibrary.OSInfo30@run applet PopupPosTest.html31*/3233import java.applet.Applet;34import java.awt.*;35import java.awt.event.*;3637import jdk.testlibrary.OSInfo;3839public class PopupPosTest extends Applet40{41public void start ()42{43if(OSInfo.getOSType().equals(OSInfo.OSType.MACOSX)) {44// On OS X, popup isn't under the mouse45return;46}47Frame frame = new TestFrame();48}49}5051class TestFrame extends Frame implements ItemListener {52Robot robot;53Toolkit tk = Toolkit.getDefaultToolkit();54Choice choice = new Choice();55boolean indexChanged = false;56final static int INITIAL_ITEM = 99;57volatile boolean stateChanged;5859public TestFrame() {60for (int i = 0; i < 100; i++) {61choice.addItem("Item Item Item " + i);62}63choice.addItemListener(this);6465choice.select(INITIAL_ITEM);66choice.setFont(new Font("Courier", Font.BOLD + Font.ITALIC, 100));6768add(choice, BorderLayout.CENTER);69Dimension screen = tk.getScreenSize();70setSize(screen.width - 10, screen.height - 70);71setVisible(true);72toFront();73try {74robot = new Robot();75robot.setAutoDelay(50);76robot.waitForIdle();77// fix for 6175418. When we take "choice.getHeight()/2"78// divider 2 is not sufficiently big to hit into the79// small box Choice. We should use bigger divider to get80// smaller value choice.getHeight()/i. 4 is sufficient.81Point pt = choice.getLocationOnScreen();82// click on 1/4 of Choice's height83mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,84pt.y + choice.getHeight()/4);8586// click on center of Choice's height87mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,88pt.y + choice.getHeight()/2);8990// click on 3/4 of Choice's height91mouseMoveAndPressOnChoice(pt.x + choice.getWidth()/2,92pt.y + choice.getHeight()*3/4);93// testing that ItemEvent doesn't generated on a simple94// mouse click when the dropdown appears under mouse : 642506795stateChanged = false;96openChoice();97closeChoice();98} catch (Throwable e) {99throw new RuntimeException("The test was not completed.\n\n" + e);100}101102if (!indexChanged){103throw new RuntimeException("Test failed. Another item wasn't selected.");104}105106if(stateChanged){107throw new RuntimeException("Test failed. ItemEvent was generated on a simple mouse click when the dropdown appears under mouse");108}109}// start()110111public void itemStateChanged(ItemEvent ie) {112System.out.println("choice.stateChanged = "+ ie);113stateChanged = true;114}115116public void mouseMoveAndPressOnChoice(int x, int y){117openChoice();118robot.mouseMove(x, y);119robot.mousePress(InputEvent.BUTTON1_MASK);120robot.delay(30);121robot.mouseRelease(InputEvent.BUTTON1_MASK);122robot.waitForIdle();123//should close choice after each test stage124closeChoice();125checkSelectedIndex();126}127128public void openChoice(){129Point pt = choice.getLocationOnScreen();130robot.mouseMove(pt.x + choice.getWidth() - choice.getHeight()/4,131pt.y + choice.getHeight()/2);132robot.mousePress(InputEvent.BUTTON1_MASK);133robot.delay(30);134robot.mouseRelease(InputEvent.BUTTON1_MASK);135robot.waitForIdle();136}137public void closeChoice(){138robot.keyPress(KeyEvent.VK_ESCAPE);139robot.keyRelease(KeyEvent.VK_ESCAPE);140robot.waitForIdle();141}142143public void checkSelectedIndex(){144if (choice.getSelectedIndex() != INITIAL_ITEM) {145System.out.println("choice.getSelectedIndex = "+ choice.getSelectedIndex());146indexChanged = true;147}148}149}// class TestFrame150151152