Path: blob/master/test/jdk/java/awt/Mouse/ExtraMouseClick/ExtraMouseClick.java
41153 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 5039416 640400827@summary REGRESSION: Extra mouse click dispatched after press-drag- release sequence.28@library ../../regtesthelpers29@build Util30@run main ExtraMouseClick31*/3233import java.awt.AWTException;34import java.awt.Frame;35import java.awt.Point;36import java.awt.Robot;37import java.awt.Toolkit;38import java.awt.event.InputEvent;39import java.awt.event.MouseAdapter;40import java.awt.event.MouseEvent;41import java.awt.event.MouseMotionAdapter;42import test.java.awt.regtesthelpers.Util;4344//**45// Here are two values of smugde used in this test (2 and 5). They should be on46// different sides from value 4 (smudge distance on both toolkits).47// Note that this test may not fail easily. But it must always pass on48// patched workspace.49//**50public class ExtraMouseClick {51Frame frame = new Frame("Extra Click After MouseDrag");52final int TRIALS = 10;53final int SMUDGE_WIDTH = 4;54final int SMUDGE_HEIGHT = 4;55Robot robot;56Point fp; //frame's location on screen57boolean dragged = false;58boolean clicked = false;59boolean pressed = false;60boolean released = false;6162public static void main(final String[] args) {63ExtraMouseClick app = new ExtraMouseClick();64app.init();65app.start();66}6768public void init()69{70frame.addMouseListener(new MouseAdapter() {71public void mousePressed(MouseEvent e) {72System.out.println("MousePressed");73pressed = true;74}7576public void mouseReleased(MouseEvent e) {77System.out.println("MouseReleased");78released = true;79}8081public void mouseClicked(MouseEvent e) {82System.out.println("MouseClicked!!!!");83clicked = true;84}85});86frame.addMouseMotionListener(new MouseMotionAdapter() {87public void mouseDragged(MouseEvent e) {88System.out.println("MouseDragged--"+e);89dragged = true;90}91public void mouseMoved(MouseEvent e) {92}93});9495}//End init()969798public void start ()99{100frame.setSize(480, 300);101frame.setLocationRelativeTo(null);102frame.setVisible(true);103try{104robot = new Robot();105}catch(AWTException e){106throw new RuntimeException(e);107}108109Util.waitForIdle(robot); //a time to show Frame110111fp = frame.getLocationOnScreen();112113for (int i = 0; i< TRIALS; i++){114checkClicked();115clearFlags();116}117118for (int i = 0; i< TRIALS; i++){119oneDrag(2);120clearFlags();121}122123for (int i = 0; i< TRIALS; i++){124oneDrag(5);125clearFlags();126}127128for (int i = 0; i< TRIALS; i++){129oneDrag(70);130clearFlags();131}132133//Check that no Drag event occur in the SMUDGE area134String sToolkit = Toolkit.getDefaultToolkit().getClass().getName();135System.out.println("Toolkit == "+sToolkit);136if ("sun.awt.windows.WToolkit".equals(sToolkit)){137int dragWidth = ((Integer)Toolkit.getDefaultToolkit().138getDesktopProperty("win.drag.width")).intValue();139int dragHeight = ((Integer)Toolkit.getDefaultToolkit().140getDesktopProperty("win.drag.height")).intValue();141System.out.println("dragWidth=="+dragWidth+":: dragHeight=="+dragHeight);142// DragWidth and dragHeight may be equal to 1. In that case the SMUDGE rectangle143// 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.146dragWidth = dragWidth > 1? dragWidth/2:1;147dragHeight = dragHeight > 1? dragHeight/2:1;148for (int i = 0; i< TRIALS; i++){149smallWin32Drag(dragWidth, dragHeight);150clearFlags();151}152} else if ("sun.lwawt.macosx.LWCToolkit".equals(sToolkit)) {153// On MacOS X every mouse move event is MOUSE_DRAGGED event and154// MOUSE_DRAGGED is sent back form the Native code to the java code155// for every mouse move. Therefore 'smallDrag test should be156// disabled for toolkit 'sun.lwawt.macosx.LWCToolkit'.157} else {158for (int i = 0; i< TRIALS; i++){159smallDrag(SMUDGE_WIDTH - 1, SMUDGE_HEIGHT - 1); //on Motif and XAWT SMUDGE area is 4-pixels wide160clearFlags();161}162}163System.out.println("Test passed.");164}// start()165166public void oneDrag(int pixels){167robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );168//drag for a short distance169robot.mousePress(InputEvent.BUTTON1_MASK );170for (int i = 1; i<pixels;i++){171robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );172}173robot.waitForIdle();174robot.mouseRelease(InputEvent.BUTTON1_MASK );175robot.waitForIdle();176177if (dragged && clicked){178throw new RuntimeException("Test failed. Clicked event follows by Dragged. Dragged = "+dragged +". Clicked = "+clicked + " : distance = "+pixels);179}180}181182public void smallDrag(int pixelsX, int pixelsY){183// by the X-axis184robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );185//drag for a short distance186robot.mousePress(InputEvent.BUTTON1_MASK );187for (int i = 1; i<pixelsX;i++){188robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );189}190robot.mouseRelease(InputEvent.BUTTON1_MASK );191robot.delay(1000);192if (dragged){193throw new RuntimeException("Test failed. Dragged event (by the X-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);194}195196// the same with Y-axis197robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );198robot.mousePress(InputEvent.BUTTON1_MASK );199for (int i = 1; i<pixelsY;i++){200robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );201}202robot.mouseRelease(InputEvent.BUTTON1_MASK );203robot.delay(1000);204if (dragged){205throw new RuntimeException("Test failed. Dragged event (by the Y-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);206}207208}209210// 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 area212public void smallWin32Drag(int pixelsX, int pixelsY){213// by the X-axis214robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );215//drag for a short distance216robot.mousePress(InputEvent.BUTTON1_MASK );217System.out.println(" pixelsX = "+ pixelsX +" pixelsY = " +pixelsY);218for (int i = 1; i<=pixelsX;i++){219System.out.println("Moving a mouse by X");220robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );221}222robot.mouseRelease(InputEvent.BUTTON1_MASK );223robot.delay(1000);224if (!dragged){225throw new RuntimeException("Test failed. Dragged event (by the X-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);226}227228// the same with Y-axis229robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );230robot.mousePress(InputEvent.BUTTON1_MASK );231for (int i = 1; i<=pixelsY;i++){232System.out.println("Moving a mouse by Y");233robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );234}235robot.mouseRelease(InputEvent.BUTTON1_MASK );236robot.delay(1000);237if (!dragged){238throw new RuntimeException("Test failed. Dragged event (by the Y-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);239}240}241242public void checkClicked(){243robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );244robot.mousePress(InputEvent.BUTTON1_MASK );245robot.delay(10);246robot.mouseRelease(InputEvent.BUTTON1_MASK );247robot.delay(1000);248if (!clicked || !pressed || !released || dragged){249throw 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}252253public void clearFlags(){254clicked = false;255pressed = false;256released = false;257dragged = false;258}259}// class260261262