Skip to content

Commit a306f88

Browse files
Srinivas Mandalikaprsadhuk
authored andcommitted
8339791: Refactor MiscUndecorated/ActiveAWTWindowTest.java
Reviewed-by: psadhukhan
1 parent a355edb commit a306f88

File tree

1 file changed

+71
-134
lines changed

1 file changed

+71
-134
lines changed

test/jdk/java/awt/Frame/MiscUndecorated/ActiveAWTWindowTest.java

Lines changed: 71 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -24,120 +24,97 @@
2424
/*
2525
* @test
2626
* @key headful
27-
* @summary To check proper WINDOW_EVENTS are triggered when Frame gains or losses the focus
28-
* @library /lib/client
29-
* @build ExtendedRobot
27+
* @summary To check proper WINDOW_EVENTS are triggered when Frame gains
28+
* or loses the focus
3029
* @run main ActiveAWTWindowTest
3130
*/
3231

32+
import java.awt.AWTException;
3333
import java.awt.BorderLayout;
3434
import java.awt.Button;
3535
import java.awt.Color;
36+
import java.awt.Component;
37+
import java.awt.Dimension;
3638
import java.awt.EventQueue;
3739
import java.awt.FlowLayout;
3840
import java.awt.Frame;
41+
import java.awt.Point;
42+
import java.awt.Robot;
3943
import java.awt.TextField;
4044
import java.awt.event.ActionEvent;
4145
import java.awt.event.ActionListener;
4246
import java.awt.event.InputEvent;
4347
import java.awt.event.WindowAdapter;
4448
import java.awt.event.WindowEvent;
4549
import java.awt.event.WindowFocusListener;
50+
import java.util.concurrent.CountDownLatch;
51+
import java.util.concurrent.TimeUnit;
52+
53+
import javax.swing.JButton;
54+
import javax.swing.JComponent;
4655

4756
public class ActiveAWTWindowTest {
4857

49-
private Frame frame, frame2;
50-
private Button button, button2;
51-
private TextField textField, textField2;
52-
private volatile int eventType;
53-
private final Object lock1 = new Object();
54-
private final Object lock2 = new Object();
55-
private final Object lock3 = new Object();
56-
private boolean passed = true;
57-
private final int delay = 150;
58+
private static Frame frame, frame2;
59+
private static Button button, button2;
60+
private static TextField textField, textField2;
5861

59-
public static void main(String[] args) throws Exception {
60-
ActiveAWTWindowTest test = new ActiveAWTWindowTest();
61-
try {
62-
test.doTest();
63-
} finally {
64-
EventQueue.invokeAndWait(() -> {
65-
if (test.frame != null) {
66-
test.frame.dispose();
67-
}
68-
if (test.frame2 != null) {
69-
test.frame2.dispose();
70-
}
71-
});
72-
}
73-
}
62+
private static CountDownLatch windowActivatedLatch = new CountDownLatch(1);
63+
private static CountDownLatch windowDeactivatedLatch = new CountDownLatch(1);
64+
private static CountDownLatch windowFocusGainedLatch = new CountDownLatch(1);
7465

75-
public ActiveAWTWindowTest() {
76-
try{
77-
EventQueue.invokeAndWait( () -> {
78-
initializeGUI();
79-
});
80-
} catch (Exception e) {
81-
e.printStackTrace();
82-
throw new RuntimeException("Interrupted or unexpected Exception occured");
83-
}
66+
public static void main(String[] args) throws Exception {
67+
EventQueue.invokeAndWait(() -> {
68+
initializeGUI();
69+
});
70+
doTest();
71+
EventQueue.invokeAndWait(() -> {
72+
if (frame != null) {
73+
frame.dispose();
74+
}
75+
if (frame2 != null) {
76+
frame2.dispose();
77+
}
78+
});
8479
}
8580

86-
private void initializeGUI() {
81+
private static void initializeGUI() {
8782
frame = new Frame();
8883
frame.setLayout(new FlowLayout());
89-
9084
frame.setLocation(5, 20);
9185
frame.setSize(200, 200);
9286
frame.setUndecorated(true);
87+
9388
frame.addWindowFocusListener(new WindowFocusListener() {
89+
@Override
9490
public void windowGainedFocus(WindowEvent event) {
9591
System.out.println("Frame Focus gained");
96-
synchronized (lock3) {
97-
try {
98-
lock3.notifyAll();
99-
} catch (Exception ex) {
100-
ex.printStackTrace();
101-
}
102-
}
92+
windowFocusGainedLatch.countDown();
10393
}
10494

95+
@Override
10596
public void windowLostFocus(WindowEvent event) {
106-
System.out.println("Frame Focus lost");
97+
System.out.println("Frame Focus lost");
10798
}
10899
});
100+
109101
frame.addWindowListener(new WindowAdapter() {
102+
@Override
110103
public void windowActivated(WindowEvent e) {
111-
eventType = WindowEvent.WINDOW_ACTIVATED;
112-
System.out.println("Undecorated Frame is activated\n");
113-
synchronized (lock1) {
114-
try {
115-
lock1.notifyAll();
116-
} catch (Exception ex) {
117-
ex.printStackTrace();
118-
}
119-
}
104+
System.out.println("Undecorated Frame is activated");
105+
windowActivatedLatch.countDown();
120106
}
121107

108+
@Override
122109
public void windowDeactivated(WindowEvent e) {
123-
eventType = WindowEvent.WINDOW_DEACTIVATED;
124-
System.out.println("Undecorated Frame got Deactivated\n");
125-
synchronized (lock2) {
126-
try {
127-
lock2.notifyAll();
128-
} catch (Exception ex) {
129-
ex.printStackTrace();
130-
}
131-
}
110+
System.out.println("Undecorated Frame got Deactivated");
111+
windowDeactivatedLatch.countDown();
132112
}
133113
});
114+
134115
textField = new TextField("TextField");
135116
button = new Button("Click me");
136-
button.addActionListener(new ActionListener() {
137-
public void actionPerformed(ActionEvent e) {
138-
textField.setText("Focus gained");
139-
}
140-
});
117+
button.addActionListener(e -> textField.setText("Focus gained"));
141118

142119
frame.setBackground(Color.green);
143120
frame.add(button);
@@ -149,86 +126,46 @@ public void actionPerformed(ActionEvent e) {
149126
frame2.setLocation(5, 250);
150127
frame2.setSize(200, 200);
151128
frame2.setBackground(Color.green);
129+
152130
button2 = new Button("Click me");
153131
textField2 = new TextField("TextField");
154-
button2.addActionListener(new ActionListener() {
155-
public void actionPerformed(ActionEvent e) {
156-
textField2.setText("Got the focus");
157-
}
158-
});
132+
button2.addActionListener(e -> textField2.setText("Got the focus"));
159133

160134
frame2.add(button2, BorderLayout.SOUTH);
161135
frame2.add(textField2, BorderLayout.NORTH);
162136
frame2.setVisible(true);
163-
164-
frame.toFront();
165137
}
166138

167-
public void doTest() {
168-
ExtendedRobot robot;
169-
try {
170-
robot = new ExtendedRobot();
171-
} catch (Exception e) {
172-
e.printStackTrace();
173-
throw new RuntimeException("Cannot create robot");
174-
}
175-
176-
robot.setAutoDelay(delay);
139+
private static void doTest() throws AWTException, InterruptedException {
140+
Robot robot = new Robot();
141+
robot.setAutoDelay(150);
177142
robot.setAutoWaitForIdle(true);
178-
179-
robot.waitForIdle(5*delay);
180-
robot.mouseMove(button.getLocationOnScreen().x + button.getSize().width / 2,
181-
button.getLocationOnScreen().y + button.getSize().height / 2);
182-
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
183-
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
184-
185-
if (eventType != WindowEvent.WINDOW_ACTIVATED) {
186-
synchronized (lock1) {
187-
try {
188-
lock1.wait(delay * 10);
189-
} catch (Exception e) {
190-
e.printStackTrace();
191-
}
192-
}
193-
}
194-
if (eventType != WindowEvent.WINDOW_ACTIVATED) {
195-
passed = false;
196-
System.err.println("WINDOW_ACTIVATED event did not occur when the " +
197-
"undecorated frame is activated!");
143+
if (!windowFocusGainedLatch.await(1000, TimeUnit.MILLISECONDS)) {
144+
throw new RuntimeException("Frame did not gain focus");
198145
}
146+
clickButtonCenter(robot, button);
199147

200-
eventType = -1;
201-
202-
robot.mouseMove(button2.getLocationOnScreen().x + button2.getSize().width / 2,
203-
button2.getLocationOnScreen().y + button2.getSize().height / 2);
204-
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
205-
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
206-
207-
if (eventType != WindowEvent.WINDOW_DEACTIVATED) {
208-
synchronized (lock2) {
209-
try {
210-
lock2.wait(delay * 10);
211-
} catch (Exception e) {
212-
}
213-
}
148+
if (!windowActivatedLatch.await(1000, TimeUnit.MILLISECONDS)) {
149+
throw new RuntimeException("Frame was not activated");
214150
}
215-
if (eventType != WindowEvent.WINDOW_DEACTIVATED) {
216-
passed = false;
217-
System.err.println("FAIL: WINDOW_DEACTIVATED event did not occur for the " +
218-
"undecorated frame when another frame gains focus!");
151+
clickButtonCenter(robot, button2);
152+
153+
if (!windowDeactivatedLatch.await(2000, TimeUnit.MILLISECONDS)) {
154+
throw new RuntimeException("Frame was not deactivated");
219155
}
220156
if (frame.hasFocus()) {
221-
passed = false;
222-
System.err.println("FAIL: The undecorated frame has focus even when " +
223-
"another frame is clicked!");
157+
throw new RuntimeException("Frame did not lose focus");
224158
}
159+
}
225160

226-
if (!passed) {
227-
//captureScreenAndSave();
228-
System.err.println("Test failed!");
229-
throw new RuntimeException("Test failed.");
230-
} else {
231-
System.out.println("Test passed");
232-
}
161+
private static void clickButtonCenter(Robot robot, Component button) {
162+
Point location = button.getLocationOnScreen();
163+
Dimension size = button.getSize();
164+
int x = location.x + size.width / 2;
165+
int y = location.y + size.height / 2;
166+
robot.mouseMove(x, y);
167+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
168+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
233169
}
234170
}
171+

0 commit comments

Comments
 (0)