Path: blob/master/test/jdk/java/awt/Choice/UnfocusableToplevel/UnfocusableToplevel.java
41152 views
/*1* Copyright (c) 2007, 2020, 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 6566434 803946727@library ../../regtesthelpers28@build Util Sysout AbstractTest29@summary Choice in unfocusable window responds to keyboard30@author Andrei Dmitriev: area=awt-choice31@run main UnfocusableToplevel32*/3334/**35* UnfocusableToplevel.java36*37* summary:38*/3940import java.awt.AWTEvent;41import java.awt.Choice;42import java.awt.FlowLayout;43import java.awt.Frame;44import java.awt.Robot;45import java.awt.Window;46import java.awt.event.ItemEvent;47import java.awt.event.ItemListener;48import java.awt.event.KeyAdapter;49import java.awt.event.KeyEvent;5051import test.java.awt.regtesthelpers.AbstractTest;52import test.java.awt.regtesthelpers.Util;5354public class UnfocusableToplevel {5556final static Robot robot = Util.createRobot();57final static int REASONABLE_PATH_TIME = 5000;5859public static void main(String []s)60{61Frame f = new Frame();62Window w = new Window(f);63final Choice ch = new Choice();6465ch.add("item 1");66ch.add("item 2");67ch.add("item 3");68ch.add("item 4");69ch.add("item 5");70w.add(ch);71w.setLayout(new FlowLayout());72w.setSize(200, 200);7374// Note that Window w is non focusable. Key press events will not be75// consumed by w, but by any previously focused window & this can76// disturb the environment. So creating tempFrameToHoldFocus frame,77// to consume key press events.78Frame tempFrameToHoldFocus = new Frame();79tempFrameToHoldFocus.setSize(300, 300);80tempFrameToHoldFocus.setLocationRelativeTo(null);81tempFrameToHoldFocus.setVisible(true);82Util.waitForIdle(robot);8384tempFrameToHoldFocus.requestFocus();85Util.clickOnComp(tempFrameToHoldFocus, robot);86Util.waitForIdle(robot);8788ch.addKeyListener(new KeyAdapter(){89public void keyTyped(KeyEvent e){90traceEvent("keytyped", e);91}92public void keyPressed(KeyEvent e){93traceEvent("keypress", e);94}95public void keyReleased(KeyEvent e){96traceEvent("keyrelease", e);97}98});99100ch.addItemListener(new ItemListener(){101public void itemStateChanged(ItemEvent ie){102traceEvent("stateChanged", ie);103}104});105w.setLocationRelativeTo(null);106w.setVisible(true);107108Util.waitForIdle(robot);109110Util.clickOnComp(ch, robot);111Util.waitForIdle(robot);112113// will not test if the dropdown become opened as there is no reliable114// technique to accomplish that rather then checking color of dropdown115// Will suppose that the dropdown appears116117testKeys();118Util.waitForIdle(robot);119120tempFrameToHoldFocus.dispose();121w.dispose();122f.dispose();123}124125private static void testKeys(){126typeKey(KeyEvent.VK_UP);127typeKey(KeyEvent.VK_DOWN);128typeKey(KeyEvent.VK_K);129typeKey(KeyEvent.VK_PAGE_UP);130typeKey(KeyEvent.VK_PAGE_DOWN);131}132133private static void typeKey(int keyChar){134try {135robot.keyPress(keyChar);136robot.delay(5);137} finally {138robot.keyRelease(keyChar);139}140robot.delay(100);141}142143private static void traceEvent(String message, AWTEvent e){144AbstractTest.fail(message + " " + e.toString());145}146}147148149