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