1 /*
   2  * Copyright (c) 2014, 2017, 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 import java.awt.*;
  25 import java.awt.event.*;
  26 
  27 /**
  28  * @test
  29  * @key headful
  30  * @summary Test that it is possible to type "Alt code" on Windows.
  31  * @requires (os.family == "windows")
  32  * @library ../../../../lib/testlibrary
  33  * @build ExtendedRobot
  34  * @run main AltPlusNumberKeyCombinationsTest
  35  */
  36 
  37 public class AltPlusNumberKeyCombinationsTest {
  38 
  39     private Frame frame;
  40     private TextField tf;
  41     private TextArea ta;
  42 
  43     private static int delay = 500;
  44     private static String euroChar = "\u20AC";
  45     private static String accChar = "\u00E3";
  46 
  47     private boolean passed = true;
  48     private ExtendedRobot robot;
  49 
  50     public AltPlusNumberKeyCombinationsTest() {
  51         try {
  52             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
  53                 public void run() {
  54                     initializeGUI();
  55                 }
  56             });
  57         } catch (Exception e) {
  58             e.printStackTrace();
  59             throw new RuntimeException("Test failed;", e.getCause());
  60         }
  61     }
  62 
  63     private void initializeGUI() {
  64         frame = new Frame("AltPlusNumberKeyCombinationsTest");
  65         frame.setLayout(new FlowLayout());
  66 
  67         tf = new TextField(15);
  68         frame.add(tf);
  69 
  70         ta = new TextArea(8, 20);
  71         frame.add(ta);
  72 
  73         frame.setSize(250,400);
  74         frame.setVisible(true);
  75     }
  76 
  77 
  78     private void doTest() throws Exception {
  79         robot = new ExtendedRobot();
  80         robot.setAutoDelay(100);
  81 
  82         robot.waitForIdle(delay);
  83 
  84         robot.mouseMove((int) ta.getLocationOnScreen().x + ta.getSize().width / 2,
  85                         (int) ta.getLocationOnScreen().y + ta.getSize().height / 2);
  86         robot.waitForIdle(delay);
  87         robot.mousePress(InputEvent.BUTTON1_MASK);
  88         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  89 
  90         robot.keyPress(KeyEvent.VK_ALT);
  91         robot.keyPress(KeyEvent.VK_RIGHT);
  92         robot.keyRelease(KeyEvent.VK_RIGHT);
  93         robot.keyRelease(KeyEvent.VK_ALT);
  94 
  95         robot.waitForIdle(delay);
  96 
  97         if (! "".equals(ta.getText())) {
  98             System.err.println("FAIL: Symbol typed in the text area when ALT + RIGHT ARROW keys typed");
  99             passed = false;
 100         }
 101 
 102         try {
 103             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 104                 public void run() {
 105                     ta.setText("");
 106                 }
 107             });
 108         } catch (Exception e) {
 109             e.printStackTrace();
 110             throw new RuntimeException("Test failed;", e.getCause());
 111         }
 112 
 113         // getLockingKeyState works on Windows;
 114         // Alt code only make sense on Windows;
 115         // so don't check availability of this and just use it.
 116         if( Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK) ) {
 117             robot.keyPress(KeyEvent.VK_NUM_LOCK);
 118             robot.keyRelease(KeyEvent.VK_NUM_LOCK);
 119         }
 120 
 121         robot.mouseMove((int) tf.getLocationOnScreen().x + tf.getSize().width / 2,
 122                         (int) tf.getLocationOnScreen().y + tf.getSize().height / 2);
 123         robot.waitForIdle(delay);
 124         robot.mousePress(InputEvent.BUTTON1_MASK);
 125         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126 
 127         robot.keyPress(KeyEvent.VK_ALT);
 128         robot.keyPress(KeyEvent.VK_NUMPAD0);
 129         robot.keyRelease(KeyEvent.VK_NUMPAD0);
 130         robot.keyPress(KeyEvent.VK_NUMPAD1);
 131         robot.keyRelease(KeyEvent.VK_NUMPAD1);
 132         robot.keyPress(KeyEvent.VK_NUMPAD2);
 133         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 134         robot.keyPress(KeyEvent.VK_NUMPAD8);
 135         robot.keyRelease(KeyEvent.VK_NUMPAD8);
 136         robot.keyRelease(KeyEvent.VK_ALT);
 137 
 138         robot.waitForIdle(delay);
 139 
 140         if (! euroChar.equals(tf.getText())) {
 141             System.err.println("FAIL: Euro symbol not typed in the text field when " +
 142                                "ALT + NUMPAD 1,2,8 keys typed");
 143             passed = false;
 144         }
 145 
 146         try {
 147             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 148                 public void run() {
 149                     tf.setText("");
 150                 }
 151             });
 152         } catch (Exception e) {
 153             e.printStackTrace();
 154             throw new RuntimeException("Test failed;", e.getCause());
 155         }
 156 
 157         robot.mouseMove((int) ta.getLocationOnScreen().x + ta.getSize().width / 2,
 158                         (int) ta.getLocationOnScreen().y + ta.getSize().height / 2);
 159         robot.waitForIdle(delay);
 160         robot.mousePress(InputEvent.BUTTON1_MASK);
 161         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 162 
 163         robot.keyPress(KeyEvent.VK_ALT);
 164         robot.keyPress(KeyEvent.VK_NUMPAD0);
 165         robot.keyRelease(KeyEvent.VK_NUMPAD0);
 166         robot.keyPress(KeyEvent.VK_NUMPAD1);
 167         robot.keyRelease(KeyEvent.VK_NUMPAD1);
 168         robot.keyPress(KeyEvent.VK_NUMPAD2);
 169         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 170         robot.keyPress(KeyEvent.VK_NUMPAD8);
 171         robot.keyRelease(KeyEvent.VK_NUMPAD8);
 172         robot.keyRelease(KeyEvent.VK_ALT);
 173 
 174         robot.waitForIdle(delay);
 175 
 176         if (! euroChar.equals(ta.getText())) {
 177             System.err.println("FAIL: Euro symbol not typed in the text area when " +
 178                                "ALT + NUMPAD 1,2,8 keys typed");
 179             passed = false;
 180         }
 181 
 182         try {
 183             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 184                 public void run() {
 185                     ta.setText("");
 186                 }
 187             });
 188         } catch (Exception e) {
 189             e.printStackTrace();
 190             throw new RuntimeException("Test failed;", e.getCause());
 191         }
 192 
 193         robot.mouseMove((int) tf.getLocationOnScreen().x + tf.getSize().width / 2,
 194                         (int) tf.getLocationOnScreen().y + tf.getSize().height / 2);
 195         robot.waitForIdle(delay);
 196         robot.mousePress(InputEvent.BUTTON1_MASK);
 197         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 198 
 199         robot.keyPress(KeyEvent.VK_ALT);
 200         robot.keyPress(KeyEvent.VK_NUMPAD0);
 201         robot.keyRelease(KeyEvent.VK_NUMPAD0);
 202         robot.keyPress(KeyEvent.VK_NUMPAD2);
 203         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 204         robot.keyPress(KeyEvent.VK_NUMPAD2);
 205         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 206         robot.keyPress(KeyEvent.VK_NUMPAD7);
 207         robot.keyRelease(KeyEvent.VK_NUMPAD7);
 208         robot.keyRelease(KeyEvent.VK_ALT);
 209 
 210         robot.waitForIdle(delay);
 211 
 212         if (! accChar.equals(tf.getText())) {
 213             System.err.println("FAIL: Symbol not typed in the text field when " +
 214                                "ALT + NUMPAD 2,2,7 keys typed");
 215             passed = false;
 216         }
 217 
 218         try {
 219             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 220                 public void run() {
 221                     tf.setText("");
 222                 }
 223             });
 224         } catch (Exception e) {
 225             e.printStackTrace();
 226             throw new RuntimeException("Test failed;", e.getCause());
 227         }
 228 
 229         robot.mouseMove((int) ta.getLocationOnScreen().x + ta.getSize().width / 2,
 230                         (int) ta.getLocationOnScreen().y + ta.getSize().height / 2);
 231         robot.waitForIdle(delay);
 232         robot.mousePress(InputEvent.BUTTON1_MASK);
 233         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 234 
 235         robot.keyPress(KeyEvent.VK_ALT);
 236         robot.keyPress(KeyEvent.VK_NUMPAD0);
 237         robot.keyRelease(KeyEvent.VK_NUMPAD0);
 238         robot.keyPress(KeyEvent.VK_NUMPAD2);
 239         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 240         robot.keyPress(KeyEvent.VK_NUMPAD2);
 241         robot.keyRelease(KeyEvent.VK_NUMPAD2);
 242         robot.keyPress(KeyEvent.VK_NUMPAD7);
 243         robot.keyRelease(KeyEvent.VK_NUMPAD7);
 244         robot.keyRelease(KeyEvent.VK_ALT);
 245 
 246         robot.waitForIdle(delay);
 247 
 248         if (! accChar.equals(ta.getText())) {
 249             System.err.println("FAIL: Symbol not typed in the text field when " +
 250                                "ALT + NUMPAD 2,2,7 keys typed");
 251             passed = false;
 252         }
 253 
 254         try {
 255             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 256                 public void run() {
 257                     ta.setText("");
 258                 }
 259             });
 260         } catch (Exception e) {
 261             e.printStackTrace();
 262             throw new RuntimeException("Test failed;", e.getCause());
 263         }
 264 
 265         if (! passed) {
 266             System.err.println("Test failed");
 267             captureScreenAndSave();
 268             throw new RuntimeException("Test failed");
 269         } else {
 270             System.out.println("Test passed");
 271         }
 272     }
 273 
 274     public static void main(String[] args) {
 275         if (System.getProperty("os.name").indexOf("Win") == -1) {
 276             System.out.println("This test is supposed to be run only on Windows! Marking as passed..");
 277             return;
 278         }
 279         try {
 280             AltPlusNumberKeyCombinationsTest test = new AltPlusNumberKeyCombinationsTest();
 281             test.doTest();
 282         } catch (Exception e) {
 283             e.printStackTrace();
 284             throw new RuntimeException("Fail; "+e.getMessage());
 285         }
 286     }
 287 
 288     /**
 289      * Do screen capture and save it as image
 290      */
 291     private static void captureScreenAndSave() {
 292 
 293         try {
 294             Robot robot = new Robot();
 295             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 296             Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
 297             System.out.println("About to screen capture - " + rectangle);
 298             java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle);
 299             javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg"));
 300             robot.delay(3000);
 301         } catch (Throwable t) {
 302             System.out.println("WARNING: Exception thrown while screen capture!");
 303             t.printStackTrace();
 304         }
 305     }
 306 }