Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java
41152 views
1
/*
2
* Copyright (c) 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
@test
25
@bug 8215921
26
@summary Test that selecting a different item does send an ItemEvent
27
@key headful
28
@run main SelectNewItemTest
29
*/
30
31
import java.awt.Choice;
32
import java.awt.Robot;
33
import java.awt.Frame;
34
import java.awt.BorderLayout;
35
import java.awt.AWTException;
36
import java.awt.Point;
37
import java.awt.Dimension;
38
import java.awt.event.InputEvent;
39
import java.awt.event.ItemListener;
40
import java.awt.event.WindowListener;
41
import java.awt.event.ItemEvent;
42
import java.awt.event.WindowEvent;
43
import java.util.concurrent.CountDownLatch;
44
import java.util.concurrent.TimeUnit;
45
46
public class SelectNewItemTest implements ItemListener, WindowListener {
47
//Declare things used in the test, like buttons and labels here
48
private Frame frame;
49
private Choice theChoice;
50
private Robot robot;
51
52
private CountDownLatch latch = new CountDownLatch(1);
53
private volatile boolean passed = false;
54
55
private void init()
56
{
57
try {
58
robot = new Robot();
59
robot.setAutoDelay(500);
60
} catch (AWTException e) {
61
throw new RuntimeException("Unable to create Robot. Test fails.");
62
}
63
64
frame = new Frame("SelectNewItemTest");
65
frame.setLayout(new BorderLayout());
66
theChoice = new Choice();
67
for (int i = 0; i < 10; i++) {
68
theChoice.add(new String("Choice Item " + i));
69
}
70
theChoice.addItemListener(this);
71
frame.add(theChoice);
72
frame.addWindowListener(this);
73
74
frame.setLocation(1,20);
75
frame.setSize(200, 50);
76
robot.mouseMove(10, 30);
77
frame.pack();
78
frame.setVisible(true);
79
}
80
81
public static void main(String... args) {
82
SelectNewItemTest test = new SelectNewItemTest();
83
test.init();
84
try {
85
test.latch.await(12000, TimeUnit.MILLISECONDS);
86
} catch (InterruptedException e) {}
87
test.robot.waitForIdle();
88
89
try {
90
if (!test.passed) {
91
throw new RuntimeException("TEST FAILED.");
92
}
93
} finally {
94
test.frame.dispose();
95
}
96
}
97
98
private void run() {
99
try {
100
Thread.sleep(1000);
101
102
Point loc = theChoice.getLocationOnScreen();
103
int selectedIndex = theChoice.getSelectedIndex();
104
Dimension size = theChoice.getSize();
105
106
robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);
107
108
robot.setAutoDelay(250);
109
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
110
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
111
112
robot.delay(1000);
113
114
//make sure that the mouse moves to a different item, so that
115
//itemStateChanged is called.
116
robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height);
117
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
118
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
119
robot.waitForIdle();
120
121
if (selectedIndex == theChoice.getSelectedIndex())
122
throw new RuntimeException("Test case failed - expected to select" +
123
" a different item than " + selectedIndex);
124
125
selectedIndex = theChoice.getSelectedIndex();
126
//now click on the same item and make sure that item event is
127
//not generated.
128
robot.delay(1000);
129
robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);
130
131
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
132
//Make sure that the popup menu scrolls back to show the index from
133
//beginning, so that the second mouse click happens on the previously
134
//selected item.
135
//For example, on windows, it automatically scrolls the list to show
136
//the currently selected item just below the choice, which can
137
//throw off the test.
138
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
139
robot.mouseWheel(-100);
140
}
141
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
142
143
robot.delay(1000);
144
robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height);
145
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
146
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
147
robot.waitForIdle();
148
149
if (selectedIndex != theChoice.getSelectedIndex())
150
throw new RuntimeException("Test failed. Expected to select the same item " +
151
"located at: " + selectedIndex + " but got an item selected at: " + theChoice.getSelectedIndex());
152
} catch(InterruptedException e) {
153
throw new RuntimeException(e.getCause());
154
} finally {
155
latch.countDown();
156
}
157
}
158
159
@Override public void itemStateChanged(ItemEvent e) {
160
if (!passed) {
161
System.out.println("ItemEvent received. Test passes");
162
passed = true;
163
} else {
164
System.out.println("ItemEvent received for second click. Test fails");
165
passed = false;
166
}
167
}
168
169
@Override public void windowOpened(WindowEvent e) {
170
System.out.println("windowActivated()");
171
(new Thread(this::run)).start();
172
}
173
174
@Override public void windowActivated(WindowEvent e) {}
175
@Override public void windowDeactivated(WindowEvent e) {}
176
@Override public void windowClosed(WindowEvent e) {}
177
@Override public void windowClosing(WindowEvent e) {}
178
@Override public void windowIconified(WindowEvent e) {}
179
@Override public void windowDeiconified(WindowEvent e) {}
180
}
181
182