CheckGetMaskForButton.java revision 14851:980da45565c8
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 InputEvent.getMaskForButton() returns the same values as in InputEvent.BUTTON_DOWN_MASK
29  @author Andrei Dmitriev : area=awt.event
30  @run main CheckGetMaskForButton
31*/
32
33import java.awt.*;
34import java.awt.event.InputEvent;
35import java.lang.reflect.*;
36import java.security.AccessController;
37import java.security.PrivilegedAction;
38
39public class CheckGetMaskForButton{
40    static Robot robot;
41
42    public static void main(String []s){
43        System.out.println("Number Of Buttons = "+ MouseInfo.getNumberOfButtons());
44        CheckGetMaskForButton f = new CheckGetMaskForButton();
45        int [] buttonMasksViaAPI = new int[MouseInfo.getNumberOfButtons()];
46        for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
47            buttonMasksViaAPI[i] = InputEvent.getMaskForButton(i+1);
48            System.out.println("Test (API): "+ buttonMasksViaAPI[i]);
49        }
50
51        //get same array via reflection
52        Object obj = AccessController.doPrivileged(
53                new PrivilegedAction() {
54            public Object run() {
55                try {
56                    Class clazz = Class.forName("java.awt.event.InputEvent");
57                    Method method  = clazz.getDeclaredMethod("getButtonDownMasks",new Class [] {});
58                    if (method != null) {
59                        method.setAccessible(true);
60                        return method.invoke(null, (Object[])null);
61                    }
62                }catch (Exception e){
63                    throw new RuntimeException("Test failed. Exception occured:", e);
64                }
65                return null;
66            }
67        });
68
69        if (obj == null){
70            throw new RuntimeException("Test failed. The value obtained via reflection is "+obj);
71        }
72
73        int [] buttonDownMasksViaReflection = new int [Array.getLength(obj)];
74        //check that length of API array greater or equals then Reflect array.
75        if (Array.getLength(obj) < buttonMasksViaAPI.length){
76            throw new RuntimeException("Test failed. The length of API array greater or equals then the length of  Reflect array.");
77        }
78
79        //Check that the values obtained via reflection from InputEvent.BUTTON_DOWN_MASK are the
80        // same as for standard API.
81        for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
82            System.out.println("Test (Reflection): "+ Array.getInt(obj, i));
83            if (buttonMasksViaAPI[i] != Array.getInt(obj, i)){
84                throw new RuntimeException("Test failed. Values of InputEvent array are different for API and Reflection invocations");
85            }
86        }
87        System.out.println("Test passed.");
88    }
89}
90