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