Skip to content

Commit cc9148d

Browse files
author
Damon Nguyen
committed
8354695: Open source several swing tests batch7
Reviewed-by: kizune, achung
1 parent 1b8f760 commit cc9148d

File tree

7 files changed

+1436
-0
lines changed

7 files changed

+1436
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2001, 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 4403624
27+
* @summary Tests JRootPane layout with invisible menubar
28+
* @key headful
29+
* @run main bug4403624
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Container;
34+
import java.awt.FlowLayout;
35+
import java.awt.Point;
36+
import java.awt.Robot;
37+
import java.awt.event.InputEvent;
38+
39+
import javax.swing.JButton;
40+
import javax.swing.JFrame;
41+
import javax.swing.JMenu;
42+
import javax.swing.JMenuBar;
43+
import javax.swing.SwingUtilities;
44+
45+
public class bug4403624 {
46+
private static JFrame f;
47+
private static Container c;
48+
private static JButton b;
49+
private static volatile Point p;
50+
private static volatile int bWidth;
51+
private static volatile int bHeight;
52+
private static final int OFFSET = 2;
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
SwingUtilities.invokeAndWait(() -> {
57+
f = new JFrame("bug4403624 Test");
58+
JMenuBar mbar;
59+
mbar = new JMenuBar();
60+
mbar.add(new JMenu("Menu"));
61+
f.setJMenuBar(mbar);
62+
b = new JButton("Hide Menu");
63+
b.addActionListener(e -> mbar.setVisible(false));
64+
c = f.getContentPane();
65+
c.setLayout(new FlowLayout());
66+
c.setBackground(Color.GREEN);
67+
c.add(b);
68+
f.pack();
69+
f.setLocationRelativeTo(null);
70+
f.setAlwaysOnTop(true);
71+
f.setVisible(true);
72+
});
73+
74+
Robot r = new Robot();
75+
r.setAutoDelay(200);
76+
r.waitForIdle();
77+
r.delay(1000);
78+
79+
SwingUtilities.invokeAndWait(() -> {
80+
p = b.getLocationOnScreen();
81+
bWidth = b.getWidth();
82+
bHeight = b.getHeight();
83+
});
84+
85+
r.mouseMove(p.x + (bWidth / 2), p.y + (bHeight / 2));
86+
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
87+
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
88+
89+
SwingUtilities.invokeAndWait(() -> p = c.getLocationOnScreen());
90+
91+
Color c = r.getPixelColor(p.x + OFFSET, p.y + OFFSET);
92+
93+
if (c.getGreen() < 240 && c.getBlue() > 10 && c.getRed() > 10) {
94+
System.out.println("EXPECTED: " + Color.GREEN);
95+
System.out.println("ACTUAL: " + c);
96+
throw new RuntimeException("Failure to hide menu bar.");
97+
}
98+
} finally {
99+
SwingUtilities.invokeAndWait(() -> {
100+
if (f != null) {
101+
f.dispose();
102+
}
103+
});
104+
}
105+
}
106+
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/*
2+
* Copyright (c) 2004, 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 5078454
27+
* @summary Test horizontal wheel scroll behavior of (including RTL)
28+
* @requires (os.family == "windows")
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual HorizScrollers
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.Color;
36+
import java.awt.Container;
37+
import java.awt.Dimension;
38+
import java.awt.FlowLayout;
39+
import java.awt.Insets;
40+
import java.awt.event.ActionEvent;
41+
import java.awt.event.ActionListener;
42+
import java.awt.event.MouseWheelEvent;
43+
import java.awt.event.MouseWheelListener;
44+
45+
import javax.swing.BorderFactory;
46+
import javax.swing.JButton;
47+
import javax.swing.JLabel;
48+
import javax.swing.JOptionPane;
49+
import javax.swing.JPanel;
50+
import javax.swing.JTextArea;
51+
import javax.swing.SwingConstants;
52+
import javax.swing.SwingUtilities;
53+
54+
public class HorizScrollers {
55+
private static final String INSTRUCTIONS = """
56+
This is a semi-automatic test with three phases.
57+
For each phase, you will need to change the mouse setting, as
58+
directed by a dialog. Once the correct setting is confirmed,
59+
the next test phase will run automatically.
60+
DO NOT TOUCH ANYTHING DURING TESTING!
61+
62+
The test will automatically FAIL during testing if something
63+
fails. Otherwise, the test will automatically PASS after the
64+
third testing phase.
65+
""";
66+
67+
public static void main(String[] args) throws Exception {
68+
PassFailJFrame.builder()
69+
.title("HorizScrollers Instructions")
70+
.instructions(INSTRUCTIONS)
71+
.columns(45)
72+
.testTimeOut(10)
73+
.splitUIRight(ConfigPanel::new)
74+
.logArea(6)
75+
.build()
76+
.awaitAndCheck();
77+
}
78+
79+
private static final int[] SCROLLAMTS = {1, 30, 3};
80+
private static final String[] CONFIG_MSGS = {
81+
"Set the scrolling speed to the slowest value (1 line).",
82+
"Set the scrolling speed to the fastest value (30 lines).",
83+
"Set the scrolling speed to two ticks above the slowest value (3 lines)."
84+
};
85+
86+
private static int current = 0;
87+
private static final String MW_TEXT = "Rotate the mouse wheel here";
88+
private static final String CONFIG_INSTRUCTION_TEMPLATE = """
89+
Configure Mouse Wheel for Phase %d
90+
91+
Open the Mouse Control Panel and go to the 'Wheel' tab.
92+
If 'Wheel' tab is not available just press Pass.
93+
94+
%s
95+
96+
Test the setting on the area below.
97+
Once the mouse is setup correctly, the area will turn green.
98+
When you're ready for the next part of the test to run, press GO!
99+
""";
100+
101+
static class ConfigPanel extends JPanel
102+
implements ActionListener, MouseWheelListener {
103+
JTextArea msg;
104+
JButton goBtn;
105+
JLabel mwArea;
106+
int scrollAmount;
107+
108+
private final Color defaultBg;
109+
110+
ConfigPanel() {
111+
this.scrollAmount = SCROLLAMTS[current];
112+
Container content = this;
113+
content.setLayout(new BorderLayout());
114+
msg = new JTextArea();
115+
msg.setMargin(new Insets(5, 5, 5, 5));
116+
msg.setEditable(false);
117+
msg.setLineWrap(true);
118+
msg.setWrapStyleWord(true);
119+
content.add(msg, BorderLayout.NORTH);
120+
121+
mwArea = new JLabel(MW_TEXT, SwingConstants.CENTER);
122+
mwArea.setPreferredSize(new Dimension(200, 250));
123+
mwArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
124+
mwArea.setOpaque(true);
125+
mwArea.addMouseWheelListener(this);
126+
content.add(mwArea, BorderLayout.CENTER);
127+
128+
defaultBg = mwArea.getBackground();
129+
setPhase(current);
130+
131+
goBtn = new JButton("GO!");
132+
goBtn.setEnabled(false);
133+
goBtn.addActionListener(this);
134+
JPanel flowPanel = new JPanel();
135+
flowPanel.setLayout(new FlowLayout());
136+
flowPanel.add(goBtn);
137+
content.add(flowPanel, BorderLayout.SOUTH);
138+
139+
setPreferredSize(new Dimension(600, 400));
140+
}
141+
142+
public void setPhase(int phase) {
143+
if (phase < 3) {
144+
setVisible(true);
145+
PassFailJFrame.log("Phase %d scroll speed %d"
146+
.formatted(phase + 1, SCROLLAMTS[phase]));
147+
PassFailJFrame.log(CONFIG_MSGS[phase]);
148+
149+
scrollAmount = SCROLLAMTS[phase];
150+
msg.setText(CONFIG_INSTRUCTION_TEMPLATE
151+
.formatted(phase + 1, CONFIG_MSGS[phase]));
152+
mwArea.setBackground(defaultBg);
153+
mwArea.setText(MW_TEXT);
154+
} else {
155+
// all cases passed
156+
showFinalReminderIfNeeded(false);
157+
}
158+
}
159+
160+
private void showFinalReminderIfNeeded(boolean isFailure) {
161+
if (scrollAmount != 3) {
162+
JOptionPane.showMessageDialog(
163+
ConfigPanel.this.getTopLevelAncestor(),
164+
("Test %s. please make sure you have restored " +
165+
"the original speed value blah blah")
166+
.formatted(isFailure
167+
? "failed"
168+
: "passed"),
169+
isFailure
170+
? "Failure"
171+
: "Success",
172+
isFailure
173+
? JOptionPane.WARNING_MESSAGE
174+
: JOptionPane.INFORMATION_MESSAGE
175+
);
176+
}
177+
178+
if (isFailure) {
179+
PassFailJFrame.forceFail();
180+
} else {
181+
PassFailJFrame.forcePass();
182+
}
183+
}
184+
185+
public void actionPerformed(ActionEvent e) {
186+
if (e.getSource() == goBtn) {
187+
goBtn.setEnabled(false);
188+
189+
new Thread(() -> { // new thread to avoid running robot on EDT
190+
boolean passed;
191+
try {
192+
passed = RTLScrollers.runTest(SCROLLAMTS[current]);
193+
} catch (Exception ex) {
194+
PassFailJFrame.log("Failure: " + ex);
195+
SwingUtilities.invokeLater(() ->
196+
showFinalReminderIfNeeded(true));
197+
return;
198+
}
199+
200+
PassFailJFrame.log("Phase %d passed: %b\n"
201+
.formatted(current + 1, passed));
202+
if (passed) {
203+
SwingUtilities.invokeLater(() -> {
204+
goBtn.setEnabled(true);
205+
setPhase(++current);
206+
});
207+
} else {
208+
SwingUtilities.invokeLater(() ->
209+
showFinalReminderIfNeeded(true));
210+
}
211+
}).start();
212+
}
213+
}
214+
215+
public void mouseWheelMoved(MouseWheelEvent e) {
216+
int eventScrollAmt = e.getScrollAmount();
217+
if (eventScrollAmt == scrollAmount) {
218+
mwArea.setBackground(Color.GREEN);
219+
mwArea.setText("Mouse wheel configured - press Go");
220+
goBtn.setEnabled(true);
221+
goBtn.requestFocusInWindow();
222+
PassFailJFrame.log("Proceed to the test with go button");
223+
return;
224+
}
225+
if (eventScrollAmt < scrollAmount) {
226+
mwArea.setText("Increase the scroll speed. (Want:"
227+
+ scrollAmount + " Got:" + eventScrollAmt + ")");
228+
} else {
229+
mwArea.setText("Decrease the scroll speed. (Want:"
230+
+ scrollAmount + " Got:" + eventScrollAmt + ")");
231+
}
232+
}
233+
}
234+
}

0 commit comments

Comments
 (0)