8159454: [TEST_BUG] javax/swing/ToolTipManager/7123767/bug7123767.java: number of checked graphics configurations should be limited

Reviewed-by: andrew, serb
Backport-of: 64a2db9060bc9fc81afe451d0b057af57fc10f94
This commit is contained in:
Kazuhisa Takakuri 2024-04-17 14:11:23 +00:00 committed by Paul Hohensee
parent 68ab0f3b24
commit 6a096374a5

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, 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
@ -22,10 +22,25 @@
*/ */
/* @test /* @test
@bug 7123767 * @bug 7123767
@summary Wrong tooltip location in Multi-Monitor configurations *
@author Vladislav Karnaukhov * @summary Check if a tooltip location in Multi-Monitor
@run main bug7123767 * configurations is correct.
* If the configurations number per device exceeds 5,
* then some 5 random configurations will be checked.
* Please Use -Dseed=X to set the random generator seed
* (if necessary).
*
* @author Vladislav Karnaukhov
*
* @key headful
* @key randomness
*
* @modules java.desktop/sun.awt
* @library /lib/testlibrary/
* @build jdk.testlibrary.*
*
* @run main/timeout=300 bug7123767
*/ */
import javax.swing.*; import javax.swing.*;
@ -34,8 +49,50 @@ import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import jdk.testlibrary.RandomFactory;
public class bug7123767 extends JFrame { public class bug7123767 extends JFrame {
// maximum number of GraphicsConfigurations checked per GraphicsDevice
private static final int MAX_N_CONFIGS = 5;
private static final List<GraphicsConfiguration> CONFIGS = getConfigs();
private static List<GraphicsConfiguration> getConfigs() {
Random rnd = RandomFactory.getRandom();
List<GraphicsConfiguration> configs = new ArrayList<>();
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = ge.getScreenDevices();
for (GraphicsDevice device : devices) {
GraphicsConfiguration[] allConfigs = device.getConfigurations();
int nConfigs = allConfigs.length;
if (nConfigs <= MAX_N_CONFIGS) {
Collections.addAll(configs, allConfigs);
} else { // see JDK-8159454
System.out.println("check only " + MAX_N_CONFIGS +
" configurations for device " + device);
configs.add(device.getDefaultConfiguration()); // check default
for (int j = 0; j < MAX_N_CONFIGS - 1; j++) {
int k = rnd.nextInt(nConfigs);
configs.add(allConfigs[k]);
}
}
}
return configs;
}
private static class TestFactory extends PopupFactory { private static class TestFactory extends PopupFactory {
private static TestFactory newFactory = new TestFactory(); private static TestFactory newFactory = new TestFactory();
@ -59,15 +116,21 @@ public class bug7123767 extends JFrame {
} }
// Actual test happens here // Actual test happens here
@Override
public Popup getPopup(Component owner, Component contents, int x, int y) { public Popup getPopup(Component owner, Component contents, int x, int y) {
GraphicsConfiguration mouseGC = testGC(MouseInfo.getPointerInfo().getLocation());
GraphicsConfiguration mouseGC =
testGC(MouseInfo.getPointerInfo().getLocation());
if (mouseGC == null) { if (mouseGC == null) {
throw new RuntimeException("Can't find GraphicsConfiguration that mouse pointer belongs to"); throw new RuntimeException("Can't find GraphicsConfiguration "
+ "that mouse pointer belongs to");
} }
GraphicsConfiguration tipGC = testGC(new Point(x, y)); GraphicsConfiguration tipGC = testGC(new Point(x, y));
if (tipGC == null) { if (tipGC == null) {
throw new RuntimeException("Can't find GraphicsConfiguration that tip belongs to"); throw new RuntimeException(
"Can't find GraphicsConfiguration that tip belongs to");
} }
if (!mouseGC.equals(tipGC)) { if (!mouseGC.equals(tipGC)) {
@ -78,17 +141,14 @@ public class bug7123767 extends JFrame {
} }
private static GraphicsConfiguration testGC(Point pt) { private static GraphicsConfiguration testGC(Point pt) {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = environment.getScreenDevices(); for (GraphicsConfiguration config: CONFIGS) {
for (GraphicsDevice device : devices) {
GraphicsConfiguration[] configs = device.getConfigurations(); Rectangle rect = config.getBounds();
for (GraphicsConfiguration config : configs) { Insets insets =
Rectangle rect = config.getBounds(); Toolkit.getDefaultToolkit().getScreenInsets(config);
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config); adjustInsets(rect, insets);
adjustInsets(rect, insets); if (rect.contains(pt)) { return config; }
if (rect.contains(pt))
return config;
}
} }
return null; return null;
@ -100,15 +160,18 @@ public class bug7123767 extends JFrame {
private static Robot robot; private static Robot robot;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new MetalLookAndFeel()); UIManager.setLookAndFeel(new MetalLookAndFeel());
setUp(); setUp();
testToolTip(); testToolTip();
TestFactory.uninstall(); TestFactory.uninstall();
if (frame != null) { frame.dispose(); }
} }
// Creates a window that is stretched across all available monitors // Creates a window that is stretched across all available monitors
// and adds itself as ContainerListener to track tooltips drawing // and adds itself as ContainerListener to track tooltips drawing
private bug7123767() { private bug7123767() {
super(); super();
ToolTipManager.sharedInstance().setInitialDelay(0); ToolTipManager.sharedInstance().setInitialDelay(0);
@ -132,17 +195,16 @@ public class bug7123767 extends JFrame {
pack(); pack();
Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = environment.getScreenDevices(); for (GraphicsConfiguration config: CONFIGS) {
for (GraphicsDevice device : devices) {
GraphicsConfiguration[] configs = device.getConfigurations(); Insets localInsets =
for (GraphicsConfiguration config : configs) { Toolkit.getDefaultToolkit().getScreenInsets(config);
Insets localInsets = Toolkit.getDefaultToolkit().getScreenInsets(config); Rectangle localRect = config.getBounds();
Rectangle localRect = config.getBounds(); adjustInsets(localRect, localInsets);
adjustInsets(localRect, localInsets); rect.add(localRect);
rect.add(localRect);
}
} }
setBounds(rect); setBounds(rect);
} }
@ -163,35 +225,32 @@ public class bug7123767 extends JFrame {
robot.setAutoDelay(20); robot.setAutoDelay(20);
robot.waitForIdle(); robot.waitForIdle();
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); for (GraphicsConfiguration config: CONFIGS) {
GraphicsDevice[] devices = environment.getScreenDevices();
for (GraphicsDevice device : devices) {
GraphicsConfiguration[] configs = device.getConfigurations();
for (GraphicsConfiguration config : configs) {
Rectangle rect = config.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
adjustInsets(rect, insets);
// Upper left Rectangle rect = config.getBounds();
glide(rect.x + rect.width / 2, rect.y + rect.height / 2, Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
rect.x + MARGIN, rect.y + MARGIN); adjustInsets(rect, insets);
robot.waitForIdle();
// Lower left // Upper left
glide(rect.x + rect.width / 2, rect.y + rect.height / 2, glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
rect.x + MARGIN, rect.y + rect.height - MARGIN); rect.x + MARGIN, rect.y + MARGIN);
robot.waitForIdle(); robot.waitForIdle();
// Upper right // Lower left
glide(rect.x + rect.width / 2, rect.y + rect.height / 2, glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
rect.x + rect.width - MARGIN, rect.y + MARGIN); rect.x + MARGIN, rect.y + rect.height - MARGIN);
robot.waitForIdle(); robot.waitForIdle();
// Lower right // Upper right
glide(rect.x + rect.width / 2, rect.y + rect.height / 2, glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
rect.x + rect.width - MARGIN, rect.y + rect.height - MARGIN); rect.x + rect.width - MARGIN, rect.y + MARGIN);
robot.waitForIdle(); robot.waitForIdle();
}
// Lower right
glide(rect.x + rect.width / 2, rect.y + rect.height / 2,
rect.x + rect.width - MARGIN, rect.y + rect.height - MARGIN);
robot.waitForIdle();
} }
} }