Skip to content

Commit d03128d

Browse files
author
Alexander Zvegintsev
committed
8306280: Open source several choice AWT tests
Reviewed-by: jdv, prr, serb
1 parent e764e9b commit d03128d

File tree

4 files changed

+435
-0
lines changed

4 files changed

+435
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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 4908468
27+
@summary Linux Empty Choice throws NPE
28+
@key headful
29+
@run main EmptyChoiceTest
30+
*/
31+
import java.awt.BorderLayout;
32+
import java.awt.Choice;
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Graphics;
36+
import java.awt.Image;
37+
import java.lang.reflect.InvocationTargetException;
38+
39+
public class EmptyChoiceTest
40+
{
41+
Frame frame;
42+
Choice choice = null;
43+
44+
public static void main(String[] args) throws
45+
InterruptedException,
46+
InvocationTargetException {
47+
EventQueue.invokeAndWait(() -> {
48+
EmptyChoiceTest emptyChoiceTest = new EmptyChoiceTest();
49+
emptyChoiceTest.init();
50+
emptyChoiceTest.test();
51+
});
52+
}
53+
54+
public void init() {
55+
frame = new Frame();
56+
frame.setLayout(new BorderLayout());
57+
choice = new Choice();
58+
frame.add(choice, BorderLayout.NORTH);
59+
frame.setSize(200, 200);
60+
frame.setLocationRelativeTo(null);
61+
frame.setVisible(true);
62+
frame.validate();
63+
}
64+
65+
public void test () {
66+
try {
67+
int iWidth = choice.getWidth();
68+
int iHeight = choice.getHeight();
69+
Image componentImage =
70+
choice.createImage(iWidth, iHeight);
71+
Graphics graphics =
72+
componentImage.getGraphics();
73+
graphics.setClip(0, 0, iWidth, iHeight);
74+
choice.printAll(graphics);
75+
System.out.println("PrintAll successful!");
76+
} catch (NullPointerException exp) {
77+
throw new RuntimeException("Test failed. " +
78+
"Empty Choice printAll throws NullPointerException");
79+
} catch (Exception exc){
80+
throw new RuntimeException("Test failed.", exc);
81+
} finally {
82+
frame.dispose();
83+
}
84+
}
85+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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 4115130
27+
@summary Tests Inserting/Removing items doesn't cause crash.
28+
@key headful
29+
@run main InsertRemoveTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Choice;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.lang.reflect.InvocationTargetException;
37+
38+
public class InsertRemoveTest {
39+
Choice choice1;
40+
Choice choice2;
41+
Choice choice3;
42+
Frame f;
43+
int itemCount = 0;
44+
int iterCount = 0;
45+
46+
public static void main(String[] args)
47+
throws InterruptedException, InvocationTargetException {
48+
EventQueue.invokeAndWait(() -> new InsertRemoveTest().start());
49+
}
50+
51+
public void start() {
52+
f = new Frame("Check Choice");
53+
f.setLayout(new BorderLayout());
54+
55+
choice1 = new Choice();
56+
choice2 = new Choice();
57+
choice3 = new Choice();
58+
59+
f.add(choice1, BorderLayout.NORTH);
60+
f.add(choice3, BorderLayout.CENTER);
61+
f.add(choice2, BorderLayout.SOUTH);
62+
63+
f.pack();
64+
f.setLocationRelativeTo(null);
65+
f.setVisible(true);
66+
67+
try {
68+
for (int i = 0; i < 50; i++) {
69+
if (choice1 != null && itemCount < 40) {
70+
choice1.insert("I am Choice, yes I am : " + iterCount,
71+
0);
72+
choice2.add("I am the same, yes I am : " + iterCount);
73+
choice3.insert("I am the same, yes I am : " + iterCount,
74+
10);
75+
itemCount++;
76+
iterCount++;
77+
}
78+
if (itemCount >= 20 && choice1 != null
79+
&& choice1.getItemCount() > 0) {
80+
choice1.remove(0);
81+
choice2.remove(10);
82+
choice3.remove(19);
83+
itemCount--;
84+
}
85+
f.validate();
86+
}
87+
} finally {
88+
f.dispose();
89+
}
90+
}
91+
92+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2005, 2023, 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 6246503
27+
@summary Disabling a choice after selection locks keyboard, \
28+
mouse and makes the system unusable
29+
@key headful
30+
@run main OpenedChoiceHangs
31+
*/
32+
33+
import java.awt.AWTException;
34+
import java.awt.Button;
35+
import java.awt.Choice;
36+
import java.awt.Color;
37+
import java.awt.EventQueue;
38+
import java.awt.FlowLayout;
39+
import java.awt.Frame;
40+
import java.awt.Robot;
41+
import java.awt.event.FocusEvent;
42+
import java.awt.event.InputEvent;
43+
import java.awt.event.ItemEvent;
44+
import java.awt.event.ItemListener;
45+
import java.awt.event.KeyEvent;
46+
import java.lang.reflect.InvocationTargetException;
47+
48+
public class OpenedChoiceHangs implements ItemListener {
49+
static final Object FOCUS_LOCK = new Object();
50+
51+
Frame frame;
52+
Choice ch = new Choice();
53+
Button b = new Button("A button");
54+
Robot robot;
55+
56+
public static void main(String[] args)
57+
throws InterruptedException, InvocationTargetException {
58+
OpenedChoiceHangs openedChoiceHangs = new OpenedChoiceHangs();
59+
EventQueue.invokeAndWait(openedChoiceHangs::init);
60+
openedChoiceHangs.test();
61+
}
62+
63+
public void init() {
64+
frame = new Frame();
65+
66+
frame.setLayout(new FlowLayout());
67+
for (int i = 1; i < 10; i++) {
68+
ch.add("item " + i);
69+
}
70+
frame.add(ch);
71+
frame.add(b);
72+
ch.setBackground(new Color(255, 0, 0));
73+
ch.setForeground(new Color(255, 0, 0));
74+
ch.addItemListener(this);
75+
76+
frame.setSize(200, 200);
77+
frame.setVisible(true);
78+
frame.setLocationRelativeTo(null);
79+
frame.validate();
80+
}
81+
82+
public void test() {
83+
try {
84+
robot = new Robot();
85+
robot.setAutoDelay(100);
86+
robot.setAutoWaitForIdle(true);
87+
robot.delay(1000);
88+
robot.mouseMove(ch.getLocationOnScreen().x + ch.getWidth() / 2,
89+
ch.getLocationOnScreen().y + ch.getHeight() / 2);
90+
91+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
92+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
93+
robot.delay(1000);
94+
if (!ch.isFocusOwner()) {
95+
synchronized (FOCUS_LOCK) {
96+
FOCUS_LOCK.wait(3000);
97+
}
98+
}
99+
if (!ch.isFocusOwner()){
100+
throw new RuntimeException(
101+
"Test failed. Choice has no focus after mouse press.");
102+
}
103+
robot.keyPress(KeyEvent.VK_DOWN);
104+
robot.keyRelease(KeyEvent.VK_DOWN);
105+
robot.delay(1000);
106+
107+
robot.keyPress(KeyEvent.VK_UP);
108+
robot.keyRelease(KeyEvent.VK_UP);
109+
robot.delay(1000);
110+
111+
Color color = robot.getPixelColor(
112+
ch.getLocationOnScreen().x + ch.getWidth() / 2,
113+
ch.getLocationOnScreen().y + ch.getHeight() * 4);
114+
System.out.println("Color is " + color);
115+
if (color.equals(new Color(255, 0,0))){
116+
throw new RuntimeException(
117+
"Test failed. Choice is disabled and still opened. ");
118+
}
119+
} catch (AWTException e) {
120+
throw new RuntimeException(
121+
"Test interrupted due to AWTException. Robot=" + robot, e);
122+
} catch (InterruptedException e) {
123+
throw new RuntimeException("Test interrupted. Robot=" + robot, e);
124+
} finally {
125+
EventQueue.invokeLater(frame::dispose);
126+
}
127+
128+
System.out.println("Test passed: Choice became closed after disabling.");
129+
}
130+
131+
public void itemStateChanged (ItemEvent ie) {
132+
System.out.println("Choice Item has changed: "+ie);
133+
ch.setEnabled(false);
134+
}
135+
public void focusGained(FocusEvent fEvent){
136+
System.out.println("focusGained"+fEvent);
137+
synchronized(FOCUS_LOCK){
138+
FOCUS_LOCK.notify();
139+
}
140+
}
141+
142+
public void focusLost(FocusEvent fEvent){
143+
System.out.println("focusLost"+fEvent);
144+
}
145+
}

0 commit comments

Comments
 (0)