jdk/test/javax/swing/SwingUtilities/7088744/bug7088744.java
changeset 27760 40c141ec4ea1
parent 10582 6bb9de67122a
child 40128 e635645d2a8a
equal deleted inserted replaced
27512:0438995dba34 27760:40c141ec4ea1
     1 /*
     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    25    @bug 7088744
    25    @bug 7088744
    26    @summary SwingUtilities.isMiddleMouseButton does not work with ALT/Meta keys
    26    @summary SwingUtilities.isMiddleMouseButton does not work with ALT/Meta keys
    27    @author Pavel Porvatov
    27    @author Pavel Porvatov
    28 */
    28 */
    29 
    29 
    30 import sun.awt.SunToolkit;
    30 import java.awt.Component;
    31 
    31 import java.awt.Event;
    32 import javax.swing.*;
    32 import java.awt.Point;
    33 import java.awt.*;
    33 import java.awt.Robot;
    34 import java.awt.event.InputEvent;
    34 import java.awt.event.InputEvent;
    35 import java.awt.event.MouseAdapter;
    35 import java.awt.event.MouseAdapter;
    36 import java.awt.event.MouseEvent;
    36 import java.awt.event.MouseEvent;
    37 
    37 
       
    38 import javax.swing.JFrame;
       
    39 import javax.swing.JLabel;
       
    40 import javax.swing.SwingUtilities;
       
    41 
    38 public class bug7088744 {
    42 public class bug7088744 {
       
    43 
    39     private static volatile JLabel label;
    44     private static volatile JLabel label;
    40 
    45     private static volatile JFrame frame;
    41     private static volatile Point point;
    46 
       
    47     private static volatile Point point = new Point();
    42 
    48 
    43     private static final int MOUSE_CLICKED = 1;
    49     private static final int MOUSE_CLICKED = 1;
    44     private static final int MOUSE_PRESSED = 2;
    50     private static final int MOUSE_PRESSED = 2;
    45     private static final int MOUSE_RELEASED = 3;
    51     private static final int MOUSE_RELEASED = 3;
    46 
    52 
   115             }
   121             }
   116         });
   122         });
   117 
   123 
   118         SwingUtilities.invokeAndWait(new Runnable() {
   124         SwingUtilities.invokeAndWait(new Runnable() {
   119             public void run() {
   125             public void run() {
   120                 JFrame frame = new JFrame();
   126                 frame = new JFrame();
   121 
   127 
   122                 label = new JLabel("A label");
   128                 label = new JLabel("A label");
   123 
   129 
   124                 label.addMouseListener(new MouseAdapter() {
   130                 label.addMouseListener(new MouseAdapter() {
   125                     public void mouseClicked(MouseEvent e) {
   131                     public void mouseClicked(MouseEvent e) {
   135                     }
   141                     }
   136                 });
   142                 });
   137                 frame.add(label);
   143                 frame.add(label);
   138                 frame.setSize(200, 100);
   144                 frame.setSize(200, 100);
   139                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   145                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   146                 frame.setLocationRelativeTo(null);
   140                 frame.setVisible(true);
   147                 frame.setVisible(true);
   141             }
   148             }
   142         });
   149         });
   143 
   150 
   144         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
       
   145 
       
   146         toolkit.realSync();
       
   147 
       
   148         // On Linux platforms realSync doesn't guaranties setSize completion
       
   149         Thread.sleep(1000);
       
   150 
       
   151         SwingUtilities.invokeAndWait(new Runnable() {
       
   152             public void run() {
       
   153                 point = label.getLocationOnScreen();
       
   154             }
       
   155         });
       
   156 
       
   157         Robot robot = new Robot();
   151         Robot robot = new Robot();
       
   152         robot.waitForIdle();
       
   153 
       
   154         SwingUtilities.invokeAndWait(new Runnable() {
       
   155             public void run() {
       
   156                 Point pt = label.getLocationOnScreen();
       
   157                 point.x = pt.x + label.getWidth() / 2;
       
   158                 point.y = pt.y + label.getHeight() / 2;
       
   159             }
       
   160         });
   158 
   161 
   159         robot.setAutoDelay(100);
   162         robot.setAutoDelay(100);
       
   163         robot.setAutoWaitForIdle(true);
   160         robot.mouseMove(point.x, point.y);
   164         robot.mouseMove(point.x, point.y);
   161         robot.mousePress(InputEvent.BUTTON1_MASK);
   165         robot.mousePress(InputEvent.BUTTON1_MASK);
   162         robot.mousePress(InputEvent.BUTTON2_MASK);
   166         robot.mousePress(InputEvent.BUTTON2_MASK);
   163         robot.mousePress(InputEvent.BUTTON3_MASK);
   167         robot.mousePress(InputEvent.BUTTON3_MASK);
   164         robot.mouseRelease(InputEvent.BUTTON1_MASK);
   168         robot.mouseRelease(InputEvent.BUTTON1_MASK);
   165         robot.mouseRelease(InputEvent.BUTTON2_MASK);
   169         robot.mouseRelease(InputEvent.BUTTON2_MASK);
   166         robot.mouseRelease(InputEvent.BUTTON3_MASK);
   170         robot.mouseRelease(InputEvent.BUTTON3_MASK);
   167 
   171 
   168         toolkit.realSync();
   172         SwingUtilities.invokeAndWait(new Runnable() {
   169 
   173             public void run() {
   170         SwingUtilities.invokeAndWait(new Runnable() {
   174                 frame.dispose();
   171             public void run() {
       
   172                 if (eventCount != BUTTON_EVENTS_SEQUENCE.length) {
   175                 if (eventCount != BUTTON_EVENTS_SEQUENCE.length) {
   173                     throw new RuntimeException("Not all events received");
   176                     throw new RuntimeException("Not all events received");
   174                 }
   177                 }
   175 
   178 
   176             }
   179             }