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