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.keyPressUnicode(unicodeCodes[i]); 101 robot.keyReleaseUnicode(unicodeCodes[i]); 102 } 103 } catch (AWTException e) { 104 robotFrame.dispose(); 105 disposeUI(); 106 throw new RuntimeException("Exception while Java Robot unicode key input"); 107 } 108 109 boolean status = testLatch.await(1, TimeUnit.MINUTES); 110 if (!status) { 111 System.out.println("Test timed out."); 112 } 113 114 if (test.testResult == false) { 115 disposeUI(); 116 throw new RuntimeException("Test Failed."); 117 } 118 119 robotFrame.dispose(); 120 } 121 122 @SuppressWarnings("serial") 123 private static class RobotFrame extends JFrame { 124 RobotFrame() { 125 this.setLocation(300, 300); 126 this.setTitle("RobotUnicodeCodeFrame"); 127 JTextArea textArea = new JTextArea(25, 50); 128 this.add(textArea); 129 this.addWindowListener(new WindowAdapter() { 130 public void windowClosing(WindowEvent e) { 131 System.exit(0); 132 } 133 }); 134 this.pack(); 135 this.setVisible(true); 136 } 137 } 138 } 139 140 class TestUI { 141 142 private static JFrame mainFrame; 143 private static JPanel mainControlPanel; 144 145 private static JTextArea instructionTextArea; 146 147 private static JPanel resultButtonPanel; 148 private static JButton passButton; 149 private static JButton failButton; 150 151 private GridBagConstraints gbc; 152 private static GridBagLayout layout; 153 private final CountDownLatch latch; 154 public boolean testResult = false; 155 156 public TestUI(CountDownLatch latch) { 157 this.latch = latch; 158 } 159 160 public final void createUI() throws Exception { 161 mainFrame = new JFrame(); 162 163 layout = new GridBagLayout(); 164 mainControlPanel = new JPanel(layout); 165 resultButtonPanel = new JPanel(layout); 166 167 gbc = new GridBagConstraints(); 168 169 // Create Test instructions 170 String instructions 171 = "See for the Unicode chars(UTF-16) printed in the following order \n" 172 + " U+49, U+4F, U+50, U+C5, U+10437(Surrogate pairs), \n" 173 + " U+24B62(Surrogate pairs), U+4B, U+4A \n" 174 + "If yes, click on 'pass' else click on 'fail'\n"; 175 176 instructionTextArea = new JTextArea(); 177 instructionTextArea.setText(instructions); 178 instructionTextArea.setEditable(false); 179 instructionTextArea.setBorder(BorderFactory. 180 createTitledBorder("Test Instructions")); 181 182 gbc.gridx = 0; 183 gbc.gridy = 0; 184 mainControlPanel.add(instructionTextArea, gbc); 185 186 // Add customization to this test ui 187 customize(); 188 189 // Create resultButtonPanel with Pass, Fail buttons 190 passButton = new JButton("Pass"); 191 passButton.setActionCommand("Pass"); 192 passButton.addActionListener((ActionEvent e) -> { 193 System.out.println("Pass Button pressed!"); 194 testResult = true; 195 latch.countDown(); 196 disposeUI(); 197 }); 198 199 failButton = new JButton("Fail"); 200 failButton.setActionCommand("Fail"); 201 failButton.addActionListener((ActionEvent e) -> { 202 System.out.println("Fail Button pressed!"); 203 testResult = false; 204 latch.countDown(); 205 disposeUI(); 206 }); 207 208 gbc.gridx = 0; 209 gbc.gridy = 0; 210 resultButtonPanel.add(passButton, gbc); 211 212 gbc.gridx = 1; 213 gbc.gridy = 0; 214 resultButtonPanel.add(failButton, gbc); 215 216 gbc.gridx = 0; 217 gbc.gridy = 2; 218 mainControlPanel.add(resultButtonPanel, gbc); 219 220 mainFrame.add(mainControlPanel); 221 mainFrame.pack(); 222 mainFrame.setVisible(true); 223 } 224 225 public void disposeUI() { 226 mainFrame.dispose(); 227 } 228 229 private void customize() throws Exception { 230 // Customize the test UI title 231 mainFrame.setTitle("RobotUnicodeCodeTest"); 232 } 233 }