1 /*
   2  * Copyright (c) 2008, 2016, 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 sun.awt.enableExtraMouseButtons = false consumes extra events
  29   @author Andrei Dmitriev : area=awt.mouse
  30   @run main/othervm -Dsun.awt.enableExtraMouseButtons=false ToolkitPropertyTest_Disable
  31  */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 
  36 // Testcase 1: set to FALSE and check
  37 // Testcase 2: set to FALSE and check that extra events are not coming
  38 //                              check that standard events are coming
  39 
  40 public class ToolkitPropertyTest_Disable extends Frame {
  41     static boolean propValue;
  42     static Robot robot;
  43     static int [] buttonsPressed;
  44     static int [] buttonsReleased;
  45     static int [] buttonsClicked;
  46 
  47     static boolean lessThenFourButtons;
  48 
  49     public static void main(String []s){
  50         propValue = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons"));
  51         buttonsPressed = new int [MouseInfo.getNumberOfButtons()];
  52         buttonsReleased = new int [MouseInfo.getNumberOfButtons()];
  53         buttonsClicked = new int [MouseInfo.getNumberOfButtons()];
  54 
  55         ToolkitPropertyTest_Disable frame = new ToolkitPropertyTest_Disable();
  56         frame.setSize(300, 300);
  57         frame.setVisible(true);
  58 
  59         MouseAdapter ma1 = new MouseAdapter() {
  60                 public void mousePressed(MouseEvent e) {
  61                     buttonsPressed[e.getButton() - 1] += 1;
  62                     System.out.println("PRESSED "+e);
  63                 }
  64                 public void mouseReleased(MouseEvent e) {
  65                     buttonsReleased[e.getButton() - 1] += 1;
  66                     System.out.println("RELEASED "+e);
  67                 }
  68                 public void mouseClicked(MouseEvent e) {
  69                     buttonsClicked[e.getButton() - 1] += 1;
  70                     System.out.println("CLICKED "+e);
  71                 }
  72             };
  73 
  74         try {
  75             robot = new Robot();
  76             robot.delay(1000);
  77             robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);
  78 
  79             System.out.println("Property = " + propValue);
  80             testCase0();
  81 
  82             testCase1();
  83             System.out.println("Number Of Buttons = "+ MouseInfo.getNumberOfButtons());
  84 
  85             lessThenFourButtons = (MouseInfo.getNumberOfButtons() <= 3);
  86             if ( !lessThenFourButtons ) {
  87                 frame.addMouseListener(ma1);
  88                 testCase2();
  89             }
  90         } catch (Exception e){
  91             e.printStackTrace();
  92 //            throw new RuntimeException(e);
  93         } finally {
  94 //            frame.removeMouseListener(ma1);
  95         }
  96     }
  97 
  98     public static void testCase0(){
  99         if (propValue){
 100             throw new RuntimeException("TEST FAILED (0): System property sun.awt.enableExtraMouseButtons = " + propValue);
 101         }
 102     }
 103 
 104     public static void testCase1(){
 105         if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() == true){
 106             throw new RuntimeException("TEST FAILED (1): setting to FALSE. Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() = " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled());
 107         }
 108     }
 109 
 110     public static void testCase2(){
 111         emptyArrays();
 112         int [] buttonMasks = new int[MouseInfo.getNumberOfButtons()]; // = InputEvent.getButtonDownMasks();
 113         for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
 114             buttonMasks[i] = InputEvent.getMaskForButton(i+1);
 115             System.out.println("TEST: "+buttonMasks[i]);
 116         }
 117 
 118         for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
 119             System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMasks[i]);
 120             try {
 121                 robot.mousePress(buttonMasks[i]);
 122                 robot.delay(70);
 123                 robot.mouseRelease(buttonMasks[i]);
 124                 robot.delay(200);
 125                 //no exception is thrown
 126                 if (i >= 3) {
 127                     throw new RuntimeException("TESTCASE 2 FAILED : robot accepted the extra button " + (i+1) + " instead of throwing an exception.");
 128                 }
 129             } catch (IllegalArgumentException e){
 130                 if (i >= 3) {
 131                     System.out.println("Passed: an exception caught for extra button.");
 132                 } else {
 133                     throw new RuntimeException("TESTCASE 2 FAILED : exception happen on standard button.", e);
 134                 }
 135             }
 136         }
 137         robot.delay(2000);
 138         if (MouseInfo.getNumberOfButtons() < 3) {
 139             for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
 140                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 141                     throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed.");
 142                 }
 143             }
 144         } else {
 145             for (int i = 0; i < 3; i++){
 146                 if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
 147                     throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed.");
 148                 }
 149             }
 150 
 151             for (int i = 3; i < MouseInfo.getNumberOfButtons(); i++){
 152                 if (buttonsPressed[i] != 0 || buttonsReleased[i] != 0 || buttonsClicked[i] != 0 ) {
 153                     throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " was pressed.");
 154                 }
 155             }
 156         }
 157     }
 158 
 159     public static void emptyArrays(){
 160         for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
 161             buttonsPressed[i] = 0;
 162             buttonsReleased[i] = 0;
 163             buttonsClicked[i] = 0;
 164         }
 165     }
 166 
 167 }