1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test %I% %E%
  26   @key headful
  27   @bug 6315717
  28   @summary verifies that Robot is accepting extra mouse buttons
  29   @author Andrei Dmitriev : area=awt.mouse
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main AcceptExtraMouseButtons
  33  */
  34 
  35 //if we do robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) the test must
  36 // 1) accept it (i.e. don't throw an IllegalArgumentException
  37 // 2) actually post a MouseEvent
  38 // Also, Robot should still accept InputEvent.BUTTONx_MASKs
  39 
  40 import java.awt.*;
  41 import java.awt.event.*;
  42 import sun.awt.SunToolkit;
  43 import test.java.awt.regtesthelpers.Util;
  44 
  45 public class AcceptExtraMouseButtons extends Frame {
  46     static String tk = Toolkit.getDefaultToolkit().getClass().getName();
  47     static Robot robot;
  48     static int [] standardButtonMasks = {InputEvent.BUTTON1_MASK,
  49                                          InputEvent.BUTTON2_MASK,
  50                                          InputEvent.BUTTON3_MASK};
  51     static int [] buttonsPressed;
  52     static int [] buttonsReleased;
  53     static int [] buttonsClicked;
  54 
  55     static int buttonsNum = MouseInfo.getNumberOfButtons();
  56 
  57     public static void main(String []s){
  58 
  59         //MouseInfo.getNumberOfButtons() reports two more buttons on XToolkit
  60         //as they reserved for wheel (both directions).
  61         if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {
  62             buttonsNum = buttonsNum - 2;
  63         }
  64         System.out.println("Number Of Buttons = "+ buttonsNum);
  65         if (buttonsNum < 3) {
  66             System.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.");
  67             buttonsNum = 3;
  68         }
  69 
  70         buttonsPressed = new int [buttonsNum];
  71         buttonsReleased = new int [buttonsNum];
  72         buttonsClicked = new int [buttonsNum];
  73 
  74         AcceptExtraMouseButtons frame = new AcceptExtraMouseButtons();
  75 
  76         MouseAdapter ma1 = new MouseAdapter() {
  77                 public void mousePressed(MouseEvent e) {
  78                     buttonsPressed[e.getButton() - 1] += 1;
  79                     System.out.println("PRESSED "+e);
  80                 }
  81                 public void mouseReleased(MouseEvent e) {
  82                     buttonsReleased[e.getButton() - 1] += 1;
  83                     System.out.println("RELEASED "+e);
  84                 }
  85                 public void mouseClicked(MouseEvent e) {
  86                     buttonsClicked[e.getButton() - 1] += 1;
  87                     System.out.println("CLICKED "+e);
  88                 }
  89             };
  90         frame.addMouseListener(ma1);
  91 
  92         frame.setSize(300, 300);
  93         frame.setVisible(true);
  94 
  95         Util.waitForIdle(robot);  //a time to show a Frame
  96 
  97         try {
  98             robot = new Robot();
  99             robot.delay(1000);
 100             robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2,
 101                             frame.getLocationOnScreen().y + frame.getHeight()/2);
 102 
 103             //TestCase 1: verify that all BUTTONx_DOWN_MASKs are accepted by the Robot.
 104 
 105             for (int i = 0; i < buttonsNum; i++){
 106                 int buttonMask = InputEvent.getMaskForButton(i+1);
 107                 System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
 108                 robot.mousePress(buttonMask);
 109                 robot.delay(30);
 110                 robot.mouseRelease(buttonMask);
 111                 Util.waitForIdle(robot);
 112             }
 113             for (int i = 0; i < buttonsNum; i++){
 114                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 115                     throw new RuntimeException("TESTCASE 1 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
 116                 }
 117             }
 118 
 119             java.util.Arrays.fill(buttonsPressed, 0);
 120             java.util.Arrays.fill(buttonsReleased, 0);
 121             java.util.Arrays.fill(buttonsClicked, 0);
 122             //TestCase 2: verify that all BUTTONx_MASKs are accepted by the Robot.
 123             for (int i = 0; i < standardButtonMasks.length; i++){
 124                 int buttonMask = standardButtonMasks[i];
 125                 System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
 126                 robot.mousePress(buttonMask);
 127                 robot.delay(30);
 128                 robot.mouseRelease(buttonMask);
 129                 Util.waitForIdle(robot);
 130             }
 131             for (int i = 0; i < standardButtonMasks.length; i++){
 132                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 133                     throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
 134                 }
 135             }
 136 
 137         } catch (Exception e){
 138             e.printStackTrace();
 139             throw new RuntimeException(e);
 140         }
 141     }
 142 }