Skip to content

Commit df6cdbe

Browse files
committed
8339794: Open source closed choice tests openjdk#1
Backport-of: 5e5942a
1 parent 0bba486 commit df6cdbe

File tree

3 files changed

+383
-0
lines changed

3 files changed

+383
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 1998, 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.Choice;
25+
import java.awt.EventQueue;
26+
import java.awt.Frame;
27+
import java.awt.GridLayout;
28+
import java.awt.Label;
29+
import java.awt.Robot;
30+
31+
/*
32+
* @test
33+
* @bug 4082078
34+
* @summary Test for bug(s): 4082078, Multiple calls to Choice.insert cause core dump
35+
* @key headful
36+
* @run main ChoiceInsertTest
37+
*/
38+
39+
public class ChoiceInsertTest extends Frame {
40+
Choice c;
41+
Label l;
42+
43+
private static ChoiceInsertTest choiceInsertTest;
44+
45+
public ChoiceInsertTest() {
46+
c = new Choice();
47+
l = new Label("If you see this, the choice insert bug is fixed!");
48+
c.add("Initial choice");
49+
add(c);
50+
}
51+
52+
public void testInsertion() {
53+
// inserting 30 or so items aborts Solaris VM
54+
// in JDK's before 1.1.5
55+
for (int nchoice = 0; nchoice < 30; nchoice++) {
56+
c.insert("new choice", 0);
57+
}
58+
// if you made it to here the bug is not there anymore...
59+
remove(l);
60+
add(l);
61+
validate();
62+
}
63+
64+
public static void main(String[] args) throws Exception {
65+
Robot robot = new Robot();
66+
try {
67+
EventQueue.invokeAndWait(() ->{
68+
choiceInsertTest = new ChoiceInsertTest();
69+
choiceInsertTest.setTitle("ChoiceInsertTest");
70+
choiceInsertTest.setLocationRelativeTo(null);
71+
choiceInsertTest.setSize(500, 300);
72+
choiceInsertTest.setLayout(new GridLayout());
73+
choiceInsertTest.setVisible(true);
74+
});
75+
robot.waitForIdle();
76+
robot.delay(500);
77+
EventQueue.invokeAndWait(choiceInsertTest::testInsertion);
78+
robot.delay(1000);
79+
} finally {
80+
EventQueue.invokeAndWait(() -> {
81+
if (choiceInsertTest != null) {
82+
choiceInsertTest.dispose();
83+
}
84+
});
85+
}
86+
87+
System.err.println("ChoiceInsertTest: Didn't abort VM inserting 30 items, so we passed!");
88+
}
89+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2000, 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+
25+
import java.awt.BorderLayout;
26+
import java.awt.Choice;
27+
import java.awt.EventQueue;
28+
import java.awt.Frame;
29+
import java.awt.Point;
30+
import java.awt.event.InputEvent;
31+
import java.awt.event.MouseAdapter;
32+
import java.awt.event.MouseEvent;
33+
34+
/*
35+
* @test
36+
* @bug 4328557
37+
* @summary Tests that MouseDragged and MouseReleased are triggered on choice
38+
* @library /lib/client
39+
* @build ExtendedRobot
40+
* @key headful
41+
* @run main ChoiceMouseDragTest
42+
*/
43+
44+
45+
public class ChoiceMouseDragTest extends Frame {
46+
private static final Choice choice = new Choice();
47+
48+
private static ExtendedRobot robot;
49+
private volatile boolean isDragged;
50+
private volatile boolean isReleased;
51+
52+
private static volatile ChoiceMouseDragTest choiceMouseDragTest;
53+
54+
public ChoiceMouseDragTest() {
55+
super("ChoiceMouseDragTest");
56+
this.setLayout(new BorderLayout());
57+
choice.add("item-1");
58+
choice.add("item-2");
59+
choice.add("item-3");
60+
choice.add("item-4");
61+
add("Center", choice);
62+
choice.addMouseListener(new MouseEventHandler());
63+
choice.addMouseMotionListener(new MouseMotionEventHandler());
64+
setSize(400, 200);
65+
setLocationRelativeTo(null);
66+
setVisible(true);
67+
}
68+
69+
public static void main(String[] args) throws Exception {
70+
try {
71+
EventQueue.invokeAndWait(() ->
72+
choiceMouseDragTest = new ChoiceMouseDragTest());
73+
74+
robot = new ExtendedRobot();
75+
robot.waitForIdle();
76+
robot.delay(500);
77+
78+
Point pointToDrag = choice.getLocationOnScreen();
79+
pointToDrag.x += choice.getWidth() - 10;
80+
pointToDrag.y += choice.getHeight() / 2 ;
81+
82+
choiceMouseDragTest.test(InputEvent.BUTTON3_DOWN_MASK, pointToDrag);
83+
choiceMouseDragTest.test(InputEvent.BUTTON1_DOWN_MASK, pointToDrag);
84+
} finally {
85+
EventQueue.invokeAndWait(() -> {
86+
if (choiceMouseDragTest != null) {
87+
choiceMouseDragTest.dispose();
88+
}
89+
});
90+
}
91+
}
92+
93+
void test(int buttonToTest, Point pointToDrag) {
94+
isDragged = false;
95+
isReleased = false;
96+
97+
robot.mouseMove(pointToDrag.x, pointToDrag.y);
98+
robot.waitForIdle();
99+
100+
robot.mousePress(buttonToTest);
101+
102+
robot.glide(pointToDrag.x + 100, pointToDrag.y);
103+
robot.waitForIdle();
104+
105+
robot.mouseRelease(buttonToTest);
106+
robot.waitForIdle();
107+
108+
if (!isReleased || !isDragged) {
109+
throw new RuntimeException(("Test failed: button %d dragged(received %b) or " +
110+
"released(received %b)")
111+
.formatted(buttonToTest, isDragged, isReleased));
112+
}
113+
114+
robot.delay(500);
115+
}
116+
117+
class MouseEventHandler extends MouseAdapter {
118+
public void mousePressed(MouseEvent me) {
119+
System.out.println(me.paramString());
120+
}
121+
122+
public void mouseReleased(MouseEvent me) {
123+
System.out.println(me.paramString());
124+
isReleased = true;
125+
}
126+
127+
public void mouseClicked(MouseEvent me) {
128+
System.out.println(me.paramString());
129+
}
130+
}
131+
132+
class MouseMotionEventHandler extends MouseAdapter {
133+
public void mouseDragged(MouseEvent me) {
134+
System.out.println(me.paramString());
135+
isDragged = true;
136+
}
137+
}
138+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright (c) 2005, 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.Choice;
25+
import java.awt.EventQueue;
26+
import java.awt.FlowLayout;
27+
import java.awt.Frame;
28+
import java.awt.Point;
29+
import java.awt.Robot;
30+
import java.awt.Toolkit;
31+
import java.awt.event.InputEvent;
32+
import java.awt.event.KeyEvent;
33+
import java.awt.event.MouseWheelEvent;
34+
import java.awt.event.MouseWheelListener;
35+
36+
/*
37+
* @test
38+
* @bug 6253211
39+
* @summary PIT: MouseWheel events not triggered for Choice drop down in XAWT
40+
* @requires (os.family == "linux")
41+
* @key headful
42+
* @run main WheelEventsConsumed
43+
*/
44+
45+
public class WheelEventsConsumed extends Frame implements MouseWheelListener
46+
{
47+
Robot robot;
48+
Choice choice1 = new Choice();
49+
Point pt;
50+
final static int delay = 100;
51+
boolean mouseWheeled = false;
52+
final static int OUTSIDE_CHOICE = 1;
53+
final static int INSIDE_LIST_OF_CHOICE = 2;
54+
final static int INSIDE_CHOICE_COMPONENT = 3;
55+
static String toolkit;
56+
57+
private static volatile WheelEventsConsumed frame = null;
58+
59+
public static void main(String[] args) throws Exception {
60+
toolkit = Toolkit.getDefaultToolkit().getClass().getName();
61+
try {
62+
EventQueue.invokeAndWait(() -> {
63+
frame = new WheelEventsConsumed();
64+
frame.initAndShow();
65+
});
66+
frame.test();
67+
} finally {
68+
EventQueue.invokeAndWait(() -> {
69+
if (frame != null) {
70+
frame.dispose();
71+
}
72+
});
73+
}
74+
}
75+
76+
public void mouseWheelMoved(MouseWheelEvent me) {
77+
mouseWheeled = true;
78+
System.out.println(me);
79+
}
80+
81+
public void initAndShow() {
82+
setTitle("WheelEventsConsumed test");
83+
for (int i = 1; i < 10; i++) {
84+
choice1.add("item-0" + i);
85+
}
86+
87+
choice1.addMouseWheelListener(this);
88+
add(choice1);
89+
setLayout(new FlowLayout());
90+
setSize(200, 200);
91+
setLocationRelativeTo(null);
92+
setVisible(true);
93+
validate();
94+
}
95+
96+
public void test() {
97+
try {
98+
robot = new Robot();
99+
robot.setAutoWaitForIdle(true);
100+
robot.setAutoDelay(50);
101+
robot.waitForIdle();
102+
robot.delay(delay * 5);
103+
testMouseWheel(1, OUTSIDE_CHOICE);
104+
robot.delay(delay);
105+
testMouseWheel(-1, INSIDE_LIST_OF_CHOICE);
106+
robot.delay(delay);
107+
testMouseWheel(1, INSIDE_CHOICE_COMPONENT);
108+
robot.delay(delay);
109+
} catch (Throwable e) {
110+
throw new RuntimeException("Test failed. Exception thrown: " + e);
111+
}
112+
}
113+
114+
public void testMouseWheel(int amt, int mousePosition) {
115+
pt = choice1.getLocationOnScreen();
116+
robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2);
117+
118+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
119+
robot.delay(50);
120+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
121+
robot.delay(50);
122+
123+
switch (mousePosition) {
124+
case OUTSIDE_CHOICE:
125+
robot.mouseMove(pt.x + choice1.getWidth() * 3 / 2, pt.y + choice1.getHeight() / 2);
126+
break;
127+
case INSIDE_LIST_OF_CHOICE:
128+
robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() * 4);
129+
break;
130+
case INSIDE_CHOICE_COMPONENT:
131+
robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2);
132+
break;
133+
}
134+
135+
robot.delay(delay);
136+
for (int i = 0; i < 10; i++) {
137+
robot.mouseWheel(amt);
138+
robot.delay(delay);
139+
}
140+
141+
if (!mouseWheeled) {
142+
if (toolkit.equals("sun.awt.windows.WToolkit") && mousePosition == OUTSIDE_CHOICE) {
143+
System.out.println("Passed. Separate case on Win32. Choice generated MouseWheel events" + mousePosition);
144+
} else {
145+
throw new RuntimeException("Test failed. Choice should generate MOUSE_WHEEL events." + mousePosition);
146+
}
147+
} else {
148+
System.out.println("Passed. Choice generated MouseWheel events" + mousePosition);
149+
}
150+
robot.keyPress(KeyEvent.VK_ESCAPE);
151+
robot.delay(10);
152+
robot.keyRelease(KeyEvent.VK_ESCAPE);
153+
robot.delay(200);
154+
mouseWheeled = false;
155+
}
156+
}

0 commit comments

Comments
 (0)