Skip to content

Commit 76dec47

Browse files
author
Harshitha Onkar
committed
8354340: Open source Swing Tests - Set 6
Reviewed-by: azvegint, achung
1 parent 38f9b3a commit 76dec47

File tree

2 files changed

+279
-0
lines changed

2 files changed

+279
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
* @key headful
27+
* @bug 4217252
28+
* @summary Verify that scrolling beyond the visible region and scrolling
29+
* a component smaller than the viewport is not allowed.
30+
* @library /javax/swing/regtesthelpers
31+
* @build Util
32+
* @run main/othervm -Dsun.java2d.uiScale=1 ScrollRectToVisibleTest3
33+
*/
34+
35+
import java.awt.BorderLayout;
36+
import java.awt.Component;
37+
import java.awt.Dimension;
38+
import java.awt.Point;
39+
import java.awt.Rectangle;
40+
import java.awt.Robot;
41+
import java.awt.event.MouseAdapter;
42+
import java.awt.event.MouseEvent;
43+
import javax.swing.JButton;
44+
import javax.swing.JFrame;
45+
import javax.swing.JScrollPane;
46+
import javax.swing.JTable;
47+
import javax.swing.SwingUtilities;
48+
import javax.swing.table.AbstractTableModel;
49+
50+
public class ScrollRectToVisibleTest3 {
51+
private static JFrame frame;
52+
private static JTable table;
53+
private static JButton scrollButton;
54+
private static volatile int clickCount = 0;
55+
private static final String[] EXPECTED_TEXT = {"99 x 0", "98 x 0",
56+
"97 x 0", "96 x 0"};
57+
public static void main(String[] args) throws Exception {
58+
Robot robot = new Robot();
59+
robot.setAutoWaitForIdle(true);
60+
61+
SwingUtilities.invokeAndWait(ScrollRectToVisibleTest3::createTestUI);
62+
robot.waitForIdle();
63+
robot.delay(1000);
64+
65+
Rectangle frameBounds = Util.invokeOnEDT(() -> getComponentBounds(frame));
66+
robot.delay(100);
67+
Point scrollBtnLoc = Util.getCenterPoint(scrollButton);
68+
69+
robot.mouseMove(scrollBtnLoc.x, scrollBtnLoc.y);
70+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
71+
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
72+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
73+
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
74+
robot.delay(50);
75+
76+
int rowHeight = Util.invokeOnEDT(() -> table.getRowHeight());
77+
for (int i = 1; i <= 4; i++) {
78+
robot.mouseMove(frameBounds.x + 50,
79+
frameBounds.y + frameBounds.height - (rowHeight * i + 2));
80+
robot.delay(300);
81+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
82+
robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
83+
// 500 ms delay added so that current mouseClicked event
84+
// is processed successfully before proceeding to the next
85+
robot.delay(500);
86+
}
87+
if (clickCount != 4) {
88+
throw new RuntimeException("Test Failed! Expected 4 mouse clicks"
89+
+ " but got " + clickCount);
90+
}
91+
}
92+
93+
private static void createTestUI() {
94+
frame = new JFrame("ScrollRectToVisibleTest3");
95+
table = new JTable(new TestModel());
96+
table.addMouseListener(new MouseAdapter() {
97+
@Override
98+
public void mouseClicked(MouseEvent e) {
99+
JTable testTable = (JTable) e.getComponent();
100+
int row = testTable.getSelectedRow();
101+
int column = testTable.getSelectedColumn();
102+
String cellContent = testTable.getValueAt(row, column).toString();
103+
if (!EXPECTED_TEXT[clickCount].equals(cellContent)) {
104+
throw new RuntimeException(("Test failed! Table Cell Content"
105+
+ " at (row %d , col %d)\n Expected: %s vs Actual: %s")
106+
.formatted(row, column,
107+
EXPECTED_TEXT[clickCount], cellContent));
108+
}
109+
clickCount++;
110+
}
111+
});
112+
113+
scrollButton = new JButton("Scroll");
114+
scrollButton.addActionListener(ae -> {
115+
Rectangle bounds = table.getBounds();
116+
bounds.y = bounds.height + table.getRowHeight();
117+
bounds.height = table.getRowHeight();
118+
System.out.println("scrolling: " + bounds);
119+
table.scrollRectToVisible(bounds);
120+
System.out.println("bounds: " + table.getVisibleRect());
121+
});
122+
123+
frame.add(scrollButton, BorderLayout.NORTH);
124+
frame.add(new JScrollPane(table), BorderLayout.CENTER);
125+
frame.setSize(400, 300);
126+
frame.setLocationRelativeTo(null);
127+
frame.setVisible(true);
128+
}
129+
130+
131+
private static class TestModel extends AbstractTableModel {
132+
@Override
133+
public String getColumnName(int column) {
134+
return Integer.toString(column);
135+
}
136+
137+
@Override
138+
public int getRowCount() {
139+
return 100;
140+
}
141+
142+
@Override
143+
public int getColumnCount() {
144+
return 5;
145+
}
146+
147+
@Override
148+
public Object getValueAt(int row, int col) {
149+
return row + " x " + col;
150+
}
151+
152+
@Override
153+
public boolean isCellEditable(int row, int column) { return false; }
154+
155+
@Override
156+
public void setValueAt(Object value, int row, int col) {
157+
}
158+
}
159+
160+
private static Rectangle getComponentBounds(Component c) {
161+
Point locationOnScreen = c.getLocationOnScreen();
162+
Dimension size = c.getSize();
163+
return new Rectangle(locationOnScreen, size);
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 4128110
27+
* @summary Verify that JViewport.setViewportView() and JScrollPane.setViewport()
28+
* force a re-layout and a repaint.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual SetViewRepaint
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.Color;
36+
import java.awt.GridLayout;
37+
import java.awt.event.ActionListener;
38+
import javax.swing.JButton;
39+
import javax.swing.JFrame;
40+
import javax.swing.JLabel;
41+
import javax.swing.JList;
42+
import javax.swing.JPanel;
43+
import javax.swing.JScrollPane;
44+
import javax.swing.JViewport;
45+
46+
public class SetViewRepaint {
47+
private static final String INSTRUCTIONS = """
48+
Verify the following two cases:
49+
50+
1) Press "JViewport.setViewportView()" button and verify that
51+
the blue label is replaced by a scrolling list.
52+
53+
2) Press "JScrollPane.setViewport()" button and verify that
54+
the red label is replaced by a scrolling list as well.
55+
56+
In either case the display should update automatically after
57+
pressing the button.
58+
59+
If the above is true, press PASS else press FAIL.
60+
""";
61+
62+
public static void main(String[] args) throws Exception {
63+
PassFailJFrame.builder()
64+
.instructions(INSTRUCTIONS)
65+
.columns(30)
66+
.testUI(SetViewRepaint::createTestUI)
67+
.build()
68+
.awaitAndCheck();
69+
}
70+
71+
private static JFrame createTestUI() {
72+
JFrame frame = new JFrame("SetViewRepaint");
73+
JPanel p1 = new JPanel(new BorderLayout());
74+
JPanel p2 = new JPanel(new BorderLayout());
75+
76+
JLabel label1 = new ColorLabel(Color.BLUE, "Blue Label");
77+
final JList list1 = new JList(new String[]{"one", "two", "three", "four"});
78+
final JScrollPane sp1 = new JScrollPane(label1);
79+
ActionListener doSetViewportView = e -> sp1.setViewportView(list1);
80+
JButton b1 = new JButton("JViewport.setViewportView()");
81+
b1.addActionListener(doSetViewportView);
82+
p1.add(sp1, BorderLayout.CENTER);
83+
p1.add(b1, BorderLayout.SOUTH);
84+
85+
JLabel label2 = new ColorLabel(Color.RED, "Red Label");
86+
final JList list2 = new JList(new String[]{"five", "six", "seven", "eight"});
87+
final JScrollPane sp2 = new JScrollPane(label2);
88+
ActionListener doSetViewport = e -> {
89+
JViewport vp = new JViewport();
90+
vp.setView(list2);
91+
sp2.setViewport(vp);
92+
};
93+
JButton b2 = new JButton("JScrollPane.setViewport()");
94+
b2.addActionListener(doSetViewport);
95+
p2.add(sp2, BorderLayout.CENTER);
96+
p2.add(b2, BorderLayout.SOUTH);
97+
frame.setLayout(new GridLayout(1, 2));
98+
frame.add(p1);
99+
frame.add(p2);
100+
frame.setResizable(false);
101+
frame.setSize(500, 120);
102+
return frame;
103+
}
104+
105+
private static class ColorLabel extends JLabel {
106+
ColorLabel(Color color, String text) {
107+
super(text);
108+
setForeground(Color.WHITE);
109+
setBackground(color);
110+
setOpaque(true);
111+
setHorizontalAlignment(CENTER);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)