|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2025, 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.Button; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.Dialog; |
| 27 | +import java.awt.Dimension; |
| 28 | +import java.awt.Frame; |
| 29 | +import java.awt.GraphicsConfiguration; |
| 30 | +import java.awt.GraphicsDevice; |
| 31 | +import java.awt.GraphicsEnvironment; |
| 32 | +import java.awt.GridLayout; |
| 33 | +import java.awt.Label; |
| 34 | +import java.awt.Panel; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.ScrollPane; |
| 37 | + |
| 38 | +import java.awt.event.ActionEvent; |
| 39 | +import java.awt.event.ActionListener; |
| 40 | +import java.awt.event.WindowAdapter; |
| 41 | +import java.awt.event.WindowEvent; |
| 42 | + |
| 43 | +import javax.swing.JDialog; |
| 44 | +import javax.swing.JLabel; |
| 45 | +import javax.swing.WindowConstants; |
| 46 | + |
| 47 | +import jtreg.SkippedException; |
| 48 | + |
| 49 | +/* |
| 50 | + * @test |
| 51 | + * @bug 4368500 |
| 52 | + * @key multimon |
| 53 | + * @summary Dialog needs a constructor with GraphicsConfiguration |
| 54 | + * @library /java/awt/regtesthelpers /test/lib |
| 55 | + * @build PassFailJFrame |
| 56 | + * @run main/manual DialogTest |
| 57 | + */ |
| 58 | + |
| 59 | +public class DialogTest { |
| 60 | + static GraphicsDevice[] gds; |
| 61 | + |
| 62 | + private static Frame f; |
| 63 | + private static Frame dummyFrame = new Frame(); |
| 64 | + private static Dialog dummyDialog = new Dialog(dummyFrame); |
| 65 | + |
| 66 | + public static void main(String[] args) throws Exception { |
| 67 | + gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); |
| 68 | + if (gds.length < 2) { |
| 69 | + throw new SkippedException("You have only one monitor in your system" + |
| 70 | + " - test skipped"); |
| 71 | + } |
| 72 | + |
| 73 | + String INSTRUCTIONS = """ |
| 74 | + This test tests the multiscreen functionality of Dialogs and JDialogs. |
| 75 | + You should see the message "X screens detected", where X |
| 76 | + is the number of screens on your system. If X is incorrect, press Fail. |
| 77 | +
|
| 78 | + In the test window, there are a list of buttons representing each |
| 79 | + type of dialog for each screen. |
| 80 | + If there aren't buttons for every screen in your system, press Fail. |
| 81 | +
|
| 82 | + Press each button, and the indicated type of dialog should appear |
| 83 | + on the indicated screen. |
| 84 | + Modal dialogs should not allow to click on the Instructions or |
| 85 | + DialogTest windows. |
| 86 | +
|
| 87 | + The buttons turn yellow once they have been pressed, to keep track |
| 88 | + of test progress. |
| 89 | +
|
| 90 | + If all Dialogs appear correctly, press Pass. |
| 91 | + If Dialogs appear on the wrong screen or don't behave in |
| 92 | + proper modality, press Fail."""; |
| 93 | + |
| 94 | + PassFailJFrame.builder() |
| 95 | + .instructions(INSTRUCTIONS) |
| 96 | + .columns(40) |
| 97 | + .logArea(5) |
| 98 | + .testUI(DialogTest::init) |
| 99 | + .build() |
| 100 | + .awaitAndCheck(); |
| 101 | + } |
| 102 | + |
| 103 | + public static Frame init() { |
| 104 | + PassFailJFrame.log(gds.length + " screens detected."); |
| 105 | + f = new Frame("DialogTest UI"); |
| 106 | + f.setSize(400, 400); |
| 107 | + MyScrollPane sp = new MyScrollPane(); |
| 108 | + |
| 109 | + Panel p = new Panel(); |
| 110 | + p.setLayout(new GridLayout(0, 1)); |
| 111 | + |
| 112 | + for (int i = 0; i < gds.length; i++) { |
| 113 | + Button btn; |
| 114 | + |
| 115 | + //screen # , modal, frame-owned, swing |
| 116 | + btn = new MyButton(new DialogInfo(i, false, false, false)); |
| 117 | + p.add(btn); |
| 118 | + |
| 119 | + btn = new MyButton(new DialogInfo(i, true, false, false)); |
| 120 | + p.add(btn); |
| 121 | + |
| 122 | + btn = new MyButton(new DialogInfo(i, false, true, false)); |
| 123 | + p.add(btn); |
| 124 | + |
| 125 | + btn = new MyButton(new DialogInfo(i, true, true, false)); |
| 126 | + p.add(btn); |
| 127 | + |
| 128 | + btn = new MyButton(new DialogInfo(i, false, false, true)); |
| 129 | + p.add(btn); |
| 130 | + |
| 131 | + btn = new MyButton(new DialogInfo(i, true, false, true)); |
| 132 | + p.add(btn); |
| 133 | + |
| 134 | + btn = new MyButton(new DialogInfo(i, false, true, true)); |
| 135 | + p.add(btn); |
| 136 | + |
| 137 | + btn = new MyButton(new DialogInfo(i, true, true, true)); |
| 138 | + p.add(btn); |
| 139 | + |
| 140 | + } |
| 141 | + sp.add(p); |
| 142 | + f.add(sp); |
| 143 | + return f; |
| 144 | + } |
| 145 | + |
| 146 | + static class MyScrollPane extends ScrollPane { |
| 147 | + @Override |
| 148 | + public Dimension getPreferredSize() { |
| 149 | + return f.getSize(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + static class MyButton extends Button { |
| 154 | + public MyButton(DialogInfo info) { |
| 155 | + setLabel(info.toString()); |
| 156 | + addActionListener(new PutupDialog(info)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + static class PutupDialog implements ActionListener { |
| 161 | + DialogInfo info; |
| 162 | + |
| 163 | + public PutupDialog(DialogInfo info) { |
| 164 | + this.info = info; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void actionPerformed(ActionEvent e) { |
| 169 | + ((Button) (e.getSource())).setBackground(Color.yellow); |
| 170 | + Dialog d = info.createDialog(); |
| 171 | + d.show(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + static class DialogInfo { |
| 176 | + int num; |
| 177 | + boolean modal; |
| 178 | + boolean frameOwned; |
| 179 | + boolean swing; |
| 180 | + |
| 181 | + public DialogInfo(int num, boolean modal, boolean frameOwned, boolean swing) { |
| 182 | + this.num = num; |
| 183 | + this.modal = modal; |
| 184 | + this.frameOwned = frameOwned; |
| 185 | + this.swing = swing; |
| 186 | + } |
| 187 | + |
| 188 | + public Dialog createDialog() { |
| 189 | + GraphicsConfiguration gc = gds[num].getDefaultConfiguration(); |
| 190 | + |
| 191 | + Dialog d; |
| 192 | + |
| 193 | + if (swing) { |
| 194 | + if (frameOwned) { |
| 195 | + d = new JDialog(dummyFrame, toString(), modal, gc); |
| 196 | + } else { |
| 197 | + d = new JDialog(dummyDialog, toString(), modal, gc); |
| 198 | + } |
| 199 | + |
| 200 | + ((JDialog) d).setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); |
| 201 | + if (modal) { |
| 202 | + ((JDialog) d).getContentPane().add(new JLabel("Check that I am modal!")); |
| 203 | + } |
| 204 | + } else { |
| 205 | + if (frameOwned) { |
| 206 | + d = new Dialog(dummyFrame, toString(), modal, gc); |
| 207 | + } else { |
| 208 | + d = new Dialog(dummyDialog, toString(), modal, gc); |
| 209 | + } |
| 210 | + |
| 211 | + d.addWindowListener(new WindowAdapter() { |
| 212 | + public void windowClosing(WindowEvent e) { |
| 213 | + e.getComponent().hide(); |
| 214 | + } |
| 215 | + }); |
| 216 | + if (modal) { |
| 217 | + d.add(new Label("Check that I am modal!")); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + d.setLocation(new Point((int) (gc.getBounds().getX() + 20) |
| 222 | + , (int) (gc.getBounds().getY() + 20))); |
| 223 | + d.setSize(300, 100); |
| 224 | + |
| 225 | + return d; |
| 226 | + } |
| 227 | + |
| 228 | + public String toString() { |
| 229 | + return "Screen " + num + (frameOwned ? " Frame-owned" : " Dialog-owned") |
| 230 | + + (modal ? " modal " : " non-modal ") |
| 231 | + + (swing ? "JDialog" : "Dialog"); |
| 232 | + } |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | + |
0 commit comments