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