1 /*
   2  * Copyright (c) 2001, 2014, 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 import java.awt.*;
  26 
  27 import java.awt.event.InputEvent;
  28 import java.awt.event.KeyEvent;
  29 import java.awt.event.MouseEvent;
  30 import java.awt.event.MouseListener;
  31 
  32 import test.java.awt.event.helpers.lwcomponents.LWButton;
  33 import test.java.awt.event.helpers.lwcomponents.LWList;
  34 
  35 import java.util.ArrayList;
  36 
  37 import static jdk.test.lib.Asserts.*;
  38 
  39 /*
  40  * @test
  41  * @key headful
  42  * @bug 8043126
  43  * @summary Check whether getButton() returns correct mouse button
  44  *          number when the mouse buttons are pressed and getModifiers()
  45  *          returns correct modifiers
  46  *
  47  * @library /lib/testlibrary/ ../../helpers/lwcomponents/
  48  * @library /test/lib
  49  * @build LWComponent
  50  * @build LWButton
  51  * @build LWList
  52  * @build ExtendedRobot
  53  * @run main/timeout=600 MouseButtonsTest
  54  */
  55 
  56 public class MouseButtonsTest implements MouseListener {
  57 
  58     private Frame frame;
  59 
  60     private Button    button;
  61     private LWButton  buttonLW;
  62     private TextField textField;
  63     private TextArea  textArea;
  64     private List      list;
  65     private LWList    listLW;
  66 
  67     private int buttonPressedNumber = 0;
  68     private int buttonReleasedNumber = 0;
  69     private int modifiers = 0;
  70 
  71 
  72     private final ExtendedRobot robot;
  73 
  74     private final static int robotDelay = 1000;
  75     private final static int waitDelay  = 3500;
  76 
  77     private boolean released = false;
  78     private boolean pressed = false;
  79     private final Object lock;
  80 
  81 
  82     MouseButtonsTest() throws Exception {
  83         lock = new Object();
  84         robot = new ExtendedRobot();
  85         EventQueue.invokeAndWait( this::createGUI );
  86     }
  87 
  88     public void createGUI() {
  89 
  90         frame = new Frame();
  91         frame.setTitle("MouseButtonsTest");
  92         frame.setLayout(new GridLayout(1, 6));
  93 
  94         button = new Button();
  95         button.addMouseListener(this);
  96         frame.add(button);
  97 
  98         buttonLW = new LWButton();
  99         buttonLW.addMouseListener(this);
 100         frame.add(buttonLW);
 101 
 102         textField = new TextField(5);
 103         textField.addMouseListener(this);
 104         frame.add(textField);
 105 
 106         textArea = new TextArea(5, 5);
 107         textArea.addMouseListener(this);
 108         frame.add(textArea);
 109 
 110         list = new List();
 111         for (int i = 1; i <= 5; ++i) { list.add("item " + i); }
 112         list.addMouseListener(this);
 113         frame.add(list);
 114 
 115         listLW = new LWList();
 116         for (int i = 1; i <= 5; ++i) { listLW.add("item " + i); }
 117         listLW.addMouseListener(this);
 118         frame.add(listLW);
 119 
 120 
 121         frame.setBackground(Color.gray);
 122         frame.setSize(500, 100);
 123         frame.setVisible(true);
 124         frame.toFront();
 125     }
 126 
 127 
 128     @Override
 129     public void mouseClicked(MouseEvent e) {}
 130 
 131     @Override
 132     public void mousePressed(MouseEvent e) {
 133 
 134         assertFalse(e.getButton() == MouseEvent.NOBUTTON, "invalid button");
 135 
 136         buttonPressedNumber = e.getButton();
 137         modifiers = e.getModifiers();
 138 
 139         pressed = true;
 140 
 141         synchronized (lock) {
 142             try {
 143                 lock.notifyAll();
 144             } catch (Exception ex) {}
 145         }
 146     }
 147 
 148     @Override
 149     public void mouseReleased(MouseEvent e) {
 150 
 151         assertFalse(e.getButton() == MouseEvent.NOBUTTON, "invalid button");
 152 
 153         buttonReleasedNumber = e.getButton();
 154         modifiers = e.getModifiers();
 155 
 156         released = true;
 157 
 158         synchronized (lock) {
 159             try {
 160                 lock.notifyAll();
 161             } catch (Exception ex) {}
 162         }
 163     }
 164 
 165     @Override
 166     public void mouseEntered(MouseEvent e) {}
 167 
 168     @Override
 169     public void mouseExited(MouseEvent e) {}
 170 
 171 
 172     void doTest() throws Exception {
 173 
 174         int masks[] = new int[]{
 175             InputEvent.BUTTON1_MASK, InputEvent.BUTTON2_MASK, InputEvent.BUTTON3_MASK};
 176 
 177         int buttons[] = new int[]{
 178             MouseEvent.BUTTON1, MouseEvent.BUTTON2, MouseEvent.BUTTON3};
 179 
 180         ArrayList<Component> components = new ArrayList();
 181         components.add(button);
 182         components.add(buttonLW);
 183         components.add(textField);
 184         components.add(textArea);
 185         components.add(list);
 186         components.add(listLW);
 187 
 188         for (Component c: components) {
 189 
 190             System.out.println(c.getClass().getName() + ":");
 191 
 192             Point origin = c.getLocationOnScreen();
 193             int xc = origin.x + c.getWidth() / 2;
 194             int yc = origin.y + c.getHeight() / 2;
 195             Point center = new Point(xc, yc);
 196 
 197             robot.delay(robotDelay);
 198             robot.glide(origin, center);
 199             robot.click();
 200             robot.delay(robotDelay);
 201 
 202             for (int i = 0; i < masks.length; ++i) {
 203 
 204                 pressed  = false;
 205                 released = false;
 206 
 207                 int mask = masks[i];
 208                 robot.mousePress(mask);
 209                 robot.delay(robotDelay);
 210 
 211                 if (!pressed) {
 212                     synchronized (lock) {
 213                         try {
 214                             lock.wait(waitDelay);
 215                         } catch (InterruptedException ex) {}
 216                     }
 217                 }
 218 
 219                 assertTrue(pressed, "mouse press event was not received");
 220                 assertEQ((modifiers & mask), mask, "invalid mask modifiers");
 221 
 222                 robot.mouseRelease(mask);
 223                 robot.delay(robotDelay);
 224 
 225                 if (!released) {
 226                     synchronized (lock) {
 227                         try {
 228                             lock.wait(waitDelay);
 229                         } catch (InterruptedException ex) {}
 230                     }
 231                 }
 232 
 233                 assertTrue(released, "mouse release event was not received");
 234                 assertEQ((modifiers & mask), mask, "invalid mask modifiers");
 235 
 236                 assertEquals(buttonPressedNumber,  buttons[i]);
 237                 assertEquals(buttonReleasedNumber, buttons[i]);
 238 
 239                 robot.type(KeyEvent.VK_ESCAPE);
 240                 robot.delay(robotDelay);
 241 
 242                 System.out.println("button " + buttons[i] + " - passed");
 243             }
 244         }
 245 
 246         robot.waitForIdle();
 247         frame.dispose();
 248     }
 249 
 250 
 251     public static void main(String[] args) throws Exception {
 252 
 253         MouseButtonsTest test = new MouseButtonsTest();
 254         test.doTest();
 255     }
 256 }