Skip to content

Commit b279b6e

Browse files
committed
8339836: Open source several AWT Mouse tests - Batch 1
Backport-of: 57c859e4adfedc963b1f4b3bf066453ace41ee36
1 parent 5834c23 commit b279b6e

File tree

5 files changed

+581
-0
lines changed

5 files changed

+581
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.BorderLayout;
25+
import java.awt.Color;
26+
import java.awt.Component;
27+
import java.awt.Container;
28+
import java.awt.Dimension;
29+
import java.awt.Frame;
30+
import java.awt.Graphics;
31+
import java.awt.GridLayout;
32+
import java.awt.Rectangle;
33+
import java.awt.event.MouseAdapter;
34+
import java.awt.event.MouseEvent;
35+
import java.awt.event.MouseListener;
36+
37+
/*
38+
* @test
39+
* @bug 4150851
40+
* @summary Tests enter and exit events when a lightweight component is on a border
41+
* @library /java/awt/regtesthelpers
42+
* @build PassFailJFrame
43+
* @run main/manual MouseEnterExitTest2
44+
*/
45+
46+
public class MouseEnterExitTest2 {
47+
48+
public static void main(String[] args) throws Exception {
49+
String INSTRUCTIONS = """
50+
1. Verify that white component turns black whenever mouse enters the frame,
51+
except when it enters the red rectangle.
52+
2. When the mouse enters the red part of the frame the component should stay white.
53+
""";
54+
PassFailJFrame.builder()
55+
.title("Test Instructions")
56+
.instructions(INSTRUCTIONS)
57+
.rows((int) INSTRUCTIONS.lines().count() + 2)
58+
.columns(35)
59+
.testUI(EntryExitTest.initialize())
60+
.build()
61+
.awaitAndCheck();
62+
}
63+
}
64+
65+
class EntryExitTest extends Component {
66+
boolean inWin;
67+
68+
public Dimension getPreferredSize() {
69+
return new Dimension(200, 150);
70+
}
71+
72+
public void paint(Graphics g) {
73+
Color c1, c2;
74+
String s;
75+
if (inWin) {
76+
c1 = Color.black;
77+
c2 = Color.white;
78+
s = "IN";
79+
} else {
80+
c2 = Color.black;
81+
c1 = Color.white;
82+
s = "OUT";
83+
}
84+
g.setColor(c1);
85+
Rectangle r = getBounds();
86+
g.fillRect(0, 0, r.width, r.height);
87+
g.setColor(c2);
88+
g.drawString(s, r.width / 2, r.height / 2);
89+
}
90+
91+
public static Frame initialize() {
92+
EntryExitTest test = new EntryExitTest();
93+
MouseListener frameEnterExitListener = new MouseAdapter() {
94+
public void mouseEntered(MouseEvent e) {
95+
test.inWin = true;
96+
test.repaint();
97+
}
98+
99+
public void mouseExited(MouseEvent e) {
100+
test.inWin = false;
101+
test.repaint();
102+
}
103+
};
104+
105+
Frame f = new Frame("Mouse Modifier Test");
106+
107+
f.add(test);
108+
Component jc = new Component() {
109+
public Dimension getPreferredSize() {
110+
return new Dimension(100, 50);
111+
}
112+
113+
public void paint(Graphics g) {
114+
Dimension d = getSize();
115+
g.setColor(Color.red);
116+
g.fillRect(0, 0, d.width, d.height);
117+
}
118+
};
119+
final Container cont = new Container() {
120+
public Dimension getPreferredSize() {
121+
return new Dimension(100, 100);
122+
}
123+
};
124+
cont.setLayout(new GridLayout(2, 1));
125+
cont.add(jc);
126+
jc.addMouseListener(new MouseAdapter() {
127+
public void mouseEntered(MouseEvent e) {
128+
//System.out.println("Component entered");
129+
}
130+
public void mouseExited(MouseEvent e) {
131+
//System.out.println("Component exited");
132+
}
133+
});
134+
135+
f.add(cont, BorderLayout.NORTH);
136+
f.addMouseListener(frameEnterExitListener);
137+
f.pack();
138+
return f;
139+
}
140+
}

0 commit comments

Comments
 (0)