1 /*
   2  * Copyright (c) 2012, 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 7146377
  28  * @summary closed/javax/swing/DataTransfer/4876520/bug4876520.java failed since b08 in jdk 8
  29  * @author Pavel Porvatov
  30  */
  31 
  32 import sun.awt.SunToolkit;
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 import java.awt.event.InputEvent;
  37 import java.awt.event.MouseEvent;
  38 import java.awt.event.MouseListener;
  39 
  40 public class bug7146377 {
  41     private static JLabel label;
  42     private static JFrame frame;
  43 
  44     private static volatile Point point;
  45 
  46     public static void main(String[] args) throws Exception {
  47         SwingUtilities.invokeAndWait(new Runnable() {
  48             public void run() {
  49                 frame = new JFrame();
  50 
  51                 label = new JLabel("A label");
  52 
  53                 label.addMouseListener(new MouseListener() {
  54                     @Override
  55                     public void mouseClicked(MouseEvent e) {
  56                         checkEvent(e);
  57                     }
  58 
  59                     @Override
  60                     public void mousePressed(MouseEvent e) {
  61                         checkEvent(e);
  62                     }
  63 
  64                     @Override
  65                     public void mouseReleased(MouseEvent e) {
  66                         checkEvent(e);
  67                     }
  68 
  69                     @Override
  70                     public void mouseEntered(MouseEvent e) {
  71                         checkEvent(e);
  72                     }
  73 
  74                     @Override
  75                     public void mouseExited(MouseEvent e) {
  76                         checkEvent(e);
  77                     }
  78                 });
  79 
  80                 frame.add(label);
  81                 frame.setSize(200, 100);
  82                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83                 frame.setVisible(true);
  84             }
  85         });
  86 
  87         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  88 
  89         toolkit.realSync();
  90 
  91         // On Linux platforms realSync doesn't guaranties setSize completion
  92         Thread.sleep(1000);
  93 
  94         SwingUtilities.invokeAndWait(new Runnable() {
  95             public void run() {
  96                 point = label.getLocationOnScreen();
  97             }
  98         });
  99 
 100         Robot robot = new Robot();
 101 
 102         robot.setAutoDelay(200);
 103 
 104         // Move mouse
 105         for (int i = 0; i < 20; i++) {
 106             robot.mouseMove(point.x + i, point.y + i);
 107         }
 108 
 109         for (int button : new int[]{InputEvent.BUTTON1_MASK, InputEvent.BUTTON2_MASK, InputEvent.BUTTON3_MASK}) {
 110             robot.mouseMove(point.x, point.y);
 111 
 112             // Mouse Drag
 113             robot.mousePress(button);
 114 
 115             for (int i = 0; i < 20; i++) {
 116                 robot.mouseMove(point.x + i, point.y + i);
 117             }
 118 
 119             robot.mouseRelease(button);
 120         }
 121 
 122         toolkit.realSync();
 123 
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125             public void run() {
 126                 frame.dispose();
 127             }
 128         });
 129 
 130         System.out.println("Test passed");
 131     }
 132 
 133     private static void checkEvent(MouseEvent e) {
 134         String eventAsStr = eventToString(e);
 135 
 136         System.out.println("Checking event " + eventAsStr);
 137 
 138         check("isLeftMouseButton", SwingUtilities.isLeftMouseButton(e), oldIsLeftMouseButton(e), eventAsStr);
 139         check("isRightMouseButton", SwingUtilities.isRightMouseButton(e), oldIsRightMouseButton(e), eventAsStr);
 140         check("isMiddleMouseButton", SwingUtilities.isMiddleMouseButton(e), oldIsMiddleMouseButton(e), eventAsStr);
 141     }
 142 
 143     private static void check(String methodName, boolean newValue, boolean oldValue, String eventAsStr) {
 144         if (newValue != oldValue) {
 145             throw new RuntimeException("Regression on " + methodName + ", newValue = " + newValue +
 146                     ", oldValue = " + oldValue + ", e = " + eventAsStr);
 147         }
 148     }
 149 
 150     private static String eventToString(MouseEvent e) {
 151         StringBuilder result = new StringBuilder();
 152 
 153         switch (e.getID()) {
 154             case MouseEvent.MOUSE_PRESSED:
 155                 result.append("MOUSE_PRESSED");
 156                 break;
 157             case MouseEvent.MOUSE_RELEASED:
 158                 result.append("MOUSE_RELEASED");
 159                 break;
 160             case MouseEvent.MOUSE_CLICKED:
 161                 result.append("MOUSE_CLICKED");
 162                 break;
 163             case MouseEvent.MOUSE_ENTERED:
 164                 result.append("MOUSE_ENTERED");
 165                 break;
 166             case MouseEvent.MOUSE_EXITED:
 167                 result.append("MOUSE_EXITED");
 168                 break;
 169             case MouseEvent.MOUSE_MOVED:
 170                 result.append("MOUSE_MOVED");
 171                 break;
 172             case MouseEvent.MOUSE_DRAGGED:
 173                 result.append("MOUSE_DRAGGED");
 174                 break;
 175             case MouseEvent.MOUSE_WHEEL:
 176                 result.append("MOUSE_WHEEL");
 177                 break;
 178             default:
 179                 result.append("unknown type");
 180         }
 181 
 182         result.append(", modifiers = " + MouseEvent.getMouseModifiersText(e.getModifiers()));
 183         result.append(", modifiersEx = " + MouseEvent.getMouseModifiersText(e.getModifiersEx()));
 184         result.append(", button = " + e.getButton());
 185 
 186         return result.toString();
 187     }
 188 
 189     // Original implementation of SwingUtilities.isLeftMouseButton
 190     private static boolean oldIsLeftMouseButton(MouseEvent e) {
 191         return ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0);
 192     }
 193 
 194     // Original implementation of SwingUtilities.isMiddleMouseButton
 195     private static boolean oldIsMiddleMouseButton(MouseEvent e) {
 196         return ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
 197     }
 198 
 199     // Original implementation of SwingUtilities.isRightMouseButton
 200     private static boolean oldIsRightMouseButton(MouseEvent e) {
 201         return ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
 202     }
 203 }