1 /*
   2  * Copyright (c) 2001, 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 /*
  26  * @test
  27  * @key headful
  28  * @bug 8043126 8145116
  29  * @summary Check whether
  30  *          1. correct extended modifiers are returned
  31  *             by KeyEvent.getModifiersEx()
  32  *          2. InputEvent.getModifiersExText() returns
  33  *             correct extended modifier keys description
  34  *
  35  * @library ../../../../../lib/testlibrary/  ../../helpers/lwcomponents/
  36  * @build LWComponent
  37  * @build LWButton
  38  * @build LWList
  39  * @build ExtendedRobot
  40  * @run main/timeout=600 ExtendedModifiersTest
  41  */
  42 import java.awt.Button;
  43 import java.awt.Color;
  44 import java.awt.Component;
  45 import java.awt.EventQueue;
  46 import java.awt.Frame;
  47 import java.awt.GridLayout;
  48 import java.awt.List;
  49 import java.awt.Point;
  50 import java.awt.TextArea;
  51 import java.awt.TextField;
  52 import java.awt.event.InputEvent;
  53 import java.awt.event.KeyEvent;
  54 import java.awt.event.KeyListener;
  55 import java.util.ArrayList;
  56 
  57 import static jdk.testlibrary.Asserts.*;
  58 import test.java.awt.event.helpers.lwcomponents.LWButton;
  59 import test.java.awt.event.helpers.lwcomponents.LWList;
  60 
  61 public class ExtendedModifiersTest implements KeyListener {
  62 
  63     Frame frame;
  64     Button button;
  65     LWButton buttonLW;
  66     TextField textField;
  67     TextArea textArea;
  68     List list;
  69     LWList listLW;
  70 
  71     private final ExtendedRobot robot;
  72     private static final int WAIT_DELAY = 5000;
  73     private static final int KEY_DELAY = 500;
  74     private final Object lock;
  75 
  76     private boolean keyPressedFlag;
  77     private int modifiersEx = 0;
  78     private String exText = "";
  79 
  80     @Override
  81     public void keyTyped(KeyEvent e) {
  82     }
  83 
  84     @Override
  85     public void keyPressed(KeyEvent e) {
  86 
  87         if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  88             return;
  89         }
  90         modifiersEx = e.getModifiersEx();
  91         exText = InputEvent.getModifiersExText(modifiersEx);
  92         keyPressedFlag = true;
  93 
  94         synchronized (lock) {
  95             lock.notifyAll();
  96         }
  97     }
  98 
  99     @Override
 100     public void keyReleased(KeyEvent e) {
 101     }
 102 
 103     public void createGUI() {
 104 
 105         frame = new Frame();
 106         frame.setTitle("ExtendedModifiersTest");
 107         frame.setLayout(new GridLayout(1, 6));
 108 
 109         button = new Button();
 110         button.addKeyListener(this);
 111         frame.add(button);
 112 
 113         buttonLW = new LWButton();
 114         buttonLW.addKeyListener(this);
 115         frame.add(buttonLW);
 116 
 117         textField = new TextField(5);
 118         textField.addKeyListener(this);
 119         frame.add(textField);
 120 
 121         textArea = new TextArea(5, 5);
 122         textArea.addKeyListener(this);
 123         frame.add(textArea);
 124 
 125         list = new List();
 126         for (int i = 1; i <= 5; ++i) {
 127             list.add("item " + i);
 128         }
 129         list.addKeyListener(this);
 130         frame.add(list);
 131 
 132         listLW = new LWList();
 133         for (int i = 1; i <= 5; ++i) {
 134             listLW.add("item " + i);
 135         }
 136         listLW.addKeyListener(this);
 137         frame.add(listLW);
 138 
 139         frame.setBackground(Color.gray);
 140         frame.setSize(500, 100);
 141         frame.setVisible(true);
 142         frame.toFront();
 143     }
 144 
 145     public ExtendedModifiersTest() throws Exception {
 146         lock = new Object();
 147         robot = new ExtendedRobot();
 148         EventQueue.invokeAndWait(this::createGUI);
 149     }
 150 
 151     private void runScenario(int keys[], int expectedMask) {
 152         if (keys.length < 1) {
 153             return;
 154         }
 155 
 156         for (int k = 0; k < keys.length; ++k) {
 157 
 158             keyPressedFlag = false;
 159             robot.keyPress(keys[k]);
 160             robot.delay(KEY_DELAY);
 161 
 162             if (!keyPressedFlag) {
 163                 synchronized (lock) {
 164                     try {
 165                         lock.wait(WAIT_DELAY);
 166                     } catch (InterruptedException ex) {
 167                         ex.printStackTrace();
 168                     }
 169                 }
 170             }
 171 
 172             if (!keyPressedFlag) {
 173                 robot.keyRelease(keys[k]);
 174                 robot.delay(KEY_DELAY);
 175                 assertTrue(false, "key press event was not received");
 176             }
 177         }
 178 
 179         int modEx = modifiersEx & expectedMask;
 180 
 181         for (int k = keys.length - 1; k >= 0; --k) {
 182             robot.keyRelease(keys[k]);
 183             robot.delay(KEY_DELAY);
 184         }
 185 
 186         assertEQ(expectedMask, modEx, "invalid extended modifiers");
 187 
 188         for (int k = 0; k < keys.length; ++k) {
 189             String keyText = KeyEvent.getKeyText(keys[k]).toLowerCase();
 190             assertTrue(exText.toLowerCase().contains(keyText),
 191                     "invalid extended modifier keys description");
 192         }
 193 
 194         System.out.println(exText + " : passed");
 195 
 196         robot.type(KeyEvent.VK_ESCAPE);
 197         robot.waitForIdle();
 198     }
 199 
 200     private void doTest() throws Exception {
 201 
 202         ArrayList<Component> components = new ArrayList();
 203         components.add(button);
 204         components.add(buttonLW);
 205         components.add(textField);
 206         components.add(textArea);
 207         components.add(list);
 208         components.add(listLW);
 209 
 210         String OS = System.getProperty("os.name").toLowerCase();
 211         System.out.println(OS);
 212 
 213         for (Component c : components) {
 214 
 215             String className = c.getClass().getName();
 216             System.out.println("component class : " + className);
 217 
 218             Point origin = c.getLocationOnScreen();
 219             int xc = origin.x + c.getWidth() / 2;
 220             int yc = origin.y + c.getHeight() / 2;
 221             Point center = new Point(xc, yc);
 222 
 223             robot.waitForIdle();
 224             robot.glide(origin, center);
 225             robot.click();
 226             robot.waitForIdle();
 227 
 228             // 1. shift + control
 229             runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL},
 230                     InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
 231 
 232             // 2. alt + shift + control
 233             runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT,
 234                 KeyEvent.VK_CONTROL}, InputEvent.ALT_DOWN_MASK
 235                     | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
 236 
 237             // 3. shift
 238             runScenario(new int[]{KeyEvent.VK_SHIFT},
 239                     InputEvent.SHIFT_DOWN_MASK);
 240 
 241             // 4. alt + control
 242             runScenario(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_CONTROL},
 243                     InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
 244 
 245             // 5. shift + alt
 246             runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_ALT},
 247                     InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
 248 
 249             if (OS.contains("os x") || OS.contains("sunos")) {
 250                 // 6. meta
 251                 runScenario(new int[]{KeyEvent.VK_META},
 252                         InputEvent.META_DOWN_MASK);
 253 
 254                 // 7. shift + ctrl + alt + meta
 255                 runScenario(new int[]{KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL,
 256                     KeyEvent.VK_ALT, KeyEvent.VK_META},
 257                         InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK
 258                         | InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK);
 259 
 260                 // 8. meta + shift + ctrl
 261                 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
 262                     KeyEvent.VK_CONTROL}, InputEvent.META_DOWN_MASK
 263                       | InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK);
 264 
 265                 // 9. meta + shift + alt
 266                 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_SHIFT,
 267                     KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
 268                       | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
 269 
 270                 // 10. meta + ctrl + alt
 271                 runScenario(new int[]{KeyEvent.VK_META, KeyEvent.VK_CONTROL,
 272                     KeyEvent.VK_ALT}, InputEvent.META_DOWN_MASK
 273                       | InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK);
 274             }
 275         }
 276 
 277         robot.waitForIdle();
 278         frame.dispose();
 279     }
 280 
 281     public static void main(String[] args) throws Exception {
 282         ExtendedModifiersTest test = new ExtendedModifiersTest();
 283         test.doTest();
 284     }
 285 }