AcceptExtraMouseButtons.java revision 8729:0242fce0f717
150476Speter/*
282486Sbde * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
321611Swosch * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
421611Swosch *
521611Swosch * This code is free software; you can redistribute it and/or modify it
618052Sbde * under the terms of the GNU General Public License version 2 only, as
794940Sru * published by the Free Software Foundation.
894940Sru *
994940Sru * This code is distributed in the hope that it will be useful, but WITHOUT
1094940Sru * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1136494Sbde * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1218052Sbde * version 2 for more details (a copy is included in the LICENSE file that
1336494Sbde * accompanied this code).
1476576Smarkm *
1539412Sphk * You should have received a copy of the GNU General Public License version
16117183Sru * 2 along with this work; if not, write to the Free Software Foundation,
17121054Semax * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1879495Sobrien *
1936494Sbde * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2076515Sbde * or visit www.oracle.com if you need additional information or have any
2169227Sbrian * questions.
2236494Sbde */
2339259Sgibbs
2476515Sbde/*
2536494Sbde  @test %I% %E%
2636494Sbde  @bug 6315717
2757461Smarkm  @summary verifies that Robot is accepting extra mouse buttons
2836494Sbde  @author Andrei Dmitriev : area=awt.mouse
2936575Speter  @library ../../regtesthelpers
3076515Sbde  @build Util
3139259Sgibbs  @run main AcceptExtraMouseButtons
3236494Sbde */
3336494Sbde
3436494Sbde//if we do robot.mousePress(InputEvent.BUTTON1_DOWN_MASK) the test must
3574535Sdes// 1) accept it (i.e. don't throw an IllegalArgumentException
3618052Sbde// 2) actually post a MouseEvent
3776515Sbde// Also, Robot should still accept InputEvent.BUTTONx_MASKs
3836494Sbde
3976515Sbdeimport java.awt.*;
4036494Sbdeimport java.awt.event.*;
41116318Speterimport sun.awt.SunToolkit;
42112461Sruimport test.java.awt.regtesthelpers.Util;
4336494Sbde
4494280Srupublic class AcceptExtraMouseButtons extends Frame {
45117184Sru    static String tk = Toolkit.getDefaultToolkit().getClass().getName();
4647570Sache    static Robot robot;
4757538Sshin    static int [] standardButtonMasks = {InputEvent.BUTTON1_MASK,
4836494Sbde                                         InputEvent.BUTTON2_MASK,
4967523Sarchie                                         InputEvent.BUTTON3_MASK};
50115832Smarkm    static int [] buttonsPressed;
51115832Smarkm    static int [] buttonsReleased;
52120949Snectar    static int [] buttonsClicked;
53104465Sru
54120492Sfjoe    static int buttonsNum = MouseInfo.getNumberOfButtons();
5576576Smarkm
5636494Sbde    public static void main(String []s){
5736494Sbde
5865916Sache        //MouseInfo.getNumberOfButtons() reports two more buttons on XToolkit
5936494Sbde        //as they reserved for wheel (both directions).
6036494Sbde        if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {
6176515Sbde            buttonsNum = buttonsNum - 2;
6290796Sgshapiro        }
6390796Sgshapiro        System.out.println("Number Of Buttons = "+ buttonsNum);
6490796Sgshapiro        if (buttonsNum < 3) {
6536494Sbde            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.");
6665916Sache            buttonsNum = 3;
6752251Sbp        }
6836494Sbde
6952419Sjulian        buttonsPressed = new int [buttonsNum];
7036494Sbde        buttonsReleased = new int [buttonsNum];
7136494Sbde        buttonsClicked = new int [buttonsNum];
7242916Sjdp
7342916Sjdp        AcceptExtraMouseButtons frame = new AcceptExtraMouseButtons();
74107256Sru
7559770Sbde        MouseAdapter ma1 = new MouseAdapter() {
7642916Sjdp                public void mousePressed(MouseEvent e) {
77107256Sru                    buttonsPressed[e.getButton() - 1] += 1;
78114709Smarkm                    System.out.println("PRESSED "+e);
7977866Smarkm                }
8077866Smarkm                public void mouseReleased(MouseEvent e) {
8177866Smarkm                    buttonsReleased[e.getButton() - 1] += 1;
8276576Smarkm                    System.out.println("RELEASED "+e);
8376576Smarkm                }
8489705Sru                public void mouseClicked(MouseEvent e) {
8594714Sdes                    buttonsClicked[e.getButton() - 1] += 1;
8689705Sru                    System.out.println("CLICKED "+e);
8794714Sdes                }
8889705Sru            };
8989705Sru        frame.addMouseListener(ma1);
9089705Sru
9142916Sjdp        frame.setSize(300, 300);
9289705Sru        frame.setVisible(true);
9342916Sjdp
9476515Sbde        Util.waitForIdle(robot);  //a time to show a Frame
9536494Sbde
9636494Sbde        try {
9739020Smarkm            robot = new Robot();
9836494Sbde            robot.delay(1000);
9941231Sjdp            robot.mouseMove(frame.getLocationOnScreen().x + frame.getWidth()/2,
10036494Sbde                            frame.getLocationOnScreen().y + frame.getHeight()/2);
10136494Sbde
10277866Smarkm            //TestCase 1: verify that all BUTTONx_DOWN_MASKs are accepted by the Robot.
10336494Sbde
104121054Semax            for (int i = 0; i < buttonsNum; i++){
10574840Sken                int buttonMask = InputEvent.getMaskForButton(i+1);
10688142Sru                System.out.println("button to press = " +(i+1) + " : value passed to robot = " +buttonMask);
10789705Sru                robot.mousePress(buttonMask);
10866913Sgshapiro                robot.delay(30);
10936494Sbde                robot.mouseRelease(buttonMask);
11041231Sjdp                Util.waitForIdle(robot);
11136494Sbde            }
11218052Sbde            for (int i = 0; i < buttonsNum; i++){
11376515Sbde                if (buttonsPressed[i] != 1 || buttonsReleased[i] != 1 || buttonsClicked[i] !=1 ) {
114109725Sru                    throw new RuntimeException("TESTCASE 1 FAILED : button " + (i+1) + " wasn't single pressed|released|clicked : "+ buttonsPressed[i] +" : "+ buttonsReleased[i] +" : "+ buttonsClicked[i]);
115101224Srwatson                }
116104465Sru            }
11736494Sbde
11876515Sbde            java.util.Arrays.fill(buttonsPressed, 0);
11944757Smarkm            java.util.Arrays.fill(buttonsReleased, 0);
12036494Sbde            java.util.Arrays.fill(buttonsClicked, 0);
12136494Sbde            //TestCase 2: verify that all BUTTONx_MASKs are accepted by the Robot.
12294578Sdes            for (int i = 0; i < standardButtonMasks.length; i++){
12336494Sbde                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