Path: blob/master/test/jdk/java/awt/List/KeyEventsTest/KeyEventsTest.java
41153 views
/*1* Copyright (c) 2005, 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 6190768 619077827@summary Tests that triggering events on AWT list by pressing CTRL + HOME,28CTRL + END, PG-UP, PG-DOWN similar Motif behavior29@library /test/lib30@build jdk.test.lib.Platform31@run main KeyEventsTest32*/3334import java.awt.*;35import java.awt.event.*;36import java.lang.reflect.*;3738import jdk.test.lib.Platform;3940public class KeyEventsTest extends Frame implements ItemListener, FocusListener, KeyListener41{42TestState currentState;43final Object LOCK = new Object();44final int ACTION_TIMEOUT = 500;4546List single = new List(3, false);47List multiple = new List(3, true);4849Panel p1 = new Panel ();50Panel p2 = new Panel ();5152public static void main(final String[] args) {53KeyEventsTest app = new KeyEventsTest();54app.init();55app.start();56}5758public void init()59{60setLayout (new BorderLayout ());6162single.add("0");63single.add("1");64single.add("2");65single.add("3");66single.add("4");67single.add("5");68single.add("6");69single.add("7");70single.add("8");7172multiple.add("0");73multiple.add("1");74multiple.add("2");75multiple.add("3");76multiple.add("4");77multiple.add("5");78multiple.add("6");79multiple.add("7");80multiple.add("8");8182single.addKeyListener(this);83single.addItemListener(this);84single.addFocusListener(this);85p1.add(single);86add("North", p1);8788multiple.addKeyListener(this);89multiple.addItemListener(this);90multiple.addFocusListener(this);91p2.add(multiple);92add("South", p2);9394}//End init()9596public void start ()97{9899try{100setSize (200,200);101validate();102setUndecorated(true);103setLocationRelativeTo(null);104setVisible(true);105106doTest();107System.out.println("Test passed.");108} catch (Exception e) {109e.printStackTrace();110throw new RuntimeException("The test failed.");111}112113}// start()114115public void itemStateChanged (ItemEvent ie) {116System.out.println("itemStateChanged-"+ie);117this.currentState.setAction(true);118}119120public void focusGained(FocusEvent e){121122synchronized (LOCK) {123LOCK.notifyAll();124}125126}127128public void focusLost(FocusEvent e){129}130131public void keyPressed(KeyEvent e){132System.out.println("keyPressed-"+e);133}134135public void keyReleased(KeyEvent e){136System.out.println("keyReleased-"+e);137}138139public void keyTyped(KeyEvent e){140System.out.println("keyTyped-"+e);141}142143private void test(TestState currentState)144throws InterruptedException, InvocationTargetException {145146synchronized (LOCK) {147148this.currentState = currentState;149System.out.println(this.currentState);150151List list;152if (currentState.getMultiple()){153list = multiple;154}else{155list = single;156}157158Robot r;159try {160r = new Robot();161} catch(AWTException e) {162throw new RuntimeException(e.getMessage());163}164165r.delay(10);166Point loc = this.getLocationOnScreen();167168r.mouseMove(loc.x+10, loc.y+10);169r.mousePress(InputEvent.BUTTON1_MASK);170r.delay(10);171r.mouseRelease(InputEvent.BUTTON1_MASK);172r.delay(10);173174list.requestFocusInWindow();175LOCK.wait(ACTION_TIMEOUT);176if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list){177throw new RuntimeException("Test failed - list isn't focus owner.");178}179180list.deselect(0);181list.deselect(1);182list.deselect(2);183list.deselect(3);184list.deselect(4);185list.deselect(5);186list.deselect(6);187list.deselect(7);188list.deselect(8);189190int selectIndex = 0;191int visibleIndex = 0;192193if (currentState.getScrollMoved()){194195if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||196currentState.getKeyID() == KeyEvent.VK_HOME){197selectIndex = 8;198visibleIndex = 8;199}else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||200currentState.getKeyID() == KeyEvent.VK_END){201selectIndex = 0;202visibleIndex = 0;203}204205}else{206207if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||208currentState.getKeyID() == KeyEvent.VK_HOME){209210if (currentState.getSelectedMoved()){211selectIndex = 1;212visibleIndex = 0;213}else{214selectIndex = 0;215visibleIndex = 0;216}217218}else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||219currentState.getKeyID() == KeyEvent.VK_END){220221if (currentState.getSelectedMoved()){222selectIndex = 7;223visibleIndex = 8;224}else{225selectIndex = 8;226visibleIndex = 8;227}228229}230231}232233list.select(selectIndex);234list.makeVisible(visibleIndex);235236r.delay(10);237238if (currentState.getKeyID() == KeyEvent.VK_HOME ||239currentState.getKeyID() == KeyEvent.VK_END){240r.keyPress(KeyEvent.VK_CONTROL);241}242243r.delay(10);244r.keyPress(currentState.getKeyID());245r.delay(10);246r.keyRelease(currentState.getKeyID());247r.delay(10);248249if (currentState.getKeyID() == KeyEvent.VK_HOME ||250currentState.getKeyID() == KeyEvent.VK_END){251r.keyRelease(KeyEvent.VK_CONTROL);252}253254r.waitForIdle();255r.delay(200);256257if (currentState.getTemplate() != currentState.getAction())258throw new RuntimeException("Test failed.");259260}261262}263264private void doTest()265throws InterruptedException, InvocationTargetException {266267boolean isWin = false;268if (Platform.isWindows()) {269isWin = true;270} else if (Platform.isOSX()) {271System.out.println("Not for OS X");272return;273}274275System.out.println("multiple? selectedMoved? ?scrollMoved keyID? template? action?");276test(new TestState(false, false, false, KeyEvent.VK_PAGE_UP, isWin?false:false));277// SelectedMoved (false) != ScrollMoved (true) for single list not emulated278test(new TestState(false, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));279test(new TestState(false, true, true, KeyEvent.VK_PAGE_UP, isWin?true:true));280test(new TestState(true, false, false, KeyEvent.VK_PAGE_UP, isWin?true:false));281test(new TestState(true, false, true, KeyEvent.VK_PAGE_UP, isWin?true:false));282test(new TestState(true, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));283test(new TestState(true, true, true, KeyEvent.VK_PAGE_UP, isWin?true:false));284285test(new TestState(false, false, false, KeyEvent.VK_PAGE_DOWN, isWin?false:false));286test(new TestState(false, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));287test(new TestState(false, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:true));288test(new TestState(true, false, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));289test(new TestState(true, false, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));290test(new TestState(true, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));291test(new TestState(true, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));292293test(new TestState(false, false, false, KeyEvent.VK_HOME, isWin?false:true));294test(new TestState(false, true, false, KeyEvent.VK_HOME, isWin?true:true));295test(new TestState(false, true, true, KeyEvent.VK_HOME, isWin?true:true));296test(new TestState(true, false, false, KeyEvent.VK_HOME, isWin?true:false));297test(new TestState(true, false, true, KeyEvent.VK_HOME, isWin?true:false));298test(new TestState(true, true, false, KeyEvent.VK_HOME, isWin?true:false));299test(new TestState(true, true, true, KeyEvent.VK_HOME, isWin?true:false));300301test(new TestState(false, false, false, KeyEvent.VK_END, isWin?false:true));302test(new TestState(false, true, false, KeyEvent.VK_END, isWin?true:true));303test(new TestState(false, true, true, KeyEvent.VK_END, isWin?true:true));304test(new TestState(true, false, false, KeyEvent.VK_END, isWin?true:false));305test(new TestState(true, false, true, KeyEvent.VK_END, isWin?true:false));306test(new TestState(true, true, false, KeyEvent.VK_END, isWin?true:false));307test(new TestState(true, true, true, KeyEvent.VK_END, isWin?true:false));308309}310}// class KeyEventsTest311312class TestState{313314private boolean multiple;315// after key pressing selected item moved316private final boolean selectedMoved;317// after key pressing scroll moved318private final boolean scrollMoved;319private final int keyID;320private final boolean template;321private boolean action;322323public TestState(boolean multiple, boolean selectedMoved, boolean scrollMoved, int keyID, boolean template){324this.multiple = multiple;325this.selectedMoved = selectedMoved;326this.scrollMoved = scrollMoved;327this.keyID = keyID;328this.template = template;329this.action = false;330}331332public boolean getMultiple(){333return multiple;334}335public boolean getSelectedMoved(){336return selectedMoved;337}338339public boolean getScrollMoved(){340return scrollMoved;341}342343public int getKeyID(){344return keyID;345}346347public boolean getTemplate(){348return template;349}350351public boolean getAction(){352return action;353}354355public void setAction(boolean action){356this.action = action;357}358359public String toString(){360return multiple + "," + selectedMoved + "," + scrollMoved + "," + keyID + "," + template + "," + action;361}362}// TestState363364365