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