1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import javax.swing.*; 25 import java.awt.*; 26 import java.awt.event.KeyAdapter; 27 import java.awt.event.KeyEvent; 28 29 import static jdk.testlibrary.Asserts.assertTrue; 30 31 /* 32 @test 33 @summary 34 @author Yuri.Nesterenko, Dmitriy.Ermashov 35 @library ../../../../lib/testlibrary 36 @build ExtendedRobot 37 @run main LockingKeyStateTest 38 */ 39 40 public class LockingKeyStateTest { 41 42 Frame frame; 43 ExtendedRobot robot; 44 45 // Note that Kana lock you may actually toggle only if you have one. 46 static int[] lockingKeys = { KeyEvent.VK_CAPS_LOCK, KeyEvent.VK_NUM_LOCK, 47 KeyEvent.VK_SCROLL_LOCK, KeyEvent.VK_KANA_LOCK }; 48 boolean[] getSupported = new boolean[lockingKeys.length]; 49 boolean[] setSupported = new boolean[lockingKeys.length]; 50 boolean[] state0 = new boolean[lockingKeys.length]; 51 52 Toolkit toolkit = Toolkit.getDefaultToolkit(); 53 54 LockingKeyStateTest() throws Exception { 55 robot = new ExtendedRobot(); 56 SwingUtilities.invokeAndWait(() -> { createGui(); }); 57 } 58 59 void fillArrays() { 60 for(int i = 0; i < lockingKeys.length; i++) { 61 getSupported[i] = false; 62 setSupported[i] = false; 63 try { 64 state0[i] = toolkit.getLockingKeyState(lockingKeys[i]); 65 getSupported[i] = true; 66 toolkit.setLockingKeyState(lockingKeys[i], state0[i]); 67 setSupported[i] = true; 68 }catch(UnsupportedOperationException uoe) { 69 } 70 System.out.println(" State get/set of "+KeyEvent.getKeyText(lockingKeys[i])+" is supported? "+ 71 getSupported[i]+", "+setSupported[i]); 72 } 73 } 74 75 void toggleAll(boolean b) { 76 for(int i = 0; i < lockingKeys.length; i++) { 77 if(setSupported[i]) { 78 toolkit.setLockingKeyState(lockingKeys[i], b); 79 } 80 } 81 } 82 83 void checkAll(boolean b) { 84 for(int i = 0; i < lockingKeys.length; i++) { 85 if(getSupported[i] && setSupported[i]) { 86 assertTrue(toolkit.getLockingKeyState(lockingKeys[i]) == b, "Oops! State of "+KeyEvent.getKeyText(lockingKeys[i])+" is not "+b); 87 System.out.println("OK, state of "+KeyEvent.getKeyText(lockingKeys[i])+" is "+b); 88 } 89 } 90 } 91 92 void restoreAll() { 93 for(int i = 0; i < lockingKeys.length; i++) { 94 if(setSupported[i] && getSupported[i]) { 95 toolkit.setLockingKeyState(lockingKeys[i], state0[i]); 96 } 97 } 98 } 99 100 public void createGui() { 101 fillArrays(); 102 frame = new Frame("LockingKeyStateTest Title"); 103 // (KeyAdapter) keyPressed(ke) -> { 104 105 frame.addKeyListener( new KeyAdapter() { 106 public void keyPressed(KeyEvent ke) { 107 switch(ke.getKeyCode()) { 108 case KeyEvent.VK_1 : 109 toggleAll(true); break; 110 case KeyEvent.VK_2 : 111 checkAll(true); break; 112 case KeyEvent.VK_3 : 113 toggleAll(false); break; 114 case KeyEvent.VK_4 : 115 checkAll(false); break; 116 case KeyEvent.VK_5 : 117 restoreAll(); break; 118 default:; 119 } 120 } 121 }); 122 frame.setSize(200,200); 123 frame.setVisible(true); 124 } 125 126 void doTest(){ 127 robot.waitForIdle(); 128 robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth() / 2, 129 frame.getLocationOnScreen().y + frame.getHeight() / 2); 130 robot.click(); 131 132 robot.type('1'); 133 robot.type('2'); 134 robot.type('3'); 135 robot.type('4'); 136 robot.type('5'); 137 robot.waitForIdle(); 138 139 } 140 141 public static void main(String argv[]) throws Exception { 142 LockingKeyStateTest af = new LockingKeyStateTest(); 143 af.doTest(); 144 145 } 146 }