Skip to content

Commit 4d0efce

Browse files
author
Satyen Subramaniam
committed
8353592: Open source several scrollbar tests
Backport-of: 5280b7b031bb3dc44fb923c3be7ae04ec22fd364
1 parent ccfe9c5 commit 4d0efce

File tree

3 files changed

+382
-0
lines changed

3 files changed

+382
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 1998, 2025, 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 4029465
27+
* @summary Win95 Multiselect List doesn't display scrollbar
28+
* @key headful
29+
* @run main ListScrollbarTest
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Dimension;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.awt.List;
37+
import java.awt.Point;
38+
import java.awt.Rectangle;
39+
import java.awt.Robot;
40+
41+
public class ListScrollbarTest {
42+
43+
private static final Color BG_COLOR = Color.RED;
44+
private static Robot robot;
45+
private static Frame frame;
46+
private static List list;
47+
private static int counter = 0;
48+
private static volatile Rectangle listBounds;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
EventQueue.invokeAndWait(ListScrollbarTest::createAndShowUI);
53+
test();
54+
} finally {
55+
EventQueue.invokeAndWait(() -> {
56+
if (frame != null) {
57+
frame.dispose();
58+
}
59+
});
60+
}
61+
}
62+
63+
private static void test() throws Exception {
64+
robot = new Robot();
65+
robot.waitForIdle();
66+
robot.delay(500);
67+
68+
EventQueue.invokeAndWait(() -> {
69+
Point locationOnScreen = list.getLocationOnScreen();
70+
Dimension size = list.getSize();
71+
listBounds = new Rectangle(locationOnScreen, size);
72+
});
73+
74+
Point point = new Point(listBounds.x + listBounds.width - 5,
75+
listBounds.y + listBounds.height / 2);
76+
77+
78+
for (int i = 0; i < 4; i++) {
79+
scrollbarCheck(point, false);
80+
addListItem();
81+
}
82+
scrollbarCheck(point, true);
83+
}
84+
85+
public static boolean areColorsSimilar(Color c1, Color c2, int tolerance) {
86+
return Math.abs(c1.getRed() - c2.getRed()) <= tolerance
87+
&& Math.abs(c1.getGreen() - c2.getGreen()) <= tolerance
88+
&& Math.abs(c1.getBlue() - c2.getBlue()) <= tolerance;
89+
}
90+
91+
private static void scrollbarCheck(Point point, boolean isScrollbarExpected) {
92+
Color pixelColor = robot.getPixelColor(point.x, point.y);
93+
boolean areColorsSimilar = areColorsSimilar(BG_COLOR, pixelColor, 5);
94+
95+
if (isScrollbarExpected && areColorsSimilar) {
96+
throw new RuntimeException(("""
97+
Scrollbar is expected, but pixel color \
98+
is similar to the background color
99+
%s pixel color
100+
%s bg color""")
101+
.formatted(pixelColor, BG_COLOR));
102+
}
103+
104+
if (!isScrollbarExpected && !areColorsSimilar) {
105+
throw new RuntimeException(("""
106+
Scrollbar is not expected, but pixel color \
107+
is not similar to the background color
108+
%s pixel color
109+
%s bg color""")
110+
.formatted(pixelColor, BG_COLOR));
111+
}
112+
}
113+
114+
private static void addListItem() throws Exception {
115+
EventQueue.invokeAndWait(() -> {
116+
counter++;
117+
System.out.println("Adding list item " + counter);
118+
list.add("List Item " + counter);
119+
frame.validate();
120+
});
121+
robot.waitForIdle();
122+
robot.delay(150);
123+
}
124+
125+
private static void createAndShowUI() {
126+
frame = new Frame("ListScrollbarTest");
127+
list = new List(3, true);
128+
list.setBackground(BG_COLOR);
129+
130+
// do not draw border around items, it can affect screen capture
131+
list.setFocusable(false);
132+
133+
frame.add(list);
134+
135+
frame.pack();
136+
frame.setLocationRelativeTo(null);
137+
frame.setVisible(true);
138+
}
139+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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 4075950
27+
* @summary Test for functionality of Control Click on Scrollbar
28+
* @key headful
29+
* @run main ScrollbarCtrlClickTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Dimension;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.awt.Point;
37+
import java.awt.Rectangle;
38+
import java.awt.Robot;
39+
import java.awt.Scrollbar;
40+
import java.awt.TextArea;
41+
import java.awt.event.KeyEvent;
42+
import java.awt.event.MouseEvent;
43+
import java.util.concurrent.CountDownLatch;
44+
import java.util.concurrent.TimeUnit;
45+
46+
public class ScrollbarCtrlClickTest {
47+
private static Frame frame;
48+
private static TextArea ta;
49+
private static Scrollbar scrollbar;
50+
private static final CountDownLatch latch = new CountDownLatch(1);
51+
private static volatile Rectangle sbBounds;
52+
53+
public static void main(String[] args) throws Exception {
54+
try {
55+
EventQueue.invokeAndWait(ScrollbarCtrlClickTest::initAndShowGUI);
56+
test();
57+
} finally {
58+
EventQueue.invokeAndWait(() -> {
59+
if (frame != null) {
60+
frame.dispose();
61+
}
62+
});
63+
}
64+
}
65+
66+
private static void initAndShowGUI() {
67+
frame = new Frame("ScrollbarDimensionTest");
68+
ta = new TextArea("", 30, 100);
69+
70+
71+
scrollbar = new Scrollbar(Scrollbar.VERTICAL,
72+
0, 10, 0, 20);
73+
74+
// Just setting layout so scrollbar thumb will be big enough to use
75+
frame.setLayout(new BorderLayout());
76+
frame.add("East", scrollbar);
77+
frame.add("West", ta);
78+
79+
scrollbar.addAdjustmentListener(e -> {
80+
System.out.println(e.paramString());
81+
ta.append(e.paramString() + "\n");
82+
latch.countDown();
83+
});
84+
85+
frame.pack();
86+
frame.setLocationRelativeTo(null);
87+
frame.setVisible(true);
88+
}
89+
90+
private static void test() throws Exception {
91+
Robot robot = new Robot();
92+
robot.waitForIdle();
93+
robot.setAutoDelay(25);
94+
robot.delay(500);
95+
96+
EventQueue.invokeAndWait(() -> {
97+
Point locationOnScreen = scrollbar.getLocationOnScreen();
98+
Dimension size = scrollbar.getSize();
99+
sbBounds = new Rectangle(locationOnScreen, size);
100+
});
101+
102+
robot.mouseMove(sbBounds.x + sbBounds.width / 2,
103+
sbBounds.y + sbBounds.height - 50);
104+
105+
robot.keyPress(KeyEvent.VK_CONTROL);
106+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
107+
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
108+
robot.keyRelease(KeyEvent.VK_CONTROL);
109+
110+
if (!latch.await(1, TimeUnit.SECONDS)) {
111+
throw new RuntimeException("Timed out waiting for latch");
112+
}
113+
}
114+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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 4169461
27+
* @summary Test for Motif Scrollbar unit increment
28+
* @key headful
29+
* @run main UnitIncrementTest
30+
*/
31+
32+
import javax.swing.UIManager;
33+
import java.awt.Dimension;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.awt.Point;
37+
import java.awt.Rectangle;
38+
import java.awt.Robot;
39+
import java.awt.Scrollbar;
40+
import java.awt.event.AdjustmentEvent;
41+
import java.awt.event.MouseEvent;
42+
import java.util.ArrayList;
43+
44+
public class UnitIncrementTest {
45+
private static Frame frame;
46+
private static Scrollbar scrollbar;
47+
private static final java.util.List<AdjustmentEvent> eventsList = new ArrayList<>();
48+
private static final int UNIT_INCREMENT_VALUE = 5;
49+
private static final int INCREMENTS_COUNT = 10;
50+
private static volatile Rectangle scrollbarBounds;
51+
52+
public static void main(String[] args) throws Exception {
53+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
54+
55+
try {
56+
EventQueue.invokeAndWait(UnitIncrementTest::createAndShowUI);
57+
test();
58+
} finally {
59+
EventQueue.invokeAndWait(() -> {
60+
if (frame != null) {
61+
frame.dispose();
62+
}
63+
});
64+
}
65+
}
66+
67+
private static void createAndShowUI() {
68+
frame = new Frame("UnitIncrementTest");
69+
70+
scrollbar = new Scrollbar(Scrollbar.HORIZONTAL);
71+
72+
scrollbar.setUnitIncrement(UNIT_INCREMENT_VALUE);
73+
scrollbar.setBlockIncrement(20);
74+
75+
scrollbar.addAdjustmentListener(e -> {
76+
eventsList.add(e);
77+
System.out.println(e);
78+
});
79+
80+
frame.add(scrollbar);
81+
82+
frame.setSize(300, 100);
83+
frame.setLocationRelativeTo(null);
84+
frame.setVisible(true);
85+
}
86+
87+
private static void test() throws Exception {
88+
Robot robot = new Robot();
89+
robot.waitForIdle();
90+
robot.setAutoDelay(25);
91+
robot.delay(500);
92+
93+
EventQueue.invokeAndWait(() -> {
94+
Point locationOnScreen = scrollbar.getLocationOnScreen();
95+
Dimension size = scrollbar.getSize();
96+
scrollbarBounds = new Rectangle(locationOnScreen, size);
97+
});
98+
99+
robot.mouseMove(scrollbarBounds.x + scrollbarBounds.width - 10,
100+
scrollbarBounds.y + scrollbarBounds.height / 2);
101+
102+
for (int i = 0; i < INCREMENTS_COUNT; i++) {
103+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
104+
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
105+
robot.delay(150);
106+
}
107+
108+
robot.waitForIdle();
109+
robot.delay(250);
110+
111+
if (eventsList.size() != INCREMENTS_COUNT) {
112+
throw new RuntimeException("Wrong number of events: " + eventsList.size());
113+
}
114+
115+
int oldValue = 0;
116+
for (AdjustmentEvent event : eventsList) {
117+
System.out.println("\nChecking event " + event);
118+
119+
int diff = event.getValue() - oldValue;
120+
System.out.printf("diff: %d - %d = %d\n", event.getValue(), oldValue, diff);
121+
122+
if (diff != UNIT_INCREMENT_VALUE) {
123+
throw new RuntimeException("Unexpected adjustment value: %d".formatted(diff));
124+
}
125+
126+
oldValue = event.getValue();
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)