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