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