Path: blob/master/test/jdk/javax/swing/JTextField/JapaneseReadingAttributes/JapaneseReadingAttributes.java
41152 views
/*1* Copyright (c) 2017, 2021, 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* @key headful25* @bug 817607226* @summary Checks whether reading attributes are obtained for Japanese IME27* @requires (os.family == "windows")28* @run main/manual JapaneseReadingAttributes29*/3031/**32* This test requires a manual intervention as the keyboard layout has to be33* changed to Japanese IME. Once the keyboard layout has been selected, click on34* Start Test to start the automated tests. Will run two passes, first with an35* enter key in between to generate the yomigana for the first block of36* characters. The second without the intermediate enter key. Without the fix,37* there will be a mismatch in the reading attributes obtained.38*/3940import java.awt.BorderLayout;41import java.awt.Dimension;42import java.awt.FlowLayout;43import java.awt.Robot;44import java.awt.event.InputMethodEvent;45import java.awt.event.InputMethodListener;46import java.awt.event.WindowAdapter;47import java.awt.event.WindowEvent;48import javax.swing.JButton;49import javax.swing.JFrame;50import javax.swing.JPanel;51import javax.swing.JTextArea;52import javax.swing.SwingUtilities;53import javax.swing.WindowConstants;54import java.awt.event.KeyEvent;55import java.text.AttributedCharacterIterator;56import java.util.ArrayList;57import java.util.concurrent.CountDownLatch;58import javax.swing.JLabel;59import javax.swing.JTextField;6061public class JapaneseReadingAttributes {62private static boolean testPassed = false;63private static boolean startTest = false;6465private static JFrame frame = null;66private static JLabel lblTestStatus = null;67private static JTextField textFieldMain = null;68private static JTextField textFieldReading = null;69private static String testResult;70private static String readingPass1;71private static String readingPass2;7273private static final CountDownLatch testStartLatch = new CountDownLatch(1);7475public static void main(String[] args) throws Exception {76SwingUtilities.invokeAndWait(() -> {77setupUI();78});7980testStartLatch.await();8182if (startTest) {83glyphTest();8485frame.dispose();8687if (testPassed) {88System.out.println(testResult);89} else {90throw new RuntimeException(testResult);91}92} else {93throw new RuntimeException("User has not executed the test");94}95}9697private static void setupUI() {98String description = " 1. Go to \"Language Preferences -> Add a Language"99+ "\" and add \"Japanese\"\n"100+ " 2. Set current IM to \"Japanese\" and IME option to \"Full width Katakana\" \n"101+ " 3. Try typing in the text field to ensure"102+ " that Japanese IME has been successfully"103+ " selected \n"104+ " 4. Now click on \"Start Test\" button \n";105String title = "Reading Attributes test Japanese IME (Windows)";106107frame = new JFrame(title);108109JPanel mainPanel = new JPanel(new BorderLayout());110111JPanel textEditPanel = new JPanel(new FlowLayout());112113textFieldMain = new JTextField(20);114115textFieldReading = new JTextField(20);116textFieldReading.setEditable(false);117118textEditPanel.add(textFieldMain);119textEditPanel.add(textFieldReading);120121mainPanel.add(textEditPanel, BorderLayout.CENTER);122123JTextArea textArea = new JTextArea(description);124textArea.setEditable(false);125final JButton btnStartTest = new JButton("Start Test");126final JButton btnCancelTest = new JButton("Cancel Test");127128btnStartTest.addActionListener((e) -> {129btnStartTest.setEnabled(false);130btnCancelTest.setEnabled(false);131startTest = true;132testStartLatch.countDown();133});134135btnCancelTest.addActionListener((e) -> {136frame.dispose();137testStartLatch.countDown();138});139mainPanel.add(textArea, BorderLayout.NORTH);140141JPanel buttonPanel = new JPanel(new FlowLayout());142buttonPanel.add(btnStartTest);143buttonPanel.add(btnCancelTest);144mainPanel.add(buttonPanel, BorderLayout.SOUTH);145146lblTestStatus = new JLabel("");147lblTestStatus.setMinimumSize(new Dimension(250, 20));148lblTestStatus.setPreferredSize(new Dimension(250, 20));149lblTestStatus.setVisible(true);150textEditPanel.add(lblTestStatus);151152frame.add(mainPanel);153frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);154frame.pack();155frame.setLocationRelativeTo(null);156157frame.addWindowListener(new WindowAdapter() {158@Override159public void windowClosing(WindowEvent e) {160testStartLatch.countDown();161}162@Override163public void windowOpened( WindowEvent e ){164textFieldMain.requestFocusInWindow();165}166});167168textFieldMain.addInputMethodListener(new InputMethodListener() {169@Override170public void caretPositionChanged(InputMethodEvent event) {171}172173@Override174public void inputMethodTextChanged(InputMethodEvent event) {175AttributedCharacterIterator itr = event.getText();176if (itr != null) {177int toCopy = event.getCommittedCharacterCount();178if (toCopy > 0) {179itr.first();180StringBuilder yomigana = new StringBuilder(181textFieldReading.getText());182while (toCopy-- > 0) {183if (itr.getIndex() == itr.getRunStart(184AttributedCharacterIterator.Attribute.READING)) {185java.text.Annotation annotatedText186= (java.text.Annotation) itr.187getAttribute(AttributedCharacterIterator.Attribute.READING);188yomigana.append(annotatedText.getValue());189}190itr.next();191}192textFieldReading.setText(yomigana.toString());193}194}195}196});197198frame.setVisible(true);199}200201private static void glyphTest() throws Exception {202Robot robotKeySimulator = new Robot();203performTasks(robotKeySimulator);204}205206public static void performTasks(Robot robotForKeyInput) throws Exception {207lblTestStatus.setText("Running Tests..");208robotForKeyInput.setAutoDelay(500);209210ArrayList<Integer> keyCodesToUse = new ArrayList<Integer>();211212keyCodesToUse.add(KeyEvent.VK_A);213keyCodesToUse.add(KeyEvent.VK_B);214keyCodesToUse.add(KeyEvent.VK_E);215keyCodesToUse.add(KeyEvent.VK_SPACE);216keyCodesToUse.add(KeyEvent.VK_SPACE);217keyCodesToUse.add(KeyEvent.VK_ENTER);218keyCodesToUse.add(KeyEvent.VK_S);219keyCodesToUse.add(KeyEvent.VK_I);220keyCodesToUse.add(KeyEvent.VK_N);221keyCodesToUse.add(KeyEvent.VK_Z);222keyCodesToUse.add(KeyEvent.VK_O);223keyCodesToUse.add(KeyEvent.VK_U);224keyCodesToUse.add(KeyEvent.VK_SPACE);225keyCodesToUse.add(KeyEvent.VK_ENTER);226227textFieldMain.requestFocusInWindow();228229robotForKeyInput.waitForIdle();230231enterInput(robotForKeyInput, keyCodesToUse);232233SwingUtilities.invokeAndWait(() -> {234readingPass1 = textFieldReading.getText();235});236237if (setTaskStatus(readingPass1, 1)) {238keyCodesToUse.remove((Integer) KeyEvent.VK_ENTER);239240enterInput(robotForKeyInput, keyCodesToUse);241242SwingUtilities.invokeAndWait(() -> {243readingPass2 = textFieldReading.getText();244});245246if (setTaskStatus(readingPass2, 2)) {247if (readingPass1.equals(readingPass2)) {248testPassed = true;249testResult = "Test Passed : Same reading attribute "250+ "obtained from both passes ";251lblTestStatus.setText(testResult);252} else {253testResult = "Test Failed : Reading attribute from Pass 1 <"254+ readingPass1 + "> != Reading attribute "255+ "from Pass 2 <" + readingPass2 + ">";256}257}258}259}260261private static void enterInput(Robot robotKeyInput,262ArrayList<Integer> keyInputs) {263textFieldReading.setText("");264textFieldMain.setText("");265266String strKeyInput = "KeyPress=>";267int nOfKeyInputs = keyInputs.size();268for (int i = 0; i < nOfKeyInputs; i++) {269int keyToUse = keyInputs.get(i);270robotKeyInput.keyPress(keyToUse);271robotKeyInput.keyRelease(keyToUse);272strKeyInput += (Integer.toHexString(keyToUse)) + ":";273}274275System.out.println(strKeyInput);276}277278public static boolean setTaskStatus(String readingValue, int passCount) {279boolean status = false;280281if (!readingValue.isEmpty()) {282testResult = "Attribute : " + readingValue283+ "read from pass " + Integer.toString(passCount);284status = true;285} else {286testResult = "Failed to read Reading attribute from pass "287+ Integer.toString(passCount);288}289290lblTestStatus.setText(testResult);291292return status;293}294}295296297