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