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