|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2024, 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.BorderLayout; |
| 25 | +import java.awt.Button; |
| 26 | +import java.awt.Color; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.GraphicsConfiguration; |
| 29 | +import java.awt.GraphicsDevice; |
| 30 | +import java.awt.GraphicsEnvironment; |
| 31 | +import java.awt.Label; |
| 32 | +import java.awt.Rectangle; |
| 33 | +import java.awt.event.WindowAdapter; |
| 34 | +import java.awt.event.WindowEvent; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 4473671 |
| 40 | + * @summary Test to verify GraphicsEnvironment.getDefaultScreenDevice always |
| 41 | + * returning first screen |
| 42 | + * @requires (os.family == "windows") |
| 43 | + * @library /java/awt/regtesthelpers |
| 44 | + * @build PassFailJFrame |
| 45 | + * @run main/manual DefaultScreenDeviceTest |
| 46 | + */ |
| 47 | + |
| 48 | +public class DefaultScreenDeviceTest { |
| 49 | + private static Frame testFrame; |
| 50 | + |
| 51 | + public static void main(String[] args) throws Exception { |
| 52 | + GraphicsEnvironment ge = GraphicsEnvironment. |
| 53 | + getLocalGraphicsEnvironment(); |
| 54 | + GraphicsDevice[] gds = ge.getScreenDevices(); |
| 55 | + if (gds.length < 2) { |
| 56 | + System.out.println("Test requires at least 2 displays"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + String INSTRUCTIONS = """ |
| 61 | + 1. The test is for systems which allows primary display |
| 62 | + selection in multiscreen systems. |
| 63 | + Set the system primary screen to be the rightmost |
| 64 | + (i.e. the right screen in two screen configuration) |
| 65 | + This can be done by going to OS Display Settings |
| 66 | + selecting the screen and checking the 'Use this device |
| 67 | + as primary monitor' checkbox. |
| 68 | + 2. When done, click on 'Frame on Primary Screen' button and |
| 69 | + see where the frame will pop up |
| 70 | + 3. If Primary Frame pops up on the primary display, |
| 71 | + the test passed, otherwise it failed |
| 72 | + """; |
| 73 | + PassFailJFrame.builder() |
| 74 | + .title("Test Instructions") |
| 75 | + .instructions(INSTRUCTIONS) |
| 76 | + .rows((int) INSTRUCTIONS.lines().count() + 2) |
| 77 | + .columns(35) |
| 78 | + .testUI(initialize()) |
| 79 | + .build() |
| 80 | + .awaitAndCheck(); |
| 81 | + } |
| 82 | + |
| 83 | + private static List<Frame> initialize() { |
| 84 | + Frame frame = new Frame("Default screen device test"); |
| 85 | + GraphicsConfiguration gc = |
| 86 | + GraphicsEnvironment.getLocalGraphicsEnvironment(). |
| 87 | + getDefaultScreenDevice().getDefaultConfiguration(); |
| 88 | + |
| 89 | + testFrame = new Frame("Primary screen frame", gc); |
| 90 | + frame.setLayout(new BorderLayout()); |
| 91 | + frame.setSize(200, 200); |
| 92 | + |
| 93 | + Button b = new Button("Frame on Primary Screen"); |
| 94 | + b.addActionListener(e -> { |
| 95 | + if (testFrame != null) { |
| 96 | + testFrame.setVisible(false); |
| 97 | + testFrame.dispose(); |
| 98 | + } |
| 99 | + |
| 100 | + testFrame.addWindowListener(new WindowAdapter() { |
| 101 | + public void windowClosing(WindowEvent e1) { |
| 102 | + testFrame.setVisible(false); |
| 103 | + testFrame.dispose(); |
| 104 | + } |
| 105 | + }); |
| 106 | + testFrame.add(new Label("This frame should be on the primary screen")); |
| 107 | + testFrame.setBackground(Color.red); |
| 108 | + testFrame.pack(); |
| 109 | + Rectangle rect = gc.getBounds(); |
| 110 | + testFrame.setLocation(rect.x + 100, rect.y + 100); |
| 111 | + testFrame.setVisible(true); |
| 112 | + }); |
| 113 | + frame.add(b); |
| 114 | + return List.of(testFrame, frame); |
| 115 | + } |
| 116 | +} |
0 commit comments