870N/A/*
870N/A @test %I% %E%
870N/A @bug 6315717
870N/A @summary verifies that sun.awt.enableExtraMouseButtons = false consumes extra events
870N/A @author Andrei Dmitriev : area=awt.mouse
870N/A @run main/othervm -Dsun.awt.enableExtraMouseButtons=false ToolkitPropertyTest_Disable
870N/A */
870N/A
870N/Aimport java.awt.*;
870N/Aimport java.awt.event.*;
870N/A
870N/A// Testcase 1: set to FALSE and check
870N/A// Testcase 2: set to FALSE and check that extra events are not coming
870N/A// check that standard events are coming
870N/A
870N/Apublic class ToolkitPropertyTest_Disable extends Frame {
870N/A static boolean propValue;
870N/A static Robot robot;
870N/A static int [] buttonsPressed;
870N/A static int [] buttonsReleased;
870N/A static int [] buttonsClicked;
870N/A
870N/A static boolean lessThenFourButtons;
870N/A
870N/A public static void main(String []s){
870N/A propValue = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons"));
870N/A buttonsPressed = new int [MouseInfo.getNumberOfButtons()];
870N/A buttonsReleased = new int [MouseInfo.getNumberOfButtons()];
870N/A buttonsClicked = new int [MouseInfo.getNumberOfButtons()];
870N/A
870N/A ToolkitPropertyTest_Disable frame = new ToolkitPropertyTest_Disable();
870N/A frame.setSize(300, 300);
870N/A frame.setVisible(true);
870N/A
870N/A MouseAdapter ma1 = new MouseAdapter() {
870N/A public void mousePressed(MouseEvent e) {
870N/A buttonsPressed[e.getButton() - 1] += 1;
870N/A System.out.println("PRESSED "+e);
870N/A }
870N/A public void mouseReleased(MouseEvent e) {
870N/A buttonsReleased[e.getButton() - 1] += 1;
870N/A System.out.println("RELEASED "+e);
870N/A }
870N/A public void mouseClicked(MouseEvent e) {
870N/A buttonsClicked[e.getButton() - 1] += 1;
870N/A System.out.println("CLICKED "+e);
870N/A }
870N/A };
870N/A
870N/A try {
870N/A robot = new Robot();
870N/A robot.delay(1000);
870N/A robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);
870N/A
870N/A System.out.println("Property = " + propValue);
870N/A testCase0();
870N/A
870N/A testCase1();
870N/A System.out.println("Number Of Buttons = "+ MouseInfo.getNumberOfButtons());
870N/A
870N/A lessThenFourButtons = (MouseInfo.getNumberOfButtons() <= 3);
870N/A if ( !lessThenFourButtons ) {
870N/A frame.addMouseListener(ma1);
870N/A testCase2();
870N/A }
870N/A } catch (Exception e){
870N/A e.printStackTrace();
870N/A// throw new RuntimeException(e);
870N/A } finally {
870N/A// frame.removeMouseListener(ma1);
870N/A }
870N/A }
870N/A
870N/A public static void testCase0(){
870N/A if (propValue){
870N/A throw new RuntimeException("TEST FAILED (0): System property sun.awt.enableExtraMouseButtons = " + propValue);
870N/A }
870N/A }
870N/A
870N/A public static void testCase1(){
870N/A if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() == true){
870N/A throw new RuntimeException("TEST FAILED (1): setting to FALSE. Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() = " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled());
870N/A }
870N/A }
870N/A
870N/A public static void testCase2(){
870N/A emptyArrays();
870N/A int [] buttonMasks = new int[MouseInfo.getNumberOfButtons()]; // = InputEvent.getButtonDownMasks();
870N/A for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
870N/A buttonMasks[i] = InputEvent.getMaskForButton(i+1);
870N/A System.out.println("TEST: "+buttonMasks[i]);
870N/A }
870N/A
870N/A for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
870N/A System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMasks[i]);
870N/A try {
870N/A robot.mousePress(buttonMasks[i]);
870N/A robot.delay(70);
870N/A robot.mouseRelease(buttonMasks[i]);
870N/A robot.delay(200);
870N/A //no exception is thrown
870N/A if (i >= 3) {
870N/A throw new RuntimeException("TESTCASE 2 FAILED : robot accepted the extra button " + (i+1) + " instead of throwing an exception.");
870N/A }
870N/A } catch (IllegalArgumentException e){
870N/A if (i >= 3) {
870N/A System.out.println("Passed: an exception caught for extra button.");
870N/A } else {
870N/A throw new RuntimeException("TESTCASE 2 FAILED : exception happen on standard button.", e);
870N/A }
870N/A }
870N/A }
870N/A robot.delay(2000);
870N/A if (MouseInfo.getNumberOfButtons() < 3) {
870N/A for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
870N/A if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
870N/A throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed.");
870N/A }
870N/A }
870N/A } else {
870N/A for (int i = 0; i < 3; i++){
870N/A if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
870N/A throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed.");
870N/A }
870N/A }
870N/A
870N/A for (int i = 3; i < MouseInfo.getNumberOfButtons(); i++){
870N/A if (buttonsPressed[i] != 0 || buttonsReleased[i] != 0 || buttonsClicked[i] != 0 ) {
870N/A throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " was pressed.");
870N/A }
870N/A }
870N/A }
870N/A }
870N/A
870N/A public static void emptyArrays(){
870N/A for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
870N/A buttonsPressed[i] = 0;
870N/A buttonsReleased[i] = 0;
870N/A buttonsClicked[i] = 0;
870N/A }
870N/A }
870N/A
870N/A}