Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Mouse/MouseComboBoxTest/MouseComboBoxTest.java
41153 views
1
/*
2
* Copyright (c) 2014, 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
/*
25
* @test
26
* @key headful
27
* @bug 8032872
28
* @summary Tests JComboBox selection via the mouse
29
* @author Dmitry Markov
30
*/
31
import javax.swing.*;
32
import javax.swing.plaf.basic.BasicComboPopup;
33
import javax.swing.plaf.basic.ComboPopup;
34
import javax.swing.plaf.metal.MetalComboBoxUI;
35
import javax.swing.plaf.metal.MetalLookAndFeel;
36
import java.awt.*;
37
import java.awt.event.InputEvent;
38
import java.awt.event.KeyEvent;
39
40
public class MouseComboBoxTest {
41
private static final String[] items = {"One", "Two", "Three", "Four", "Five"};
42
43
private static Robot robot = null;
44
private static JFrame frame = null;
45
private static JComboBox comboBox = null;
46
private static MyComboBoxUI comboBoxUI = null;
47
48
public static void main(String[] args) throws Exception {
49
robot = new Robot();
50
robot.setAutoDelay(100);
51
52
UIManager.setLookAndFeel(new MetalLookAndFeel());
53
SwingUtilities.invokeAndWait(new Runnable() {
54
@Override
55
public void run() {
56
createAndShowGUI();
57
}
58
});
59
robot.waitForIdle();
60
robot.delay(1000);
61
62
for (int i = 0; i < items.length; i++) {
63
// Open popup
64
robot.keyPress(KeyEvent.VK_DOWN);
65
robot.keyRelease(KeyEvent.VK_DOWN);
66
robot.waitForIdle();
67
68
Point point = getItemPointToClick(i);
69
robot.mouseMove(point.x, point.y);
70
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
71
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
72
robot.waitForIdle();
73
74
if (i != getSelectedIndex()) {
75
throw new RuntimeException("Test Failed! Incorrect value of selected index = " + getSelectedIndex() +
76
", expected value = " + i);
77
}
78
}
79
}
80
81
private static Point getItemPointToClick(final int item) throws Exception {
82
final Point[] result = new Point[1];
83
84
SwingUtilities.invokeAndWait(new Runnable() {
85
@Override
86
public void run() {
87
BasicComboPopup popup = (BasicComboPopup)comboBoxUI.getComboPopup();
88
Point point = popup.getLocationOnScreen();
89
Dimension size = popup.getSize();
90
91
int step = size.height / items.length;
92
point.x += size.width / 2;
93
point.y += step / 2 + step * item;
94
result[0] = point;
95
}
96
});
97
return result[0];
98
}
99
100
private static int getSelectedIndex() throws Exception {
101
final int[] result = new int[1];
102
103
SwingUtilities.invokeAndWait(new Runnable() {
104
@Override
105
public void run() {
106
result[0] = comboBox.getSelectedIndex();
107
}
108
});
109
return result[0];
110
}
111
112
private static void createAndShowGUI() {
113
frame = new JFrame("MouseComboBoxTest");
114
115
comboBox = new JComboBox(items);
116
comboBox.setEditable(true);
117
comboBoxUI = new MyComboBoxUI();
118
comboBox.setUI(comboBoxUI);
119
120
frame.setUndecorated(true);
121
frame.pack();
122
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
123
frame.setVisible(true);
124
125
JWindow window = new JWindow(frame);
126
window.add(comboBox);
127
window.pack();
128
window.setLocationRelativeTo(null);
129
window.setVisible(true);
130
}
131
132
private static class MyComboBoxUI extends MetalComboBoxUI {
133
public ComboPopup getComboPopup() {
134
return popup;
135
}
136
}
137
}
138
139
140