1/*
2 * Copyright (c) 2007, 2016, 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.BorderLayout;
25import java.awt.Canvas;
26import java.awt.EventQueue;
27import java.awt.Frame;
28import java.awt.event.FocusAdapter;
29import java.awt.event.FocusEvent;
30import java.awt.event.InputEvent;
31import java.awt.event.KeyAdapter;
32import java.awt.event.KeyEvent;
33import java.awt.event.MouseEvent;
34
35import static jdk.testlibrary.Asserts.assertTrue;
36
37/*
38 * @test 8155742
39 * @key headful
40 * @summary Make sure that modifier key mask is set when robot press
41 *          some key with one or more modifiers.
42 * @library ../../../../lib/testlibrary/
43 * @build ExtendedRobot
44 * @run main ModifierRobotKeyTest
45 */
46
47public class ModifierRobotKeyTest extends KeyAdapter {
48
49    private boolean focusGained = false;
50    private boolean startTest = false;
51    private ExtendedRobot robot;
52    private Frame frame;
53    private Canvas canvas;
54
55    private volatile boolean tempPress = false;
56
57    private int[] textKeys, modifierKeys, inputMasks;
58    private boolean[] modifierStatus, textStatus;
59
60    private final static int waitDelay = 5000;
61    private Object tempLock = new Object();
62    private Object keyLock = new Object();
63
64    public static void main(String[] args) throws Exception {
65        ModifierRobotKeyTest test = new ModifierRobotKeyTest();
66        test.doTest();
67    }
68
69    public ModifierRobotKeyTest() throws Exception {
70        modifierKeys =  new int[4];
71        modifierKeys[0] = KeyEvent.VK_SHIFT;
72        modifierKeys[1] = KeyEvent.VK_CONTROL;
73        modifierKeys[2] = KeyEvent.VK_ALT;
74        modifierKeys[3] = KeyEvent.VK_ALT_GRAPH;
75
76        inputMasks = new int[4];
77        inputMasks[0] =  InputEvent.SHIFT_MASK;
78        inputMasks[1] =  InputEvent.CTRL_MASK;
79        inputMasks[2] =  InputEvent.ALT_MASK;
80        inputMasks[3] =  InputEvent.ALT_GRAPH_MASK;
81
82        modifierStatus = new boolean[modifierKeys.length];
83
84        textKeys = new int[2];
85        textKeys[0] = KeyEvent.VK_A;
86
87        String os = System.getProperty("os.name").toLowerCase();
88
89        if (os.contains("solaris") || os.contains("sunos"))
90            textKeys[1] = KeyEvent.VK_S;
91        else if (os.contains("os x"))
92            textKeys[1] = KeyEvent.VK_K;
93        else
94            textKeys[1] = KeyEvent.VK_I;
95
96        textStatus = new boolean[textKeys.length];
97
98        EventQueue.invokeAndWait( () -> { initializeGUI(); });
99    }
100
101    public void keyPressed(KeyEvent event) {
102
103        tempPress = true;
104        synchronized (tempLock) { tempLock.notifyAll(); }
105
106        if (! startTest) {
107            return;
108        }
109        for (int x = 0; x < inputMasks.length; x++) {
110            if ((event.getModifiers() & inputMasks[x]) != 0) {
111                System.out.println("Modifier set: " + event.getKeyModifiersText(inputMasks[x]));
112                modifierStatus[x] = true;
113            }
114        }
115        for (int x = 0; x < textKeys.length; x++) {
116            if (event.getKeyCode() == textKeys[x]) {
117                System.out.println("Text set: " + event.getKeyText(textKeys[x]));
118                textStatus[x] = true;
119            }
120        }
121
122        synchronized (keyLock) { keyLock.notifyAll(); }
123    }
124
125    private void initializeGUI() {
126        frame = new Frame("Test frame");
127        canvas = new Canvas();
128        canvas.addFocusListener(new FocusAdapter() {
129            public void focusGained(FocusEvent event) { focusGained = true; }
130        });
131        canvas.addKeyListener(this);
132        frame.setLayout(new BorderLayout());
133        frame.add(canvas);
134        frame.setSize(200, 200);
135        frame.setVisible(true);
136    }
137
138    public void doTest() throws Exception {
139        robot = new ExtendedRobot();
140
141        robot.mouseMove((int) frame.getLocationOnScreen().getX() + frame.getSize().width / 2,
142                        (int) frame.getLocationOnScreen().getY() + frame.getSize().height / 2);
143        robot.click(MouseEvent.BUTTON1_MASK);
144        robot.waitForIdle();
145
146        assertTrue(focusGained, "FAIL: Canvas gained focus!");
147
148        for (int i = 0; i < modifierKeys.length; i++) {
149            for (int j = 0; j < textKeys.length; j++) {
150                tempPress = false;
151                robot.keyPress(modifierKeys[i]);
152                robot.waitForIdle();
153                if (! tempPress) {
154                    synchronized (tempLock) { tempLock.wait(waitDelay); }
155                }
156                assertTrue(tempPress, "FAIL: keyPressed triggered for i=" + i);
157
158                resetStatus();
159                startTest = true;
160                robot.keyPress(textKeys[j]);
161                robot.waitForIdle();
162                if (! modifierStatus[i] || ! textStatus[j]) {
163                    synchronized (keyLock) { keyLock.wait(waitDelay); }
164                }
165
166
167                assertTrue(modifierStatus[i] && textStatus[j],
168                        "FAIL: KeyEvent not proper!"+
169                        "Key checked: i=" + i + "; j=" + j+
170                        "ModifierStatus = " + modifierStatus[i]+
171                        "TextStatus = " + textStatus[j]);
172                startTest = false;
173                robot.keyRelease(textKeys[j]);
174                robot.waitForIdle();
175                robot.keyRelease(modifierKeys[i]);
176                robot.waitForIdle();
177            }
178        }
179
180        for (int i = 0; i < modifierKeys.length; i++) {
181            for (int j = i + 1; j < modifierKeys.length; j++) {
182                for (int k = 0; k < textKeys.length; k++) {
183                    tempPress = false;
184                    robot.keyPress(modifierKeys[i]);
185                    robot.waitForIdle();
186                    if (! tempPress) {
187                        synchronized (tempLock) { tempLock.wait(waitDelay); }
188                    }
189
190                    assertTrue(tempPress, "FAIL: MultiKeyTest: keyPressed triggered for i=" + i);
191
192                    tempPress = false;
193                    robot.keyPress(modifierKeys[j]);
194                    robot.waitForIdle();
195                    if (! tempPress) {
196                        synchronized (tempLock) { tempLock.wait(waitDelay); }
197                    }
198                    assertTrue(tempPress, "FAIL: MultiKeyTest keyPressed triggered for j=" + j);
199
200                    resetStatus();
201                    startTest = true;
202                    robot.keyPress(textKeys[k]);
203                    robot.waitForIdle();
204                    if (! modifierStatus[i] || ! modifierStatus[j] || ! textStatus[k]) {
205                        synchronized (keyLock) {
206                            keyLock.wait(waitDelay);
207                        }
208                    }
209                    assertTrue(modifierStatus[i] && modifierStatus[j] && textStatus[k],
210                            "FAIL: KeyEvent not proper!"+
211                            "Key checked: i=" + i + "; j=" + j + "; k=" + k+
212                            "Modifier1Status = " + modifierStatus[i]+
213                            "Modifier2Status = " + modifierStatus[j]+
214                            "TextStatus = " + textStatus[k]);
215
216                    startTest = false;
217                    robot.keyRelease(textKeys[k]);
218                    robot.waitForIdle();
219                    robot.keyRelease(modifierKeys[j]);
220                    robot.waitForIdle();
221                    robot.keyRelease(modifierKeys[i]);
222                    robot.waitForIdle();
223                }
224            }
225        }
226
227        frame.dispose();
228    }
229
230    private void resetStatus() {
231        for (int i = 0; i < modifierStatus.length; i++) {
232            modifierStatus[i] = false;
233        }
234        for (int i = 0; i < textStatus.length; i++) {
235            textStatus[i] = false;
236        }
237    }
238
239}
240