1 /*
   2  * Copyright (c) 2000, 2018, 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 4350402
  28   @summary Tests that mouse behavior on LW component
  29   @compile ../../../regtesthelpers/Util.java
  30   @run main RobotLWTest
  31 */
  32 
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import test.java.awt.regtesthelpers.Util;
  36 
  37 public class RobotLWTest {
  38 
  39     public static void main(String[] args) {
  40         RobotLWTest app = new RobotLWTest();
  41         app.start();
  42     }
  43 
  44     public void start ()
  45     {
  46         Frame frame = new Frame();
  47         MyLWContainer c = new MyLWContainer();
  48         MyLWComponent b = new MyLWComponent();
  49         c.add(b);
  50         frame.add(c);
  51         frame.setSize(400,400);
  52         frame.setLocationRelativeTo(null);
  53         frame.setVisible(true);
  54 
  55         try {
  56             Robot robot = new Robot();
  57             robot.setAutoWaitForIdle(true);
  58             robot.setAutoDelay(100);
  59             robot.waitForIdle();
  60 
  61             Util.waitForIdle(robot);
  62 
  63             Point pt = frame.getLocationOnScreen();
  64             Point pt1 = b.getLocationOnScreen();
  65 
  66             //Testing capture with multiple buttons
  67             robot.mouseMove(pt1.x + b.getWidth()/2, pt1.y + b.getHeight()/2);
  68             robot.mousePress(InputEvent.BUTTON1_MASK);
  69             robot.mousePress(InputEvent.BUTTON2_MASK);
  70             robot.mouseMove(pt.x + frame.getWidth()+10, pt.y + frame.getHeight()+10);
  71             robot.mouseRelease(InputEvent.BUTTON2_MASK);
  72             Util.waitForIdle(robot);
  73 
  74             b.last = null;
  75             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  76             Util.waitForIdle(robot);
  77 
  78             if (b.last == null) {
  79                 throw new RuntimeException("RobotLWTest failed. Mouse Capture failed");
  80             }
  81             //Enter/Exit
  82             b.last = null;
  83             robot.mouseMove(pt1.x + b.getWidth()/2, pt1.y + b.getHeight()/2);
  84             Util.waitForIdle(robot);
  85 
  86             if (b.last == null || b.last.getID() != MouseEvent.MOUSE_ENTERED) {
  87                 throw new RuntimeException("RobotLWTest failed. Enter/Exit failed");
  88             }
  89             b.last = b.prev = null;
  90             robot.mousePress(InputEvent.BUTTON1_MASK);
  91             Util.waitForIdle(robot);
  92 
  93             if (b.prev != null && b.prev.getID() == MouseEvent.MOUSE_ENTERED) {
  94                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
  95                 throw new RuntimeException("RobotLWTest failed. Enter/Exit failed");
  96             }
  97             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  98 
  99         } catch (Exception e) {
 100             throw new RuntimeException("The test was not completed.", e);
 101         }
 102     }// start()
 103 
 104 }// class RobotLWTest
 105 
 106 class MyLWContainer extends Container {
 107     public MouseEvent last = null;
 108     public MouseEvent prev = null;
 109 
 110     MyLWContainer() {
 111         enableEvents(MouseEvent.MOUSE_MOTION_EVENT_MASK);
 112     }
 113 
 114     public void processMouseEvent(MouseEvent e) {
 115         prev = last;
 116         last = e;
 117         System.out.println(e.toString());
 118         super.processMouseEvent(e);
 119     }
 120 }
 121 
 122 class MyLWComponent extends Component {
 123     public MouseEvent last = null;
 124     public MouseEvent prev = null;
 125 
 126     MyLWComponent() {
 127         setSize(50,30);
 128         enableEvents(MouseEvent.MOUSE_EVENT_MASK);
 129     }
 130 
 131     public void processMouseEvent(MouseEvent e) {
 132         prev = last;
 133         last = e;
 134         System.out.println(e.toString());
 135         super.processMouseEvent(e);
 136     }
 137 
 138     public void paint(Graphics g) {
 139         Dimension d = getSize();
 140         setBackground(isEnabled() ? Color.red : Color.gray);
 141         g.clearRect(0, 0, d.width - 1, d.height -1);
 142     }
 143 }