1 /*
   2   @test %I% %E%
   3   @bug 6315717
   4   @summary verifies that drag events are coming for every button if the property is set to true
   5   @author Andrei Dmitriev : area=awt.mouse
   6   @run main ExtraButtonDrag
   7  */
   8 
   9 //events from standard should also come
  10 
  11 import java.awt.*;
  12 import java.awt.event.*;
  13 
  14 public class ExtraButtonDrag extends Frame {
  15     static String tk = Toolkit.getDefaultToolkit().getClass().getName();
  16     static Robot robot;
  17     static int [] buttonsPressed;
  18     static int [] buttonsReleased;
  19     static int [] buttonsClicked;
  20     volatile static boolean dragged = false;
  21     volatile static boolean moved = false;
  22 
  23     public ExtraButtonDrag(){
  24         super("ExtraButtonDrag");
  25     }
  26 
  27     public static void main(String []s){
  28         Frame frame = new ExtraButtonDrag();
  29 
  30         MouseAdapter ma = new MouseAdapter() {
  31                 public void mouseDragged(MouseEvent e) {
  32                     System.out.println("Dragged "+e);// +" : "+ e.getButton() + " : " +e.getButtonState(e.getButton()));
  33                     dragged = true;
  34                 }
  35                 public void mouseMoved(MouseEvent e) {
  36                     System.out.println("Moved "+e);
  37                     moved = true;
  38                 }
  39                 public void mousePressed(MouseEvent e) {
  40                     System.out.println(">>> "+e);
  41                 }
  42                 public void mouseReleased(MouseEvent e) {
  43                     System.out.println(">>> "+e);
  44                 }
  45 
  46             };
  47 
  48         frame.addMouseMotionListener(ma);
  49         frame.addMouseListener(ma);
  50 
  51         frame.setSize(300, 300);
  52         frame.setVisible(true);
  53 
  54         int [] buttonMask = new int [MouseInfo.getNumberOfButtons()]; //InputEvent.getButtonMasks();
  55 
  56         for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
  57             buttonMask[i] = InputEvent.getMaskForButton(i+1);
  58             //            System.out.println("TEST: "+tmp[i]);
  59         }
  60 
  61         try {
  62             robot = new Robot();
  63             robot.delay(1000);
  64             Point centerFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()/2, frame.getLocationOnScreen().y + frame.getHeight()/2);
  65             Point outboundsFrame = new Point(frame.getLocationOnScreen().x + frame.getWidth()*3/2, frame.getLocationOnScreen().y + frame.getHeight()/2);
  66 
  67             System.out.println("areExtraMouseButtonsEnabled() == " + Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() );
  68 
  69             for (int i = 0; i < MouseInfo.getNumberOfButtons(); i++){
  70                 System.out.println("button to drag = " +(i+1) + " : value passed to robot = " +buttonMask[i]);
  71 
  72                 try {
  73                     dragMouse(buttonMask[i], centerFrame.x, centerFrame.y, outboundsFrame.x, outboundsFrame.y);
  74                 } catch (IllegalArgumentException e){
  75                     throw new RuntimeException("Test failed. Exception occured.", e);
  76                 }
  77 
  78                 robot.delay(500);
  79                 //this is a choice-case for X protocol issue: native events from extra buttons doesn't contain
  80                 // the correct state so it's unable to decide if there is a drag or move. By default we send MOVED event.
  81                 //XToolkit: extra buttons should report MOVED events only
  82                 //WToolkit: extra buttons should report DRAGGED events only
  83                 if (i > 2){ //extra buttons only
  84                     if (tk.equals("sun.awt.X11.XToolkit") || tk.equals("sun.awt.motif.MToolkit")) {
  85                         if (!moved || dragged) {
  86                             throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);
  87                         }
  88                     } else { //WToolkit
  89                         if (moved || !dragged) {
  90                             throw new RuntimeException("Test failed."+ tk +" Button = " +(i+1) + " moved = "+moved +" : dragged = " +dragged);
  91                         }
  92                     }
  93                 } else {
  94                     if (moved || !dragged){
  95                         throw new RuntimeException("Test failed. Button = " +(i+1) + " not dragged.");
  96                     }
  97                 }
  98             }
  99         } catch (Exception e){
 100             throw new RuntimeException("", e);
 101         }
 102     }
 103 
 104     public static void dragMouse(int button, int x0, int y0, int x1, int y1){
 105         int curX = x0;
 106         int curY = y0;
 107         int dx = x0 < x1 ? 1 : -1;
 108         int dy = y0 < y1 ? 1 : -1;
 109         robot.mouseMove(x0, y0);
 110 
 111         robot.delay(200);
 112         dragged = false;
 113         moved = false;
 114 
 115         robot.mousePress(button);
 116 
 117         while (curX != x1){
 118             curX += dx;
 119             robot.mouseMove(curX, curY);
 120             robot.delay(5);
 121         }
 122         while (curY != y1 ){
 123             curY += dy;
 124             robot.mouseMove(curX, curY);
 125             robot.delay(5);
 126         }
 127         robot.mouseRelease(button);
 128     }
 129 
 130 }
 131