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   @bug 6315717
  27   @summary verifies that Robot is accepting extra mouse buttons
  28   @author Andrei Dmitriev : area=awt.mouse
  29   @library ../../regtesthelpers
  30   @build Util
  31   @run main AcceptExtraMouseButtons
  32  */
  33 
  34 //if we do robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) the test must
  35 // 1) accept it (i.e. don't throw an IllegalArgumentException
  36 // 2) actually post a MouseEvent
  37 // Also, Robot should still accept InputEvent.BUTTONx_MASKs
  38 
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 import test.java.awt.regtesthelpers.Util;
  42 
  43 public class AcceptExtraMouseButtons extends Frame {
  44     static String tk = Toolkit.getDefaultToolkit().getClass().getName();
  45     static Robot robot;
  46     static int [] standardButtonMasks = {InputEvent.BUTTON1_MASK,
  47                                          InputEvent.BUTTON2_MASK,
  48                                          InputEvent.BUTTON3_MASK};
  49     static int [] buttonsPressed;
  50     static int [] buttonsReleased;
  51     static int [] buttonsClicked;
  52 
  53     static int buttonsNum = MouseInfo.getNumberOfButtons();
  54 
  55     public static void main(String []s){
  56 
  57         //MouseInfo.getNumberOfButtons() reports two more buttons on XToolkit
  58         //as they reserved for wheel (both directions).
  59         if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {
  60             buttonsNum = buttonsNum - 2;
  61         }
  62         System.out.println("Number Of Buttons = "+ buttonsNum);
  63         if (buttonsNum < 3) {
  64             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.");
  65             buttonsNum = 3;
  66         }
  67 
  68         buttonsPressed = new int [buttonsNum];
  69         buttonsReleased = new int [buttonsNum];
  70         buttonsClicked = new int [buttonsNum];
  71 
  72         AcceptExtraMouseButtons frame = new AcceptExtraMouseButtons();
  73 
  74         MouseAdapter ma1 = new MouseAdapter() {
  75                 public void mousePressed(MouseEvent e) {
  76                     buttonsPressed[e.getButton() - 1] += 1;
  77                     System.out.println("PRESSED "+e);
  78                 }
  79                 public void mouseReleased(MouseEvent e) {
  80                     buttonsReleased[e.getButton() - 1] += 1;
  81                     System.out.println("RELEASED "+e);
  82                 }
  83                 public void mouseClicked(MouseEvent e) {
  84                     buttonsClicked[e.getButton() - 1] += 1;
  85                     System.out.println("CLICKED "+e);
  86                 }
  87             };
  88         frame.addMouseListener(ma1);
  89 
  90         frame.setSize(300, 300);
  91         frame.setVisible(true);
  92 
  93         Util.waitForIdle(robot);  //a time to show a Frame
  94 
  95         try {
  96             robot = new Robot();
  97             robot.delay(1000);
  98             robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2,
  99                             frame.getLocationOnScreen().y + frame.getHeight()/2);
 100 
 101             //TestCase 1: verify that all BUTTONx_DOWN_MASKs are accepted by the Robot.
 102 
 103             for (int i = 0; i < buttonsNum; i++){
 104                 int buttonMask = InputEvent.getMaskForButton(i+1);
 105                 System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
 106                 robot.mousePress(buttonMask);
 107                 robot.delay(30);
 108                 robot.mouseRelease(buttonMask);
 109                 Util.waitForIdle(robot);
 110             }
 111             for (int i = 0; i < buttonsNum; i++){
 112                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 113                     throw new RuntimeException("TESTCASE 1 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
 114                 }
 115             }
 116 
 117             java.util.Arrays.fill(buttonsPressed, 0);
 118             java.util.Arrays.fill(buttonsReleased, 0);
 119             java.util.Arrays.fill(buttonsClicked, 0);
 120             //TestCase 2: verify that all BUTTONx_MASKs are accepted by the Robot.
 121             for (int i = 0; i < standardButtonMasks.length; i++){
 122                 int buttonMask = standardButtonMasks[i];
 123                 System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
 124                 robot.mousePress(buttonMask);
 125                 robot.delay(30);
 126                 robot.mouseRelease(buttonMask);
 127                 Util.waitForIdle(robot);
 128             }
 129             for (int i = 0; i < standardButtonMasks.length; i++){
 130                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 131                     throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
 132                 }
 133             }
 134 
 135         } catch (Exception e){
 136             e.printStackTrace();
 137             throw new RuntimeException(e);
 138         }
 139     }
 140 }