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