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