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