Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/Mouse/ExtraMouseClick/ExtraMouseClick.java
48803 views
/*1* Copyright (c) 2005, 2006, 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/*24test25@bug 5039416 640400826@summary REGRESSION: Extra mouse click dispatched after press-drag- release sequence.27@library ../../regtesthelpers28@build Util29@author andrei.dmitriev area=awt.event30@run applet ExtraMouseClick.html31*/3233import java.applet.Applet;34import java.awt.*;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738//**39// Here are two values of smugde used in this test (2 and 5). They should be on40// different sides from value 4 (smudge distance on both toolkits).41// Note that this test may not fail easily. But it must always pass on42// patched workspace.43//**4445public class ExtraMouseClick extends Applet46{47Frame frame = new Frame("Extra Click After MouseDrag");48final int TRIALS = 10;49final int SMUDGE_WIDTH = 4;50final int SMUDGE_HEIGHT = 4;51Robot robot;52Point fp; //frame's location on screen53boolean dragged = false;54boolean clicked = false;55boolean pressed = false;56boolean released = false;5758public void init()59{60this.setLayout (new BorderLayout ());6162frame.addMouseListener(new MouseAdapter() {63public void mousePressed(MouseEvent e) {64System.out.println("MousePressed");65pressed = true;66}6768public void mouseReleased(MouseEvent e) {69System.out.println("MouseReleased");70released = true;71}7273public void mouseClicked(MouseEvent e) {74System.out.println("MouseClicked!!!!");75clicked = true;76}77});78frame.addMouseMotionListener(new MouseMotionAdapter() {79public void mouseDragged(MouseEvent e) {80System.out.println("MouseDragged--"+e);81dragged = true;82}83public void mouseMoved(MouseEvent e) {84}85});8687}//End init()888990public void start ()91{92frame.setSize(480, 300);93frame.setVisible(true);94try{95robot = new Robot();96}catch(AWTException e){97throw new RuntimeException(e);98}99100Util.waitForIdle(robot); //a time to show Frame101102fp = frame.getLocationOnScreen();103104for (int i = 0; i< TRIALS; i++){105checkClicked();106clearFlags();107}108109for (int i = 0; i< TRIALS; i++){110oneDrag(2);111clearFlags();112}113114for (int i = 0; i< TRIALS; i++){115oneDrag(5);116clearFlags();117}118119for (int i = 0; i< TRIALS; i++){120oneDrag(70);121clearFlags();122}123124//Check that no Drag event occur in the SMUDGE area125String sToolkit = Toolkit.getDefaultToolkit().getClass().getName();126System.out.println("Toolkit == "+sToolkit);127if ("sun.awt.windows.WToolkit".equals(sToolkit)){128int dragWidth = ((Integer)Toolkit.getDefaultToolkit().129getDesktopProperty("win.drag.width")).intValue();130int dragHeight = ((Integer)Toolkit.getDefaultToolkit().131getDesktopProperty("win.drag.height")).intValue();132System.out.println("dragWidth=="+dragWidth+":: dragHeight=="+dragHeight);133// DragWidth and dragHeight may be equal to 1. In that case the SMUDGE rectangle134// narrowed into 1x1 pixel and we can't drag a mouse in it.135// In that case we may skip following testcase but I'd prefer if we move mouse on 1 pixel only.136// And that should pass as well.137dragWidth = dragWidth > 1? dragWidth/2:1;138dragHeight = dragHeight > 1? dragHeight/2:1;139for (int i = 0; i< TRIALS; i++){140smallWin32Drag(dragWidth, dragHeight);141clearFlags();142}143}else{144for (int i = 0; i< TRIALS; i++){145smallDrag(SMUDGE_WIDTH - 1, SMUDGE_HEIGHT - 1); //on Motif and XAWT SMUDGE area is 4-pixels wide146clearFlags();147}148}149System.out.println("Test passed.");150}// start()151152public void oneDrag(int pixels){153robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );154//drag for a short distance155robot.mousePress(InputEvent.BUTTON1_MASK );156for (int i = 1; i<pixels;i++){157robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );158}159robot.mouseRelease(InputEvent.BUTTON1_MASK );160robot.delay(1000);161if (dragged && clicked){162throw new RuntimeException("Test failed. Clicked event follows by Dragged. Dragged = "+dragged +". Clicked = "+clicked + " : distance = "+pixels);163}164}165166public void smallDrag(int pixelsX, int pixelsY){167// by the X-axis168robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );169//drag for a short distance170robot.mousePress(InputEvent.BUTTON1_MASK );171for (int i = 1; i<pixelsX;i++){172robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );173}174robot.mouseRelease(InputEvent.BUTTON1_MASK );175robot.delay(1000);176if (dragged){177throw new RuntimeException("Test failed. Dragged event (by the X-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);178}179180// the same with Y-axis181robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );182robot.mousePress(InputEvent.BUTTON1_MASK );183for (int i = 1; i<pixelsY;i++){184robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );185}186robot.mouseRelease(InputEvent.BUTTON1_MASK );187robot.delay(1000);188if (dragged){189throw new RuntimeException("Test failed. Dragged event (by the Y-axis) occured in SMUDGE area. Dragged = "+dragged +". Clicked = "+clicked);190}191192}193194// The difference between X-system and Win32: on Win32 Dragged event start to be generated after any mouse drag.195// On X-systems Dragged event first fired when mouse has left the SMUDGE area196public void smallWin32Drag(int pixelsX, int pixelsY){197// by the X-axis198robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );199//drag for a short distance200robot.mousePress(InputEvent.BUTTON1_MASK );201System.out.println(" pixelsX = "+ pixelsX +" pixelsY = " +pixelsY);202for (int i = 1; i<=pixelsX;i++){203System.out.println("Moving a mouse by X");204robot.mouseMove(fp.x + frame.getWidth()/2 + i, fp.y + frame.getHeight()/2 );205}206robot.mouseRelease(InputEvent.BUTTON1_MASK );207robot.delay(1000);208if (!dragged){209throw new RuntimeException("Test failed. Dragged event (by the X-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);210}211212// the same with Y-axis213robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );214robot.mousePress(InputEvent.BUTTON1_MASK );215for (int i = 1; i<=pixelsY;i++){216System.out.println("Moving a mouse by Y");217robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 + i );218}219robot.mouseRelease(InputEvent.BUTTON1_MASK );220robot.delay(1000);221if (!dragged){222throw new RuntimeException("Test failed. Dragged event (by the Y-axis) didn't occur in the SMUDGE area. Dragged = "+dragged);223}224}225226public void checkClicked(){227robot.mouseMove(fp.x + frame.getWidth()/2, fp.y + frame.getHeight()/2 );228robot.mousePress(InputEvent.BUTTON1_MASK );229robot.delay(10);230robot.mouseRelease(InputEvent.BUTTON1_MASK );231robot.delay(1000);232if (!clicked || !pressed || !released || dragged){233throw new RuntimeException("Test failed. Some of Pressed/Released/Clicked events are missed or dragged occured. Pressed/Released/Clicked/Dragged = "+pressed + ":"+released+":"+clicked +":" +dragged);234}235}236237public void clearFlags(){238clicked = false;239pressed = false;240released = false;241dragged = false;242}243}// class244245246