1/*
2 * Copyright (c) 2007, 2014, 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
24import java.awt.*;
25import static jdk.testlibrary.Asserts.*;
26
27
28public class UnblockedDialogTest {
29
30    private TestDialog dialog;
31
32    private static final int delay = 500;
33    private final ExtendedRobot robot;
34
35    private Dialog parentDialog;
36    private Frame  parentFrame;
37
38    private enum DialogOwner {HIDDEN_DIALOG, HIDDEN_FRAME, NULL_DIALOG, NULL_FRAME};
39
40    Dialog.ModalityType modalityType;
41    boolean setModal;
42
43    private UnblockedDialogTest(Dialog.ModalityType modType,
44                                boolean             set) throws Exception {
45
46        robot = new ExtendedRobot();
47        modalityType = modType;
48        setModal = set;
49    }
50
51    public UnblockedDialogTest(Dialog.ModalityType modType) throws Exception {
52        this(modType, false);
53    }
54
55    public UnblockedDialogTest() throws Exception { this(null, true); }
56
57
58    private void createGUI(DialogOwner owner) {
59
60        switch (owner) {
61            case HIDDEN_DIALOG:
62                parentDialog = new Dialog((Frame) null);
63                dialog = new TestDialog(parentDialog);
64                break;
65            case NULL_DIALOG:
66                dialog = new TestDialog((Dialog) null);
67                break;
68            case HIDDEN_FRAME:
69                parentFrame = new Frame();
70                dialog = new TestDialog(parentFrame);
71                break;
72            case NULL_FRAME:
73                dialog = new TestDialog((Frame) null);
74                break;
75        }
76
77        assertFalse(dialog == null, "error: null dialog");
78
79        dialog.setLocation(50, 50);
80        if (setModal) {
81            dialog.setModal(true);
82        } else if (modalityType != null) {
83            dialog.setModalityType(modalityType);
84        }
85
86        dialog.setVisible(true);
87    }
88
89    public void doTest() throws Exception {
90
91        try {
92
93            robot.waitForIdle(delay);
94
95            for (DialogOwner owner: DialogOwner.values()) {
96
97                EventQueue.invokeLater(() -> { createGUI(owner); });
98
99                robot.waitForIdle(delay);
100
101                dialog.activated.waitForFlagTriggered();
102                assertTrue(dialog.activated.flag(), "Dialog did not trigger " +
103                    "Window Activated event when it became visible");
104
105                dialog.closeGained.waitForFlagTriggered();
106                assertTrue(dialog.closeGained.flag(), "The 1st button did not " +
107                    "gain focus when the dialog became visible");
108
109                dialog.checkUnblockedDialog(robot, "");
110                robot.waitForIdle(delay);
111            }
112
113        } finally {
114            EventQueue.invokeAndWait(this::closeAll);
115        }
116    }
117
118    private void closeAll() {
119        if (dialog != null) { dialog.dispose(); }
120        if (parentDialog != null) { parentDialog.dispose(); }
121        if (parentFrame  != null) {  parentFrame.dispose(); }
122    }
123}
124