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