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