|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.AWTException; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.GraphicsDevice; |
| 27 | +import java.awt.GraphicsEnvironment; |
| 28 | +import java.awt.Rectangle; |
| 29 | +import java.awt.Robot; |
| 30 | +import java.awt.Window; |
| 31 | + |
| 32 | +import javax.swing.JWindow; |
| 33 | +import javax.swing.SwingUtilities; |
| 34 | + |
| 35 | +import java.awt.event.MouseAdapter; |
| 36 | +import java.awt.event.MouseEvent; |
| 37 | +import java.lang.reflect.InvocationTargetException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +/* |
| 42 | + * @test |
| 43 | + * @bug 8280482 |
| 44 | + * @key headful |
| 45 | + * @summary Test to check if window GC doesn't change within same screen. |
| 46 | + * @run main MultiScreenCheckScreenIDTest |
| 47 | + */ |
| 48 | + |
| 49 | +public class MultiScreenCheckScreenIDTest extends MouseAdapter { |
| 50 | + private static final int COLS = 12; |
| 51 | + private static final int ROWS = 8; |
| 52 | + private static final Color BACKGROUND = new Color(0, 0, 255, 64); |
| 53 | + private static GraphicsDevice[] screens; |
| 54 | + static List<Window> windowList = new ArrayList<>(); |
| 55 | + static Robot robot; |
| 56 | + static JWindow window; |
| 57 | + |
| 58 | + |
| 59 | + public static void main(final String[] args) throws Exception { |
| 60 | + try { |
| 61 | + createGUI(); |
| 62 | + } finally { |
| 63 | + for (Window win : windowList) { |
| 64 | + win.dispose(); |
| 65 | + } |
| 66 | + if (window != null) { |
| 67 | + window.dispose(); |
| 68 | + } |
| 69 | + } |
| 70 | + System.out.println("Test Pass"); |
| 71 | + } |
| 72 | + |
| 73 | + private static void createGUI() throws AWTException { |
| 74 | + new MultiScreenCheckScreenIDTest().createWindowGrid(); |
| 75 | + } |
| 76 | + |
| 77 | + private void createWindowGrid() throws AWTException { |
| 78 | + screens = GraphicsEnvironment |
| 79 | + .getLocalGraphicsEnvironment() |
| 80 | + .getScreenDevices(); |
| 81 | + |
| 82 | + if (screens.length < 2) { |
| 83 | + System.out.println("Testing aborted. Required min of 2 screens. " + |
| 84 | + "Available : " + screens.length); |
| 85 | + return; |
| 86 | + } |
| 87 | + robot = new Robot(); |
| 88 | + |
| 89 | + int screenNumber = 1; |
| 90 | + for (GraphicsDevice screen : screens) { |
| 91 | + Rectangle screenBounds = screen.getDefaultConfiguration().getBounds(); |
| 92 | + |
| 93 | + for (Rectangle r : gridOfRectangles(screenBounds, COLS, ROWS)) { |
| 94 | + try { |
| 95 | + SwingUtilities.invokeAndWait(() -> { |
| 96 | + try { |
| 97 | + window = createWindow(r); |
| 98 | + } catch (Exception e) { |
| 99 | + throw new RuntimeException(e); |
| 100 | + } |
| 101 | + }); |
| 102 | + } catch (InterruptedException | InvocationTargetException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + } |
| 105 | + robot.delay(50); |
| 106 | + robot.waitForIdle(); |
| 107 | + if (window.getBounds().intersects(screenBounds)) { |
| 108 | + if (!(window.getGraphicsConfiguration().getBounds(). |
| 109 | + intersects(screenBounds))) { |
| 110 | + throw new RuntimeException("Graphics configuration " + |
| 111 | + "changed for screen :" + screenNumber); |
| 112 | + } |
| 113 | + } |
| 114 | + windowList.add(window); |
| 115 | + } |
| 116 | + screenNumber++; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private JWindow createWindow(Rectangle bounds) { |
| 121 | + JWindow window = new JWindow(); |
| 122 | + window.setBounds(bounds); |
| 123 | + window.setBackground(BACKGROUND); |
| 124 | + window.setAlwaysOnTop(true); |
| 125 | + window.addMouseListener(this); |
| 126 | + window.setVisible(true); |
| 127 | + return window; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void mouseClicked(MouseEvent e) { |
| 132 | + ((Window) e.getSource()).dispose(); |
| 133 | + } |
| 134 | + |
| 135 | + private static List<Rectangle> gridOfRectangles(Rectangle r, int cols, int rows) { |
| 136 | + List<Rectangle> l = new ArrayList<>(); |
| 137 | + for (int row = 0; row < rows; row++) { |
| 138 | + int y1 = r.y + (int) Math.round(r.height * (double) row / rows); |
| 139 | + int y2 = r.y + (int) Math.round(r.height * (double) (row + 1) / rows); |
| 140 | + for (int col = 0; col < cols; col++) { |
| 141 | + int x1 = r.x + (int) Math.round(r.width * (double) col / cols); |
| 142 | + int x2 = r.x + (int) Math.round(r.width * (double) (col + 1) / cols); |
| 143 | + l.add(new Rectangle(x1, y1, x2 - x1, y2 - y1)); |
| 144 | + } |
| 145 | + } |
| 146 | + return l; |
| 147 | + } |
| 148 | +} |
0 commit comments