Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Robot/AcceptExtraMouseButtons/AcceptExtraMouseButtons.java
38828 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 Robot is accepting extra mouse buttons27@author Andrei Dmitriev : area=awt.mouse28@library ../../regtesthelpers29@build Util30@run main AcceptExtraMouseButtons31*/3233//if we do robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) the test must34// 1) accept it (i.e. don't throw an IllegalArgumentException35// 2) actually post a MouseEvent36// Also, Robot should still accept InputEvent.BUTTONx_MASKs3738import java.awt.*;39import java.awt.event.*;40import sun.awt.SunToolkit;41import test.java.awt.regtesthelpers.Util;4243public class AcceptExtraMouseButtons extends Frame {44static String tk = Toolkit.getDefaultToolkit().getClass().getName();45static Robot robot;46static int [] standardButtonMasks = {InputEvent.BUTTON1_MASK,47InputEvent.BUTTON2_MASK,48InputEvent.BUTTON3_MASK};49static int [] buttonsPressed;50static int [] buttonsReleased;51static int [] buttonsClicked;5253static int buttonsNum = MouseInfo.getNumberOfButtons();5455public static void main(String []s){5657//MouseInfo.getNumberOfButtons() reports two more buttons on XToolkit58//as they reserved for wheel (both directions).59if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {60buttonsNum = buttonsNum - 2;61}62System.out.println("Number Of Buttons = "+ buttonsNum);63if (buttonsNum < 3) {64System.out.println("Linux and Windows systems should emulate three buttons if even there are only 1 or 2 are phsically available. Setting number of buttons to 3.");65buttonsNum = 3;66}6768buttonsPressed = new int [buttonsNum];69buttonsReleased = new int [buttonsNum];70buttonsClicked = new int [buttonsNum];7172AcceptExtraMouseButtons frame = new AcceptExtraMouseButtons();7374MouseAdapter ma1 = new MouseAdapter() {75public void mousePressed(MouseEvent e) {76buttonsPressed[e.getButton() - 1] += 1;77System.out.println("PRESSED "+e);78}79public void mouseReleased(MouseEvent e) {80buttonsReleased[e.getButton() - 1] += 1;81System.out.println("RELEASED "+e);82}83public void mouseClicked(MouseEvent e) {84buttonsClicked[e.getButton() - 1] += 1;85System.out.println("CLICKED "+e);86}87};88frame.addMouseListener(ma1);8990frame.setSize(300, 300);91frame.setVisible(true);9293Util.waitForIdle(robot); //a time to show a Frame9495try {96robot = new Robot();97robot.delay(1000);98robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2,99frame.getLocationOnScreen().y + frame.getHeight()/2);100101//TestCase 1: verify that all BUTTONx_DOWN_MASKs are accepted by the Robot.102103for (int i = 0; i < buttonsNum; i++){104int buttonMask = InputEvent.getMaskForButton(i+1);105System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);106robot.mousePress(buttonMask);107robot.delay(30);108robot.mouseRelease(buttonMask);109Util.waitForIdle(robot);110}111for (int i = 0; i < buttonsNum; i++){112if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {113throw new RuntimeException("TESTCASE 1 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);114}115}116117java.util.Arrays.fill(buttonsPressed, 0);118java.util.Arrays.fill(buttonsReleased, 0);119java.util.Arrays.fill(buttonsClicked, 0);120//TestCase 2: verify that all BUTTONx_MASKs are accepted by the Robot.121for (int i = 0; i < standardButtonMasks.length; i++){122int buttonMask = standardButtonMasks[i];123System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);124robot.mousePress(buttonMask);125robot.delay(30);126robot.mouseRelease(buttonMask);127Util.waitForIdle(robot);128}129for (int i = 0; i < standardButtonMasks.length; i++){130if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {131throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);132}133}134135} catch (Exception e){136e.printStackTrace();137throw new RuntimeException(e);138}139}140}141142143