< prev index next >

java/awt/Scrollbar/ScrollbarMouseWheelTest/ScrollbarMouseWheelTest.java

Print this page


   1 /*
   2   test 1.2 98/08/05
   3   @bug 4449139
   4   @summary test MouseWheelEvent generation by Scrollbar component
   5   @author  : area=Scrollbar
   6   @run applet ScrollbarMouseWheelTest.html
   7 */















   8 
   9 import java.applet.Applet;
  10 import java.awt.*;
  11 import java.awt.event.*;
  12 
  13 public class ScrollbarMouseWheelTest extends Applet implements
  14  MouseWheelListener, WindowListener
  15  {





  16      final static  String tk = Toolkit.getDefaultToolkit().getClass().getName();
  17      final static int REPS = 5;
  18      // There is a bug on Windows: 4616935.
  19      // Wheel events comes to every component in the hierarchy so we should
  20      // check a platform.
  21      // There are two scrollbars within one Panel and both accept 5 clicks, so
  22      // Panel would accept 5*2 clicks on Windows.
  23      final static int PANEL_REPS = tk.equals("sun.awt.windows.WToolkit")? REPS * 2: REPS;
  24 
  25     static boolean testDone = false;
  26 
  27     Frame frame;
  28     Scrollbar sb1;
  29     Scrollbar sb2;
  30     Panel pnl;
  31     class Sema {
  32         boolean flag;
  33         boolean getVal() { return flag;}
  34         void setVal(boolean b) { flag = b;}
  35     }
  36     Sema sema = new Sema();
  37 
  38     Robot robot;
  39 
  40     int sb1upevents, sb2upevents, pnlupevents;
  41     int sb1downevents, sb2downevents, pnldownevents;
  42 
  43    public void init()
  44     {
  45       //Create instructions for the user here, as well as set up
  46       // the environment -- set the layout manager, add buttons,
  47       // etc.
  48 
  49       this.setLayout (new BorderLayout ());
  50 
  51       String[] instructions =
  52        {
  53          "This is an AUTOMATIC test",
  54          "simply wait until it is done"
  55        };
  56       Sysout.createDialog( );
  57       Sysout.printInstructions( instructions );
  58 
  59     }//End  init()
  60 
  61    public void start ()
  62    {
  63 
  64        //Get things going.  Request focus, set size, et cetera
  65        setSize (200,200);
  66        show();
  67 
  68        //What would normally go into main() will probably go here.
  69        //Use System.out.println for diagnostic messages that you want
  70        //to read after the test is done.
  71        //Use Sysout.println for messages you want the tester to read.
  72 

  73        // Move mouse to upper-right area
  74        try {
  75            robot = new Robot();
  76        } catch (AWTException e) {
  77            System.out.println("Problem creating Robot.  FAIL.");
  78            throw new RuntimeException("Problem creating Robot.  FAIL.");
  79 
  80        }
  81 
  82        robot.setAutoDelay(500);
  83        robot.setAutoWaitForIdle(true);
  84        robot.mouseMove(150, 200);
  85 
  86        // Show test Frame
  87        Frame frame = new Frame("ScrollbarMouseWheelTest");
  88        frame.addWindowListener(this);
  89        pnl = new Panel();
  90        pnl.setLayout(new GridLayout(1,2));
  91        pnl.addMouseWheelListener(this);
  92        sb1 = new Scrollbar();
  93        sb1.addMouseWheelListener(this);
  94        pnl.add(sb1);
  95        sb2 = new Scrollbar();
  96        pnl.add(sb2);
  97        frame.add(pnl);
  98        frame.setSize(200, 400);

  99        frame.setVisible(true);
 100        frame.toFront();
 101 
 102        // When Frame is active, start testing (handled in windowActivated())
 103        while (true) {
 104            synchronized (sema) {
 105                if (sema.getVal()) {
 106                    break;
 107                }
 108            }
 109        }
 110        doTest();
 111 
 112        Sysout.println("Test done.");







 113        if (sb1upevents == REPS &&
 114                sb2upevents == 0 &&
 115                pnlupevents == PANEL_REPS &&
 116                sb1downevents == REPS &&
 117                sb2downevents == 0 &&
 118                pnldownevents == PANEL_REPS) {
 119            Sysout.println("PASSED.");
 120        } else {
 121            System.out.println("Test Failed:" +
 122                               "\n\tsb1upevents =" + sb1upevents +
 123                               "\n\tsb2upevents = " + sb2upevents +
 124                               "\n\tpnlupevents = " + pnlupevents +
 125                               "\n\tsb1downevents =" + sb1downevents +
 126                               "\n\tsb2downevents = " + sb2downevents +
 127                               "\n\tpnldownevents = " + pnldownevents);
 128            throw new RuntimeException("Test FAILED.");
 129        }
 130    }// start()
 131 
 132     public void doTest() {
 133         if (!testDone) {
 134         // up on sb1
 135             testComp(sb1, true);
 136         // down on sb1
 137             testComp(sb1, false);
 138         // up on sb2
 139             testComp(sb2, true);
 140         // down on sb2
 141             testComp(sb2, false);
 142         }
 143         else {
 144             Sysout.println("Skipping test - already done");
 145         }
 146     }
 147 
 148     public void testComp(Component comp, boolean up) {
 149         int loop;
 150         Rectangle bounds;
 151 
 152         bounds = comp.getBounds();
 153         robot.mouseMove(bounds.x + bounds.width / 2,
 154                         bounds.y + bounds.height / 2);
 155         for (loop = 0; loop < REPS; loop++) {
 156             Sysout.println("Robot.mouseWheel() on " + comp.getName());
 157             robot.mouseWheel(up ? -1 : 1);
 158         }
 159     }
 160 
 161     public void mouseWheelMoved(MouseWheelEvent mwe) {
 162         Component src = mwe.getComponent();
 163         Sysout.println("mouseWheelMoved() on " + src.getName());
 164         if (mwe.getWheelRotation() == -1) {
 165             if (src == sb1) {
 166                 sb1upevents++;
 167             } else if (src == sb2) {
 168                 sb2upevents++;
 169             } else if (src == pnl) {
 170                 pnlupevents++;
 171             } else {
 172                 System.out.println("weird source component");
 173             }
 174         } else if (mwe.getWheelRotation() == 1) {
 175             if (src == sb1) {
 176                 sb1downevents++;
 177             } else if (src == sb2) {
 178                 sb2downevents++;
 179             } else if (src == pnl) {
 180                 pnldownevents++;
 181             } else {
 182                 System.out.println("weird source component");
 183             }
 184         } else {
 185             System.out.println("weird wheel rotation");
 186         }
 187     }
 188 
 189     public void windowActivated(WindowEvent we) {
 190         synchronized (sema) {
 191             sema.setVal(true);
 192         }
 193     }
 194 
 195     public void windowClosed(WindowEvent we) {}
 196     public void windowClosing(WindowEvent we) {}
 197     public void windowDeactivated(WindowEvent we) {}
 198     public void windowDeiconified(WindowEvent we) {}
 199     public void windowIconified(WindowEvent we) {}
 200     public void windowOpened(WindowEvent we) {}
 201 
 202 
 203 
 204  }// class ScrollbarMouseWheelTest
 205 
 206 
 207 /****************************************************
 208  Standard Test Machinery
 209  DO NOT modify anything below -- it's a standard
 210   chunk of code whose purpose is to make user
 211   interaction uniform, and thereby make it simpler
 212   to read and understand someone else's test.
 213  ****************************************************/
 214 
 215 /**
 216  This is part of the standard test machinery.
 217  It creates a dialog (with the instructions), and is the interface
 218   for sending text messages to the user.
 219  To print the instructions, send an array of strings to Sysout.createDialog
 220   WithInstructions method.  Put one line of instructions per array entry.
 221  To display a message for the tester to see, simply call Sysout.println
 222   with the string to be displayed.
 223  This mimics System.out.println but works within the test harness as well
 224   as standalone.
 225  */
 226 
 227 class Sysout
 228  {
 229    private static TestDialog dialog;
 230 
 231    public static void createDialogWithInstructions( String[] instructions )
 232     {
 233       dialog = new TestDialog( new Frame(), "Instructions" );
 234       dialog.printInstructions( instructions );
 235       dialog.show();
 236     }
 237 
 238    public static void createDialog( )
 239     {
 240       dialog = new TestDialog( new Frame(), "Instructions" );
 241       String[] defInstr = { "Instructions will appear here. ", "" } ;
 242       dialog.printInstructions( defInstr );
 243       dialog.show();
 244     }
 245 
 246 
 247    public static void printInstructions( String[] instructions )
 248     {
 249       dialog.printInstructions( instructions );
 250     }
 251 
 252 
 253    public static void println( String messageIn )
 254     {
 255       dialog.displayMessage( messageIn );
 256     }
 257 
 258  }// Sysout  class
 259 
 260 /**
 261   This is part of the standard test machinery.  It provides a place for the
 262    test instructions to be displayed, and a place for interactive messages
 263    to the user to be displayed.
 264   To have the test instructions displayed, see Sysout.
 265   To have a message to the user be displayed, see Sysout.
 266   Do not call anything in this dialog directly.
 267   */
 268 class TestDialog extends Dialog
 269  {
 270 
 271    TextArea instructionsText;
 272    TextArea messageText;
 273    int maxStringLength = 80;
 274 
 275    //DO NOT call this directly, go through Sysout
 276    public TestDialog( Frame frame, String name )
 277     {
 278       super( frame, name );
 279       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 280       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 281       add( "North", instructionsText );
 282 
 283       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 284       add("South", messageText);
 285 
 286       pack();
 287       setLocation(300,0);
 288 
 289       show();
 290     }// TestDialog()
 291 
 292    //DO NOT call this directly, go through Sysout
 293    public void printInstructions( String[] instructions )
 294     {
 295       //Clear out any current instructions
 296       instructionsText.setText( "" );
 297 
 298       //Go down array of instruction strings
 299 
 300       String printStr, remainingStr;
 301       for( int i=0; i < instructions.length; i++ )
 302        {
 303          //chop up each into pieces maxSringLength long
 304          remainingStr = instructions[ i ];
 305          while( remainingStr.length() > 0 )
 306           {
 307             //if longer than max then chop off first max chars to print
 308             if( remainingStr.length() >= maxStringLength )
 309              {
 310                //Try to chop on a word boundary
 311                int posOfSpace = remainingStr.
 312                   lastIndexOf( ' ', maxStringLength - 1 );
 313 
 314                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 315 
 316                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 317                remainingStr = remainingStr.substring( posOfSpace + 1 );
 318              }
 319             //else just print
 320             else
 321              {
 322                printStr = remainingStr;
 323                remainingStr = "";
 324              }
 325 
 326             instructionsText.append( printStr + "\n" );
 327 
 328           }// while
 329 
 330        }// for
 331 
 332     }//printInstructions()
 333 
 334    //DO NOT call this directly, go through Sysout
 335    public void displayMessage( String messageIn )
 336     {
 337       messageText.append( messageIn + "\n" );
 338     }
 339 
 340  }// TestDialog  class
   1 /*
   2  * Copyright (c) 1998, 2015, 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 import java.awt.*;
  25 import java.awt.event.*;
  26 
  27 /**
  28  * @test
  29  * @bug 4449139
  30  * @summary test MouseWheelEvent generation by Scrollbar component
  31  */
  32 public final class ScrollbarMouseWheelTest
  33         implements MouseWheelListener, WindowListener {
  34 
  35     final static String tk = Toolkit.getDefaultToolkit().getClass().getName();
  36     final static int REPS = 5;
  37     // There is a bug on Windows: 4616935.
  38     // Wheel events comes to every component in the hierarchy so we should
  39     // check a platform.
  40     // There are two scrollbars within one Panel and both accept 5 clicks, so
  41     // Panel would accept 5*2 clicks on Windows.
  42     final static int PANEL_REPS = tk.equals("sun.awt.windows.WToolkit")? REPS * 2: REPS;
  43 



  44     Scrollbar sb1;
  45     Scrollbar sb2;
  46     Panel pnl;
  47     class Sema {
  48         boolean flag;
  49         boolean getVal() { return flag;}
  50         void setVal(boolean b) { flag = b;}
  51     }
  52     Sema sema = new Sema();
  53 
  54     Robot robot;
  55 
  56     int sb1upevents, sb2upevents, pnlupevents;
  57     int sb1downevents, sb2downevents, pnldownevents;
  58 
  59     public static void main(final String[] args) {
  60         new ScrollbarMouseWheelTest().test();
  61     }


























  62 
  63     public void test() {
  64         // Move mouse to upper-right area
  65         try {
  66             robot = new Robot();
  67         } catch (AWTException e) {
  68             System.out.println("Problem creating Robot.  FAIL.");
  69             throw new RuntimeException("Problem creating Robot.  FAIL.");
  70 
  71         }
  72 
  73         robot.setAutoDelay(500);
  74         robot.setAutoWaitForIdle(true);

  75 
  76         // Show test Frame
  77         Frame frame = new Frame("ScrollbarMouseWheelTest");
  78         frame.addWindowListener(this);
  79         pnl = new Panel();
  80         pnl.setLayout(new GridLayout(1, 2));
  81         pnl.addMouseWheelListener(this);
  82         sb1 = new Scrollbar();
  83         sb1.addMouseWheelListener(this);
  84         pnl.add(sb1);
  85         sb2 = new Scrollbar();
  86         pnl.add(sb2);
  87         frame.add(pnl);
  88         frame.setSize(200, 400);
  89         frame.setLocationRelativeTo(null);
  90         frame.setVisible(true);
  91         frame.toFront();
  92 
  93         // When Frame is active, start testing (handled in windowActivated())
  94         while (true) {
  95             synchronized (sema) {
  96                 if (sema.getVal()) {
  97                     break;
  98                 }
  99             }
 100         }
 101         // up on sb1
 102         testComp(sb1, true);
 103         // down on sb1
 104         testComp(sb1, false);
 105         // up on sb2
 106         testComp(sb2, true);
 107         // down on sb2
 108         testComp(sb2, false);
 109         frame.dispose();
 110         System.out.println("Test done.");
 111         if (sb1upevents == REPS &&
 112                 sb2upevents == 0 &&
 113                 pnlupevents == PANEL_REPS &&
 114                 sb1downevents == REPS &&
 115                 sb2downevents == 0 &&
 116                 pnldownevents == PANEL_REPS) {
 117             System.out.println("PASSED.");
 118         } else {
 119             System.out.println("Test Failed:" +
 120                                        "\n\tsb1upevents =" + sb1upevents +
 121                                        "\n\tsb2upevents = " + sb2upevents +
 122                                        "\n\tpnlupevents = " + pnlupevents +
 123                                        "\n\tsb1downevents =" + sb1downevents +
 124                                        "\n\tsb2downevents = " + sb2downevents +
 125                                        "\n\tpnldownevents = " + pnldownevents);
 126             throw new RuntimeException("Test FAILED.");
 127         }
















 128     }
 129 
 130     public void testComp(Component comp, boolean up) {
 131         Point loc = comp.getLocationOnScreen();
 132         robot.mouseMove(loc.x + comp.getWidth() / 2,
 133                         loc.y + comp.getHeight() / 2);
 134         for (int loop = 0; loop < REPS; loop++) {
 135             System.out.println("Robot.mouseWheel() on " + comp.getName());



 136             robot.mouseWheel(up ? -1 : 1);
 137         }
 138     }
 139 
 140     public void mouseWheelMoved(MouseWheelEvent mwe) {
 141         Component src = mwe.getComponent();
 142         System.out.println("mouseWheelMoved() on " + src.getName());
 143         if (mwe.getWheelRotation() == -1) {
 144             if (src == sb1) {
 145                 sb1upevents++;
 146             } else if (src == sb2) {
 147                 sb2upevents++;
 148             } else if (src == pnl) {
 149                 pnlupevents++;
 150             } else {
 151                 System.out.println("weird source component");
 152             }
 153         } else if (mwe.getWheelRotation() == 1) {
 154             if (src == sb1) {
 155                 sb1downevents++;
 156             } else if (src == sb2) {
 157                 sb2downevents++;
 158             } else if (src == pnl) {
 159                 pnldownevents++;
 160             } else {
 161                 System.out.println("weird source component");
 162             }
 163         } else {
 164             System.out.println("weird wheel rotation");
 165         }
 166     }
 167 
 168     public void windowActivated(WindowEvent we) {
 169         synchronized (sema) {
 170             sema.setVal(true);
 171         }
 172     }
 173 
 174     public void windowClosed(WindowEvent we) {}
 175     public void windowClosing(WindowEvent we) {}
 176     public void windowDeactivated(WindowEvent we) {}
 177     public void windowDeiconified(WindowEvent we) {}
 178     public void windowIconified(WindowEvent we) {}
 179     public void windowOpened(WindowEvent we) {}
 180 }// class ScrollbarMouseWheelTest











































































































































< prev index next >