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