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 java.awt.*;
  25 import java.awt.event.KeyAdapter;
  26 import java.awt.event.KeyEvent;
  27 
  28 import static jdk.testlibrary.Asserts.assertTrue;
  29 
  30 /*
  31   @test
  32   @summary
  33   @author Yuri.Nesterenko, Dmitriy.Ermashov
  34   @library ../../../../lib/testlibrary
  35   @build ExtendedRobot
  36   @run main LockingKeyStateTest
  37 */
  38 
  39 public class LockingKeyStateTest {
  40     
  41     Frame frame;
  42     ExtendedRobot robot;
  43 
  44     // Note that Kana lock you may actually toggle only if you have one.
  45     static int[] lockingKeys = { KeyEvent.VK_CAPS_LOCK, KeyEvent.VK_NUM_LOCK,
  46                                  KeyEvent.VK_SCROLL_LOCK, KeyEvent.VK_KANA_LOCK };
  47     boolean[] getSupported = new boolean[lockingKeys.length];
  48     boolean[] setSupported = new boolean[lockingKeys.length];
  49     boolean[] state0 = new boolean[lockingKeys.length];
  50 
  51     Toolkit toolkit = Toolkit.getDefaultToolkit();
  52 
  53     LockingKeyStateTest() throws Exception {
  54         robot = new ExtendedRobot();
  55         EventQueue.invokeAndWait( this::createGui );
  56     }
  57 
  58     void fillArrays() {
  59         for(int i = 0; i < lockingKeys.length; i++) {
  60             getSupported[i] = false;
  61             setSupported[i] = false;
  62             try {
  63                 state0[i] = toolkit.getLockingKeyState(lockingKeys[i]);
  64                 getSupported[i] = true;
  65                 toolkit.setLockingKeyState(lockingKeys[i], state0[i]);
  66                 setSupported[i] = true;
  67             }catch(UnsupportedOperationException uoe) {
  68             }
  69             System.out.println(" State get/set of "+KeyEvent.getKeyText(lockingKeys[i])+" is supported? "+
  70                    getSupported[i]+", "+setSupported[i]);
  71         }
  72     }
  73 
  74     void toggleAll(boolean b) {
  75         for(int i = 0; i < lockingKeys.length; i++) {
  76             if(setSupported[i]) {
  77                 toolkit.setLockingKeyState(lockingKeys[i], b);
  78             }
  79         } 
  80     }
  81 
  82     void checkAll(boolean b) {
  83         for(int i = 0; i < lockingKeys.length; i++) {
  84             if(getSupported[i]  && setSupported[i]) {
  85                 assertTrue(toolkit.getLockingKeyState(lockingKeys[i]) == b, "State of "+KeyEvent.getKeyText(lockingKeys[i])+" is "+b);
  86                 System.out.println("OK, state of "+KeyEvent.getKeyText(lockingKeys[i])+" is "+b);
  87             }
  88         } 
  89     }
  90 
  91     void restoreAll() {
  92         for(int i = 0; i < lockingKeys.length; i++) {
  93             if(setSupported[i] && getSupported[i]) {
  94                 toolkit.setLockingKeyState(lockingKeys[i], state0[i]);
  95             }
  96         } 
  97     }
  98 
  99     public void createGui() {
 100         fillArrays();
 101         frame = new Frame("LockingKeyStateTest Title");
 102         frame.addKeyListener( new KeyAdapter() {
 103             public void keyPressed(KeyEvent ke) {
 104                 switch(ke.getKeyCode()) {
 105                     case KeyEvent.VK_1 :
 106                         toggleAll(true); break;
 107                     case KeyEvent.VK_2 :
 108                         checkAll(true); break;
 109                     case KeyEvent.VK_3 :
 110                         toggleAll(false); break;
 111                     case KeyEvent.VK_4 :
 112                         checkAll(false); break;
 113                     case KeyEvent.VK_5 :
 114                         restoreAll(); break;
 115                     default:;
 116                 }
 117             }
 118         });
 119         frame.setSize(200,200);
 120         frame.setVisible(true);
 121     }
 122 
 123     void doTest(){
 124         robot.waitForIdle();
 125         robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth() / 2,
 126                         frame.getLocationOnScreen().y + frame.getHeight() / 2);
 127         robot.click();
 128 
 129         robot.type('1');
 130         robot.type('2');
 131         robot.type('3');
 132         robot.type('4');
 133         robot.type('5');
 134         robot.waitForIdle();
 135 
 136     }
 137 
 138     public static void main(String argv[]) throws Exception {
 139         LockingKeyStateTest af = new LockingKeyStateTest();
 140         af.doTest();
 141 
 142     }
 143 }