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