1 /*
   2  * Copyright (c) 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 /*
  25  * @test
  26  * @bug 8148344
  27  * @summary Unicode text print via Java Robot
  28    @run main/manual RobotUnicodeCodeTest
  29  */
  30 import java.awt.GridBagConstraints;
  31 import java.awt.GridBagLayout;
  32 import java.awt.event.ActionEvent;
  33 import java.util.concurrent.CountDownLatch;
  34 import java.util.concurrent.TimeUnit;
  35 import javax.swing.BorderFactory;
  36 import javax.swing.JButton;
  37 import javax.swing.JFrame;
  38 import javax.swing.JPanel;
  39 import javax.swing.JTextArea;
  40 import javax.swing.SwingUtilities;
  41 import java.awt.AWTException;
  42 import java.awt.Robot;
  43 import java.awt.event.InputEvent;
  44 import java.awt.event.WindowAdapter;
  45 import java.awt.event.WindowEvent;
  46 
  47 public class RobotUnicodeCodeTest {
  48     private static TestUI test = null;
  49     private static CountDownLatch testLatch = null;
  50 
  51     public static void disposeUI() throws Exception {
  52         SwingUtilities.invokeAndWait(() -> {
  53             if(test != null) {
  54                 test.disposeUI();
  55             }
  56         });
  57     }
  58 
  59     public static void main(String[] args) throws Exception {
  60         testLatch = new CountDownLatch(1);
  61         test = new TestUI(testLatch);
  62         int [] unicodeCodes = {
  63                 // U+201A(8218 in decimal) represents comma characher(,)
  64                 // U+49, U+4F, U+50,
  65                 73, 8218, 79, 8218, 80, 8218,  
  66 
  67                 // U+C5,
  68                 197, 8218,
  69 
  70                 // Surrogate pair for U+10437,
  71                 55297, 56375,
  72                 8218,
  73 
  74                 // Surrogare pair for U+24B62,
  75                 55378, 57186,
  76                 8218,
  77 
  78                 // U+4B, U+4A
  79                 75, 8218, 74}; 
  80 
  81         // initialize the test UI system
  82         SwingUtilities.invokeAndWait(() -> {
  83             try {
  84                 test.createUI();
  85             } catch (Exception ex) {
  86                 throw new RuntimeException("Exception while creating UI");
  87             }
  88         });
  89 
  90         RobotFrame robotFrame = new RobotFrame();
  91         try {
  92             Robot robot = new Robot();
  93             robot.setAutoDelay(300);
  94             robot.mouseMove(450, 450);
  95             robot.mousePress(InputEvent.BUTTON1_MASK);
  96             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  97 
  98             // Input unicode keys to Robot
  99             for (int i = 0; i < unicodeCodes.length; i++) {
 100                 robot.keyUnicode(unicodeCodes[i]);
 101             }
 102         } catch (AWTException e) {
 103             robotFrame.dispose();
 104             disposeUI();
 105             throw new RuntimeException("Exception while Java Robot unicode key input");
 106         }
 107 
 108         boolean status = testLatch.await(1, TimeUnit.MINUTES);
 109         if (!status) {
 110             System.out.println("Test timed out.");
 111         }
 112 
 113         if (test.testResult == false) {
 114             disposeUI();
 115             throw new RuntimeException("Test Failed.");
 116         }
 117 
 118         robotFrame.dispose();
 119     }
 120 
 121     @SuppressWarnings("serial")
 122     private static class RobotFrame extends JFrame {
 123         RobotFrame() {
 124             this.setLocation(300, 300);
 125             this.setTitle("RobotUnicodeCodeFrame");
 126             JTextArea textArea = new JTextArea(25, 50);
 127             this.add(textArea);
 128             this.addWindowListener(new WindowAdapter() {
 129                 public void windowClosing(WindowEvent e) {
 130                     System.exit(0);
 131                 }
 132             });
 133             this.pack();
 134             this.setVisible(true);
 135         }
 136     }
 137 }
 138 
 139 class TestUI {
 140 
 141     private static JFrame mainFrame;
 142     private static JPanel mainControlPanel;
 143 
 144     private static JTextArea instructionTextArea;
 145 
 146     private static JPanel resultButtonPanel;
 147     private static JButton passButton;
 148     private static JButton failButton;
 149 
 150     private GridBagConstraints gbc;
 151     private static GridBagLayout layout;
 152     private final CountDownLatch latch;
 153     public boolean testResult = false;
 154 
 155     public TestUI(CountDownLatch latch) {
 156         this.latch = latch;
 157     }
 158 
 159     public final void createUI() throws Exception {
 160         mainFrame = new JFrame();
 161 
 162         layout = new GridBagLayout();
 163         mainControlPanel = new JPanel(layout);
 164         resultButtonPanel = new JPanel(layout);
 165 
 166         gbc = new GridBagConstraints();
 167 
 168         // Create Test instructions
 169         String instructions
 170             = "See for the Unicode chars(UTF-16) printed in the following order \n"
 171             + "   U+49, U+4F, U+50, U+C5, U+10437(Surrogate pairs), \n"
 172             + "   U+24B62(Surrogate pairs), U+4B, U+4A \n"
 173             + "If yes, click on 'pass' else click on 'fail'\n";
 174 
 175         instructionTextArea = new JTextArea();
 176         instructionTextArea.setText(instructions);
 177         instructionTextArea.setEditable(false);
 178         instructionTextArea.setBorder(BorderFactory.
 179                 createTitledBorder("Test Instructions"));
 180 
 181         gbc.gridx = 0;
 182         gbc.gridy = 0;
 183         mainControlPanel.add(instructionTextArea, gbc);
 184 
 185         // Add customization to this test ui
 186         customize();
 187 
 188         // Create resultButtonPanel with Pass, Fail buttons
 189         passButton = new JButton("Pass");
 190         passButton.setActionCommand("Pass");
 191         passButton.addActionListener((ActionEvent e) -> {
 192             System.out.println("Pass Button pressed!");
 193             testResult = true;
 194             latch.countDown();
 195             disposeUI();
 196         });
 197 
 198         failButton = new JButton("Fail");
 199         failButton.setActionCommand("Fail");
 200         failButton.addActionListener((ActionEvent e) -> {
 201             System.out.println("Fail Button pressed!");
 202             testResult = false;
 203             latch.countDown();
 204             disposeUI();
 205         });
 206 
 207         gbc.gridx = 0;
 208         gbc.gridy = 0;
 209         resultButtonPanel.add(passButton, gbc);
 210 
 211         gbc.gridx = 1;
 212         gbc.gridy = 0;
 213         resultButtonPanel.add(failButton, gbc);
 214 
 215         gbc.gridx = 0;
 216         gbc.gridy = 2;
 217         mainControlPanel.add(resultButtonPanel, gbc);
 218 
 219         mainFrame.add(mainControlPanel);
 220         mainFrame.pack();
 221         mainFrame.setVisible(true);
 222     }
 223 
 224     public void disposeUI() {
 225         mainFrame.dispose();
 226     }
 227 
 228     private void customize() throws Exception {
 229         // Customize the test UI title
 230         mainFrame.setTitle("RobotUnicodeCodeTest");
 231    }
 232 }