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 
  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.assertTrue;
  36 
  37 /*
  38  * @test 8155742
  39  * @summary Make sure that modifier key mask is set when robot press
  40  *          some key with one or more modifiers.
  41  * @library ../../../../lib/testlibrary/
  42  * @build ExtendedRobot
  43  * @key headful
  44  * @run main/timeout=600 ModifierRobotEnhancedKeyTest
  45  */
  46 
  47 public class ModifierRobotEnhancedKeyTest 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         String os = System.getProperty("os.name").toLowerCase();
  66         if (!os.contains("windows")) {
  67             System.out.println("*** this test is for windows only because some of the tested key combinations " +
  68                                "might be caught by the os and therefore don't reach the canvas ***");
  69             return;
  70         }
  71 
  72         ModifierRobotEnhancedKeyTest test = new ModifierRobotEnhancedKeyTest();
  73         test.doTest();
  74     }
  75 
  76     public ModifierRobotEnhancedKeyTest() throws Exception {
  77         modifierKeys =  new int[4];
  78         modifierKeys[0] = KeyEvent.VK_SHIFT;
  79         modifierKeys[1] = KeyEvent.VK_CONTROL;
  80         modifierKeys[2] = KeyEvent.VK_ALT;
  81         modifierKeys[3] = KeyEvent.VK_ALT_GRAPH;
  82 
  83         inputMasks = new int[4];
  84         inputMasks[0] =  InputEvent.SHIFT_MASK;
  85         inputMasks[1] =  InputEvent.CTRL_MASK;
  86         inputMasks[2] =  InputEvent.ALT_MASK;
  87         inputMasks[3] =  InputEvent.ALT_GRAPH_MASK;
  88 
  89         modifierStatus = new boolean[modifierKeys.length];
  90 
  91         textKeys = new int[6];
  92         textKeys[0] = KeyEvent.VK_A;
  93         textKeys[1] = KeyEvent.VK_S;
  94         textKeys[2] = KeyEvent.VK_DELETE;
  95         textKeys[3] = KeyEvent.VK_HOME;
  96         textKeys[4] = KeyEvent.VK_F12;
  97         textKeys[5] = KeyEvent.VK_LEFT;
  98 
  99         textStatus = new boolean[textKeys.length];
 100 
 101         EventQueue.invokeAndWait( () -> { initializeGUI(); });
 102     }
 103 
 104     public void keyPressed(KeyEvent event) {
 105 
 106         tempPress = true;
 107         synchronized (tempLock) { tempLock.notifyAll(); }
 108 
 109         if (! startTest) {
 110             return;
 111         }
 112         for (int x = 0; x < inputMasks.length; x++) {
 113             if ((event.getModifiers() & inputMasks[x]) != 0) {
 114                 System.out.println("Modifier set: " + 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: " + event.getKeyText(textKeys[x]));
 121                 textStatus[x] = true;
 122             }
 123         }
 124 
 125         synchronized (keyLock) { keyLock.notifyAll(); }
 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.setSize(200, 200);
 138         frame.setVisible(true);
 139     }
 140 
 141     public void doTest() throws Exception {
 142         robot = new ExtendedRobot();
 143 
 144         robot.mouseMove((int) frame.getLocationOnScreen().getX() + frame.getSize().width / 2,
 145                         (int) frame.getLocationOnScreen().getY() + frame.getSize().height / 2);
 146         robot.click(MouseEvent.BUTTON1_MASK);
 147         robot.waitForIdle();
 148 
 149         assertTrue(focusGained, "FAIL: Canvas gained focus!");
 150 
 151         for (int i = 0; i < modifierKeys.length; i++) {
 152             for (int j = 0; j < textKeys.length; j++) {
 153                 tempPress = false;
 154                 robot.keyPress(modifierKeys[i]);
 155                 robot.waitForIdle();
 156                 if (! tempPress) {
 157                     synchronized (tempLock) { tempLock.wait(waitDelay); }
 158                 }
 159                 assertTrue(tempPress, "FAIL: keyPressed triggered for i=" + i);
 160 
 161                 resetStatus();
 162                 startTest = true;
 163                 robot.keyPress(textKeys[j]);
 164                 robot.waitForIdle();
 165                 if (! modifierStatus[i] || ! textStatus[j]) {
 166                     synchronized (keyLock) { keyLock.wait(waitDelay); }
 167                 }
 168 
 169 
 170                 assertTrue(modifierStatus[i] && textStatus[j],
 171                         "FAIL: KeyEvent not proper!"+
 172                         "Key checked: i=" + i + "; j=" + j+
 173                         "ModifierStatus = " + modifierStatus[i]+
 174                         "TextStatus = " + textStatus[j]);
 175                 startTest = false;
 176                 robot.keyRelease(textKeys[j]);
 177                 robot.waitForIdle();
 178                 robot.keyRelease(modifierKeys[i]);
 179                 robot.waitForIdle();
 180             }
 181         }
 182 
 183         for (int i = 0; i < modifierKeys.length; i++) {
 184             for (int j = i + 1; j < modifierKeys.length; j++) {
 185                 for (int k = 0; k < textKeys.length; k++) {
 186                     tempPress = false;
 187                     robot.keyPress(modifierKeys[i]);
 188                     robot.waitForIdle();
 189                     if (! tempPress) {
 190                         synchronized (tempLock) { tempLock.wait(waitDelay); }
 191                     }
 192 
 193                     assertTrue(tempPress, "FAIL: MultiKeyTest: keyPressed triggered for i=" + i);
 194 
 195                     tempPress = false;
 196                     robot.keyPress(modifierKeys[j]);
 197                     robot.waitForIdle();
 198                     if (! tempPress) {
 199                         synchronized (tempLock) { tempLock.wait(waitDelay); }
 200                     }
 201                     assertTrue(tempPress, "FAIL: MultiKeyTest keyPressed triggered for j=" + j);
 202 
 203                     resetStatus();
 204                     startTest = true;
 205                     robot.keyPress(textKeys[k]);
 206                     robot.waitForIdle();
 207                     if (! modifierStatus[i] || ! modifierStatus[j] || ! textStatus[k]) {
 208                         synchronized (keyLock) {
 209                             keyLock.wait(waitDelay);
 210                         }
 211                     }
 212                     assertTrue(modifierStatus[i] && modifierStatus[j] && textStatus[k],
 213                             "FAIL: KeyEvent not proper!"+
 214                             "Key checked: i=" + i + "; j=" + j + "; k=" + k+
 215                             "Modifier1Status = " + modifierStatus[i]+
 216                             "Modifier2Status = " + modifierStatus[j]+
 217                             "TextStatus = " + textStatus[k]);
 218 
 219                     startTest = false;
 220                     robot.keyRelease(textKeys[k]);
 221                     robot.waitForIdle();
 222                     robot.keyRelease(modifierKeys[j]);
 223                     robot.waitForIdle();
 224                     robot.keyRelease(modifierKeys[i]);
 225                     robot.waitForIdle();
 226                 }
 227             }
 228         }
 229 
 230         frame.dispose();
 231     }
 232 
 233     private void resetStatus() {
 234         for (int i = 0; i < modifierStatus.length; i++) {
 235             modifierStatus[i] = false;
 236         }
 237         for (int i = 0; i < textStatus.length; i++) {
 238             textStatus[i] = false;
 239         }
 240     }
 241 
 242 }