jdk/test/java/awt/Robot/ModifierRobotKey/ModifierRobotKeyTest.java
changeset 47127 56441eb0a8ec
parent 40128 e635645d2a8a
equal deleted inserted replaced
47126:188ef162f019 47127:56441eb0a8ec
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    30 import java.awt.event.InputEvent;
    30 import java.awt.event.InputEvent;
    31 import java.awt.event.KeyAdapter;
    31 import java.awt.event.KeyAdapter;
    32 import java.awt.event.KeyEvent;
    32 import java.awt.event.KeyEvent;
    33 import java.awt.event.MouseEvent;
    33 import java.awt.event.MouseEvent;
    34 
    34 
       
    35 import static jdk.testlibrary.Asserts.assertNull;
    35 import static jdk.testlibrary.Asserts.assertTrue;
    36 import static jdk.testlibrary.Asserts.assertTrue;
    36 
    37 
    37 /*
    38 /*
    38  * @test 8155742
    39  * @test 8155742
    39  * @key headful
    40  * @key headful
    55     private volatile boolean tempPress = false;
    56     private volatile boolean tempPress = false;
    56 
    57 
    57     private int[] textKeys, modifierKeys, inputMasks;
    58     private int[] textKeys, modifierKeys, inputMasks;
    58     private boolean[] modifierStatus, textStatus;
    59     private boolean[] modifierStatus, textStatus;
    59 
    60 
    60     private final static int waitDelay = 5000;
    61     private final static int WAIT_DELAY = 5000;
    61     private Object tempLock = new Object();
    62     private final Object lock = new Object();
    62     private Object keyLock = new Object();
       
    63 
    63 
    64     public static void main(String[] args) throws Exception {
    64     public static void main(String[] args) throws Exception {
    65         ModifierRobotKeyTest test = new ModifierRobotKeyTest();
    65         ModifierRobotKeyTest test = new ModifierRobotKeyTest();
    66         test.doTest();
    66         test.doTest();
    67     }
    67     }
    97 
    97 
    98         EventQueue.invokeAndWait( () -> { initializeGUI(); });
    98         EventQueue.invokeAndWait( () -> { initializeGUI(); });
    99     }
    99     }
   100 
   100 
   101     public void keyPressed(KeyEvent event) {
   101     public void keyPressed(KeyEvent event) {
   102 
   102         synchronized (lock) {
   103         tempPress = true;
   103             tempPress = true;
   104         synchronized (tempLock) { tempLock.notifyAll(); }
   104             lock.notifyAll();
   105 
   105 
   106         if (! startTest) {
   106             if (! startTest) {
   107             return;
   107                 return;
   108         }
   108             }
   109         for (int x = 0; x < inputMasks.length; x++) {
   109             for (int x = 0; x < inputMasks.length; x++) {
   110             if ((event.getModifiers() & inputMasks[x]) != 0) {
   110                 if ((event.getModifiers() & inputMasks[x]) != 0) {
   111                 System.out.println("Modifier set: " + event.getKeyModifiersText(inputMasks[x]));
   111                     System.out.println("Modifier set: " +
   112                 modifierStatus[x] = true;
   112                                       event.getKeyModifiersText(inputMasks[x]));
   113             }
   113                     modifierStatus[x] = true;
   114         }
   114                 }
   115         for (int x = 0; x < textKeys.length; x++) {
   115             }
   116             if (event.getKeyCode() == textKeys[x]) {
   116             for (int x = 0; x < textKeys.length; x++) {
   117                 System.out.println("Text set: " + event.getKeyText(textKeys[x]));
   117                 if (event.getKeyCode() == textKeys[x]) {
   118                 textStatus[x] = true;
   118                     System.out.println("Text set: " +
   119             }
   119                                                  event.getKeyText(textKeys[x]));
   120         }
   120                     textStatus[x] = true;
   121 
   121                 }
   122         synchronized (keyLock) { keyLock.notifyAll(); }
   122             }
       
   123         }
   123     }
   124     }
   124 
   125 
   125     private void initializeGUI() {
   126     private void initializeGUI() {
   126         frame = new Frame("Test frame");
   127         frame = new Frame("Test frame");
   127         canvas = new Canvas();
   128         canvas = new Canvas();
   129             public void focusGained(FocusEvent event) { focusGained = true; }
   130             public void focusGained(FocusEvent event) { focusGained = true; }
   130         });
   131         });
   131         canvas.addKeyListener(this);
   132         canvas.addKeyListener(this);
   132         frame.setLayout(new BorderLayout());
   133         frame.setLayout(new BorderLayout());
   133         frame.add(canvas);
   134         frame.add(canvas);
   134         frame.setSize(200, 200);
   135         frame.setBounds(200, 200, 200, 200);
   135         frame.setVisible(true);
   136         frame.setVisible(true);
   136     }
   137     }
   137 
   138 
   138     public void doTest() throws Exception {
   139     public void doTest() throws Exception {
   139         robot = new ExtendedRobot();
   140         robot = new ExtendedRobot();
   140 
   141         robot.setAutoDelay(20);
   141         robot.mouseMove((int) frame.getLocationOnScreen().getX() + frame.getSize().width / 2,
   142         robot.waitForIdle();
   142                         (int) frame.getLocationOnScreen().getY() + frame.getSize().height / 2);
   143 
       
   144         robot.mouseMove((int) frame.getLocationOnScreen().getX() +
       
   145                                                     frame.getSize().width / 2,
       
   146                         (int) frame.getLocationOnScreen().getY() +
       
   147                                                     frame.getSize().height / 2);
   143         robot.click(MouseEvent.BUTTON1_MASK);
   148         robot.click(MouseEvent.BUTTON1_MASK);
   144         robot.waitForIdle();
   149         robot.waitForIdle();
   145 
       
   146         assertTrue(focusGained, "FAIL: Canvas gained focus!");
   150         assertTrue(focusGained, "FAIL: Canvas gained focus!");
   147 
   151 
       
   152         String error = null;
       
   153         exit1:
   148         for (int i = 0; i < modifierKeys.length; i++) {
   154         for (int i = 0; i < modifierKeys.length; i++) {
   149             for (int j = 0; j < textKeys.length; j++) {
   155             for (int j = 0; j < textKeys.length; j++) {
   150                 tempPress = false;
   156                 if (error != null) {
   151                 robot.keyPress(modifierKeys[i]);
   157                     break exit1;
   152                 robot.waitForIdle();
   158                 }
   153                 if (! tempPress) {
   159                 robot.waitForIdle(100);
   154                     synchronized (tempLock) { tempLock.wait(waitDelay); }
   160                 synchronized (lock) {
   155                 }
   161                     tempPress = false;
   156                 assertTrue(tempPress, "FAIL: keyPressed triggered for i=" + i);
   162                     robot.keyPress(modifierKeys[i]);
   157 
   163                     lock.wait(WAIT_DELAY);
   158                 resetStatus();
   164                 }
   159                 startTest = true;
   165                 if (!tempPress) {
   160                 robot.keyPress(textKeys[j]);
   166                     error ="FAIL: keyPressed triggered for i=" + i;
   161                 robot.waitForIdle();
   167                 }
   162                 if (! modifierStatus[i] || ! textStatus[j]) {
   168 
   163                     synchronized (keyLock) { keyLock.wait(waitDelay); }
   169                 synchronized (lock) {
   164                 }
   170                     resetStatus();
   165 
   171                     startTest = true;
   166 
   172                     robot.keyPress(textKeys[j]);
   167                 assertTrue(modifierStatus[i] && textStatus[j],
   173                     lock.wait(WAIT_DELAY);
   168                         "FAIL: KeyEvent not proper!"+
   174                 }
   169                         "Key checked: i=" + i + "; j=" + j+
   175 
   170                         "ModifierStatus = " + modifierStatus[i]+
   176                 if (!(modifierStatus[i] && textStatus[j])) {
   171                         "TextStatus = " + textStatus[j]);
   177                     error = "FAIL: KeyEvent not proper!"+
       
   178                             "Key checked: i=" + i + "; j=" + j+
       
   179                             "ModifierStatus = " + modifierStatus[i]+
       
   180                             "TextStatus = " + textStatus[j];
       
   181                 }
       
   182 
   172                 startTest = false;
   183                 startTest = false;
   173                 robot.keyRelease(textKeys[j]);
   184                 robot.keyRelease(textKeys[j]);
   174                 robot.waitForIdle();
       
   175                 robot.keyRelease(modifierKeys[i]);
   185                 robot.keyRelease(modifierKeys[i]);
   176                 robot.waitForIdle();
   186             }
   177             }
   187         }
   178         }
   188 
   179 
   189         exit2:
   180         for (int i = 0; i < modifierKeys.length; i++) {
   190         for (int i = 0; i < modifierKeys.length; i++) {
   181             for (int j = i + 1; j < modifierKeys.length; j++) {
   191             for (int j = i + 1; j < modifierKeys.length; j++) {
   182                 for (int k = 0; k < textKeys.length; k++) {
   192                 for (int k = 0; k < textKeys.length; k++) {
   183                     tempPress = false;
   193                     if (error != null) {
   184                     robot.keyPress(modifierKeys[i]);
   194                         break exit2;
   185                     robot.waitForIdle();
   195                     }
   186                     if (! tempPress) {
   196                     robot.waitForIdle(100);
   187                         synchronized (tempLock) { tempLock.wait(waitDelay); }
   197                     synchronized (lock) {
   188                     }
   198                         tempPress = false;
   189 
   199                         robot.keyPress(modifierKeys[i]);
   190                     assertTrue(tempPress, "FAIL: MultiKeyTest: keyPressed triggered for i=" + i);
   200                         lock.wait(WAIT_DELAY);
   191 
   201                     }
   192                     tempPress = false;
   202 
   193                     robot.keyPress(modifierKeys[j]);
   203                     if (!tempPress) {
   194                     robot.waitForIdle();
   204                         error = "FAIL: MultiKeyTest: keyPressed " +
   195                     if (! tempPress) {
   205                                                          "triggered for i=" + i;
   196                         synchronized (tempLock) { tempLock.wait(waitDelay); }
   206                     }
   197                     }
   207 
   198                     assertTrue(tempPress, "FAIL: MultiKeyTest keyPressed triggered for j=" + j);
   208                     synchronized (lock) {
   199 
   209                         tempPress = false;
   200                     resetStatus();
   210                         robot.keyPress(modifierKeys[j]);
   201                     startTest = true;
   211                         lock.wait(WAIT_DELAY);
   202                     robot.keyPress(textKeys[k]);
   212                     }
   203                     robot.waitForIdle();
   213                     if (!tempPress) {
   204                     if (! modifierStatus[i] || ! modifierStatus[j] || ! textStatus[k]) {
   214                         error = "FAIL: MultiKeyTest keyPressed " +
   205                         synchronized (keyLock) {
   215                                                          "triggered for j=" + j;
   206                             keyLock.wait(waitDelay);
   216                     };
   207                         }
   217 
   208                     }
   218                     synchronized (lock) {
   209                     assertTrue(modifierStatus[i] && modifierStatus[j] && textStatus[k],
   219                         resetStatus();
   210                             "FAIL: KeyEvent not proper!"+
   220                         startTest = true;
   211                             "Key checked: i=" + i + "; j=" + j + "; k=" + k+
   221                         robot.keyPress(textKeys[k]);
   212                             "Modifier1Status = " + modifierStatus[i]+
   222                         lock.wait(WAIT_DELAY);
   213                             "Modifier2Status = " + modifierStatus[j]+
   223                     }
   214                             "TextStatus = " + textStatus[k]);
   224                     if (!(modifierStatus[i] && modifierStatus[j]
       
   225                                                               && textStatus[k]))
       
   226                     {
       
   227                         error = "FAIL: KeyEvent not proper!" +
       
   228                                "Key checked: i=" + i + "; j=" + j + "; k=" + k +
       
   229                                "Modifier1Status = " + modifierStatus[i] +
       
   230                                "Modifier2Status = " + modifierStatus[j] +
       
   231                                "TextStatus = " + textStatus[k];
       
   232                     }
   215 
   233 
   216                     startTest = false;
   234                     startTest = false;
   217                     robot.keyRelease(textKeys[k]);
   235                     robot.keyRelease(textKeys[k]);
   218                     robot.waitForIdle();
       
   219                     robot.keyRelease(modifierKeys[j]);
   236                     robot.keyRelease(modifierKeys[j]);
   220                     robot.waitForIdle();
       
   221                     robot.keyRelease(modifierKeys[i]);
   237                     robot.keyRelease(modifierKeys[i]);
   222                     robot.waitForIdle();
       
   223                 }
   238                 }
   224             }
   239             }
   225         }
   240         }
   226 
   241 
   227         frame.dispose();
   242         frame.dispose();
       
   243         assertNull(error, error);
   228     }
   244     }
   229 
   245 
   230     private void resetStatus() {
   246     private void resetStatus() {
   231         for (int i = 0; i < modifierStatus.length; i++) {
   247         for (int i = 0; i < modifierStatus.length; i++) {
   232             modifierStatus[i] = false;
   248             modifierStatus[i] = false;