Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Choice/ChoiceKeyEventReaction/ChoiceKeyEventReaction.java
41152 views
1
/*
2
* Copyright (c) 2005, 2019, 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
/*
25
* @test
26
* @bug 6252982
27
* @key headful
28
* @summary PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
29
* @author andrei.dmitriev : area=awt.choice
30
* @run main ChoiceKeyEventReaction
31
*/
32
33
import java.awt.Choice;
34
import java.awt.FlowLayout;
35
import java.awt.Frame;
36
import java.awt.Point;
37
import java.awt.Robot;
38
import java.awt.TextField;
39
import java.awt.Toolkit;
40
import java.awt.event.InputEvent;
41
import java.awt.event.ItemEvent;
42
import java.awt.event.ItemListener;
43
import java.awt.event.KeyAdapter;
44
import java.awt.event.KeyEvent;
45
46
public class ChoiceKeyEventReaction
47
{
48
private static Robot robot;
49
private static Choice choice1 = new Choice();
50
private static Point pt;
51
private static TextField tf = new TextField("Hi");
52
private static boolean keyTypedOnTextField = false;
53
private static boolean itemChanged = false;
54
private static Frame frame;
55
private static String toolkit;
56
57
public static void main(String[] args) {
58
createAndShowGUI();
59
60
try {
61
robot = new Robot();
62
robot.setAutoDelay(100);
63
robot.waitForIdle();
64
65
moveFocusToTextField();
66
testKeyOnChoice(InputEvent.BUTTON1_MASK, KeyEvent.VK_UP);
67
} catch (Exception e) {
68
throw new RuntimeException("Test failed. Exception thrown: "+e);
69
} finally {
70
if (frame != null) {
71
frame.dispose();
72
}
73
}
74
}
75
76
private static void createAndShowGUI() {
77
frame = new Frame();
78
toolkit = Toolkit.getDefaultToolkit().getClass().getName();
79
System.out.println("Current toolkit is :" +toolkit);
80
for (int i = 1; i<20; i++){
81
choice1.add("item-0"+i);
82
}
83
84
tf.addKeyListener(new KeyAdapter(){
85
public void keyPressed(KeyEvent ke) {
86
keyTypedOnTextField = true;
87
System.out.println(ke);
88
}
89
});
90
91
choice1.addItemListener(new ItemListener() {
92
public void itemStateChanged(ItemEvent e) {
93
itemChanged = true;
94
System.out.println(e);
95
}
96
});
97
choice1.setFocusable(false);
98
99
frame.add(tf);
100
frame.add(choice1);
101
frame.setLayout (new FlowLayout());
102
frame.setSize (200,200);
103
frame.setLocationRelativeTo(null);
104
frame.setVisible(true);
105
}
106
107
private static void testKeyOnChoice(int button, int key) {
108
pt = choice1.getLocationOnScreen();
109
robot.mouseMove(pt.x + choice1.getWidth()/2, pt.y + choice1.getHeight()/2);
110
111
robot.mousePress(button);
112
robot.mouseRelease(button);
113
robot.waitForIdle();
114
115
robot.keyPress(key);
116
robot.keyRelease(key);
117
robot.waitForIdle();
118
119
System.out.println("keyTypedOnTextField = "+keyTypedOnTextField +": itemChanged = " + itemChanged);
120
if (itemChanged) {
121
throw new RuntimeException("Test failed. ItemChanged event occur on Choice.");
122
}
123
124
// We may just write
125
// if (toolkit.equals("sun.awt.windows.WToolkit") == keyTypedOnTextField) {fail;}
126
// but must report differently in these cases so put two separate if statements for simplicity.
127
if (!toolkit.equals("sun.awt.X11.XToolkit") &&
128
!keyTypedOnTextField) {
129
throw new RuntimeException("Test failed. (Win32/MacOS) KeyEvent wasn't addressed to TextField. ");
130
}
131
132
if (toolkit.equals("sun.awt.X11.XToolkit") &&
133
keyTypedOnTextField) {
134
throw new RuntimeException("Test failed. (XToolkit/MToolkit). KeyEvent was addressed to TextField.");
135
}
136
137
System.out.println("Test passed. Unfocusable Choice doesn't react on keys.");
138
}
139
140
public static void moveFocusToTextField() {
141
pt = tf.getLocationOnScreen();
142
robot.mouseMove(pt.x + tf.getWidth()/2, pt.y + tf.getHeight()/2);
143
144
robot.mousePress(InputEvent.BUTTON1_MASK);
145
robot.mouseRelease(InputEvent.BUTTON1_MASK);
146
}
147
}
148
149