Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/List/KeyEventsTest/KeyEventsTest.java
38828 views
/*1* Copyright (c) 2005, 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 6190768 619077826@summary Tests that triggering events on AWT list by pressing CTRL + HOME, CTRL + END, PG-UP, PG-DOWN similar Motif behavior27@author [email protected] area=awt.list28@library ../../../../lib/testlibrary29@build jdk.testlibrary.OSInfo30@run applet KeyEventsTest.html31*/3233/**34* KeyEventsTest.html35*36* summary:37*/3839import java.applet.Applet;40import java.awt.*;41import java.awt.event.*;42import java.util.Set;43import java.lang.reflect.*;4445import jdk.testlibrary.OSInfo;4647public class KeyEventsTest extends Applet implements ItemListener, FocusListener, KeyListener48{49TestState currentState;50final Object LOCK = new Object();51final int ACTION_TIMEOUT = 500;5253List single = new List(3, false);54List multiple = new List(3, true);5556Panel p1 = new Panel ();57Panel p2 = new Panel ();5859public void init()60{61setLayout (new BorderLayout ());6263single.add("0");64single.add("1");65single.add("2");66single.add("3");67single.add("4");68single.add("5");69single.add("6");70single.add("7");71single.add("8");7273multiple.add("0");74multiple.add("1");75multiple.add("2");76multiple.add("3");77multiple.add("4");78multiple.add("5");79multiple.add("6");80multiple.add("7");81multiple.add("8");8283single.addKeyListener(this);84single.addItemListener(this);85single.addFocusListener(this);86p1.add(single);87add("North", p1);8889multiple.addKeyListener(this);90multiple.addItemListener(this);91multiple.addFocusListener(this);92p2.add(multiple);93add("South", p2);9495}//End init()9697public void start ()98{99100try{101setSize (200,200);102setVisible(true);103validate();104105main(null);106107} catch (Exception e) {108e.printStackTrace();109throw new RuntimeException("The test failed.");110}111112}// start()113114private void main(String[] args)115throws InterruptedException, InvocationTargetException {116117doTest();118119System.out.println("Test passed.");120}121122public void itemStateChanged (ItemEvent ie) {123System.out.println("itemStateChanged-"+ie);124this.currentState.setAction(true);125}126127public void focusGained(FocusEvent e){128129synchronized (LOCK) {130LOCK.notifyAll();131}132133}134135public void focusLost(FocusEvent e){136}137138public void keyPressed(KeyEvent e){139System.out.println("keyPressed-"+e);140}141142public void keyReleased(KeyEvent e){143System.out.println("keyReleased-"+e);144}145146public void keyTyped(KeyEvent e){147System.out.println("keyTyped-"+e);148}149150private void test(TestState currentState)151throws InterruptedException, InvocationTargetException {152153synchronized (LOCK) {154155this.currentState = currentState;156System.out.println(this.currentState);157158List list;159if (currentState.getMultiple()){160list = multiple;161}else{162list = single;163}164165Robot r;166try {167r = new Robot();168} catch(AWTException e) {169throw new RuntimeException(e.getMessage());170}171172r.delay(10);173Point loc = this.getLocationOnScreen();174175r.mouseMove(loc.x+10, loc.y+10);176r.mousePress(InputEvent.BUTTON1_MASK);177r.delay(10);178r.mouseRelease(InputEvent.BUTTON1_MASK);179r.delay(10);180181list.requestFocusInWindow();182LOCK.wait(ACTION_TIMEOUT);183if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list){184throw new RuntimeException("Test failed - list isn't focus owner.");185}186187list.deselect(0);188list.deselect(1);189list.deselect(2);190list.deselect(3);191list.deselect(4);192list.deselect(5);193list.deselect(6);194list.deselect(7);195list.deselect(8);196197int selectIndex = 0;198int visibleIndex = 0;199200if (currentState.getScrollMoved()){201202if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||203currentState.getKeyID() == KeyEvent.VK_HOME){204selectIndex = 8;205visibleIndex = 8;206}else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||207currentState.getKeyID() == KeyEvent.VK_END){208selectIndex = 0;209visibleIndex = 0;210}211212}else{213214if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||215currentState.getKeyID() == KeyEvent.VK_HOME){216217if (currentState.getSelectedMoved()){218selectIndex = 1;219visibleIndex = 0;220}else{221selectIndex = 0;222visibleIndex = 0;223}224225}else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||226currentState.getKeyID() == KeyEvent.VK_END){227228if (currentState.getSelectedMoved()){229selectIndex = 7;230visibleIndex = 8;231}else{232selectIndex = 8;233visibleIndex = 8;234}235236}237238}239240list.select(selectIndex);241list.makeVisible(visibleIndex);242243r.delay(10);244245if (currentState.getKeyID() == KeyEvent.VK_HOME ||246currentState.getKeyID() == KeyEvent.VK_END){247r.keyPress(KeyEvent.VK_CONTROL);248}249250r.delay(10);251r.keyPress(currentState.getKeyID());252r.delay(10);253r.keyRelease(currentState.getKeyID());254r.delay(10);255256if (currentState.getKeyID() == KeyEvent.VK_HOME ||257currentState.getKeyID() == KeyEvent.VK_END){258r.keyRelease(KeyEvent.VK_CONTROL);259}260261r.waitForIdle();262r.delay(200);263264if (currentState.getTemplate() != currentState.getAction())265throw new RuntimeException("Test failed.");266267}268269}270271private void doTest()272throws InterruptedException, InvocationTargetException {273274boolean isWin = false;275if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {276isWin = true;277}else if(OSInfo.getOSType() == OSInfo.OSType.MACOSX) {278System.out.println("Not for OS X");279return;280}281282System.out.println("multiple? selectedMoved? ?scrollMoved keyID? template? action?");283test(new TestState(false, false, false, KeyEvent.VK_PAGE_UP, isWin?false:false));284// SelectedMoved (false) != ScrollMoved (true) for single list not emulated285test(new TestState(false, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));286test(new TestState(false, true, true, KeyEvent.VK_PAGE_UP, isWin?true:true));287test(new TestState(true, false, false, KeyEvent.VK_PAGE_UP, isWin?true:false));288test(new TestState(true, false, true, KeyEvent.VK_PAGE_UP, isWin?true:false));289test(new TestState(true, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));290test(new TestState(true, true, true, KeyEvent.VK_PAGE_UP, isWin?true:false));291292test(new TestState(false, false, false, KeyEvent.VK_PAGE_DOWN, isWin?false:false));293test(new TestState(false, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));294test(new TestState(false, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:true));295test(new TestState(true, false, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));296test(new TestState(true, false, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));297test(new TestState(true, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));298test(new TestState(true, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));299300test(new TestState(false, false, false, KeyEvent.VK_HOME, isWin?false:true));301test(new TestState(false, true, false, KeyEvent.VK_HOME, isWin?true:true));302test(new TestState(false, true, true, KeyEvent.VK_HOME, isWin?true:true));303test(new TestState(true, false, false, KeyEvent.VK_HOME, isWin?true:false));304test(new TestState(true, false, true, KeyEvent.VK_HOME, isWin?true:false));305test(new TestState(true, true, false, KeyEvent.VK_HOME, isWin?true:false));306test(new TestState(true, true, true, KeyEvent.VK_HOME, isWin?true:false));307308test(new TestState(false, false, false, KeyEvent.VK_END, isWin?false:true));309test(new TestState(false, true, false, KeyEvent.VK_END, isWin?true:true));310test(new TestState(false, true, true, KeyEvent.VK_END, isWin?true:true));311test(new TestState(true, false, false, KeyEvent.VK_END, isWin?true:false));312test(new TestState(true, false, true, KeyEvent.VK_END, isWin?true:false));313test(new TestState(true, true, false, KeyEvent.VK_END, isWin?true:false));314test(new TestState(true, true, true, KeyEvent.VK_END, isWin?true:false));315316}317}// class KeyEventsTest318319class TestState{320321private boolean multiple;322// after key pressing selected item moved323private final boolean selectedMoved;324// after key pressing scroll moved325private final boolean scrollMoved;326private final int keyID;327private final boolean template;328private boolean action;329330public TestState(boolean multiple, boolean selectedMoved, boolean scrollMoved, int keyID, boolean template){331this.multiple = multiple;332this.selectedMoved = selectedMoved;333this.scrollMoved = scrollMoved;334this.keyID = keyID;335this.template = template;336this.action = false;337}338339public boolean getMultiple(){340return multiple;341}342public boolean getSelectedMoved(){343return selectedMoved;344}345346public boolean getScrollMoved(){347return scrollMoved;348}349350public int getKeyID(){351return keyID;352}353354public boolean getTemplate(){355return template;356}357358public boolean getAction(){359return action;360}361362public void setAction(boolean action){363this.action = action;364}365366public String toString(){367return multiple + "," + selectedMoved + "," + scrollMoved + "," + keyID + "," + template + "," + action;368}369}// TestState370371372