1 /*
   2  * Copyright (c) 2001, 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 
  25 /*
  26  * @test
  27  * @key headful
  28  * @bug 8043126
  29  * @summary Check whether KeyEvent.getModifiers() returns correct modifiers
  30  *          when Ctrl, Alt or Shift keys are pressed.
  31  *
  32  * @library /lib/client/ ../../helpers/lwcomponents/
  33  * @library /test/lib
  34  * @build LWComponent
  35  * @build LWButton
  36  * @build LWList
  37  * @build ExtendedRobot
  38  * @run main/timeout=600 KeyMaskTest
  39  */
  40 
  41 
  42 import java.awt.*;
  43 import java.awt.event.InputEvent;
  44 
  45 import java.awt.event.KeyAdapter;
  46 import java.awt.event.KeyEvent;
  47 
  48 import java.util.ArrayList;
  49 
  50 import static jdk.test.lib.Asserts.*;
  51 
  52 import test.java.awt.event.helpers.lwcomponents.LWButton;
  53 import test.java.awt.event.helpers.lwcomponents.LWList;
  54 
  55 
  56 
  57 public class KeyMaskTest extends KeyAdapter {
  58 
  59     Frame frame;
  60 
  61     Button    button;
  62     LWButton  buttonLW;
  63     TextField textField;
  64     TextArea  textArea;
  65     List      list;
  66     LWList    listLW;
  67 
  68     int buttonPressedNumber;
  69     int buttonReleasedNumber;
  70 
  71     ExtendedRobot robot;
  72 
  73     private final static int robotDelay = 1500;
  74     private final static int waitDelay  = 3500;
  75 
  76     final Object lock;
  77 
  78     private boolean keyPressReceived = false;
  79     private int keyCode = -1;
  80 
  81     KeyMaskTest() throws Exception {
  82         lock = new Object();
  83         robot = new ExtendedRobot();
  84         EventQueue.invokeAndWait( this::createGUI );
  85     }
  86 
  87     public void createGUI() {
  88 
  89         frame = new Frame();
  90         frame.setTitle("KeyMaskTest");
  91         frame.setLayout(new GridLayout(1, 6));
  92 
  93         button = new Button();
  94         button.addKeyListener(this);
  95         frame.add(button);
  96 
  97         buttonLW = new LWButton();
  98         buttonLW.addKeyListener(this);
  99         frame.add(buttonLW);
 100 
 101         textField = new TextField(5);
 102         textField.addKeyListener(this);
 103         frame.add(textField);
 104 
 105         textArea = new TextArea(5, 5);
 106         textArea.addKeyListener(this);
 107         frame.add(textArea);
 108 
 109         list = new List();
 110         for (int i = 1; i <= 5; ++i) { list.add("item " + i); }
 111         list.addKeyListener(this);
 112         frame.add(list);
 113 
 114         listLW = new LWList();
 115         for (int i = 1; i <= 5; ++i) { listLW.add("item " + i); }
 116         listLW.addKeyListener(this);
 117         frame.add(listLW);
 118 
 119 
 120         frame.setBackground(Color.gray);
 121         frame.setSize(500, 100);
 122         frame.setVisible(true);
 123         frame.toFront();
 124     }
 125 
 126     @Override
 127     public void keyPressed(KeyEvent e) {
 128 
 129         keyPressReceived = true;
 130 
 131         int code = e.getKeyCode();
 132 
 133         assertEQ(code, keyCode, "wrong key code");
 134 
 135         int mask = 0;
 136 
 137         if (code == KeyEvent.VK_SHIFT) {
 138             mask = InputEvent.SHIFT_MASK;
 139         } else if (code == KeyEvent.VK_CONTROL) {
 140             mask = InputEvent.CTRL_MASK;
 141         } else if (code == KeyEvent.VK_ALT) {
 142             mask = InputEvent.ALT_MASK;
 143         } else if (code == KeyEvent.VK_META) {
 144             mask = InputEvent.META_MASK;
 145         }
 146 
 147         int mod = e.getModifiers() & mask;
 148         assertEQ(mod, mask, "invalid key mask");
 149 
 150         synchronized (lock) { lock.notifyAll(); }
 151     }
 152 
 153 
 154     void doTest() throws Exception {
 155 
 156         ArrayList<Component> components = new ArrayList();
 157         components.add(button);
 158         components.add(buttonLW);
 159         components.add(textField);
 160         components.add(textArea);
 161         components.add(list);
 162         components.add(listLW);
 163 
 164         int keys[];
 165         String OS = System.getProperty("os.name").toLowerCase();
 166         System.out.println(OS);
 167         if (OS.contains("os x") || OS.contains("sunos")) {
 168             keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_META};
 169         } else {
 170             keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT};
 171         }
 172 
 173         for (Component c: components) {
 174 
 175             System.out.print(c.getClass().getName() + ": ");
 176 
 177             Point origin = c.getLocationOnScreen();
 178             int xc = origin.x + c.getWidth() / 2;
 179             int yc = origin.y + c.getHeight() / 2;
 180             Point center = new Point(xc, yc);
 181 
 182             robot.delay(robotDelay);
 183             robot.glide(origin, center);
 184             robot.click();
 185             robot.delay(robotDelay);
 186 
 187             for (int k = 0; k < keys.length; ++k) {
 188 
 189                 keyPressReceived = false;
 190 
 191                 keyCode = keys[k];
 192 
 193                 robot.type(keyCode);
 194 
 195                 robot.delay(robotDelay);
 196 
 197                 if (!keyPressReceived) {
 198                     synchronized (lock) {
 199                         try {
 200                             lock.wait(waitDelay);
 201                         } catch (InterruptedException e) {}
 202                     }
 203                 }
 204 
 205                 assertTrue(keyPressReceived, "key press event was not received");
 206             }
 207 
 208             System.out.println("passed");
 209         }
 210 
 211         robot.waitForIdle();
 212         frame.dispose();
 213     }
 214 
 215 
 216     public static void main(String[] args) throws Exception {
 217 
 218         KeyMaskTest test = new KeyMaskTest();
 219         test.doTest();
 220     }
 221 }