AcceptExtraMouseButtons.java revision 8729:0242fce0f717
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 Robot is accepting extra mouse buttons
28  @author Andrei Dmitriev : area=awt.mouse
29  @library ../../regtesthelpers
30  @build Util
31  @run main AcceptExtraMouseButtons
32 */
33
34//if we do robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) the test must
35// 1) accept it (i.e. don't throw an IllegalArgumentException
36// 2) actually post a MouseEvent
37// Also, Robot should still accept InputEvent.BUTTONx_MASKs
38
39import java.awt.*;
40import java.awt.event.*;
41import sun.awt.SunToolkit;
42import test.java.awt.regtesthelpers.Util;
43
44public class AcceptExtraMouseButtons extends Frame {
45    static String tk = Toolkit.getDefaultToolkit().getClass().getName();
46    static Robot robot;
47    static int [] standardButtonMasks = {InputEvent.BUTTON1_MASK,
48                                         InputEvent.BUTTON2_MASK,
49                                         InputEvent.BUTTON3_MASK};
50    static int [] buttonsPressed;
51    static int [] buttonsReleased;
52    static int [] buttonsClicked;
53
54    static int buttonsNum = MouseInfo.getNumberOfButtons();
55
56    public static void main(String []s){
57
58        //MouseInfo.getNumberOfButtons() reports two more buttons on XToolkit
59        //as they reserved for wheel (both directions).
60        if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {
61            buttonsNum = buttonsNum - 2;
62        }
63        System.out.println("Number Of Buttons = "+ buttonsNum);
64        if (buttonsNum < 3) {
65            System.out.println("Linux and Windows systems should emulate three buttons if even there are only 1 or 2 are phsically available. Setting number of buttons to 3.");
66            buttonsNum = 3;
67        }
68
69        buttonsPressed = new int [buttonsNum];
70        buttonsReleased = new int [buttonsNum];
71        buttonsClicked = new int [buttonsNum];
72
73        AcceptExtraMouseButtons frame = new AcceptExtraMouseButtons();
74
75        MouseAdapter ma1 = new MouseAdapter() {
76                public void mousePressed(MouseEvent e) {
77                    buttonsPressed[e.getButton() - 1] += 1;
78                    System.out.println("PRESSED "+e);
79                }
80                public void mouseReleased(MouseEvent e) {
81                    buttonsReleased[e.getButton() - 1] += 1;
82                    System.out.println("RELEASED "+e);
83                }
84                public void mouseClicked(MouseEvent e) {
85                    buttonsClicked[e.getButton() - 1] += 1;
86                    System.out.println("CLICKED "+e);
87                }
88            };
89        frame.addMouseListener(ma1);
90
91        frame.setSize(300, 300);
92        frame.setVisible(true);
93
94        Util.waitForIdle(robot);  //a time to show a Frame
95
96        try {
97            robot = new Robot();
98            robot.delay(1000);
99            robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2,
100                            frame.getLocationOnScreen().y + frame.getHeight()/2);
101
102            //TestCase 1: verify that all BUTTONx_DOWN_MASKs are accepted by the Robot.
103
104            for (int i = 0; i < buttonsNum; i++){
105                int buttonMask = InputEvent.getMaskForButton(i+1);
106                System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
107                robot.mousePress(buttonMask);
108                robot.delay(30);
109                robot.mouseRelease(buttonMask);
110                Util.waitForIdle(robot);
111            }
112            for (int i = 0; i < buttonsNum; i++){
113                if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
114                    throw new RuntimeException("TESTCASE 1 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
115                }
116            }
117
118            java.util.Arrays.fill(buttonsPressed, 0);
119            java.util.Arrays.fill(buttonsReleased, 0);
120            java.util.Arrays.fill(buttonsClicked, 0);
121            //TestCase 2: verify that all BUTTONx_MASKs are accepted by the Robot.
122            for (int i = 0; i < standardButtonMasks.length; i++){
123                int buttonMask = standardButtonMasks[i];
124                System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
125                robot.mousePress(buttonMask);
126                robot.delay(30);
127                robot.mouseRelease(buttonMask);
128                Util.waitForIdle(robot);
129            }
130            for (int i = 0; i < standardButtonMasks.length; i++){
131                if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
132                    throw new RuntimeException("TESTCASE 2 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
133                }
134            }
135
136        } catch (Exception e){
137            e.printStackTrace();
138            throw new RuntimeException(e);
139        }
140    }
141}
142