Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTextField/JapaneseReadingAttributes/JapaneseReadingAttributes.java
38853 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 817607225* @summary Checks whether reading attributes are obtained for Japanese IME26* @requires (os.family == "windows")27* @run main/manual JapaneseReadingAttributes28*/2930/**31* This test requires a manual intervention as the keyboard layout has to be32* changed to Japanese IME. Once the keyboard layout has been selected, click on33* Start Test to start the automated tests. Will run two passes, first with an34* enter key in between to generate the yomigana for the first block of35* characters. The second without the intermediate enter key. Without the fix,36* there will be a mismatch in the reading attributes obtained.37*/3839import java.awt.BorderLayout;40import java.awt.Dimension;41import java.awt.FlowLayout;42import java.awt.Robot;43import java.awt.event.InputMethodEvent;44import java.awt.event.InputMethodListener;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.text.AttributedCharacterIterator;55import java.util.ArrayList;56import java.util.concurrent.CountDownLatch;57import javax.swing.JLabel;58import javax.swing.JTextField;5960public class JapaneseReadingAttributes {61private static boolean testPassed = false;62private static boolean startTest = false;6364private static JFrame frame = null;65private static JLabel lblTestStatus = null;66private static JTextField textFieldMain = null;67private static JTextField textFieldReading = null;68private static String testResult;69private static String readingPass1;70private static String readingPass2;7172private static final CountDownLatch testStartLatch = new CountDownLatch(1);7374public static void main(String[] args) throws Exception {75SwingUtilities.invokeAndWait(() -> {76setupUI();77});7879testStartLatch.await();8081if (startTest) {82glyphTest();8384frame.dispose();8586if (testPassed) {87System.out.println(testResult);88} else {89throw new RuntimeException(testResult);90}91} else {92throw new RuntimeException("User has not executed the test");93}94}9596private static void setupUI() {97String description = " 1. Go to \"Language Preferences -> Add a Language"98+ "\" and add \"Japanese\"\n"99+ " 2. Set current IM to \"Japanese\" \n"100+ " 3. Try typing in the text field to ensure"101+ " that Japanese IME has been successfully"102+ " selected \n"103+ " 4. Now click on \"Start Test\" button \n";104String title = "Reading Attributes test Japanese IME (Windows)";105106frame = new JFrame(title);107108JPanel mainPanel = new JPanel(new BorderLayout());109110JPanel textEditPanel = new JPanel(new FlowLayout());111112textFieldMain = new JTextField(20);113114textFieldReading = new JTextField(20);115textFieldReading.setEditable(false);116117textEditPanel.add(textFieldMain);118textEditPanel.add(textFieldReading);119120mainPanel.add(textEditPanel, BorderLayout.CENTER);121122JTextArea textArea = new JTextArea(description);123textArea.setEditable(false);124final JButton btnStartTest = new JButton("Start Test");125final JButton btnCancelTest = new JButton("Cancel Test");126127btnStartTest.addActionListener((e) -> {128btnStartTest.setEnabled(false);129btnCancelTest.setEnabled(false);130startTest = true;131testStartLatch.countDown();132});133134btnCancelTest.addActionListener((e) -> {135frame.dispose();136testStartLatch.countDown();137});138mainPanel.add(textArea, BorderLayout.NORTH);139140JPanel buttonPanel = new JPanel(new FlowLayout());141buttonPanel.add(btnStartTest);142buttonPanel.add(btnCancelTest);143mainPanel.add(buttonPanel, BorderLayout.SOUTH);144145lblTestStatus = new JLabel("");146lblTestStatus.setMinimumSize(new Dimension(250, 20));147lblTestStatus.setPreferredSize(new Dimension(250, 20));148lblTestStatus.setVisible(true);149textEditPanel.add(lblTestStatus);150151frame.add(mainPanel);152frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);153frame.pack();154frame.setLocationRelativeTo(null);155156frame.addWindowListener(new WindowAdapter() {157@Override158public void windowClosing(WindowEvent e) {159testStartLatch.countDown();160}161@Override162public void windowOpened( WindowEvent e ){163textFieldMain.requestFocusInWindow();164}165});166167textFieldMain.addInputMethodListener(new InputMethodListener() {168@Override169public void caretPositionChanged(InputMethodEvent event) {170}171172@Override173public void inputMethodTextChanged(InputMethodEvent event) {174AttributedCharacterIterator itr = event.getText();175if (itr != null) {176int toCopy = event.getCommittedCharacterCount();177if (toCopy > 0) {178itr.first();179StringBuilder yomigana = new StringBuilder(180textFieldReading.getText());181while (toCopy-- > 0) {182if (itr.getIndex() == itr.getRunStart(183AttributedCharacterIterator.Attribute.READING)) {184java.text.Annotation annotatedText185= (java.text.Annotation) itr.186getAttribute(AttributedCharacterIterator.Attribute.READING);187yomigana.append(annotatedText.getValue());188}189itr.next();190}191textFieldReading.setText(yomigana.toString());192}193}194}195});196197frame.setVisible(true);198}199200private static void glyphTest() throws Exception {201Robot robotKeySimulator = new Robot();202performTasks(robotKeySimulator);203}204205public static void performTasks(Robot robotForKeyInput) throws Exception {206lblTestStatus.setText("Running Tests..");207robotForKeyInput.setAutoDelay(500);208209ArrayList<Integer> keyCodesToUse = new ArrayList<Integer>();210211keyCodesToUse.add(KeyEvent.VK_A);212keyCodesToUse.add(KeyEvent.VK_B);213keyCodesToUse.add(KeyEvent.VK_E);214keyCodesToUse.add(KeyEvent.VK_SPACE);215keyCodesToUse.add(KeyEvent.VK_SPACE);216keyCodesToUse.add(KeyEvent.VK_ENTER);217keyCodesToUse.add(KeyEvent.VK_S);218keyCodesToUse.add(KeyEvent.VK_I);219keyCodesToUse.add(KeyEvent.VK_N);220keyCodesToUse.add(KeyEvent.VK_Z);221keyCodesToUse.add(KeyEvent.VK_O);222keyCodesToUse.add(KeyEvent.VK_U);223keyCodesToUse.add(KeyEvent.VK_SPACE);224keyCodesToUse.add(KeyEvent.VK_ENTER);225226textFieldMain.requestFocusInWindow();227228robotForKeyInput.waitForIdle();229230enterInput(robotForKeyInput, keyCodesToUse);231232SwingUtilities.invokeAndWait(() -> {233readingPass1 = textFieldReading.getText();234});235236if (setTaskStatus(readingPass1, 1)) {237keyCodesToUse.remove((Integer) KeyEvent.VK_ENTER);238239enterInput(robotForKeyInput, keyCodesToUse);240241SwingUtilities.invokeAndWait(() -> {242readingPass2 = textFieldReading.getText();243});244245if (setTaskStatus(readingPass2, 2)) {246if (readingPass1.equals(readingPass2)) {247testPassed = true;248testResult = "Test Passed : Same reading attribute "249+ "obtained from both passes ";250lblTestStatus.setText(testResult);251} else {252testResult = "Test Failed : Reading attribute from Pass 1 <"253+ readingPass1 + "> != Reading attribute "254+ "from Pass 2 <" + readingPass2 + ">";255}256}257}258}259260private static void enterInput(Robot robotKeyInput,261ArrayList<Integer> keyInputs) {262textFieldReading.setText("");263textFieldMain.setText("");264265String strKeyInput = "KeyPress=>";266int nOfKeyInputs = keyInputs.size();267for (int i = 0; i < nOfKeyInputs; i++) {268int keyToUse = keyInputs.get(i);269robotKeyInput.keyPress(keyToUse);270robotKeyInput.keyRelease(keyToUse);271strKeyInput += (Integer.toHexString(keyToUse)) + ":";272}273274System.out.println(strKeyInput);275}276277public static boolean setTaskStatus(String readingValue, int passCount) {278boolean status = false;279280if (!readingValue.isEmpty()) {281testResult = "Attribute : " + readingValue282+ "read from pass " + Integer.toString(passCount);283status = true;284} else {285testResult = "Failed to read Reading attribute from pass "286+ Integer.toString(passCount);287}288289lblTestStatus.setText(testResult);290291return status;292}293}294295296