Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTextField/JapaneseReadingAttributes/JapaneseReadingAttributes.java
38853 views
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
* @test
25
* @bug 8176072
26
* @summary Checks whether reading attributes are obtained for Japanese IME
27
* @requires (os.family == "windows")
28
* @run main/manual JapaneseReadingAttributes
29
*/
30
31
/**
32
* This test requires a manual intervention as the keyboard layout has to be
33
* changed to Japanese IME. Once the keyboard layout has been selected, click on
34
* Start Test to start the automated tests. Will run two passes, first with an
35
* enter key in between to generate the yomigana for the first block of
36
* characters. The second without the intermediate enter key. Without the fix,
37
* there will be a mismatch in the reading attributes obtained.
38
*/
39
40
import java.awt.BorderLayout;
41
import java.awt.Dimension;
42
import java.awt.FlowLayout;
43
import java.awt.Robot;
44
import java.awt.event.InputMethodEvent;
45
import java.awt.event.InputMethodListener;
46
import java.awt.event.WindowAdapter;
47
import java.awt.event.WindowEvent;
48
import javax.swing.JButton;
49
import javax.swing.JFrame;
50
import javax.swing.JPanel;
51
import javax.swing.JTextArea;
52
import javax.swing.SwingUtilities;
53
import javax.swing.WindowConstants;
54
import java.awt.event.KeyEvent;
55
import java.text.AttributedCharacterIterator;
56
import java.util.ArrayList;
57
import java.util.concurrent.CountDownLatch;
58
import javax.swing.JLabel;
59
import javax.swing.JTextField;
60
61
public class JapaneseReadingAttributes {
62
private static boolean testPassed = false;
63
private static boolean startTest = false;
64
65
private static JFrame frame = null;
66
private static JLabel lblTestStatus = null;
67
private static JTextField textFieldMain = null;
68
private static JTextField textFieldReading = null;
69
private static String testResult;
70
private static String readingPass1;
71
private static String readingPass2;
72
73
private static final CountDownLatch testStartLatch = new CountDownLatch(1);
74
75
public static void main(String[] args) throws Exception {
76
SwingUtilities.invokeAndWait(() -> {
77
setupUI();
78
});
79
80
testStartLatch.await();
81
82
if (startTest) {
83
glyphTest();
84
85
frame.dispose();
86
87
if (testPassed) {
88
System.out.println(testResult);
89
} else {
90
throw new RuntimeException(testResult);
91
}
92
} else {
93
throw new RuntimeException("User has not executed the test");
94
}
95
}
96
97
private static void setupUI() {
98
String description = " 1. Go to \"Language Preferences -> Add a Language"
99
+ "\" and add \"Japanese\"\n"
100
+ " 2. Set current IM to \"Japanese\" \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";
105
String title = "Reading Attributes test Japanese IME (Windows)";
106
107
frame = new JFrame(title);
108
109
JPanel mainPanel = new JPanel(new BorderLayout());
110
111
JPanel textEditPanel = new JPanel(new FlowLayout());
112
113
textFieldMain = new JTextField(20);
114
115
textFieldReading = new JTextField(20);
116
textFieldReading.setEditable(false);
117
118
textEditPanel.add(textFieldMain);
119
textEditPanel.add(textFieldReading);
120
121
mainPanel.add(textEditPanel, BorderLayout.CENTER);
122
123
JTextArea textArea = new JTextArea(description);
124
textArea.setEditable(false);
125
final JButton btnStartTest = new JButton("Start Test");
126
final JButton btnCancelTest = new JButton("Cancel Test");
127
128
btnStartTest.addActionListener((e) -> {
129
btnStartTest.setEnabled(false);
130
btnCancelTest.setEnabled(false);
131
startTest = true;
132
testStartLatch.countDown();
133
});
134
135
btnCancelTest.addActionListener((e) -> {
136
frame.dispose();
137
testStartLatch.countDown();
138
});
139
mainPanel.add(textArea, BorderLayout.NORTH);
140
141
JPanel buttonPanel = new JPanel(new FlowLayout());
142
buttonPanel.add(btnStartTest);
143
buttonPanel.add(btnCancelTest);
144
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
145
146
lblTestStatus = new JLabel("");
147
lblTestStatus.setMinimumSize(new Dimension(250, 20));
148
lblTestStatus.setPreferredSize(new Dimension(250, 20));
149
lblTestStatus.setVisible(true);
150
textEditPanel.add(lblTestStatus);
151
152
frame.add(mainPanel);
153
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
154
frame.pack();
155
frame.setLocationRelativeTo(null);
156
157
frame.addWindowListener(new WindowAdapter() {
158
@Override
159
public void windowClosing(WindowEvent e) {
160
testStartLatch.countDown();
161
}
162
@Override
163
public void windowOpened( WindowEvent e ){
164
textFieldMain.requestFocusInWindow();
165
}
166
});
167
168
textFieldMain.addInputMethodListener(new InputMethodListener() {
169
@Override
170
public void caretPositionChanged(InputMethodEvent event) {
171
}
172
173
@Override
174
public void inputMethodTextChanged(InputMethodEvent event) {
175
AttributedCharacterIterator itr = event.getText();
176
if (itr != null) {
177
int toCopy = event.getCommittedCharacterCount();
178
if (toCopy > 0) {
179
itr.first();
180
StringBuilder yomigana = new StringBuilder(
181
textFieldReading.getText());
182
while (toCopy-- > 0) {
183
if (itr.getIndex() == itr.getRunStart(
184
AttributedCharacterIterator.Attribute.READING)) {
185
java.text.Annotation annotatedText
186
= (java.text.Annotation) itr.
187
getAttribute(AttributedCharacterIterator.Attribute.READING);
188
yomigana.append(annotatedText.getValue());
189
}
190
itr.next();
191
}
192
textFieldReading.setText(yomigana.toString());
193
}
194
}
195
}
196
});
197
198
frame.setVisible(true);
199
}
200
201
private static void glyphTest() throws Exception {
202
Robot robotKeySimulator = new Robot();
203
performTasks(robotKeySimulator);
204
}
205
206
public static void performTasks(Robot robotForKeyInput) throws Exception {
207
lblTestStatus.setText("Running Tests..");
208
robotForKeyInput.setAutoDelay(500);
209
210
ArrayList<Integer> keyCodesToUse = new ArrayList<Integer>();
211
212
keyCodesToUse.add(KeyEvent.VK_A);
213
keyCodesToUse.add(KeyEvent.VK_B);
214
keyCodesToUse.add(KeyEvent.VK_E);
215
keyCodesToUse.add(KeyEvent.VK_SPACE);
216
keyCodesToUse.add(KeyEvent.VK_SPACE);
217
keyCodesToUse.add(KeyEvent.VK_ENTER);
218
keyCodesToUse.add(KeyEvent.VK_S);
219
keyCodesToUse.add(KeyEvent.VK_I);
220
keyCodesToUse.add(KeyEvent.VK_N);
221
keyCodesToUse.add(KeyEvent.VK_Z);
222
keyCodesToUse.add(KeyEvent.VK_O);
223
keyCodesToUse.add(KeyEvent.VK_U);
224
keyCodesToUse.add(KeyEvent.VK_SPACE);
225
keyCodesToUse.add(KeyEvent.VK_ENTER);
226
227
textFieldMain.requestFocusInWindow();
228
229
robotForKeyInput.waitForIdle();
230
231
enterInput(robotForKeyInput, keyCodesToUse);
232
233
SwingUtilities.invokeAndWait(() -> {
234
readingPass1 = textFieldReading.getText();
235
});
236
237
if (setTaskStatus(readingPass1, 1)) {
238
keyCodesToUse.remove((Integer) KeyEvent.VK_ENTER);
239
240
enterInput(robotForKeyInput, keyCodesToUse);
241
242
SwingUtilities.invokeAndWait(() -> {
243
readingPass2 = textFieldReading.getText();
244
});
245
246
if (setTaskStatus(readingPass2, 2)) {
247
if (readingPass1.equals(readingPass2)) {
248
testPassed = true;
249
testResult = "Test Passed : Same reading attribute "
250
+ "obtained from both passes ";
251
lblTestStatus.setText(testResult);
252
} else {
253
testResult = "Test Failed : Reading attribute from Pass 1 <"
254
+ readingPass1 + "> != Reading attribute "
255
+ "from Pass 2 <" + readingPass2 + ">";
256
}
257
}
258
}
259
}
260
261
private static void enterInput(Robot robotKeyInput,
262
ArrayList<Integer> keyInputs) {
263
textFieldReading.setText("");
264
textFieldMain.setText("");
265
266
String strKeyInput = "KeyPress=>";
267
int nOfKeyInputs = keyInputs.size();
268
for (int i = 0; i < nOfKeyInputs; i++) {
269
int keyToUse = keyInputs.get(i);
270
robotKeyInput.keyPress(keyToUse);
271
robotKeyInput.keyRelease(keyToUse);
272
strKeyInput += (Integer.toHexString(keyToUse)) + ":";
273
}
274
275
System.out.println(strKeyInput);
276
}
277
278
public static boolean setTaskStatus(String readingValue, int passCount) {
279
boolean status = false;
280
281
if (!readingValue.isEmpty()) {
282
testResult = "Attribute : " + readingValue
283
+ "read from pass " + Integer.toString(passCount);
284
status = true;
285
} else {
286
testResult = "Failed to read Reading attribute from pass "
287
+ Integer.toString(passCount);
288
}
289
290
lblTestStatus.setText(testResult);
291
292
return status;
293
}
294
}
295
296