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