1 /*
   2  * Copyright (c) 2007, 2017, 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.testlibrary.Asserts.assertNull;
  36 import static jdk.testlibrary.Asserts.assertTrue;
  37 
  38 /*
  39  * @test 8155742
  40  * @key headful
  41  * @summary Make sure that modifier key mask is set when robot press
  42  *          some key with one or more modifiers.
  43  * @library ../../../../lib/testlibrary/
  44  * @build ExtendedRobot
  45  * @run main ModifierRobotKeyTest
  46  */
  47 
  48 public class ModifierRobotKeyTest extends KeyAdapter {
  49 
  50     private boolean focusGained = false;
  51     private boolean startTest = false;
  52     private ExtendedRobot robot;
  53     private Frame frame;
  54     private Canvas canvas;
  55 
  56     private volatile boolean tempPress = false;
  57 
  58     private int[] textKeys, modifierKeys, inputMasks;
  59     private boolean[] modifierStatus, textStatus;
  60 
  61     private final static int WAIT_DELAY = 5000;
  62     private final Object lock = 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         synchronized (lock) {
 103             tempPress = true;
 104             lock.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: " +
 112                                       event.getKeyModifiersText(inputMasks[x]));
 113                     modifierStatus[x] = true;
 114                 }
 115             }
 116             for (int x = 0; x < textKeys.length; x++) {
 117                 if (event.getKeyCode() == textKeys[x]) {
 118                     System.out.println("Text set: " +
 119                                                  event.getKeyText(textKeys[x]));
 120                     textStatus[x] = true;
 121                 }
 122             }
 123         }
 124     }
 125 
 126     private void initializeGUI() {
 127         frame = new Frame("Test frame");
 128         canvas = new Canvas();
 129         canvas.addFocusListener(new FocusAdapter() {
 130             public void focusGained(FocusEvent event) { focusGained = true; }
 131         });
 132         canvas.addKeyListener(this);
 133         frame.setLayout(new BorderLayout());
 134         frame.add(canvas);
 135         frame.setBounds(200, 200, 200, 200);
 136         frame.setVisible(true);
 137     }
 138 
 139     public void doTest() throws Exception {
 140         robot = new ExtendedRobot();
 141         robot.setAutoDelay(20);
 142         robot.waitForIdle();
 143 
 144         robot.mouseMove((int) frame.getLocationOnScreen().getX() +
 145                                                     frame.getSize().width / 2,
 146                         (int) frame.getLocationOnScreen().getY() +
 147                                                     frame.getSize().height / 2);
 148         robot.click(MouseEvent.BUTTON1_MASK);
 149         robot.waitForIdle();
 150         assertTrue(focusGained, "FAIL: Canvas gained focus!");
 151 
 152         String error = null;
 153         exit1:
 154         for (int i = 0; i < modifierKeys.length; i++) {
 155             for (int j = 0; j < textKeys.length; j++) {
 156                 if (error != null) {
 157                     break exit1;
 158                 }
 159                 robot.waitForIdle(100);
 160                 synchronized (lock) {
 161                     tempPress = false;
 162                     robot.keyPress(modifierKeys[i]);
 163                     lock.wait(WAIT_DELAY);
 164                 }
 165                 if (!tempPress) {
 166                     error ="FAIL: keyPressed triggered for i=" + i;
 167                 }
 168 
 169                 synchronized (lock) {
 170                     resetStatus();
 171                     startTest = true;
 172                     robot.keyPress(textKeys[j]);
 173                     lock.wait(WAIT_DELAY);
 174                 }
 175 
 176                 if (!(modifierStatus[i] && textStatus[j])) {
 177                     error = "FAIL: KeyEvent not proper!"+
 178                             "Key checked: i=" + i + "; j=" + j+
 179                             "ModifierStatus = " + modifierStatus[i]+
 180                             "TextStatus = " + textStatus[j];
 181                 }
 182 
 183                 startTest = false;
 184                 robot.keyRelease(textKeys[j]);
 185                 robot.keyRelease(modifierKeys[i]);
 186             }
 187         }
 188 
 189         exit2:
 190         for (int i = 0; i < modifierKeys.length; i++) {
 191             for (int j = i + 1; j < modifierKeys.length; j++) {
 192                 for (int k = 0; k < textKeys.length; k++) {
 193                     if (error != null) {
 194                         break exit2;
 195                     }
 196                     robot.waitForIdle(100);
 197                     synchronized (lock) {
 198                         tempPress = false;
 199                         robot.keyPress(modifierKeys[i]);
 200                         lock.wait(WAIT_DELAY);
 201                     }
 202 
 203                     if (!tempPress) {
 204                         error = "FAIL: MultiKeyTest: keyPressed " +
 205                                                          "triggered for i=" + i;
 206                     }
 207 
 208                     synchronized (lock) {
 209                         tempPress = false;
 210                         robot.keyPress(modifierKeys[j]);
 211                         lock.wait(WAIT_DELAY);
 212                     }
 213                     if (!tempPress) {
 214                         error = "FAIL: MultiKeyTest keyPressed " +
 215                                                          "triggered for j=" + j;
 216                     };
 217 
 218                     synchronized (lock) {
 219                         resetStatus();
 220                         startTest = true;
 221                         robot.keyPress(textKeys[k]);
 222                         lock.wait(WAIT_DELAY);
 223                     }
 224                     if (!(modifierStatus[i] && modifierStatus[j]
 225                                                               && textStatus[k]))
 226                     {
 227                         error = "FAIL: KeyEvent not proper!" +
 228                                "Key checked: i=" + i + "; j=" + j + "; k=" + k +
 229                                "Modifier1Status = " + modifierStatus[i] +
 230                                "Modifier2Status = " + modifierStatus[j] +
 231                                "TextStatus = " + textStatus[k];
 232                     }
 233 
 234                     startTest = false;
 235                     robot.keyRelease(textKeys[k]);
 236                     robot.keyRelease(modifierKeys[j]);
 237                     robot.keyRelease(modifierKeys[i]);
 238                 }
 239             }
 240         }
 241 
 242         frame.dispose();
 243         assertNull(error, error);
 244     }
 245 
 246     private void resetStatus() {
 247         for (int i = 0; i < modifierStatus.length; i++) {
 248             modifierStatus[i] = false;
 249         }
 250         for (int i = 0; i < textStatus.length; i++) {
 251             textStatus[i] = false;
 252         }
 253     }
 254 
 255 }