|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 @test %W% %E% %I%, %G% |
|
26 @bug 6524352 |
|
27 @summary support for high-resolution mouse wheel |
|
28 @author dmitry cherepanov: area=awt.event |
|
29 @run main/manual SmoothWheel |
|
30 */ |
|
31 |
|
32 /** |
|
33 * SmoothWheel.java |
|
34 * |
|
35 * summary: |
|
36 */ |
|
37 |
|
38 import java.awt.*; |
|
39 import java.awt.event.*; |
|
40 |
|
41 public class SmoothWheel |
|
42 { |
|
43 |
|
44 //*** test-writer defined static variables go here *** |
|
45 |
|
46 |
|
47 private static void init() |
|
48 { |
|
49 String[] instructions = |
|
50 { |
|
51 "1. the test is for high-resolution mouse wheel only, ", |
|
52 " refer to the cr# 6524352 for more info about such devices, ", |
|
53 "2. you'll see a frame, the frame contains a checkbox, ", |
|
54 "3. initially, the state of the checkbox is off, ", |
|
55 " use mouse wheel over the frame, ", |
|
56 " and the frame will change its size gradually, ", |
|
57 "4. turn on the checkbox, ", |
|
58 " use mouse wheel again over the frame, ", |
|
59 " now the frame will change its size smoothly, ", |
|
60 "5. if the frame has always the same size or", |
|
61 " if the frame changes its size equally in 3,4 cases, ", |
|
62 " then the test failed. Otherwise, it passed." |
|
63 }; |
|
64 |
|
65 Sysout.createDialog( ); |
|
66 Sysout.printInstructions( instructions ); |
|
67 |
|
68 final Frame frame = new Frame(); |
|
69 final Checkbox checkbox = new Checkbox("smooth wheel?"); |
|
70 checkbox.setState(false); |
|
71 |
|
72 frame.setLayout (new FlowLayout()); |
|
73 frame.add(checkbox); |
|
74 |
|
75 frame.addMouseWheelListener(new MouseWheelListener() { |
|
76 public void mouseWheelMoved(MouseWheelEvent e) { |
|
77 Sysout.println(e.toString()); |
|
78 double wheelRotation = 0; |
|
79 if (checkbox.getState()) { |
|
80 wheelRotation = e.getPreciseWheelRotation(); |
|
81 } else { |
|
82 wheelRotation = e.getWheelRotation(); |
|
83 } |
|
84 Dimension size = frame.getSize(); |
|
85 size.width += 10 * wheelRotation; |
|
86 size.height += 10 * wheelRotation; |
|
87 frame.setSize(size); |
|
88 } |
|
89 }); |
|
90 |
|
91 frame.setBounds(200, 200, 200, 200); |
|
92 frame.setVisible(true); |
|
93 |
|
94 }//End init() |
|
95 |
|
96 |
|
97 |
|
98 /***************************************************** |
|
99 * Standard Test Machinery Section |
|
100 * DO NOT modify anything in this section -- it's a |
|
101 * standard chunk of code which has all of the |
|
102 * synchronisation necessary for the test harness. |
|
103 * By keeping it the same in all tests, it is easier |
|
104 * to read and understand someone else's test, as |
|
105 * well as insuring that all tests behave correctly |
|
106 * with the test harness. |
|
107 * There is a section following this for test-defined |
|
108 * classes |
|
109 ******************************************************/ |
|
110 private static boolean theTestPassed = false; |
|
111 private static boolean testGeneratedInterrupt = false; |
|
112 private static String failureMessage = ""; |
|
113 |
|
114 private static Thread mainThread = null; |
|
115 |
|
116 private static int sleepTime = 300000; |
|
117 |
|
118 public static void main( String args[] ) throws InterruptedException |
|
119 { |
|
120 mainThread = Thread.currentThread(); |
|
121 try |
|
122 { |
|
123 init(); |
|
124 } |
|
125 catch( TestPassedException e ) |
|
126 { |
|
127 //The test passed, so just return from main and harness will |
|
128 // interepret this return as a pass |
|
129 return; |
|
130 } |
|
131 //At this point, neither test passed nor test failed has been |
|
132 // called -- either would have thrown an exception and ended the |
|
133 // test, so we know we have multiple threads. |
|
134 |
|
135 //Test involves other threads, so sleep and wait for them to |
|
136 // called pass() or fail() |
|
137 try |
|
138 { |
|
139 Thread.sleep( sleepTime ); |
|
140 //Timed out, so fail the test |
|
141 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); |
|
142 } |
|
143 catch (InterruptedException e) |
|
144 { |
|
145 if( ! testGeneratedInterrupt ) throw e; |
|
146 |
|
147 //reset flag in case hit this code more than once for some reason (just safety) |
|
148 testGeneratedInterrupt = false; |
|
149 if ( theTestPassed == false ) |
|
150 { |
|
151 throw new RuntimeException( failureMessage ); |
|
152 } |
|
153 } |
|
154 |
|
155 }//main |
|
156 |
|
157 public static synchronized void setTimeoutTo( int seconds ) |
|
158 { |
|
159 sleepTime = seconds * 1000; |
|
160 } |
|
161 |
|
162 public static synchronized void pass() |
|
163 { |
|
164 Sysout.println( "The test passed." ); |
|
165 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); |
|
166 //first check if this is executing in main thread |
|
167 if ( mainThread == Thread.currentThread() ) |
|
168 { |
|
169 //Still in the main thread, so set the flag just for kicks, |
|
170 // and throw a test passed exception which will be caught |
|
171 // and end the test. |
|
172 theTestPassed = true; |
|
173 throw new TestPassedException(); |
|
174 } |
|
175 //pass was called from a different thread, so set the flag and interrupt |
|
176 // the main thead. |
|
177 theTestPassed = true; |
|
178 testGeneratedInterrupt = true; |
|
179 if (mainThread != null){ |
|
180 mainThread.interrupt(); |
|
181 } |
|
182 }//pass() |
|
183 |
|
184 public static synchronized void fail() |
|
185 { |
|
186 //test writer didn't specify why test failed, so give generic |
|
187 fail( "it just plain failed! :-)" ); |
|
188 } |
|
189 |
|
190 public static synchronized void fail( String whyFailed ) |
|
191 { |
|
192 Sysout.println( "The test failed: " + whyFailed ); |
|
193 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); |
|
194 //check if this called from main thread |
|
195 if ( mainThread == Thread.currentThread() ) |
|
196 { |
|
197 //If main thread, fail now 'cause not sleeping |
|
198 throw new RuntimeException( whyFailed ); |
|
199 } |
|
200 theTestPassed = false; |
|
201 testGeneratedInterrupt = true; |
|
202 failureMessage = whyFailed; |
|
203 mainThread.interrupt(); |
|
204 }//fail() |
|
205 |
|
206 }// class ManualMainTest |
|
207 |
|
208 //This exception is used to exit from any level of call nesting |
|
209 // when it's determined that the test has passed, and immediately |
|
210 // end the test. |
|
211 class TestPassedException extends RuntimeException |
|
212 { |
|
213 } |
|
214 |
|
215 //*********** End Standard Test Machinery Section ********** |
|
216 |
|
217 |
|
218 //************ Begin classes defined for the test **************** |
|
219 |
|
220 // make listeners in a class defined here, and instantiate them in init() |
|
221 |
|
222 /* Example of a class which may be written as part of a test |
|
223 class NewClass implements anInterface |
|
224 { |
|
225 static int newVar = 0; |
|
226 |
|
227 public void eventDispatched(AWTEvent e) |
|
228 { |
|
229 //Counting events to see if we get enough |
|
230 eventCount++; |
|
231 |
|
232 if( eventCount == 20 ) |
|
233 { |
|
234 //got enough events, so pass |
|
235 |
|
236 ManualMainTest.pass(); |
|
237 } |
|
238 else if( tries == 20 ) |
|
239 { |
|
240 //tried too many times without getting enough events so fail |
|
241 |
|
242 ManualMainTest.fail(); |
|
243 } |
|
244 |
|
245 }// eventDispatched() |
|
246 |
|
247 }// NewClass class |
|
248 |
|
249 */ |
|
250 |
|
251 |
|
252 //************** End classes defined for the test ******************* |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 /**************************************************** |
|
258 Standard Test Machinery |
|
259 DO NOT modify anything below -- it's a standard |
|
260 chunk of code whose purpose is to make user |
|
261 interaction uniform, and thereby make it simpler |
|
262 to read and understand someone else's test. |
|
263 ****************************************************/ |
|
264 |
|
265 /** |
|
266 This is part of the standard test machinery. |
|
267 It creates a dialog (with the instructions), and is the interface |
|
268 for sending text messages to the user. |
|
269 To print the instructions, send an array of strings to Sysout.createDialog |
|
270 WithInstructions method. Put one line of instructions per array entry. |
|
271 To display a message for the tester to see, simply call Sysout.println |
|
272 with the string to be displayed. |
|
273 This mimics System.out.println but works within the test harness as well |
|
274 as standalone. |
|
275 */ |
|
276 |
|
277 class Sysout |
|
278 { |
|
279 private static TestDialog dialog; |
|
280 private static boolean numbering = false; |
|
281 private static int messageNumber = 0; |
|
282 |
|
283 public static void createDialogWithInstructions( String[] instructions ) |
|
284 { |
|
285 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
286 dialog.printInstructions( instructions ); |
|
287 dialog.setVisible(true); |
|
288 println( "Any messages for the tester will display here." ); |
|
289 } |
|
290 |
|
291 public static void createDialog( ) |
|
292 { |
|
293 dialog = new TestDialog( new Frame(), "Instructions" ); |
|
294 String[] defInstr = { "Instructions will appear here. ", "" } ; |
|
295 dialog.printInstructions( defInstr ); |
|
296 dialog.setVisible(true); |
|
297 println( "Any messages for the tester will display here." ); |
|
298 } |
|
299 |
|
300 |
|
301 /* Enables message counting for the tester. */ |
|
302 public static void enableNumbering(boolean enable){ |
|
303 numbering = enable; |
|
304 } |
|
305 |
|
306 public static void printInstructions( String[] instructions ) |
|
307 { |
|
308 dialog.printInstructions( instructions ); |
|
309 } |
|
310 |
|
311 |
|
312 public static void println( String messageIn ) |
|
313 { |
|
314 if (numbering) { |
|
315 messageIn = "" + messageNumber + " " + messageIn; |
|
316 messageNumber++; |
|
317 } |
|
318 dialog.displayMessage( messageIn ); |
|
319 } |
|
320 |
|
321 }// Sysout class |
|
322 |
|
323 /** |
|
324 This is part of the standard test machinery. It provides a place for the |
|
325 test instructions to be displayed, and a place for interactive messages |
|
326 to the user to be displayed. |
|
327 To have the test instructions displayed, see Sysout. |
|
328 To have a message to the user be displayed, see Sysout. |
|
329 Do not call anything in this dialog directly. |
|
330 */ |
|
331 class TestDialog extends Dialog implements ActionListener |
|
332 { |
|
333 |
|
334 TextArea instructionsText; |
|
335 TextArea messageText; |
|
336 int maxStringLength = 80; |
|
337 Panel buttonP = new Panel(); |
|
338 Button passB = new Button( "pass" ); |
|
339 Button failB = new Button( "fail" ); |
|
340 |
|
341 //DO NOT call this directly, go through Sysout |
|
342 public TestDialog( Frame frame, String name ) |
|
343 { |
|
344 super( frame, name ); |
|
345 int scrollBoth = TextArea.SCROLLBARS_BOTH; |
|
346 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); |
|
347 add( "North", instructionsText ); |
|
348 |
|
349 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); |
|
350 add("Center", messageText); |
|
351 |
|
352 passB = new Button( "pass" ); |
|
353 passB.setActionCommand( "pass" ); |
|
354 passB.addActionListener( this ); |
|
355 buttonP.add( "East", passB ); |
|
356 |
|
357 failB = new Button( "fail" ); |
|
358 failB.setActionCommand( "fail" ); |
|
359 failB.addActionListener( this ); |
|
360 buttonP.add( "West", failB ); |
|
361 |
|
362 add( "South", buttonP ); |
|
363 pack(); |
|
364 |
|
365 setVisible(true); |
|
366 }// TestDialog() |
|
367 |
|
368 //DO NOT call this directly, go through Sysout |
|
369 public void printInstructions( String[] instructions ) |
|
370 { |
|
371 //Clear out any current instructions |
|
372 instructionsText.setText( "" ); |
|
373 |
|
374 //Go down array of instruction strings |
|
375 |
|
376 String printStr, remainingStr; |
|
377 for( int i=0; i < instructions.length; i++ ) |
|
378 { |
|
379 //chop up each into pieces maxSringLength long |
|
380 remainingStr = instructions[ i ]; |
|
381 while( remainingStr.length() > 0 ) |
|
382 { |
|
383 //if longer than max then chop off first max chars to print |
|
384 if( remainingStr.length() >= maxStringLength ) |
|
385 { |
|
386 //Try to chop on a word boundary |
|
387 int posOfSpace = remainingStr. |
|
388 lastIndexOf( ' ', maxStringLength - 1 ); |
|
389 |
|
390 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; |
|
391 |
|
392 printStr = remainingStr.substring( 0, posOfSpace + 1 ); |
|
393 remainingStr = remainingStr.substring( posOfSpace + 1 ); |
|
394 } |
|
395 //else just print |
|
396 else |
|
397 { |
|
398 printStr = remainingStr; |
|
399 remainingStr = ""; |
|
400 } |
|
401 |
|
402 instructionsText.append( printStr + "\n" ); |
|
403 |
|
404 }// while |
|
405 |
|
406 }// for |
|
407 |
|
408 }//printInstructions() |
|
409 |
|
410 //DO NOT call this directly, go through Sysout |
|
411 public void displayMessage( String messageIn ) |
|
412 { |
|
413 messageText.append( messageIn + "\n" ); |
|
414 System.out.println(messageIn); |
|
415 } |
|
416 |
|
417 //catch presses of the passed and failed buttons. |
|
418 //simply call the standard pass() or fail() static methods of |
|
419 //ManualMainTest |
|
420 public void actionPerformed( ActionEvent e ) |
|
421 { |
|
422 if( e.getActionCommand() == "pass" ) |
|
423 { |
|
424 SmoothWheel.pass(); |
|
425 } |
|
426 else |
|
427 { |
|
428 SmoothWheel.fail(); |
|
429 } |
|
430 } |
|
431 |
|
432 }// TestDialog class |