|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2024, 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 | +import java.awt.Color; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Container; |
| 27 | +import java.awt.Cursor; |
| 28 | +import java.awt.Dimension; |
| 29 | +import java.awt.EventQueue; |
| 30 | +import java.awt.FlowLayout; |
| 31 | +import java.awt.Frame; |
| 32 | +import java.awt.Graphics; |
| 33 | +import java.awt.Robot; |
| 34 | +import java.awt.event.KeyAdapter; |
| 35 | +import java.awt.event.KeyEvent; |
| 36 | +import java.awt.event.MouseAdapter; |
| 37 | +import java.awt.event.MouseEvent; |
| 38 | +import java.awt.event.WindowAdapter; |
| 39 | +import java.awt.event.WindowEvent; |
| 40 | + |
| 41 | +/* |
| 42 | + * @test |
| 43 | + * @bug 4050138 |
| 44 | + * @key headful |
| 45 | + * @summary Test to verify Lightweight components don't get |
| 46 | + * enter/exit during drags |
| 47 | + * @run main MouseEnterExitTest |
| 48 | + */ |
| 49 | + |
| 50 | +class LWSquare extends Container { |
| 51 | + int width; |
| 52 | + int height; |
| 53 | + |
| 54 | + public LWSquare(Color color, int w, int h) { |
| 55 | + setBackground(color); |
| 56 | + setLayout(new FlowLayout()); |
| 57 | + width = w; |
| 58 | + height = h; |
| 59 | + addMouseListener(new EnterExitAdapter(this)); |
| 60 | + setName("LWSquare-" + color.toString()); |
| 61 | + } |
| 62 | + |
| 63 | + public void paint(Graphics g) { |
| 64 | + g.setColor(getBackground()); |
| 65 | + g.fillRect(0, 0, getSize().width, getSize().height); |
| 66 | + super.paint(g); |
| 67 | + } |
| 68 | + |
| 69 | + public Dimension getPreferredSize() { |
| 70 | + return new Dimension(width, height); |
| 71 | + } |
| 72 | + |
| 73 | + public Cursor getCursor() { |
| 74 | + return new Cursor(Cursor.CROSSHAIR_CURSOR); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +class MouseFrame extends Frame { |
| 79 | + public LWSquare lw; |
| 80 | + |
| 81 | + public MouseFrame() { |
| 82 | + super("MouseEnterExitTest"); |
| 83 | + setLayout(new FlowLayout()); |
| 84 | + |
| 85 | + lw = new LWSquare(Color.red, 75, 75); |
| 86 | + add(lw); |
| 87 | + setBounds(50, 50, 300, 200); |
| 88 | + setVisible(true); |
| 89 | + System.out.println(getInsets()); |
| 90 | + |
| 91 | + addMouseListener(new EnterExitAdapter(this)); |
| 92 | + addWindowListener( |
| 93 | + new WindowAdapter() { |
| 94 | + public void windowClosing(WindowEvent ev) { |
| 95 | + dispose(); |
| 96 | + } |
| 97 | + } |
| 98 | + ); |
| 99 | + addKeyListener( |
| 100 | + new KeyAdapter() { |
| 101 | + public void keyPressed(KeyEvent ev) { |
| 102 | + MouseEnterExitTest.getFrame().setTitle("MouseEnterExitTest"); |
| 103 | + } |
| 104 | + } |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +public class MouseEnterExitTest { |
| 111 | + static MouseFrame testFrame; |
| 112 | + |
| 113 | + public static void main(String[] args) throws Exception { |
| 114 | + Robot robot = new Robot(); |
| 115 | + |
| 116 | + robot.setAutoDelay(100); |
| 117 | + try { |
| 118 | + EventQueue.invokeAndWait(() -> testFrame = new MouseFrame()); |
| 119 | + if (testFrame.lw.getBackground() != Color.red) { |
| 120 | + throw new RuntimeException("Initial Background color not matching"); |
| 121 | + } |
| 122 | + robot.waitForIdle(); |
| 123 | + robot.delay(100); |
| 124 | + EventQueue.invokeAndWait(() -> robot.mouseMove( |
| 125 | + testFrame.getLocationOnScreen().x + testFrame.getSize().width / 2, |
| 126 | + testFrame.getLocationOnScreen().y + testFrame.getSize().height / 2)); |
| 127 | + robot.waitForIdle(); |
| 128 | + robot.delay(100); |
| 129 | + |
| 130 | + if (testFrame.lw.getBackground() != Color.green) { |
| 131 | + throw new RuntimeException("Initial Background color not matching"); |
| 132 | + } |
| 133 | + EventQueue.invokeAndWait(() -> robot.mouseMove( |
| 134 | + testFrame.getLocationOnScreen().x + testFrame.getSize().width * 2, |
| 135 | + testFrame.getLocationOnScreen().y + testFrame.getSize().height / 2)); |
| 136 | + robot.waitForIdle(); |
| 137 | + robot.delay(100); |
| 138 | + |
| 139 | + if (testFrame.lw.getBackground() != Color.red) { |
| 140 | + throw new RuntimeException("Initial Background color not matching"); |
| 141 | + } |
| 142 | + } finally { |
| 143 | + EventQueue.invokeAndWait(() -> { |
| 144 | + if (testFrame != null) { |
| 145 | + testFrame.dispose(); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static Frame getFrame() { |
| 152 | + return testFrame; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +class EnterExitAdapter extends MouseAdapter { |
| 157 | + Component compToColor; |
| 158 | + Color colorNormal; |
| 159 | + |
| 160 | + EnterExitAdapter(Component comp) { |
| 161 | + compToColor = comp; |
| 162 | + colorNormal = comp.getBackground(); |
| 163 | + } |
| 164 | + |
| 165 | + public void mouseEntered(MouseEvent ev) { |
| 166 | + compToColor.setBackground(Color.green); |
| 167 | + compToColor.repaint(); |
| 168 | + } |
| 169 | + |
| 170 | + public void mouseExited(MouseEvent ev) { |
| 171 | + compToColor.setBackground(colorNormal); |
| 172 | + compToColor.repaint(); |
| 173 | + } |
| 174 | +} |
0 commit comments