870N/A/*
870N/A test %I% %E%
870N/A @bug 6315717
870N/A @summary verifies that MouseEvent could be constructed correctly for mouse extra buttons in regard to sun.awt.enableExtraMouseButtons property
870N/A @author Andrei Dmitriev : area=awt.event
870N/A @run main CTORRestrictions
870N/A */
870N/A
870N/A/*
870N/A * verify that user can create the MouseEvent? with button1|2|3|4|5|... when property "sun.awt.enableExtraMouseButtons" is true by default
870N/A */
870N/Aimport java.awt.*;
870N/Aimport java.awt.event.*;
870N/A
870N/Apublic class CTORRestrictions{
870N/A static Frame frame = new Frame("MouseEvent Test Frame");
870N/A static Point mousePosition;
870N/A static Point mousePositionOnScreen;
870N/A
870N/A public static void main(String []s){
870N/A Robot robot = null;
870N/A try {
870N/A robot = new Robot();
870N/A } catch (AWTException ex) {
870N/A throw new RuntimeException("Test Failed", ex);
870N/A }
870N/A frame.setSize (200,200);
870N/A frame.setLocation (300, 400);
870N/A frame.setVisible(true);
870N/A robot.delay(1000);
870N/A System.out.println("sun.awt.enableExtraMouseButtons = "+Toolkit.getDefaultToolkit().getDesktopProperty("sun.awt.enableExtraMouseButtons"));
870N/A mousePosition = new Point(100, 100);
870N/A mousePositionOnScreen = new Point(frame.getLocationOnScreen().x + mousePosition.x,
870N/A frame.getLocationOnScreen().y + mousePosition.y);
870N/A
870N/A /*
870N/A * On Linux the native system count a wheel (both directions) as two more buttons on a mouse.
870N/A * So, MouseInfo.getNumberOfButtons() would report 5 buttons on a three-button mouse.
870N/A * On Windows it would still report that MouseInfo.getNumberOfButtons() == 3.
870N/A * We should handle XToolkit case and iterate through the buttons
870N/A * up to (MouseInfo.getNumberOfButtons() - 2) value.
870N/A */
870N/A int numberOfButtons;
870N/A if (Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.windows.WToolkit")){
870N/A numberOfButtons = MouseInfo.getNumberOfButtons();
870N/A } else {
870N/A numberOfButtons = MouseInfo.getNumberOfButtons() - 2;
870N/A }
870N/A System.out.println("Stage 1. Number of buttons = "+ numberOfButtons);
870N/A
870N/A for (int buttonId = 1; buttonId <= numberOfButtons; buttonId++){
870N/A postMouseEventNewCtor(buttonId);
870N/A }
870N/A
870N/A System.out.println("Stage 2. Number of buttons = "+ numberOfButtons);
870N/A for (int buttonId = 1; buttonId <= numberOfButtons; buttonId++){
870N/A postMouseEventOldCtor(buttonId);
870N/A }
870N/A System.out.println("Test passed.");
870N/A }
870N/A
870N/A public static void postMouseEventNewCtor(int buttonId) {
870N/A MouseEvent me = new MouseEvent(frame,
870N/A MouseEvent.MOUSE_PRESSED,
870N/A System.currentTimeMillis(),
870N/A MouseEvent.BUTTON1_DOWN_MASK,
870N/A mousePosition.x, mousePosition.y,
870N/A mousePositionOnScreen.x,
870N/A mousePositionOnScreen.y,
870N/A 1,
870N/A false, //popupTrigger
870N/A buttonId //button
870N/A );
870N/A frame.dispatchEvent( ( AWTEvent )me );
870N/A }
870N/A
870N/A public static void postMouseEventOldCtor(int buttonId) {
870N/A MouseEvent meOld = new MouseEvent(frame,
870N/A MouseEvent.MOUSE_PRESSED,
870N/A System.currentTimeMillis(),
870N/A MouseEvent.BUTTON1_DOWN_MASK,
870N/A mousePosition.x, mousePosition.y,
870N/A 1,
870N/A false, //popupTrigger
870N/A buttonId //button
870N/A );
870N/A frame.dispatchEvent( ( AWTEvent )meOld );
870N/A }
870N/A}