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