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