CTORRestrictions_Disable.java revision 12677:a4299d47bd00
1/*
2 * Copyright (c) 2008, 2013, 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/othervm -Dsun.awt.enableExtraMouseButtons=false CTORRestrictions_Disable
30 */
31
32/*
33 * verify that user can't create the MouseEvent? with button4|5|... when property "sun.awt.enableExtraMouseButtons"=false
34 * verify that user can create the MouseEvent? with button1|2|3 when property "sun.awt.enableExtraMouseButtons"=false
35 */
36
37import java.awt.*;
38import java.awt.event.*;
39
40public class CTORRestrictions_Disable {
41    static Frame frame = new Frame("MouseEvent Test Frame");
42    static Point mousePosition;
43    static Point mousePositionOnScreen;
44
45    public static void main(String []s){
46        Robot robot = null;
47        try {
48            robot = new Robot();
49        } catch (AWTException ex) {
50            throw new RuntimeException("Test Failed", ex);
51        }
52        frame.setSize (200,200);
53        frame.setLocation (300, 400);
54        frame.setVisible(true);
55        robot.delay(1000);
56        System.out.println(Toolkit.getDefaultToolkit().getDesktopProperty("sun.awt.enableExtraMouseButtons"));
57        mousePosition = new Point(100, 100);
58        mousePositionOnScreen = new  Point(frame.getLocationOnScreen().x + mousePosition.x,
59                                                 frame.getLocationOnScreen().y + mousePosition.y);
60
61        System.out.println("Stage 1");
62        for (int buttonId = 1; buttonId <= MouseInfo.getNumberOfButtons(); buttonId++){
63           try {
64               postMouseEventNewCtor(buttonId);
65               if (buttonId > 3) {
66                   throw new RuntimeException("Stage 1 FAILED: MouseEvent CTOR accepted the extra button " + (buttonId+1) + " instead of throwing an exception.");
67               }
68           } catch (IllegalArgumentException e){
69                if (buttonId > 3) {
70                    System.out.println("Passed: an exception caught for extra button.");
71                } else {
72                    throw new RuntimeException("Stage 1 FAILED : exception happen on standard button.", e);
73                }
74            }
75        }
76
77        System.out.println("Stage 2");
78        for (int buttonId = 1; buttonId <= MouseInfo.getNumberOfButtons(); buttonId++){
79           try {
80               postMouseEventOldCtor(buttonId);
81               if (buttonId > 3) {
82                   throw new RuntimeException("Stage 2 FAILED: MouseEvent CTOR accepted the extra button " + (buttonId+1) + " instead of throwing an exception.");
83               }
84           } catch (IllegalArgumentException e){
85                if (buttonId > 3) {
86                    System.out.println("Passed: an exception caught for extra button.");
87                } else {
88                    throw new RuntimeException("Stage 2 FAILED : exception happen on standard button.", e);
89                }
90            }
91        }
92        System.out.println("Test passed.");
93    }
94
95    public static void postMouseEventNewCtor(int buttonId)    {
96        MouseEvent me = new MouseEvent(frame,
97                                       MouseEvent.MOUSE_PRESSED,
98                                       System.currentTimeMillis(),
99                                       MouseEvent.BUTTON1_DOWN_MASK,
100                                       mousePosition.x, mousePosition.y,
101                                       mousePositionOnScreen.x,
102                                       mousePositionOnScreen.y,
103                                       1,
104                                       false,              //popupTrigger
105                                       buttonId            //button
106                                       );
107        frame.dispatchEvent( ( AWTEvent )me );
108    }
109
110    public static void postMouseEventOldCtor(int buttonId)    {
111        MouseEvent meOld = new MouseEvent(frame,
112                                          MouseEvent.MOUSE_PRESSED,
113                                          System.currentTimeMillis(),
114                                          MouseEvent.BUTTON1_DOWN_MASK,
115                                          mousePosition.x, mousePosition.y,
116                                          1,
117                                          false,              //popupTrigger
118                                          buttonId //button
119                                          );
120        frame.dispatchEvent( ( AWTEvent )meOld );
121    }
122}
123