98N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
98N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
98N/A *
98N/A * This code is free software; you can redistribute it and/or modify it
98N/A * under the terms of the GNU General Public License version 2 only, as
98N/A * published by the Free Software Foundation.
98N/A *
98N/A * This code is distributed in the hope that it will be useful, but WITHOUT
98N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
98N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
98N/A * version 2 for more details (a copy is included in the LICENSE file that
98N/A * accompanied this code).
98N/A *
98N/A * You should have received a copy of the GNU General Public License version
98N/A * 2 along with this work; if not, write to the Free Software Foundation,
98N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
98N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
98N/A */
98N/A
98N/A/*
98N/A @test %W% %E% %I%, %G%
98N/A @bug 6524352
98N/A @summary support for high-resolution mouse wheel
98N/A @author dmitry cherepanov: area=awt.event
98N/A @run main/manual SmoothWheel
98N/A*/
98N/A
98N/A/**
98N/A * SmoothWheel.java
98N/A *
98N/A * summary:
98N/A */
98N/A
98N/Aimport java.awt.*;
98N/Aimport java.awt.event.*;
98N/A
98N/Apublic class SmoothWheel
98N/A{
98N/A
98N/A //*** test-writer defined static variables go here ***
98N/A
98N/A
98N/A private static void init()
98N/A {
98N/A String[] instructions =
98N/A {
98N/A "1. the test is for high-resolution mouse wheel only, ",
98N/A " refer to the cr# 6524352 for more info about such devices, ",
98N/A "2. you'll see a frame, the frame contains a checkbox, ",
98N/A "3. initially, the state of the checkbox is off, ",
98N/A " use mouse wheel over the frame, ",
98N/A " and the frame will change its size gradually, ",
98N/A "4. turn on the checkbox, ",
98N/A " use mouse wheel again over the frame, ",
98N/A " now the frame will change its size smoothly, ",
98N/A "5. if the frame has always the same size or",
98N/A " if the frame changes its size equally in 3,4 cases, ",
98N/A " then the test failed. Otherwise, it passed."
98N/A };
98N/A
98N/A Sysout.createDialog( );
98N/A Sysout.printInstructions( instructions );
98N/A
98N/A final Frame frame = new Frame();
98N/A final Checkbox checkbox = new Checkbox("smooth wheel?");
98N/A checkbox.setState(false);
98N/A
98N/A frame.setLayout (new FlowLayout());
98N/A frame.add(checkbox);
98N/A
98N/A frame.addMouseWheelListener(new MouseWheelListener() {
98N/A public void mouseWheelMoved(MouseWheelEvent e) {
98N/A Sysout.println(e.toString());
98N/A double wheelRotation = 0;
98N/A if (checkbox.getState()) {
98N/A wheelRotation = e.getPreciseWheelRotation();
98N/A } else {
98N/A wheelRotation = e.getWheelRotation();
98N/A }
98N/A Dimension size = frame.getSize();
98N/A size.width += 10 * wheelRotation;
98N/A size.height += 10 * wheelRotation;
98N/A frame.setSize(size);
98N/A }
98N/A });
98N/A
98N/A frame.setBounds(200, 200, 200, 200);
98N/A frame.setVisible(true);
98N/A
98N/A }//End init()
98N/A
98N/A
98N/A
98N/A /*****************************************************
98N/A * Standard Test Machinery Section
98N/A * DO NOT modify anything in this section -- it's a
98N/A * standard chunk of code which has all of the
98N/A * synchronisation necessary for the test harness.
98N/A * By keeping it the same in all tests, it is easier
98N/A * to read and understand someone else's test, as
98N/A * well as insuring that all tests behave correctly
98N/A * with the test harness.
98N/A * There is a section following this for test-defined
98N/A * classes
98N/A ******************************************************/
98N/A private static boolean theTestPassed = false;
98N/A private static boolean testGeneratedInterrupt = false;
98N/A private static String failureMessage = "";
98N/A
98N/A private static Thread mainThread = null;
98N/A
98N/A private static int sleepTime = 300000;
98N/A
98N/A public static void main( String args[] ) throws InterruptedException
98N/A {
98N/A mainThread = Thread.currentThread();
98N/A try
98N/A {
98N/A init();
98N/A }
98N/A catch( TestPassedException e )
98N/A {
98N/A //The test passed, so just return from main and harness will
98N/A // interepret this return as a pass
98N/A return;
98N/A }
98N/A //At this point, neither test passed nor test failed has been
98N/A // called -- either would have thrown an exception and ended the
98N/A // test, so we know we have multiple threads.
98N/A
98N/A //Test involves other threads, so sleep and wait for them to
98N/A // called pass() or fail()
98N/A try
98N/A {
98N/A Thread.sleep( sleepTime );
98N/A //Timed out, so fail the test
98N/A throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
98N/A }
98N/A catch (InterruptedException e)
98N/A {
98N/A if( ! testGeneratedInterrupt ) throw e;
98N/A
98N/A //reset flag in case hit this code more than once for some reason (just safety)
98N/A testGeneratedInterrupt = false;
98N/A if ( theTestPassed == false )
98N/A {
98N/A throw new RuntimeException( failureMessage );
98N/A }
98N/A }
98N/A
98N/A }//main
98N/A
98N/A public static synchronized void setTimeoutTo( int seconds )
98N/A {
98N/A sleepTime = seconds * 1000;
98N/A }
98N/A
98N/A public static synchronized void pass()
98N/A {
98N/A Sysout.println( "The test passed." );
98N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
98N/A //first check if this is executing in main thread
98N/A if ( mainThread == Thread.currentThread() )
98N/A {
98N/A //Still in the main thread, so set the flag just for kicks,
98N/A // and throw a test passed exception which will be caught
98N/A // and end the test.
98N/A theTestPassed = true;
98N/A throw new TestPassedException();
98N/A }
98N/A //pass was called from a different thread, so set the flag and interrupt
98N/A // the main thead.
98N/A theTestPassed = true;
98N/A testGeneratedInterrupt = true;
98N/A if (mainThread != null){
98N/A mainThread.interrupt();
98N/A }
98N/A }//pass()
98N/A
98N/A public static synchronized void fail()
98N/A {
98N/A //test writer didn't specify why test failed, so give generic
98N/A fail( "it just plain failed! :-)" );
98N/A }
98N/A
98N/A public static synchronized void fail( String whyFailed )
98N/A {
98N/A Sysout.println( "The test failed: " + whyFailed );
98N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
98N/A //check if this called from main thread
98N/A if ( mainThread == Thread.currentThread() )
98N/A {
98N/A //If main thread, fail now 'cause not sleeping
98N/A throw new RuntimeException( whyFailed );
98N/A }
98N/A theTestPassed = false;
98N/A testGeneratedInterrupt = true;
98N/A failureMessage = whyFailed;
98N/A mainThread.interrupt();
98N/A }//fail()
98N/A
98N/A}// class ManualMainTest
98N/A
98N/A//This exception is used to exit from any level of call nesting
98N/A// when it's determined that the test has passed, and immediately
98N/A// end the test.
98N/Aclass TestPassedException extends RuntimeException
98N/A{
98N/A}
98N/A
98N/A//*********** End Standard Test Machinery Section **********
98N/A
98N/A
98N/A//************ Begin classes defined for the test ****************
98N/A
98N/A// make listeners in a class defined here, and instantiate them in init()
98N/A
98N/A/* Example of a class which may be written as part of a test
98N/Aclass NewClass implements anInterface
98N/A {
98N/A static int newVar = 0;
98N/A
98N/A public void eventDispatched(AWTEvent e)
98N/A {
98N/A //Counting events to see if we get enough
98N/A eventCount++;
98N/A
98N/A if( eventCount == 20 )
98N/A {
98N/A //got enough events, so pass
98N/A
98N/A ManualMainTest.pass();
98N/A }
98N/A else if( tries == 20 )
98N/A {
98N/A //tried too many times without getting enough events so fail
98N/A
98N/A ManualMainTest.fail();
98N/A }
98N/A
98N/A }// eventDispatched()
98N/A
98N/A }// NewClass class
98N/A
98N/A*/
98N/A
98N/A
98N/A//************** End classes defined for the test *******************
98N/A
98N/A
98N/A
98N/A
98N/A/****************************************************
98N/A Standard Test Machinery
98N/A DO NOT modify anything below -- it's a standard
98N/A chunk of code whose purpose is to make user
98N/A interaction uniform, and thereby make it simpler
98N/A to read and understand someone else's test.
98N/A ****************************************************/
98N/A
98N/A/**
98N/A This is part of the standard test machinery.
98N/A It creates a dialog (with the instructions), and is the interface
98N/A for sending text messages to the user.
98N/A To print the instructions, send an array of strings to Sysout.createDialog
98N/A WithInstructions method. Put one line of instructions per array entry.
98N/A To display a message for the tester to see, simply call Sysout.println
98N/A with the string to be displayed.
98N/A This mimics System.out.println but works within the test harness as well
98N/A as standalone.
98N/A */
98N/A
98N/Aclass Sysout
98N/A{
98N/A private static TestDialog dialog;
98N/A private static boolean numbering = false;
98N/A private static int messageNumber = 0;
98N/A
98N/A public static void createDialogWithInstructions( String[] instructions )
98N/A {
98N/A dialog = new TestDialog( new Frame(), "Instructions" );
98N/A dialog.printInstructions( instructions );
98N/A dialog.setVisible(true);
98N/A println( "Any messages for the tester will display here." );
98N/A }
98N/A
98N/A public static void createDialog( )
98N/A {
98N/A dialog = new TestDialog( new Frame(), "Instructions" );
98N/A String[] defInstr = { "Instructions will appear here. ", "" } ;
98N/A dialog.printInstructions( defInstr );
98N/A dialog.setVisible(true);
98N/A println( "Any messages for the tester will display here." );
98N/A }
98N/A
98N/A
98N/A /* Enables message counting for the tester. */
98N/A public static void enableNumbering(boolean enable){
98N/A numbering = enable;
98N/A }
98N/A
98N/A public static void printInstructions( String[] instructions )
98N/A {
98N/A dialog.printInstructions( instructions );
98N/A }
98N/A
98N/A
98N/A public static void println( String messageIn )
98N/A {
98N/A if (numbering) {
98N/A messageIn = "" + messageNumber + " " + messageIn;
98N/A messageNumber++;
98N/A }
98N/A dialog.displayMessage( messageIn );
98N/A }
98N/A
98N/A}// Sysout class
98N/A
98N/A/**
98N/A This is part of the standard test machinery. It provides a place for the
98N/A test instructions to be displayed, and a place for interactive messages
98N/A to the user to be displayed.
98N/A To have the test instructions displayed, see Sysout.
98N/A To have a message to the user be displayed, see Sysout.
98N/A Do not call anything in this dialog directly.
98N/A */
98N/Aclass TestDialog extends Dialog implements ActionListener
98N/A{
98N/A
98N/A TextArea instructionsText;
98N/A TextArea messageText;
98N/A int maxStringLength = 80;
98N/A Panel buttonP = new Panel();
98N/A Button passB = new Button( "pass" );
98N/A Button failB = new Button( "fail" );
98N/A
98N/A //DO NOT call this directly, go through Sysout
98N/A public TestDialog( Frame frame, String name )
98N/A {
98N/A super( frame, name );
98N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
98N/A instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
98N/A add( "North", instructionsText );
98N/A
98N/A messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
98N/A add("Center", messageText);
98N/A
98N/A passB = new Button( "pass" );
98N/A passB.setActionCommand( "pass" );
98N/A passB.addActionListener( this );
98N/A buttonP.add( "East", passB );
98N/A
98N/A failB = new Button( "fail" );
98N/A failB.setActionCommand( "fail" );
98N/A failB.addActionListener( this );
98N/A buttonP.add( "West", failB );
98N/A
98N/A add( "South", buttonP );
98N/A pack();
98N/A
98N/A setVisible(true);
98N/A }// TestDialog()
98N/A
98N/A //DO NOT call this directly, go through Sysout
98N/A public void printInstructions( String[] instructions )
98N/A {
98N/A //Clear out any current instructions
98N/A instructionsText.setText( "" );
98N/A
98N/A //Go down array of instruction strings
98N/A
98N/A String printStr, remainingStr;
98N/A for( int i=0; i < instructions.length; i++ )
98N/A {
98N/A //chop up each into pieces maxSringLength long
98N/A remainingStr = instructions[ i ];
98N/A while( remainingStr.length() > 0 )
98N/A {
98N/A //if longer than max then chop off first max chars to print
98N/A if( remainingStr.length() >= maxStringLength )
98N/A {
98N/A //Try to chop on a word boundary
98N/A int posOfSpace = remainingStr.
98N/A lastIndexOf( ' ', maxStringLength - 1 );
98N/A
98N/A if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
98N/A
98N/A printStr = remainingStr.substring( 0, posOfSpace + 1 );
98N/A remainingStr = remainingStr.substring( posOfSpace + 1 );
98N/A }
98N/A //else just print
98N/A else
98N/A {
98N/A printStr = remainingStr;
98N/A remainingStr = "";
98N/A }
98N/A
98N/A instructionsText.append( printStr + "\n" );
98N/A
98N/A }// while
98N/A
98N/A }// for
98N/A
98N/A }//printInstructions()
98N/A
98N/A //DO NOT call this directly, go through Sysout
98N/A public void displayMessage( String messageIn )
98N/A {
98N/A messageText.append( messageIn + "\n" );
98N/A System.out.println(messageIn);
98N/A }
98N/A
98N/A //catch presses of the passed and failed buttons.
98N/A //simply call the standard pass() or fail() static methods of
98N/A //ManualMainTest
98N/A public void actionPerformed( ActionEvent e )
98N/A {
98N/A if( e.getActionCommand() == "pass" )
98N/A {
98N/A SmoothWheel.pass();
98N/A }
98N/A else
98N/A {
98N/A SmoothWheel.fail();
98N/A }
98N/A }
98N/A
98N/A}// TestDialog class