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