Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JScrollBar/bug4202954/bug4202954.java
41153 views
1
/*
2
* Copyright (c) 2013, 2018, 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 4202954
27
@library /test/lib
28
@library ../../regtesthelpers
29
@build Util jdk.test.lib.Platform
30
@author Michael C. Albers
31
@run main bug4202954
32
*/
33
34
import java.awt.*;
35
import java.awt.event.InputEvent;
36
import javax.swing.*;
37
38
import jdk.test.lib.Platform;
39
40
public class bug4202954 {
41
static JScrollPane buttonScrollPane;
42
static Robot robot;
43
static JFrame testFrame;
44
public static void main(String[] args) throws Exception {
45
try {
46
if (Platform.isOSX()) {
47
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
48
}
49
50
SwingUtilities.invokeAndWait(new Runnable() {
51
public void run() {
52
createAndShowGUI();
53
}
54
});
55
Point centerOfScrollPane = Util.getCenterPoint(buttonScrollPane);
56
JButton rightScrollButton = findJButton(buttonScrollPane.getHorizontalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
57
JButton bottomScrollButton = findJButton(buttonScrollPane.getVerticalScrollBar(), centerOfScrollPane.x, centerOfScrollPane.y);
58
59
if (rightScrollButton == null || bottomScrollButton == null) {
60
String errMessage = "Test can't be executed: ";
61
errMessage = errMessage + (rightScrollButton == null ? "can't find right button for horizontal scroll bar; " : ""
62
+ (bottomScrollButton == null ? "can't find bottom scroll button for vertical scroll bar" : ""));
63
throw new RuntimeException(errMessage);
64
}
65
66
robot = new Robot();
67
robot.setAutoDelay(50);
68
69
// test right, left and middle mouse buttons for horizontal scroll bar
70
if (!doTest(rightScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
71
throw new RuntimeException("Test failed: right arrow button didn't respond on left mouse button.");
72
}
73
if (!doTest(rightScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
74
throw new RuntimeException("Test failed: right arrow button respond on right mouse button.");
75
}
76
if (!doTest(rightScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
77
throw new RuntimeException("Test failed: right arrow button respond on middle mouse button.");
78
}
79
80
// test right, left and middle mouse buttons for vertical scroll bar
81
if (!doTest(bottomScrollButton, InputEvent.BUTTON1_DOWN_MASK, true)) {
82
throw new RuntimeException("Test failed: bottom arrow button didn't respond on left mouse button.");
83
}
84
if (!doTest(bottomScrollButton, InputEvent.BUTTON2_DOWN_MASK, false)) {
85
throw new RuntimeException("Test failed: bottom arrow button respond on right mouse button.");
86
}
87
if (!doTest(bottomScrollButton, InputEvent.BUTTON3_DOWN_MASK, false)) {
88
throw new RuntimeException("Test failed: bottom arrow button respond on middle mouse button.");
89
}
90
} finally {
91
if (testFrame != null) SwingUtilities.invokeAndWait(() -> testFrame.dispose());
92
}
93
}
94
public static void createAndShowGUI() {
95
JPanel buttonPanel = new JPanel();
96
buttonPanel.setLayout(new GridLayout(5,5, 15,15));
97
int buttonCount = 1;
98
while (buttonCount <= 25) {
99
buttonPanel.add(new JButton("Button #"+buttonCount));
100
buttonCount++;
101
}
102
buttonScrollPane = new JScrollPane();
103
buttonScrollPane.setViewportView(buttonPanel);
104
105
testFrame = new JFrame("bug4202954");
106
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
107
testFrame.setLayout(new BorderLayout());
108
testFrame.add(BorderLayout.CENTER, buttonScrollPane);
109
testFrame.setSize(450, 100);
110
testFrame.setVisible(true);
111
}
112
public static JButton findJButton(final JScrollBar scrollBar, final int minX, final int minY) throws Exception {
113
JButton button = Util.invokeOnEDT(new java.util.concurrent.Callable<JButton>() {
114
@Override
115
public JButton call() throws Exception {
116
int currentXorY = 0;
117
JButton scrollButton = null;
118
for (Component c: scrollBar.getComponents()) {
119
if (c instanceof JButton) {
120
Point p = c.getLocationOnScreen();
121
if (scrollBar.getOrientation() == Adjustable.VERTICAL){
122
if (currentXorY <= p.y){
123
currentXorY = p.y;
124
scrollButton = (JButton)c;
125
}
126
}else if (scrollBar.getOrientation() == Adjustable.HORIZONTAL){
127
if (currentXorY <= p.x){
128
currentXorY = p.x;
129
scrollButton = (JButton)c;
130
}
131
}
132
}
133
}
134
return scrollButton;
135
}
136
});
137
return button;
138
}
139
public static void clickMouseOnComponent(Component c, int buttons) throws Exception {
140
Point p = Util.getCenterPoint(c);
141
robot.mouseMove(p.x, p.y);
142
robot.mousePress(buttons);
143
robot.mouseRelease(buttons);
144
}
145
public static boolean doTest(JButton scrollButton, int buttons, boolean expectScroll) throws Exception {
146
java.util.concurrent.Callable<Integer> horizontalValue = new java.util.concurrent.Callable<Integer>() {
147
@Override
148
public Integer call() throws Exception {
149
return buttonScrollPane.getHorizontalScrollBar().getValue();
150
}
151
};
152
java.util.concurrent.Callable<Integer> verticalValue = new java.util.concurrent.Callable<Integer>() {
153
@Override
154
public Integer call() throws Exception {
155
return buttonScrollPane.getVerticalScrollBar().getValue();
156
}
157
};
158
Integer oldHValue = Util.invokeOnEDT(horizontalValue);
159
robot.waitForIdle();
160
Integer oldVValue = Util.invokeOnEDT(verticalValue);
161
robot.waitForIdle();
162
163
clickMouseOnComponent(scrollButton, buttons);
164
robot.waitForIdle();
165
166
int newHValue = Util.invokeOnEDT(horizontalValue);
167
robot.waitForIdle();
168
int newVValue = Util.invokeOnEDT(verticalValue);
169
robot.waitForIdle();
170
171
return (oldHValue != newHValue || oldVValue != newVValue) == expectScroll;
172
}
173
}
174
175