ToolkitPropertyTest_Enable.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2008, 2010, 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 is working
28  @author Andrei Dmitriev : area=awt.mouse
29  @run main/othervm -Dsun.awt.enableExtraMouseButtons=true ToolkitPropertyTest_Enable
30 */
31
32import java.awt.*;
33import java.awt.event.*;
34
35// Testcase 1: set to TRUE (via jtreg option)
36// Testcase 2: set to TRUE and check that extra events are coming
37//                             check that standard events are coming
38
39public class ToolkitPropertyTest_Enable extends Frame {
40    static boolean propValue;
41    static Robot robot;
42    static int [] buttonsPressed;
43    static int [] buttonsReleased;
44    static int [] buttonsClicked;
45
46    public static void main(String []s){
47        propValue = Boolean.parseBoolean(System.getProperty("sun.awt.enableExtraMouseButtons"));
48        buttonsPressed = new int [MouseInfo.getNumberOfButtons()];
49        buttonsReleased = new int [MouseInfo.getNumberOfButtons()];
50        buttonsClicked = new int [MouseInfo.getNumberOfButtons()];
51
52        ToolkitPropertyTest_Enable frame = new ToolkitPropertyTest_Enable();
53        frame.setSize(300, 300);
54        frame.setVisible(true);
55
56        MouseAdapter ma1 = new MouseAdapter() {
57                public void mousePressed(MouseEvent e) {
58                    buttonsPressed[e.getButton() - 1] += 1;
59                    System.out.println("PRESSED "+e);
60                }
61                public void mouseReleased(MouseEvent e) {
62                    buttonsReleased[e.getButton() - 1] += 1;
63                    System.out.println("RELEASED "+e);
64                }
65                public void mouseClicked(MouseEvent e) {
66                    buttonsClicked[e.getButton() - 1] += 1;
67                    System.out.println("CLICKED "+e);
68                }
69            };
70        //        frame.addMouseListener(ma1);
71
72        try {
73            robot = new Robot();
74            robot.delay(1000);
75            robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);
76
77            System.out.println("Property = " + propValue);
78            testCase0();
79
80            testCase1();
81            System.out.println("Number Of Buttons = "+ MouseInfo.getNumberOfButtons());
82
83            boolean lessThenFourButtons = (MouseInfo.getNumberOfButtons() <= 3);
84            if ( !lessThenFourButtons ) {
85                frame.addMouseListener(ma1);
86                testCase2();
87                //                testCase3();
88                //                testCase4();
89                frame.removeMouseListener(ma1);
90            }
91        } catch (Exception e){
92            e.printStackTrace();
93            throw new RuntimeException(e);
94        }
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() == false){
106            throw new RuntimeException("TEST FAILED (1) : setting to TRUE. enabled = " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled());
107        }
108    }
109
110    public static void testCase2(){
111        emptyArrays();
112        //we can't post a message from an unexistent button
113        int [] buttonMasks = new int[MouseInfo.getNumberOfButtons()]; // = InputEvent.getButtonDownMasks();
114        for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
115            buttonMasks[i] = InputEvent.getMaskForButton(i+1);
116            System.out.println("TEST: buttonMasks["+ i +"] = " + buttonMasks[i]);
117        }
118
119        for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
120            System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMasks[i]);
121            robot.mousePress(buttonMasks[i]);
122            robot.delay(70);
123            robot.mouseRelease(buttonMasks[i]);
124            robot.delay(200);
125        }
126        robot.delay(1000);
127
128        for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
129            if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
130                throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
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}
144