1 /*
   2  * Copyright (c) 2007, 2010, 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 6479820
  28   @library ../../../regtesthelpers
  29   @build Util
  30   @summary verify that enter/exit events still comes correctly
  31   @author andrei dmitriev: area=awt.event
  32   @run main SpuriousExitEnter_3
  33 */
  34 
  35 /**
  36  * SpuriousExitEnter_3.java
  37  *
  38             "There is a plain JFrame with JButton in it.",
  39             "Let A area is the area inside JButton.",
  40             "Let B area is the area inside JFrame but not inside JButton.",
  41             "Let C area is the area outside JFrame.",
  42             "Now check that the correct events and are in the correct number generates when you",
  43             "move the pointer between those areas.",
  44             " 1) Verify that the Enter and Exit events comes to JButton and JFrame when ",
  45             " you move the pointer between A and B areas.",
  46             " 2) Verify that the Enter and Exit events comes to JButton when you",
  47             " move the pointer between A to C.",
  48             " 3) Verify that the Enter and Exit events comes to JFrame when you",
  49             " move the pointer between B to C.",
  50  */
  51 
  52 import java.awt.*;
  53 import java.awt.event.*;
  54 import test.java.awt.regtesthelpers.Util;
  55 import javax.swing.*;
  56 
  57 public class SpuriousExitEnter_3 {
  58     static JFrame frame = new JFrame("SpuriousExitEnter_3_LW");
  59     static JButton jbutton = new JButton("jbutton");
  60     static Frame frame1 = new Frame("SpuriousExitEnter_3_HW");
  61     static Button button1 = new Button("button");
  62 
  63     static EnterExitAdapter frameAdapter;
  64     static EnterExitAdapter buttonAdapter;
  65     static Robot r = Util.createRobot();
  66 
  67     public static void testCase(Window w, Component comp) {
  68         frameAdapter = new EnterExitAdapter(w);
  69         buttonAdapter = new EnterExitAdapter(comp);
  70 
  71         w.addMouseListener(frameAdapter);
  72         comp.addMouseListener(buttonAdapter);
  73 
  74         w.setSize(200, 200);
  75         w.add(comp, BorderLayout.NORTH);
  76         w.setLocationRelativeTo(null);
  77         w.setVisible(true);
  78 
  79         Point centerA = new Point(comp.getLocationOnScreen().x + comp.getWidth() / 2,
  80                                   comp.getLocationOnScreen().y + comp.getHeight() / 2);
  81         Point centerB = new Point(w.getLocationOnScreen().x + w.getWidth() / 2,
  82                                   w.getLocationOnScreen().y + w.getHeight() / 2);
  83         //for moving from A outside: don't cross the A area. Move straight to the right.
  84         Point centerC_1 = new Point(w.getLocationOnScreen().x + w.getWidth() + 20,  //go right off the border
  85                                     comp.getLocationOnScreen().y + comp.getHeight() / 2); //don't cross the A area!
  86 
  87         //for moving from B outside: don't cross the B area. Move straight to the bottom.
  88         Point centerC_2 = new Point(w.getLocationOnScreen().x + w.getWidth() / 2,
  89                                     w.getLocationOnScreen().y + w.getHeight() + 20); //go below the bottom border
  90         //A and B areas
  91         Util.pointOnComp(comp, r);
  92         Util.waitForIdle(r);
  93         frameAdapter.zeroCounters();
  94         buttonAdapter.zeroCounters();
  95 
  96         moveBetween(r, centerA, centerB);
  97         checkEvents(frameAdapter, 1, 1);
  98         checkEvents(buttonAdapter, 1, 1);
  99 
 100         //A and C areas
 101         Util.pointOnComp(comp, r);
 102         Util.waitForIdle(r);
 103         frameAdapter.zeroCounters();
 104         buttonAdapter.zeroCounters();
 105         moveBetween(r, centerA, centerC_1);
 106         checkEvents(frameAdapter, 0, 0);
 107         checkEvents(buttonAdapter, 1, 1);
 108 
 109         //B and C areas
 110         Util.pointOnComp(w, r);
 111         Util.waitForIdle(r);
 112         frameAdapter.zeroCounters();
 113         buttonAdapter.zeroCounters();
 114         moveBetween(r, centerB, centerC_2);
 115         checkEvents(frameAdapter, 1, 1);
 116         checkEvents(buttonAdapter, 0, 0);
 117         w.setVisible(false);
 118         Util.waitForIdle(r);
 119     }
 120 
 121     public static void main(String []s)
 122     {
 123         //LW case:
 124         testCase(frame, jbutton);
 125         //HW case
 126         testCase(frame1, button1);
 127     }
 128 
 129     private static void moveBetween(Robot r, Point first, Point second) {
 130         Util.waitForIdle(r);
 131         Util.mouseMove(r, first, second);
 132         Util.waitForIdle(r);
 133         Util.mouseMove(r, second, first);
 134         Util.waitForIdle(r);
 135     }
 136 
 137     // component should get exactly entered of Entered events and exited of Exited events.
 138     private static void checkEvents(EnterExitAdapter adapter, int entered, int exited) {
 139         if (adapter.getEnteredEventCount() != entered ||
 140             adapter.getExitedEventCount() != exited)
 141         {
 142             throw new RuntimeException(adapter.getTarget().getClass().getName()+": incorrect event number: Entered got: " +
 143                                        adapter.getEnteredEventCount() +" expected : " + entered
 144                                        + ". Exited got : " + adapter.getExitedEventCount() + " expected : "
 145                                        + exited);
 146         }
 147     }
 148 
 149 }
 150 
 151 
 152 class EnterExitAdapter extends MouseAdapter {
 153     private Component target;
 154     private int enteredEventCount = 0;
 155     private int exitedEventCount = 0;
 156 
 157     public EnterExitAdapter(Component target) {
 158         this.target = target;
 159     }
 160 
 161     public Component getTarget(){
 162         return target;
 163     }
 164     public int getEnteredEventCount(){
 165         return enteredEventCount;
 166     }
 167 
 168     public int getExitedEventCount(){
 169         return exitedEventCount;
 170     }
 171 
 172     public void zeroCounters(){
 173         System.out.println("Zeroeing on " +target.getClass().getName());
 174         enteredEventCount = 0;
 175         exitedEventCount = 0;
 176     }
 177 
 178     public void mouseEntered(MouseEvent e){
 179         System.out.println("Entered on " + e.getSource().getClass().getName());
 180         enteredEventCount ++;
 181     }
 182     public void mouseExited(MouseEvent e){
 183         System.out.println("Exited on " + e.getSource().getClass().getName());
 184         exitedEventCount ++;
 185     }
 186 }