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