J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | } |
| 118 | |
| 119 | public static void main(String []s) |
| 120 | { |
| 121 | //LW case: |
| 122 | testCase(frame, jbutton); |
| 123 | //HW case |
| 124 | testCase(frame1, button1); |
| 125 | } |
| 126 | |
| 127 | private static void moveBetween(Robot r, Point first, Point second) { |
| 128 | Util.waitForIdle(r); |
| 129 | Util.mouseMove(r, first, second); |
| 130 | Util.waitForIdle(r); |
| 131 | Util.mouseMove(r, second, first); |
| 132 | Util.waitForIdle(r); |
| 133 | } |
| 134 | |
| 135 | // component should get exactly entered of Entered events and exited of Exited events. |
| 136 | private static void checkEvents(EnterExitAdapter adapter, int entered, int exited) { |
| 137 | if (adapter.getEnteredEventCount() != entered || |
| 138 | adapter.getExitedEventCount() != exited) |
| 139 | { |
| 140 | throw new RuntimeException(adapter.getTarget().getClass().getName()+": incorrect event number: Entered got: " + |
| 141 | adapter.getEnteredEventCount() +" expected : " + entered |
| 142 | + ". Exited got : " + adapter.getExitedEventCount() + " expected : " |
| 143 | + exited); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | |
| 150 | class EnterExitAdapter extends MouseAdapter { |
| 151 | private Component target; |
| 152 | private int enteredEventCount = 0; |
| 153 | private int exitedEventCount = 0; |
| 154 | |
| 155 | public EnterExitAdapter(Component target) { |
| 156 | this.target = target; |
| 157 | } |
| 158 | |
| 159 | public Component getTarget(){ |
| 160 | return target; |
| 161 | } |
| 162 | public int getEnteredEventCount(){ |
| 163 | return enteredEventCount; |
| 164 | } |
| 165 | |
| 166 | public int getExitedEventCount(){ |
| 167 | return exitedEventCount; |
| 168 | } |
| 169 | |
| 170 | public void zeroCounters(){ |
| 171 | System.out.println("Zeroeing on " +target.getClass().getName()); |
| 172 | enteredEventCount = 0; |
| 173 | exitedEventCount = 0; |
| 174 | } |
| 175 | |
| 176 | public void mouseEntered(MouseEvent e){ |
| 177 | System.out.println("Entered on " + e.getSource().getClass().getName()); |
| 178 | enteredEventCount ++; |
| 179 | } |
| 180 | public void mouseExited(MouseEvent e){ |
| 181 | System.out.println("Exited on " + e.getSource().getClass().getName()); |
| 182 | exitedEventCount ++; |
| 183 | } |
| 184 | } |