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