CTORRestrictions.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 MouseEvent could be constructed correctly for mouse extra buttons in regard to sun.awt.enableExtraMouseButtons property
28  @author Andrei Dmitriev : area=awt.event
29  @run main CTORRestrictions
30 */
31
32/*
33 * verify that user can create the MouseEvent? with button1|2|3|4|5|... when property "sun.awt.enableExtraMouseButtons" is true by default
34 */
35import java.awt.*;
36import java.awt.event.*;
37
38public class CTORRestrictions{
39    static Frame frame = new Frame("MouseEvent Test Frame");
40    static Point mousePosition;
41    static Point mousePositionOnScreen;
42
43    public static void main(String []s){
44        Robot robot = null;
45        try {
46            robot = new Robot();
47        } catch (AWTException ex) {
48            throw new RuntimeException("Test Failed", ex);
49        }
50        frame.setSize (200,200);
51        frame.setLocation (300, 400);
52        frame.setVisible(true);
53        robot.delay(1000);
54        System.out.println("sun.awt.enableExtraMouseButtons = "+Toolkit.getDefaultToolkit().getDesktopProperty("sun.awt.enableExtraMouseButtons"));
55        mousePosition = new Point(100, 100);
56        mousePositionOnScreen = new  Point(frame.getLocationOnScreen().x + mousePosition.x,
57                                                 frame.getLocationOnScreen().y + mousePosition.y);
58
59        /*
60         * On Linux the native system count a wheel (both directions) as two more buttons on a mouse.
61         * So, MouseInfo.getNumberOfButtons() would report 5 buttons on a three-button mouse.
62         * On Windows it would still report that MouseInfo.getNumberOfButtons() == 3.
63         * We should handle XToolkit case and iterate through the buttons
64         * up to (MouseInfo.getNumberOfButtons() - 2) value.
65         */
66        int numberOfButtons;
67        if (Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.windows.WToolkit")){
68            numberOfButtons = MouseInfo.getNumberOfButtons();
69        } else {
70            numberOfButtons = MouseInfo.getNumberOfButtons() - 2;
71        }
72        System.out.println("Stage 1. Number of buttons = "+ numberOfButtons);
73
74        for (int buttonId = 1; buttonId <= numberOfButtons; buttonId++){
75            postMouseEventNewCtor(buttonId);
76        }
77
78        System.out.println("Stage 2. Number of buttons = "+ numberOfButtons);
79        for (int buttonId = 1; buttonId <= numberOfButtons; buttonId++){
80            postMouseEventOldCtor(buttonId);
81        }
82        System.out.println("Test passed.");
83    }
84
85    public static void postMouseEventNewCtor(int buttonId)    {
86        MouseEvent me = new MouseEvent(frame,
87                                       MouseEvent.MOUSE_PRESSED,
88                                       System.currentTimeMillis(),
89                                       MouseEvent.BUTTON1_DOWN_MASK,
90                                       mousePosition.x, mousePosition.y,
91                                       mousePositionOnScreen.x,
92                                       mousePositionOnScreen.y,
93                                       1,
94                                       false,              //popupTrigger
95                                       buttonId            //button
96                                       );
97        frame.dispatchEvent( ( AWTEvent )me );
98    }
99
100    public static void postMouseEventOldCtor(int buttonId)    {
101        MouseEvent meOld = new MouseEvent(frame,
102                                          MouseEvent.MOUSE_PRESSED,
103                                          System.currentTimeMillis(),
104                                          MouseEvent.BUTTON1_DOWN_MASK,
105                                          mousePosition.x, mousePosition.y,
106                                          1,
107                                          false,              //popupTrigger
108                                          buttonId //button
109                                          );
110        frame.dispatchEvent( ( AWTEvent )meOld );
111    }
112}
113