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