Skip to content

Commit 864baf7

Browse files
kurashige23Paul Hohensee
authored andcommitted
8222323: ChildAlwaysOnTopTest.java fails with "RuntimeException: Failed to unset alwaysOnTop"
Reviewed-by: phh Backport-of: 837928ba7955dbfd4a9c966209c3469c0fb5e195
1 parent c06d748 commit 864baf7

File tree

1 file changed

+188
-94
lines changed

1 file changed

+188
-94
lines changed
Lines changed: 188 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,142 +21,236 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @key headful
27-
* @summary setAlwaysOnTop doesn't behave correctly in Linux/Solaris under
28-
* certain scenarios
2927
* @bug 8021961
30-
* @author Semyon Sadetsky
28+
* @summary To test setAlwaysOnTop functionality.
3129
* @run main/othervm -Dsun.java2d.uiScale=1 ChildAlwaysOnTopTest
3230
*/
3331

34-
import javax.swing.*;
35-
import java.awt.*;
32+
import java.awt.Color;
33+
import java.awt.Dialog;
34+
import java.awt.Frame;
35+
import java.awt.Window;
36+
import java.awt.Rectangle;
37+
import java.awt.Robot;
38+
import java.awt.Panel;
39+
import java.awt.Point;
40+
import java.awt.Toolkit;
41+
import java.awt.image.BufferedImage;
42+
43+
import java.io.File;
44+
import java.io.IOException;
45+
import javax.imageio.ImageIO;
46+
47+
import javax.swing.JDialog;
48+
import javax.swing.JLabel;
49+
import javax.swing.SwingUtilities;
3650

3751
public class ChildAlwaysOnTopTest {
3852

3953
private static Window win1;
4054
private static Window win2;
4155
private static Point point;
56+
private static Robot robot;
57+
private static int caseNo = 0;
58+
private static StringBuffer errorLog = new StringBuffer();
59+
private static String[] errorMsg= new String[] {
60+
" Scenario 1 Failed: alwaysOnTop window is sent back by another" +
61+
" child window with setVisible().",
62+
" Scenario 2 Failed: alwaysOnTop window is" +
63+
" sent back by another child window with toFront().",
64+
" Scenario 3 Failed: Failed to unset alwaysOnTop ",
65+
};
4266

4367
public static void main(String[] args) throws Exception {
44-
if( Toolkit.getDefaultToolkit().isAlwaysOnTopSupported() ) {
4568

69+
if (!Toolkit.getDefaultToolkit().isAlwaysOnTopSupported()) {
70+
System.out.println("alwaysOnTop not supported by: "+
71+
Toolkit.getDefaultToolkit().getClass().getName());
72+
return;
73+
}
4674

47-
test(null);
75+
// CASE 1 - JDialog without parent/owner
76+
System.out.println("Testing CASE 1: JDialog without parent/owner");
77+
caseNo = 1;
78+
test(null);
79+
System.out.println("CASE 1 Completed");
80+
System.out.println();
4881

49-
Window f = new Frame();
50-
f.setBackground(Color.darkGray);
51-
f.setSize(500, 500);
52-
try {
53-
test(f);
54-
} finally {
55-
f.dispose();
56-
}
82+
// CASE 2 - JDialog with JFrame as owner
83+
System.out.println("Testing CASE 2: JDialog with JFrame as owner");
84+
caseNo = 2;
85+
Window f = new Frame();
86+
f.setBackground(Color.darkGray);
87+
f.setSize(500, 500);
88+
try {
89+
test(f);
90+
} finally {
91+
f.dispose();
92+
}
93+
System.out.println("CASE 2 Completed");
94+
System.out.println();
5795

58-
f = new Frame();
59-
f.setBackground(Color.darkGray);
60-
f.setSize(500, 500);
61-
f.setVisible(true);
62-
f = new Dialog((Frame)f);
63-
try {
64-
test(f);
65-
} finally {
66-
((Frame)f.getParent()).dispose();
67-
}
96+
// CASE 3 - JDialog within another JDialog as owner
97+
System.out.println("Testing CASE 3:Dialog within another"+
98+
" JDialog as owner");
99+
caseNo = 3;
100+
f = new Frame();
101+
f.setBackground(Color.darkGray);
102+
f.setSize(500, 500);
103+
f.setVisible(true);
104+
f = new Dialog((Frame)f);
105+
try {
106+
test(f);
107+
} finally {
108+
((Frame)f.getParent()).dispose();
109+
}
110+
System.out.println("CASE 3 Completed");
111+
System.out.println();
112+
113+
if (errorLog.length() == 0)
114+
System.out.println("All three cases passed !!");
115+
}
116+
else {
117+
throw new RuntimeException("Following cases and scenarios failed."+
118+
" Please check the saved screenshots.\n"+ errorLog);
68119
}
69-
System.out.println("ok");
70120
}
71121

72122
public static void test(Window parent) throws Exception {
73-
SwingUtilities.invokeAndWait(new Runnable() {
74-
@Override
75-
public void run() {
76-
win1 = parent == null ? new JDialog() : new JDialog(parent);
77-
win1.setName("top");
78-
win2 = parent == null ? new JDialog() : new JDialog(parent);
79-
win2.setName("behind");
80-
win1.setSize(200, 200);
81-
Panel panel = new Panel();
82-
panel.setBackground(Color.GREEN);
83-
win1.add(panel);
84-
panel = new Panel();
85-
panel.setBackground(Color.RED);
86-
win2.add(panel);
87-
win1.setAlwaysOnTop(true);
88-
win2.setAlwaysOnTop(false);
89-
win1.setVisible(true);
90-
}
91-
});
123+
try {
124+
SwingUtilities.invokeAndWait(new Runnable() {
125+
@Override
126+
public void run() {
127+
win1 = parent == null ? new JDialog() : new JDialog(parent);
128+
win1.setName("Top");
92129

93-
Robot robot = new Robot();
94-
robot.delay(500);
95-
robot.waitForIdle();
130+
win2 = parent == null ? new JDialog() : new JDialog(parent);
131+
win2.setName("Behind");
132+
133+
JLabel label = new JLabel("TOP WINDOW");
134+
// top window - green and smaller
135+
win1.setSize(200, 200);
136+
Panel panel = new Panel();
137+
panel.setBackground(Color.GREEN);
138+
panel.add(label);
139+
win1.add(panel);
140+
win1.setAlwaysOnTop(true);
141+
142+
// behind window - red and bigger
143+
label = new JLabel("BEHIND WINDOW");
144+
win2.setSize(300, 300);
145+
panel = new Panel();
146+
panel.setBackground(Color.RED);
147+
panel.add(label);
148+
win2.add(panel);
96149

97-
SwingUtilities.invokeAndWait(new Runnable() {
98-
@Override
99-
public void run() {
150+
win1.setVisible(true);
151+
win2.setVisible(true);
152+
}
153+
});
154+
155+
robot = new Robot();
156+
robot.setAutoDelay(300);
157+
robot.waitForIdle();
158+
159+
// Scenario 1: Trying to unset the alwaysOnTop (green window)
160+
// by setting the setVisible to true for behind (red) window
161+
System.out.println(" >> Testing Scenario 1 ...");
162+
SwingUtilities.invokeAndWait(()-> {
100163
point = win1.getLocationOnScreen();
101-
win2.setBounds(win1.getBounds());
102164
win2.setVisible(true);
103-
}
104-
});
165+
});
105166

106-
robot.delay(500);
107-
robot.waitForIdle();
167+
checkTopWindow(caseNo, 1, Color.GREEN);
108168

109-
Color color = robot.getPixelColor(point.x + 100, point.y + 100);
110-
if(!color.equals(Color.GREEN)) {
111-
win1.dispose();
112-
win2.dispose();
113-
throw new RuntimeException("alawaysOnTop window is sent back by " +
114-
"another child window setVisible(). " + color);
115-
}
169+
/*---------------------------------------------------------------*/
116170

117-
SwingUtilities.invokeAndWait(new Runnable() {
118-
@Override
119-
public void run() {
171+
// Scenario 2: Trying to unset the alwaysOnTop (green window)
172+
// by setting toFront() to true for behind (red) window
173+
System.out.println(" >> Testing Scenario 2 ...");
174+
SwingUtilities.invokeAndWait(()-> {
120175
win2.toFront();
121176
if (parent != null) {
122177
parent.setLocation(win1.getLocation());
123178
parent.toFront();
124179
}
125-
}
126-
});
180+
});
127181

128-
robot.delay(500);
129-
robot.waitForIdle();
182+
checkTopWindow(caseNo, 2, Color.GREEN);
130183

131-
color = robot.getPixelColor(point.x + 100, point.y + 100);
132-
if(!color.equals(Color.GREEN)) {
133-
win1.dispose();
134-
win2.dispose();
135-
throw new RuntimeException("alawaysOnTop window is sent back by " +
136-
"another child window toFront(). " + color);
137-
}
184+
/*----------------------------------------------------------------*/
138185

139-
SwingUtilities.invokeAndWait(new Runnable() {
140-
@Override
141-
public void run() {
142-
win1.setAlwaysOnTop(false);
143-
if (parent != null) {
144-
parent.setVisible(false);
145-
parent.setVisible(true);
186+
// Scenario 3: Trying to unset the alwaysOnTop (green window)
187+
// by setting alwaysOnTop to false. The unsetting should work
188+
// in this case and bring the red window to the top.
189+
System.out.println(" >> Testing Scenario 3 ...");
190+
SwingUtilities.invokeAndWait(new Runnable() {
191+
@Override
192+
public void run() {
193+
win1.setAlwaysOnTop(false);
194+
if (parent != null) {
195+
parent.setVisible(false);
196+
parent.setVisible(true);
197+
}
146198
}
147-
win2.toFront();
199+
});
200+
201+
robot.delay(300);
202+
robot.waitForIdle();
203+
204+
SwingUtilities.invokeAndWait(new Runnable() {
205+
@Override
206+
public void run() {
207+
win2.toFront();
208+
}
209+
});
210+
211+
checkTopWindow(caseNo, 3, Color.RED);
212+
213+
} finally {
214+
if (win1 != null) {
215+
SwingUtilities.invokeAndWait(()-> win1.dispose());
148216
}
149-
});
217+
if (win2 != null) {
218+
SwingUtilities.invokeAndWait(()-> win2.dispose());
219+
}
220+
}
221+
}
222+
// to check if the current top window background color
223+
// matches the expected color
224+
private static void checkTopWindow(int caseNo, int scenarioNo,
225+
Color expectedColor) {
150226

151227
robot.delay(500);
152228
robot.waitForIdle();
229+
Color actualColor = robot.getPixelColor(point.x + 100, point.y + 100);
230+
231+
saveScreenCapture(caseNo , scenarioNo);
153232

154-
color = robot.getPixelColor(point.x + 100, point.y + 100);
155-
if(!color.equals(Color.RED)) {
156-
throw new RuntimeException("Failed to unset alawaysOnTop " + color);
233+
if (!actualColor.equals(expectedColor)) {
234+
System.out.println(" >> Scenario "+ scenarioNo +" FAILED !!");
235+
errorLog.append("Case "+ caseNo + errorMsg[scenarioNo - 1]
236+
+" Expected Color: "+ expectedColor +" vs Actual Color: "
237+
+ actualColor +"\n");
238+
}
239+
else {
240+
System.out.println(" >> Scenario "+ scenarioNo +" Passed");
157241
}
242+
}
158243

159-
win1.dispose();
160-
win2.dispose();
244+
// For Debugging purpose - method used to save the screen capture as
245+
// BufferedImage in the event the test fails
246+
private static void saveScreenCapture(int caseNo, int scenarioNo) {
247+
String filename = "img_"+ caseNo +"_"+ scenarioNo;
248+
BufferedImage image = robot.createScreenCapture(
249+
new Rectangle(0, 0, 500, 500));
250+
try {
251+
ImageIO.write(image, "png", new File(filename));
252+
} catch (IOException e) {
253+
e.printStackTrace();
254+
}
161255
}
162-
}
256+
}

0 commit comments

Comments
 (0)