1 /*
   2  * Copyright (c) 2005, 2006, 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 5039416 6404008 7087869
  27   @summary REGRESSION: Extra mouse click dispatched after press-drag- release sequence.
  28   @library ../../regtesthelpers
  29   @build Util
  30   @author  andrei.dmitriev area=awt.event
  31   @run applet ExtraMouseClick.html
  32  */
  33 
  34 import java.applet.Applet;
  35 import java.awt.AWTException;
  36 import java.awt.BorderLayout;
  37 import java.awt.Frame;
  38 import java.awt.Point;
  39 import java.awt.Robot;
  40 import java.awt.Toolkit;
  41 import java.awt.event.InputEvent;
  42 import java.awt.event.MouseAdapter;
  43 import java.awt.event.MouseEvent;
  44 import java.awt.event.MouseMotionAdapter;
  45 import test.java.awt.regtesthelpers.Util;
  46 
  47 //**
  48 // Here are two values of smugde used in this test (2 and 5). They should be on
  49 // different sides from value 4 (smudge distance on both toolkits).
  50 // Note that this test may not fail easily. But it must always pass on
  51 // patched workspace.
  52 //**
  53 
  54 public class ExtraMouseClick extends Applet
  55 {
  56     Frame frame = new Frame("Extra Click After MouseDrag");
  57     final int TRIALS = 10;
  58     final int SMUDGE_WIDTH = 4;
  59     final int SMUDGE_HEIGHT = 4;
  60     Robot robot;
  61     Point fp; //frame's location on screen
  62     boolean dragged = false;
  63     boolean clicked = false;
  64     boolean pressed = false;
  65     boolean released = false;
  66 
  67     public void init()
  68     {
  69         this.setLayout (new BorderLayout ());
  70 
  71         frame.addMouseListener(new MouseAdapter() {
  72                 public void mousePressed(MouseEvent e) {
  73                     System.out.println("MousePressed");
  74                     pressed = true;
  75                 }
  76 
  77                 public void mouseReleased(MouseEvent e) {
  78                     System.out.println("MouseReleased");
  79                     released = true;
  80                 }
  81 
  82                 public void mouseClicked(MouseEvent e) {
  83                     System.out.println("MouseClicked!!!!");
  84                     clicked = true;
  85                 }
  86             });
  87         frame.addMouseMotionListener(new MouseMotionAdapter() {
  88                 public void mouseDragged(MouseEvent e) {
  89                     System.out.println("MouseDragged--"+e);
  90                     dragged = true;
  91                 }
  92                 public void mouseMoved(MouseEvent e) {
  93                 }
  94             });
  95 
  96     }//End  init()
  97 
  98 
  99     public void start ()
 100     {
 101         frame.setSize(480, 300);
 102         frame.setVisible(true);
 103         try{
 104             robot = new Robot();
 105         }catch(AWTException e){
 106             throw new RuntimeException(e);
 107         }
 108 
 109         Util.waitForIdle(robot);  //a time to show Frame
 110 
 111         fp = frame.getLocationOnScreen();
 112 
 113         for (int i = 0; i< TRIALS; i++){
 114             checkClicked();
 115             clearFlags();
 116         }
 117 
 118         for (int i = 0; i< TRIALS; i++){
 119             oneDrag(2);
 120             clearFlags();
 121         }
 122 
 123         for (int i = 0; i< TRIALS; i++){
 124             oneDrag(5);
 125             clearFlags();
 126         }
 127 
 128         for (int i = 0; i< TRIALS; i++){
 129             oneDrag(70);
 130             clearFlags();
 131         }
 132 
 133         //Check that no Drag event occur in the SMUDGE area
 134         String sToolkit = Toolkit.getDefaultToolkit().getClass().getName();
 135         System.out.println("Toolkit == "+sToolkit);
 136         if ("sun.awt.windows.WToolkit".equals(sToolkit)){
 137             int dragWidth = ((Integer)Toolkit.getDefaultToolkit().
 138                              getDesktopProperty("win.drag.width")).intValue();
 139             int dragHeight = ((Integer)Toolkit.getDefaultToolkit().
 140                             getDesktopProperty("win.drag.height")).intValue();
 141             System.out.println("dragWidth=="+dragWidth+":: dragHeight=="+dragHeight);
 142             // DragWidth and dragHeight may be equal to 1. In that case the SMUDGE rectangle
 143             // narrowed into 1x1 pixel and we can't drag a mouse in it.
 144             // In that case we may skip following testcase but I'd prefer if we move mouse on 1 pixel only.
 145             // And that should pass as well.
 146             dragWidth = dragWidth > 1? dragWidth/2:1;
 147             dragHeight = dragHeight > 1? dragHeight/2:1;
 148             for (int i = 0; i< TRIALS; i++){
 149                 smallWin32Drag(dragWidth, dragHeight);
 150                 clearFlags();
 151             }
 152         } else if ("sun.lwawt.macosx.LWCToolkit".equals(sToolkit)) {
 153             // On MacOS X every mouse move event is MOUSE_DRAGGED event and
 154             // MOUSE_DRAGGED is sent back form the Native code to the java code
 155             // for every mouse move. Therefore 'smallDrag test should be
 156             // disabled for toolkit 'sun.lwawt.macosx.LWCToolkit'.
 157         } else {
 158             for (int i = 0; i< TRIALS; i++){
 159                 smallDrag(SMUDGE_WIDTH - 1, SMUDGE_HEIGHT - 1); //on Motif and XAWT SMUDGE area is 4-pixels wide
 160                 clearFlags();
 161             }
 162         }
 163         System.out.println("Test passed.");
 164     }// start()
 165 
 166     public void oneDrag(int pixels){
 167         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 168         //drag for a short distance
 169         robot.mousePress(InputEvent.BUTTON1_MASK );
 170         for (int i = 1; i<pixels;i++){
 171             robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );
 172         }
 173         robot.waitForIdle();
 174         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 175         robot.waitForIdle();
 176 
 177         if (dragged && clicked){
 178             throw new RuntimeException("Test failed. Clicked event follows by Dragged. Dragged = "+dragged +". Clicked = "+clicked + " : distance = "+pixels);
 179         }
 180     }
 181 
 182   public void smallDrag(int pixelsX, int pixelsY){
 183         // by the X-axis
 184         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 185         //drag for a short distance
 186         robot.mousePress(InputEvent.BUTTON1_MASK );
 187         for (int i = 1; i<pixelsX;i++){
 188             robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );
 189         }
 190         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 191         robot.delay(1000);
 192         if (dragged){
 193             throw new RuntimeException("Test failed. Dragged event (by the X-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);
 194         }
 195 
 196         // the same with Y-axis
 197         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 198         robot.mousePress(InputEvent.BUTTON1_MASK );
 199         for (int i = 1; i<pixelsY;i++){
 200             robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );
 201         }
 202         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 203         robot.delay(1000);
 204         if (dragged){
 205             throw new RuntimeException("Test failed. Dragged event (by the Y-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);
 206         }
 207 
 208     }
 209 
 210     // The difference between X-system and Win32: on Win32 Dragged event start to be generated after any mouse drag.
 211     // On X-systems Dragged event first fired when mouse has left the SMUDGE area
 212     public void smallWin32Drag(int pixelsX, int pixelsY){
 213         // by the X-axis
 214         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 215         //drag for a short distance
 216         robot.mousePress(InputEvent.BUTTON1_MASK );
 217         System.out.println(" pixelsX = "+ pixelsX +" pixelsY = " +pixelsY);
 218         for (int i = 1; i<=pixelsX;i++){
 219             System.out.println("Moving a mouse by X");
 220             robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );
 221         }
 222         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 223         robot.delay(1000);
 224         if (!dragged){
 225             throw new RuntimeException("Test failed. Dragged event (by the X-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);
 226         }
 227 
 228         // the same with Y-axis
 229         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 230         robot.mousePress(InputEvent.BUTTON1_MASK );
 231         for (int i = 1; i<=pixelsY;i++){
 232             System.out.println("Moving a mouse by Y");
 233             robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );
 234         }
 235         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 236         robot.delay(1000);
 237         if (!dragged){
 238             throw new RuntimeException("Test failed. Dragged event (by the Y-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);
 239         }
 240     }
 241 
 242     public void checkClicked(){
 243         robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );
 244         robot.mousePress(InputEvent.BUTTON1_MASK );
 245         robot.delay(10);
 246         robot.mouseRelease(InputEvent.BUTTON1_MASK );
 247         robot.delay(1000);
 248         if (!clicked || !pressed || !released || dragged){
 249             throw new RuntimeException("Test failed. Some of Pressed/Released/Clicked events are missed or dragged occured. Pressed/Released/Clicked/Dragged = "+pressed + ":"+released+":"+clicked +":" +dragged);
 250         }
 251     }
 252 
 253     public void clearFlags(){
 254         clicked = false;
 255         pressed = false;
 256         released = false;
 257         dragged = false;
 258     }
 259 }// class