Path: blob/master/test/jdk/javax/swing/JTextField/MissingCharsKorean/MissingCharsKorean.java
41154 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 818037025* @summary Checks whether non-alpha chars are skipped while entering KoreanText26* @requires (os.family == "mac")27* @run main/manual MissingCharsKorean28*/2930/**31* This test requires a manual intervention as the keyboard layout has to be32* changed to 2-set Korean. Once the keyboard layout has been selected, click on33* Start Test to start the automated tests. Along with testing for non-alpha34* chars, this test also ensures that the MarkedText property is not broken by35* running cases where different glyphs are combined and also cases where36* combined glyphs are broken.37*/3839import java.awt.AWTException;40import java.awt.Font;41import java.awt.BorderLayout;42import java.awt.Dimension;43import java.awt.FlowLayout;44import java.awt.Robot;45import java.awt.event.WindowAdapter;46import java.awt.event.WindowEvent;47import javax.swing.JButton;48import javax.swing.JFrame;49import javax.swing.JPanel;50import javax.swing.JTextArea;51import javax.swing.SwingUtilities;52import javax.swing.WindowConstants;53import java.awt.event.KeyEvent;54import java.util.concurrent.CountDownLatch;55import javax.swing.JLabel;56import javax.swing.JTextField;5758public class MissingCharsKorean {59private static boolean testPassed = false;60private static boolean startTest = false;61private static int expectedResults[] = null;62private static int inKeyCodes[][] = null;6364private static JFrame frame = null;65private static JLabel lblTestStatus = null;66private static JTextField textFieldMain = null;67private static String testResult;6869private static final CountDownLatch testStartLatch = new CountDownLatch(1);7071public static void main(String[] args) throws Exception {72SwingUtilities.invokeAndWait(() -> {73setupUI();74});7576testStartLatch.await();7778if (startTest) {79glyphTest();8081frame.dispose();8283if (testPassed) {84System.out.println(testResult);85} else {86throw new RuntimeException("Korean text missing characters : "87+ testResult);88}89} else {90throw new RuntimeException("User has not executed the test");91}92}9394private static void setupUI() {95String description = " 1. Go to \"System Preferences -> Keyboard -> "96+ "Input Sources\" and add \"2-Set Korean\""97+ " from Korean language group \n"98+ " 2. Set current IM to \"2-Set Korean\" \n"99+ " 3. Try typing in the text field to ensure"100+ " that Korean keyboard has been successfully"101+ " selected \n"102+ " 4. Now click on \"Start Test\" button \n";103String title = "Missing Characters Korean Test (Mac OS)";104105frame = new JFrame(title);106107JPanel mainPanel = new JPanel(new BorderLayout());108109JPanel textEditPanel = new JPanel(new FlowLayout());110111textFieldMain = new JTextField(20);112Font font = new Font("Source Han Serif K", Font.BOLD,12);113textFieldMain.setFont(font);114115textEditPanel.add(textFieldMain);116117mainPanel.add(textEditPanel, BorderLayout.CENTER);118119JTextArea textArea = new JTextArea(description);120textArea.setEditable(false);121final JButton btnStartTest = new JButton("Start Test");122final JButton btnCancelTest = new JButton("Cancel Test");123124btnStartTest.addActionListener((e) -> {125btnStartTest.setEnabled(false);126btnCancelTest.setEnabled(false);127startTest = true;128testStartLatch.countDown();129});130131btnCancelTest.addActionListener((e) -> {132frame.dispose();133testStartLatch.countDown();134});135mainPanel.add(textArea, BorderLayout.NORTH);136137JPanel buttonPanel = new JPanel(new FlowLayout());138buttonPanel.add(btnStartTest);139buttonPanel.add(btnCancelTest);140mainPanel.add(buttonPanel, BorderLayout.SOUTH);141142lblTestStatus = new JLabel("");143lblTestStatus.setMinimumSize(new Dimension(150, 20));144lblTestStatus.setPreferredSize(new Dimension(150, 20));145lblTestStatus.setVisible(true);146textEditPanel.add(lblTestStatus);147148frame.add(mainPanel);149frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);150frame.pack();151frame.setLocationRelativeTo(null);152153frame.addWindowListener(new WindowAdapter() {154@Override155public void windowClosing(WindowEvent e) {156testStartLatch.countDown();157}158@Override159public void windowOpened( WindowEvent e ){160textFieldMain.requestFocusInWindow();161}162});163164frame.setVisible(true);165}166167private static void glyphTest() {168try {169Robot robotKeySimulator = new Robot();170performTasks(robotKeySimulator);171} catch (AWTException e) {172System.err.print("Creation Of Robot Failed : " + e.getMessage());173testPassed = false;174}175}176177public static void performTasks(Robot robotForKeyInput) {178int taskCount = 0;179180lblTestStatus.setText("Running Tests..");181robotForKeyInput.setAutoDelay(500);182183while (setKeyInput(taskCount)) {184textFieldMain.setText("");185textFieldMain.requestFocusInWindow();186enterInput(robotForKeyInput, inKeyCodes);187taskCount++;188189try {190SwingUtilities.invokeAndWait(() -> {191validateInput();192});193} catch (Exception e) {194System.err.print("validateInput Failed : " + e.getMessage());195testPassed = false;196break;197}198199if (!testPassed) {200break;201}202setTaskStatus(false, taskCount);203}204setTaskStatus(true, taskCount);205}206207private static boolean setKeyInput(int iCount) {208boolean inputSet = true;209210switch(iCount) {211case 0:212// Input Korean q (#12610) /(#47)213expectedResults = new int[]{ 12610, 47 };214inKeyCodes = new int[][] { {KeyEvent.VK_Q},215{KeyEvent.VK_SLASH}216};217break;218219case 1:220// Input Korean q (#12610) /(#47) gh (#54840) \(#92)221expectedResults = new int[]{ 12610, 47, 54840, 92 };222inKeyCodes = new int[][] { {KeyEvent.VK_Q},223{KeyEvent.VK_SLASH},224{KeyEvent.VK_G},225{KeyEvent.VK_H},226{KeyEvent.VK_BACK_SLASH}227};228break;229230case 2:231// Input Korean q (#12610) /(#47) ghq (#54857) \(#92)232expectedResults = new int[]{ 12610, 47, 54857, 92 };233inKeyCodes = new int[][] { {KeyEvent.VK_Q},234{KeyEvent.VK_SLASH},235{KeyEvent.VK_G},236{KeyEvent.VK_H},237{KeyEvent.VK_Q},238{KeyEvent.VK_BACK_SLASH}239};240break;241242case 3:243// Input Korean q (#12610) /(#47) gh (#54840) \(#92)244expectedResults = new int[]{ 12610, 47, 54840, 92 };245inKeyCodes = new int[][] { {KeyEvent.VK_Q},246{KeyEvent.VK_SLASH},247{KeyEvent.VK_G},248{KeyEvent.VK_H},249{KeyEvent.VK_Q},250{KeyEvent.VK_BACK_SPACE},251{KeyEvent.VK_BACK_SLASH}252};253break;254255case 4:256// Input Korean q (#12610) /(#47) g (#12622) \(#92)257expectedResults = new int[]{ 12610, 47, 12622, 92 };258inKeyCodes = new int[][] { {KeyEvent.VK_Q},259{KeyEvent.VK_SLASH},260{KeyEvent.VK_G},261{KeyEvent.VK_H},262{KeyEvent.VK_Q},263{KeyEvent.VK_BACK_SPACE},264{KeyEvent.VK_BACK_SPACE},265{KeyEvent.VK_BACK_SLASH}266};267break;268269default:270inputSet = false;271break;272}273274return inputSet;275}276277private static void enterInput(Robot robotKeyInput, int keyInputs[][]) {278for (int i = 0; i < keyInputs.length; i++) {279String strKeyInput = "KeyPress=>";280final int noOfKeyInputs = keyInputs[i].length;281for (int j = 0; j < noOfKeyInputs; j++) {282robotKeyInput.keyPress(keyInputs[i][j]);283strKeyInput += (Integer.toHexString(keyInputs[i][j])) + ":";284}285286strKeyInput += "KeyRelease=>";287for (int j = noOfKeyInputs - 1; j >= 0; j--) {288robotKeyInput.keyRelease(keyInputs[i][j]);289strKeyInput += (Integer.toHexString(keyInputs[i][j])) + ":";290}291System.out.println(strKeyInput);292}293}294295private static void validateInput() {296testPassed = false;297298if (expectedResults != null) {299String strCurr = textFieldMain.getText();300if (expectedResults.length == strCurr.length()) {301testPassed = true;302303for (int i = 0; i < strCurr.length(); i++) {304final int charActual = strCurr.charAt(i);305if (charActual != expectedResults[i]) {306System.err.println("<" + i + "> Actual = " + charActual307+ " Expected = " + expectedResults[i]);308testPassed = false;309break;310}311}312}313}314}315316public static void setTaskStatus(boolean allTasksPerformed, int taskCount) {317if (testPassed) {318if (allTasksPerformed) {319testResult = "All Tests Passed";320} else {321testResult = "Test " + Integer.toString(taskCount)322+ " Passed";323}324} else {325testResult = "Test " + Integer.toString(taskCount)326+ " Failed";327}328lblTestStatus.setText(testResult);329}330}331332333