8222323: ChildAlwaysOnTopTest.java fails with "RuntimeException: Failed to unset alwaysOnTop"

Reviewed-by: sgehwolf
Backport-of: 837928ba7955dbfd4a9c966209c3469c0fb5e195
This commit is contained in:
Taizo Kurashige 2023-11-29 15:12:07 +00:00 committed by Paul Hohensee
parent 8cf7c96813
commit b05e6799d3

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,140 +21,236 @@
* questions. * questions.
*/ */
/** /*
* @test @summary setAlwaysOnTop doesn't behave correctly in Linux/Solaris under * @test
* certain scenarios * @key headful
* @bug 8021961 * @bug 8021961
* @author Semyon Sadetsky * @summary To test setAlwaysOnTop functionality.
* @run main ChildAlwaysOnTopTest * @run main/othervm -Dsun.java2d.uiScale=1 ChildAlwaysOnTopTest
*/ */
import javax.swing.*; import java.awt.Color;
import java.awt.*; import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ChildAlwaysOnTopTest { public class ChildAlwaysOnTopTest {
private static Window win1; private static Window win1;
private static Window win2; private static Window win2;
private static Point point; private static Point point;
private static Robot robot;
private static int caseNo = 0;
private static StringBuffer errorLog = new StringBuffer();
private static String[] errorMsg= new String[] {
" Scenario 1 Failed: alwaysOnTop window is sent back by another" +
" child window with setVisible().",
" Scenario 2 Failed: alwaysOnTop window is" +
" sent back by another child window with toFront().",
" Scenario 3 Failed: Failed to unset alwaysOnTop ",
};
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if( Toolkit.getDefaultToolkit().isAlwaysOnTopSupported() ) {
if (!Toolkit.getDefaultToolkit().isAlwaysOnTopSupported()) {
test(null); System.out.println("alwaysOnTop not supported by: "+
Toolkit.getDefaultToolkit().getClass().getName());
Window f = new Frame(); return;
f.setBackground(Color.darkGray); }
f.setSize(500, 500);
try { // CASE 1 - JDialog without parent/owner
test(f); System.out.println("Testing CASE 1: JDialog without parent/owner");
} finally { caseNo = 1;
f.dispose(); test(null);
} System.out.println("CASE 1 Completed");
System.out.println();
f = new Frame();
f.setBackground(Color.darkGray); // CASE 2 - JDialog with JFrame as owner
f.setSize(500, 500); System.out.println("Testing CASE 2: JDialog with JFrame as owner");
f.setVisible(true); caseNo = 2;
f = new Dialog((Frame)f); Window f = new Frame();
try { f.setBackground(Color.darkGray);
test(f); f.setSize(500, 500);
} finally { try {
((Frame)f.getParent()).dispose(); test(f);
} } finally {
f.dispose();
}
System.out.println("CASE 2 Completed");
System.out.println();
// CASE 3 - JDialog within another JDialog as owner
System.out.println("Testing CASE 3:Dialog within another"+
" JDialog as owner");
caseNo = 3;
f = new Frame();
f.setBackground(Color.darkGray);
f.setSize(500, 500);
f.setVisible(true);
f = new Dialog((Frame)f);
try {
test(f);
} finally {
((Frame)f.getParent()).dispose();
}
System.out.println("CASE 3 Completed");
System.out.println();
if (errorLog.length() == 0) {
System.out.println("All three cases passed !!");
}
else {
throw new RuntimeException("Following cases and scenarios failed."+
" Please check the saved screenshots.\n"+ errorLog);
} }
System.out.println("ok");
} }
public static void test(Window parent) throws Exception { public static void test(Window parent) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() { try {
@Override SwingUtilities.invokeAndWait(new Runnable() {
public void run() { @Override
win1 = parent == null ? new JDialog() : new JDialog(parent); public void run() {
win1.setName("top"); win1 = parent == null ? new JDialog() : new JDialog(parent);
win2 = parent == null ? new JDialog() : new JDialog(parent); win1.setName("Top");
win2.setName("behind");
win1.setSize(200, 200);
Panel panel = new Panel();
panel.setBackground(Color.GREEN);
win1.add(panel);
panel = new Panel();
panel.setBackground(Color.RED);
win2.add(panel);
win1.setAlwaysOnTop(true);
win2.setAlwaysOnTop(false);
win1.setVisible(true);
}
});
Robot robot = new Robot(); win2 = parent == null ? new JDialog() : new JDialog(parent);
robot.delay(200); win2.setName("Behind");
robot.waitForIdle();
SwingUtilities.invokeAndWait(new Runnable() { JLabel label = new JLabel("TOP WINDOW");
@Override // top window - green and smaller
public void run() { win1.setSize(200, 200);
Panel panel = new Panel();
panel.setBackground(Color.GREEN);
panel.add(label);
win1.add(panel);
win1.setAlwaysOnTop(true);
// behind window - red and bigger
label = new JLabel("BEHIND WINDOW");
win2.setSize(300, 300);
panel = new Panel();
panel.setBackground(Color.RED);
panel.add(label);
win2.add(panel);
win1.setVisible(true);
win2.setVisible(true);
}
});
robot = new Robot();
robot.setAutoDelay(300);
robot.waitForIdle();
// Scenario 1: Trying to unset the alwaysOnTop (green window)
// by setting the setVisible to true for behind (red) window
System.out.println(" >> Testing Scenario 1 ...");
SwingUtilities.invokeAndWait(()-> {
point = win1.getLocationOnScreen(); point = win1.getLocationOnScreen();
win2.setBounds(win1.getBounds());
win2.setVisible(true); win2.setVisible(true);
} });
});
robot.delay(200); checkTopWindow(caseNo, 1, Color.GREEN);
robot.waitForIdle();
Color color = robot.getPixelColor(point.x + 100, point.y + 100); /*---------------------------------------------------------------*/
if(!color.equals(Color.GREEN)) {
win1.dispose();
win2.dispose();
throw new RuntimeException("alawaysOnTop window is sent back by " +
"another child window setVisible(). " + color);
}
SwingUtilities.invokeAndWait(new Runnable() { // Scenario 2: Trying to unset the alwaysOnTop (green window)
@Override // by setting toFront() to true for behind (red) window
public void run() { System.out.println(" >> Testing Scenario 2 ...");
SwingUtilities.invokeAndWait(()-> {
win2.toFront(); win2.toFront();
if (parent != null) { if (parent != null) {
parent.setLocation(win1.getLocation()); parent.setLocation(win1.getLocation());
parent.toFront(); parent.toFront();
} }
} });
});
robot.delay(200); checkTopWindow(caseNo, 2, Color.GREEN);
robot.waitForIdle();
color = robot.getPixelColor(point.x + 100, point.y + 100); /*----------------------------------------------------------------*/
if(!color.equals(Color.GREEN)) {
win1.dispose();
win2.dispose();
throw new RuntimeException("alawaysOnTop window is sent back by " +
"another child window toFront(). " + color);
}
SwingUtilities.invokeAndWait(new Runnable() { // Scenario 3: Trying to unset the alwaysOnTop (green window)
@Override // by setting alwaysOnTop to false. The unsetting should work
public void run() { // in this case and bring the red window to the top.
win1.setAlwaysOnTop(false); System.out.println(" >> Testing Scenario 3 ...");
if (parent != null) { SwingUtilities.invokeAndWait(new Runnable() {
parent.setVisible(false); @Override
parent.setVisible(true); public void run() {
win1.setAlwaysOnTop(false);
if (parent != null) {
parent.setVisible(false);
parent.setVisible(true);
}
} }
win2.toFront(); });
robot.delay(300);
robot.waitForIdle();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
win2.toFront();
}
});
checkTopWindow(caseNo, 3, Color.RED);
} finally {
if (win1 != null) {
SwingUtilities.invokeAndWait(()-> win1.dispose());
}
if (win2 != null) {
SwingUtilities.invokeAndWait(()-> win2.dispose());
} }
});
robot.delay(200);
robot.waitForIdle();
color = robot.getPixelColor(point.x + 100, point.y + 100);
if(!color.equals(Color.RED)) {
throw new RuntimeException("Failed to unset alawaysOnTop " + color);
} }
win1.dispose();
win2.dispose();
} }
} // to check if the current top window background color
// matches the expected color
private static void checkTopWindow(int caseNo, int scenarioNo,
Color expectedColor) {
robot.delay(500);
robot.waitForIdle();
Color actualColor = robot.getPixelColor(point.x + 100, point.y + 100);
saveScreenCapture(caseNo , scenarioNo);
if (!actualColor.equals(expectedColor)) {
System.out.println(" >> Scenario "+ scenarioNo +" FAILED !!");
errorLog.append("Case "+ caseNo + errorMsg[scenarioNo - 1]
+" Expected Color: "+ expectedColor +" vs Actual Color: "
+ actualColor +"\n");
}
else {
System.out.println(" >> Scenario "+ scenarioNo +" Passed");
}
}
// For Debugging purpose - method used to save the screen capture as
// BufferedImage in the event the test fails
private static void saveScreenCapture(int caseNo, int scenarioNo) {
String filename = "img_"+ caseNo +"_"+ scenarioNo;
BufferedImage image = robot.createScreenCapture(
new Rectangle(0, 0, 500, 500));
try {
ImageIO.write(image, "png", new File(filename));
} catch (IOException e) {
e.printStackTrace();
}
}
}