1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- /**
24
+ /*
25
25
* @test
26
26
* @key headful
27
- * @summary setAlwaysOnTop doesn't behave correctly in Linux/Solaris under
28
- * certain scenarios
29
27
* @bug 8021961
30
- * @author Semyon Sadetsky
28
+ * @summary To test setAlwaysOnTop functionality.
31
29
* @run main/othervm -Dsun.java2d.uiScale=1 ChildAlwaysOnTopTest
32
30
*/
33
31
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 ;
36
50
37
51
public class ChildAlwaysOnTopTest {
38
52
39
53
private static Window win1 ;
40
54
private static Window win2 ;
41
55
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
+ };
42
66
43
67
public static void main (String [] args ) throws Exception {
44
- if ( Toolkit .getDefaultToolkit ().isAlwaysOnTopSupported () ) {
45
68
69
+ if (!Toolkit .getDefaultToolkit ().isAlwaysOnTopSupported ()) {
70
+ System .out .println ("alwaysOnTop not supported by: " +
71
+ Toolkit .getDefaultToolkit ().getClass ().getName ());
72
+ return ;
73
+ }
46
74
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 ();
48
81
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 ();
57
95
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 );
68
119
}
69
- System .out .println ("ok" );
70
120
}
71
121
72
122
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" );
92
129
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 );
96
149
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 (()-> {
100
163
point = win1 .getLocationOnScreen ();
101
- win2 .setBounds (win1 .getBounds ());
102
164
win2 .setVisible (true );
103
- }
104
- });
165
+ });
105
166
106
- robot .delay (500 );
107
- robot .waitForIdle ();
167
+ checkTopWindow (caseNo , 1 , Color .GREEN );
108
168
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
+ /*---------------------------------------------------------------*/
116
170
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 (()-> {
120
175
win2 .toFront ();
121
176
if (parent != null ) {
122
177
parent .setLocation (win1 .getLocation ());
123
178
parent .toFront ();
124
179
}
125
- }
126
- });
180
+ });
127
181
128
- robot .delay (500 );
129
- robot .waitForIdle ();
182
+ checkTopWindow (caseNo , 2 , Color .GREEN );
130
183
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
+ /*----------------------------------------------------------------*/
138
185
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
+ }
146
198
}
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 ());
148
216
}
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 ) {
150
226
151
227
robot .delay (500 );
152
228
robot .waitForIdle ();
229
+ Color actualColor = robot .getPixelColor (point .x + 100 , point .y + 100 );
230
+
231
+ saveScreenCapture (caseNo , scenarioNo );
153
232
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" );
157
241
}
242
+ }
158
243
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
+ }
161
255
}
162
- }
256
+ }
0 commit comments