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