Path: blob/master/test/jdk/java/awt/Mouse/MouseModifiersUnitTest/ExtraButtonDrag.java
41152 views
/*1* Copyright (c) 2008, 2016, 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 631571727@summary verifies that drag events are coming for every button if the property is set to true28@author Andrei Dmitriev : area=awt.mouse29@run main ExtraButtonDrag30*/3132//events from standard should also come3334import java.awt.*;35import java.awt.event.*;3637public class ExtraButtonDrag extends Frame {38static String tk = Toolkit.getDefaultToolkit().getClass().getName();39static Robot robot;40static int [] buttonsPressed;41static int [] buttonsReleased;42static int [] buttonsClicked;43volatile static boolean dragged = false;44volatile static boolean moved = false;4546public ExtraButtonDrag(){47super("ExtraButtonDrag");48}4950public static void main(String []s){51Frame frame = new ExtraButtonDrag();5253MouseAdapter ma = new MouseAdapter() {54public void mouseDragged(MouseEvent e) {55System.out.println("Dragged "+e);// +" : "+ e.getButton() + " : " +e.getButtonState(e.getButton()));56dragged = true;57}58public void mouseMoved(MouseEvent e) {59System.out.println("Moved "+e);60moved = true;61}62public void mousePressed(MouseEvent e) {63System.out.println(">>> "+e);64}65public void mouseReleased(MouseEvent e) {66System.out.println(">>> "+e);67}6869};7071frame.addMouseMotionListener(ma);72frame.addMouseListener(ma);7374frame.setSize(300, 300);75frame.setVisible(true);7677int [] buttonMask = new int [MouseInfo.getNumberOfButtons()]; //InputEvent.getButtonMasks();7879for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){80buttonMask[i] = InputEvent.getMaskForButton(i+1);81// System.out.println("TEST: "+tmp[i]);82}8384try {85robot = new Robot();86robot.delay(1000);87Point centerFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);88Point outboundsFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()*3/2, frame.getLocationOnScreen().y + frame.getHeight()/2);8990System.out.println("areExtraMouseButtonsEnabled() == " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() );9192for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){93System.out.println("button to drag = " +(i+1) + " : value passed to robot = " +buttonMask[i]);9495try {96dragMouse(buttonMask[i], centerFrame.x, centerFrame.y, outboundsFrame.x, outboundsFrame.y);97} catch (IllegalArgumentException e){98throw new RuntimeException("Test failed. Exception occured.", e);99}100101robot.delay(500);102//this is a choice-case for X protocol issue: native events from extra buttons doesn't contain103// the correct state so it's unable to decide if there is a drag or move. By default we send MOVED event.104//XToolkit: extra buttons should report MOVED events only105//WToolkit: extra buttons should report DRAGGED events only106if (i > 2){ //extra buttons only107if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {108if (!moved || dragged) {109throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);110}111} else { //WToolkit112if (moved || !dragged) {113throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);114}115}116} else {117if (moved || !dragged){118throw new RuntimeException("Test failed. Button = " +(i+1) + " not dragged.");119}120}121}122} catch (Exception e){123throw new RuntimeException("", e);124}125}126127public static void dragMouse(int button, int x0, int y0, int x1, int y1){128int curX = x0;129int curY = y0;130int dx = x0 < x1 ? 1 : -1;131int dy = y0 < y1 ? 1 : -1;132robot.mouseMove(x0, y0);133134robot.delay(200);135dragged = false;136moved = false;137138robot.mousePress(button);139140while (curX != x1){141curX += dx;142robot.mouseMove(curX, curY);143robot.delay(5);144}145while (curY != y1 ){146curY += dy;147robot.mouseMove(curX, curY);148robot.delay(5);149}150robot.mouseRelease(button);151}152153}154155156157