Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/Mouse/MouseModifiersUnitTest/ExtraButtonDrag.java
48795 views
/*1* Copyright (c) 2008, 2013, 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@test %I% %E%25@bug 631571726@summary verifies that drag events are coming for every button if the property is set to true27@author Andrei Dmitriev : area=awt.mouse28@run main ExtraButtonDrag29*/3031//events from standard should also come3233import java.awt.*;34import java.awt.event.*;3536public class ExtraButtonDrag extends Frame {37static String tk = Toolkit.getDefaultToolkit().getClass().getName();38static Robot robot;39static int [] buttonsPressed;40static int [] buttonsReleased;41static int [] buttonsClicked;42volatile static boolean dragged = false;43volatile static boolean moved = false;4445public ExtraButtonDrag(){46super("ExtraButtonDrag");47}4849public static void main(String []s){50Frame frame = new ExtraButtonDrag();5152MouseAdapter ma = new MouseAdapter() {53public void mouseDragged(MouseEvent e) {54System.out.println("Dragged "+e);// +" : "+ e.getButton() + " : " +e.getButtonState(e.getButton()));55dragged = true;56}57public void mouseMoved(MouseEvent e) {58System.out.println("Moved "+e);59moved = true;60}61public void mousePressed(MouseEvent e) {62System.out.println(">>> "+e);63}64public void mouseReleased(MouseEvent e) {65System.out.println(">>> "+e);66}6768};6970frame.addMouseMotionListener(ma);71frame.addMouseListener(ma);7273frame.setSize(300, 300);74frame.setVisible(true);7576int [] buttonMask = new int [MouseInfo.getNumberOfButtons()]; //InputEvent.getButtonMasks();7778for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){79buttonMask[i] = InputEvent.getMaskForButton(i+1);80// System.out.println("TEST: "+tmp[i]);81}8283try {84robot = new Robot();85robot.delay(1000);86Point centerFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);87Point outboundsFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()*3/2, frame.getLocationOnScreen().y + frame.getHeight()/2);8889System.out.println("areExtraMouseButtonsEnabled() == " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() );9091for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){92System.out.println("button to drag = " +(i+1) + " : value passed to robot = " +buttonMask[i]);9394try {95dragMouse(buttonMask[i], centerFrame.x, centerFrame.y, outboundsFrame.x, outboundsFrame.y);96} catch (IllegalArgumentException e){97throw new RuntimeException("Test failed. Exception occured.", e);98}99100robot.delay(500);101//this is a choice-case for X protocol issue: native events from extra buttons doesn't contain102// the correct state so it's unable to decide if there is a drag or move. By default we send MOVED event.103//XToolkit: extra buttons should report MOVED events only104//WToolkit: extra buttons should report DRAGGED events only105if (i > 2){ //extra buttons only106if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {107if (!moved || dragged) {108throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);109}110} else { //WToolkit111if (moved || !dragged) {112throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);113}114}115} else {116if (moved || !dragged){117throw new RuntimeException("Test failed. Button = " +(i+1) + " not dragged.");118}119}120}121} catch (Exception e){122throw new RuntimeException("", e);123}124}125126public static void dragMouse(int button, int x0, int y0, int x1, int y1){127int curX = x0;128int curY = y0;129int dx = x0 < x1 ? 1 : -1;130int dy = y0 < y1 ? 1 : -1;131robot.mouseMove(x0, y0);132133robot.delay(200);134dragged = false;135moved = false;136137robot.mousePress(button);138139while (curX != x1){140curX += dx;141robot.mouseMove(curX, curY);142robot.delay(5);143}144while (curY != y1 ){145curY += dy;146robot.mouseMove(curX, curY);147robot.delay(5);148}149robot.mouseRelease(button);150}151152}153154155156