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