1 /*
   2  * Copyright (c) 2007, 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   @test
  26   @key headful
  27   @bug 6267983
  28   @summary PIT: MouseClicked is triggered when the mouse is released outside the tray icon, Win32
  29   @library    ../../../regtesthelpers
  30   @build      Util
  31   @run main MouseClickTest
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.util.concurrent.atomic.AtomicInteger;
  37 
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class MouseClickTest
  41 {
  42     private static final int TIMEOUT = 3000;
  43 
  44     public void start ()
  45     {
  46         runTests();
  47     }
  48 
  49     public static final void main(String args[])
  50     {
  51         MouseClickTest test = new MouseClickTest();
  52         test.start();
  53     }
  54 
  55     void runTests()
  56     {
  57         Frame frame = new Frame("frame");
  58         frame.setBounds(100, 100, 100, 100);
  59         frame.setVisible(true);
  60         frame.validate();
  61         Util.waitTillShown(frame);
  62 
  63         Robot robot = Util.createRobot();
  64         robot.setAutoDelay(10);
  65 
  66         oneButtonPressRelease(frame, robot, false, 1);
  67         oneButtonPressRelease(frame, robot, true, 0);
  68         twoButtonPressRelease(frame, robot, false, 2);
  69         twoButtonPressRelease(frame, robot, true, 1);
  70     }
  71 
  72     /*
  73      * The function tests that the MOUSE_CLICKED event is triggered
  74      * when the robot just makes one button click. Also it verifies that
  75      * no event is coming when dragging happens until button will be
  76      * relealed.
  77      */
  78     public static void oneButtonPressRelease(final Component comp, final Robot robot,
  79                                              final boolean dragging, final int EXPECTED_EVENT_COUNT)
  80     {
  81         final AtomicInteger eventCount = new AtomicInteger(0);
  82         final MouseAdapter adapter = new MouseEventAdapter(eventCount, EXPECTED_EVENT_COUNT);
  83         comp.addMouseListener(adapter);
  84 
  85         Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
  86 
  87         robot.mouseMove(bounds.x + bounds.width / 4, bounds.y + bounds.height / 2);
  88         robot.mousePress(InputEvent.BUTTON1_MASK);
  89         if (dragging) {
  90             robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
  91         }
  92         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  93 
  94         Util.waitForIdle(robot);
  95         waitForCondition(eventCount, EXPECTED_EVENT_COUNT, TIMEOUT);
  96 
  97         if (eventCount.get() != EXPECTED_EVENT_COUNT) {
  98             System.out.println("Wrong number of MouseClick events were generated: " +
  99                                eventCount.get() + ", expected: " + EXPECTED_EVENT_COUNT);
 100             throw new RuntimeException("Test failed!");
 101         }
 102 
 103         comp.removeMouseListener(adapter);
 104         System.out.println("Test passed.");
 105     }
 106 
 107     /*
 108      * The function tests sending of the MOUSE_CLICKED events when two
 109      * different mouse buttons are used to generate MOUSE_CLICKED event.
 110      * It verifies that number of coming MouseClick events equals to number
 111      * of mouse clicks are performed by the robot.
 112      */
 113     public static void twoButtonPressRelease(final Component comp, final Robot robot,
 114                                              final boolean dragging, final int EXPECTED_EVENT_COUNT)
 115     {
 116         final AtomicInteger eventCount = new AtomicInteger(0);
 117         final MouseAdapter adapter = new MouseEventAdapter(eventCount, EXPECTED_EVENT_COUNT);
 118         comp.addMouseListener(adapter);
 119 
 120         Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
 121 
 122         robot.mouseMove(bounds.x + bounds.width / 4, bounds.y + bounds.height / 2);
 123         robot.mousePress(InputEvent.BUTTON1_MASK);
 124         if (dragging) {
 125             robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
 126         }
 127         robot.mousePress(InputEvent.BUTTON2_MASK);
 128         robot.mouseRelease(InputEvent.BUTTON2_MASK);
 129         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 130 
 131         Util.waitForIdle(robot);
 132         waitForCondition(eventCount, EXPECTED_EVENT_COUNT, TIMEOUT);
 133 
 134         if (eventCount.get() != EXPECTED_EVENT_COUNT) {
 135             System.out.println("Wrong number of MouseClick events were generated: " +
 136                                eventCount.get() + ", expected: " + EXPECTED_EVENT_COUNT);
 137             throw new RuntimeException("Test failed!");
 138         }
 139 
 140         comp.removeMouseListener(adapter);
 141         System.out.println("Test passed.");
 142     }
 143 
 144     private static void waitForCondition(final AtomicInteger eventCount, int EXPECTED_EVENT_COUNT,
 145                                          long timeout)
 146     {
 147         synchronized (eventCount) {
 148             if (eventCount.get() != EXPECTED_EVENT_COUNT) {
 149                 try {
 150                     eventCount.wait(timeout);
 151                 } catch (InterruptedException e) {
 152                     System.out.println("Interrupted unexpectedly!");
 153                     throw new RuntimeException(e);
 154                 }
 155             }
 156         }
 157     }
 158 }
 159 
 160 class MouseEventAdapter extends MouseAdapter {
 161 
 162     private final int EXPECTED_EVENT_COUNT;
 163     private final AtomicInteger eventCount;
 164 
 165     public MouseEventAdapter(final AtomicInteger eventCount, final int EXPECTED_EVENT_COUNT) {
 166         this.EXPECTED_EVENT_COUNT = EXPECTED_EVENT_COUNT;
 167         this.eventCount = eventCount;
 168     }
 169 
 170     public void mouseClicked(MouseEvent e) {
 171         System.out.println(e);
 172         synchronized (eventCount) {
 173             if (eventCount.incrementAndGet() == EXPECTED_EVENT_COUNT) {
 174                 eventCount.notifyAll();
 175             }
 176         }
 177     }
 178 }