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