|
| 1 | +/* |
| 2 | + * Copyright (c) 1998, 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.Button; |
| 25 | +import java.awt.Choice; |
| 26 | +import java.awt.Dialog; |
| 27 | +import java.awt.FileDialog; |
| 28 | +import java.awt.FlowLayout; |
| 29 | +import java.awt.Frame; |
| 30 | +import java.awt.GridLayout; |
| 31 | +import java.awt.List; |
| 32 | +import java.awt.Panel; |
| 33 | +import java.awt.Point; |
| 34 | +import java.awt.Window; |
| 35 | +import java.awt.event.ActionEvent; |
| 36 | +import java.awt.event.ActionListener; |
| 37 | +import java.awt.event.WindowAdapter; |
| 38 | +import java.awt.event.WindowEvent; |
| 39 | +import java.util.Vector; |
| 40 | +import java.util.Enumeration; |
| 41 | + |
| 42 | +/* |
| 43 | + * @test |
| 44 | + * @bug 4110094 4178930 4178390 |
| 45 | + * @summary Test: Rewrite of Win modal dialogs |
| 46 | + * @library /java/awt/regtesthelpers |
| 47 | + * @build PassFailJFrame |
| 48 | + * @run main/manual NestedDialogTest |
| 49 | + */ |
| 50 | + |
| 51 | +public class NestedDialogTest { |
| 52 | + private static Vector windows = new Vector(); |
| 53 | + static String instructions = """ |
| 54 | + To solve various race conditions, windows modal dialogs were rewritten. This |
| 55 | + test exercises various modal dialog boundary conditions and checks that |
| 56 | + previous fixes to modality are incorporated in the rewrite. |
| 57 | +
|
| 58 | + Check the following: |
| 59 | + - No IllegalMonitorStateException is thrown when a dialog closes |
| 60 | +
|
| 61 | + - Open multiple nested dialogs and verify that all other windows |
| 62 | + are disabled when modal dialog is active. |
| 63 | +
|
| 64 | + - Check that the proper window is activated when a modal dialog closes. |
| 65 | +
|
| 66 | + - Close nested dialogs out of order (e.g. close dialog1 before dialog2) |
| 67 | + and verify that this works and no deadlock occurs. |
| 68 | +
|
| 69 | + - Check that all other windows are disabled when a FileDialog is open. |
| 70 | +
|
| 71 | + - Check that the proper window is activated when a FileDialog closes. |
| 72 | +
|
| 73 | + - Verify that the active window nevers switches to another application |
| 74 | + when closing dialogs, even temporarily. |
| 75 | +
|
| 76 | + - Check that choosing Hide always sucessfully hides a dialog. You should |
| 77 | + try this multiple times to catch any race conditions. |
| 78 | +
|
| 79 | + - Check that the scrollbar on the Choice component in the dialog works, as opposed |
| 80 | + to just using drag-scrolling or the cursor keys |
| 81 | + """; |
| 82 | + |
| 83 | + public static void main(String[] args) throws Exception { |
| 84 | + PassFailJFrame.builder() |
| 85 | + .title("NestedDialogTest") |
| 86 | + .instructions(instructions) |
| 87 | + .testTimeOut(5) |
| 88 | + .rows((int) instructions.lines().count() + 2) |
| 89 | + .columns(35) |
| 90 | + .testUI(NestedDialogTest::createGUI) |
| 91 | + .build() |
| 92 | + .awaitAndCheck(); |
| 93 | + } |
| 94 | + |
| 95 | + public static Frame createGUI() { |
| 96 | + Frame frame1 = new NestedDialogTestFrame("frame0"); |
| 97 | + Frame frame2 = new NestedDialogTestFrame("frame1"); |
| 98 | + frame2.setLocation(100, 100); |
| 99 | + return frame1; |
| 100 | + } |
| 101 | + |
| 102 | + public static void addWindow(Window window) { |
| 103 | + // System.out.println("Pushing window " + window); |
| 104 | + windows.removeElement(window); |
| 105 | + windows.addElement(window); |
| 106 | + } |
| 107 | + |
| 108 | + public static void removeWindow(Window window) { |
| 109 | + // System.out.println("Popping window " + window); |
| 110 | + windows.removeElement(window); |
| 111 | + } |
| 112 | + |
| 113 | + public static Window getWindow(int index) { |
| 114 | + return (Window) windows.elementAt(index); |
| 115 | + } |
| 116 | + |
| 117 | + public static Enumeration enumWindows() { |
| 118 | + return windows.elements(); |
| 119 | + } |
| 120 | + |
| 121 | + public static int getWindowIndex(Window win) { |
| 122 | + return windows.indexOf(win); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class NestedDialogTestFrame extends Frame { |
| 127 | + NestedDialogTestFrame(String name) { |
| 128 | + super(name); |
| 129 | + setSize(200, 200); |
| 130 | + show(); |
| 131 | + |
| 132 | + setLayout(new FlowLayout()); |
| 133 | + Button btnDlg = new Button("Dialog..."); |
| 134 | + add(btnDlg); |
| 135 | + Button btnFileDlg = new Button("FileDialog..."); |
| 136 | + add(btnFileDlg); |
| 137 | + |
| 138 | + addWindowListener(new WindowAdapter() { |
| 139 | + public void windowClosing(WindowEvent ev) { |
| 140 | + System.exit(0); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + btnDlg.addActionListener( |
| 145 | + new ActionListener() { |
| 146 | + public void actionPerformed(ActionEvent e) { |
| 147 | + Dialog d1 = new SimpleDialog(NestedDialogTestFrame.this, null, true); |
| 148 | + System.out.println("Returned from showing dialog: " + d1); |
| 149 | + } |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + btnFileDlg.addActionListener( |
| 154 | + new ActionListener() { |
| 155 | + public void actionPerformed(ActionEvent e) { |
| 156 | + FileDialog dlg = new FileDialog(NestedDialogTestFrame.this); |
| 157 | + dlg.show(); |
| 158 | + } |
| 159 | + } |
| 160 | + ); |
| 161 | + |
| 162 | + validate(); |
| 163 | + } |
| 164 | + |
| 165 | + public void show() { |
| 166 | + if (!isVisible()) { |
| 167 | + NestedDialogTest.addWindow(this); |
| 168 | + } |
| 169 | + super.show(); |
| 170 | + } |
| 171 | + |
| 172 | + public void dispose() { |
| 173 | + NestedDialogTest.removeWindow(this); |
| 174 | + super.dispose(); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +class SimpleDialog extends Dialog { |
| 179 | + Button btnNested; |
| 180 | + Button btnFileDlg; |
| 181 | + Button btnShow; |
| 182 | + Button btnHide; |
| 183 | + Button btnDispose; |
| 184 | + Button btnExit; |
| 185 | + List listWins; |
| 186 | + Dialog dlgPrev; |
| 187 | + |
| 188 | + public SimpleDialog(Frame frame, Dialog prev, boolean isModal) { |
| 189 | + super(frame, "", isModal); |
| 190 | + |
| 191 | + dlgPrev = prev; |
| 192 | + |
| 193 | + addWindowListener(new WindowAdapter() { |
| 194 | + public void windowActivated(WindowEvent ev) { |
| 195 | + populateListWin(); |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + setTitle(getName()); |
| 200 | + |
| 201 | + Panel panelNorth = new Panel(); |
| 202 | + panelNorth.setLayout(new GridLayout(1, 1)); |
| 203 | + listWins = new List(); |
| 204 | + panelNorth.add(listWins); |
| 205 | + |
| 206 | + Panel panelSouth = new Panel(); |
| 207 | + panelSouth.setLayout(new FlowLayout()); |
| 208 | + btnNested = new Button("Dialog..."); |
| 209 | + panelSouth.add(btnNested); |
| 210 | + btnFileDlg = new Button("FileDialog..."); |
| 211 | + panelSouth.add(btnFileDlg); |
| 212 | + btnShow = new Button("Show"); |
| 213 | + panelSouth.add(btnShow); |
| 214 | + btnHide = new Button("Hide"); |
| 215 | + panelSouth.add(btnHide); |
| 216 | + btnDispose = new Button("Dispose"); |
| 217 | + panelSouth.add(btnDispose); |
| 218 | + |
| 219 | + Choice cbox = new Choice(); |
| 220 | + cbox.add("Test1"); |
| 221 | + cbox.add("Test2"); |
| 222 | + cbox.add("Test3"); |
| 223 | + cbox.add("Test4"); |
| 224 | + cbox.add("Test5"); |
| 225 | + cbox.add("Test6"); |
| 226 | + cbox.add("Test7"); |
| 227 | + cbox.add("Test8"); |
| 228 | + cbox.add("Test9"); |
| 229 | + cbox.add("Test10"); |
| 230 | + cbox.add("Test11"); |
| 231 | + panelSouth.add(cbox); |
| 232 | + |
| 233 | + validate(); |
| 234 | + |
| 235 | + add("Center", panelNorth); |
| 236 | + add("South", panelSouth); |
| 237 | + |
| 238 | + btnNested.addActionListener(new ActionListener() { |
| 239 | + public void actionPerformed(ActionEvent e) { |
| 240 | + Dialog dlg = new SimpleDialog((Frame) getParent(), SimpleDialog.this, true); |
| 241 | + System.out.println("Returned from showing dialog: " + dlg); |
| 242 | + } |
| 243 | + }); |
| 244 | + |
| 245 | + btnFileDlg.addActionListener(new ActionListener() { |
| 246 | + public void actionPerformed(ActionEvent e) { |
| 247 | + FileDialog dlg = new FileDialog((Frame) getParent()); |
| 248 | + dlg.show(); |
| 249 | + } |
| 250 | + }); |
| 251 | + |
| 252 | + btnHide.addActionListener(new ActionListener() { |
| 253 | + public void actionPerformed(ActionEvent e) { |
| 254 | + Window wnd = getSelectedWindow(); |
| 255 | + System.out.println(wnd); |
| 256 | + wnd.hide(); |
| 257 | + } |
| 258 | + }); |
| 259 | + |
| 260 | + btnShow.addActionListener(new ActionListener() { |
| 261 | + public void actionPerformed(ActionEvent e) { |
| 262 | + getSelectedWindow().show(); |
| 263 | + } |
| 264 | + }); |
| 265 | + |
| 266 | + btnDispose.addActionListener(new ActionListener() { |
| 267 | + public void actionPerformed(ActionEvent e) { |
| 268 | + getSelectedWindow().dispose(); |
| 269 | + populateListWin(); |
| 270 | + } |
| 271 | + }); |
| 272 | + |
| 273 | + pack(); |
| 274 | + setSize(getSize().width, getSize().height * 2); |
| 275 | + if (dlgPrev != null) { |
| 276 | + Point pt = dlgPrev.getLocation(); |
| 277 | + setLocation(pt.x + 30, pt.y + 50); |
| 278 | + } |
| 279 | + show(); |
| 280 | + } |
| 281 | + |
| 282 | + private Window getSelectedWindow() { |
| 283 | + Window window; |
| 284 | + int index = listWins.getSelectedIndex(); |
| 285 | + |
| 286 | + window = NestedDialogTest.getWindow(index); |
| 287 | + return window; |
| 288 | + } |
| 289 | + |
| 290 | + private void populateListWin() { |
| 291 | + Enumeration enumWindows = NestedDialogTest.enumWindows(); |
| 292 | + |
| 293 | + listWins.removeAll(); |
| 294 | + while (enumWindows.hasMoreElements()) { |
| 295 | + Window win = (Window) enumWindows.nextElement(); |
| 296 | + listWins.add(win.getName()); |
| 297 | + } |
| 298 | + listWins.select(NestedDialogTest.getWindowIndex(this)); |
| 299 | + } |
| 300 | + |
| 301 | + public void show() { |
| 302 | + if (!isVisible()) { |
| 303 | + NestedDialogTest.addWindow(this); |
| 304 | + } |
| 305 | + super.show(); |
| 306 | + } |
| 307 | + |
| 308 | + public void dispose() { |
| 309 | + NestedDialogTest.removeWindow(this); |
| 310 | + super.dispose(); |
| 311 | + } |
| 312 | +} |
0 commit comments