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.*;
25
26public class DialogToFrontModalBlockedTest {
27
28    private volatile CustomDialog dialog;
29    private volatile TestDialog leftDialog;
30    private volatile TestFrame  rightFrame;
31    private volatile Frame parent;
32
33    private static final int delay = 500;
34    private final ExtendedRobot robot;
35
36    private DialogToFrontModalBlockedTest(Dialog.ModalityType modalityType,
37                                          boolean             setModal) throws Exception {
38
39        robot = new ExtendedRobot();
40        EventQueue.invokeLater(() -> {
41            createGUI(modalityType, setModal);
42        });
43    }
44
45    public DialogToFrontModalBlockedTest(Dialog.ModalityType modalityType) throws Exception {
46        this(modalityType, false);
47    }
48
49    public DialogToFrontModalBlockedTest() throws Exception {
50        this(null, true);
51    }
52
53    private void createGUI(Dialog.ModalityType modalityType,
54                           boolean             setModal) {
55
56        parent = new Frame();
57        leftDialog = new TestDialog(parent);
58        leftDialog.setSize(200, 100);
59        leftDialog.setLocation(50, 50);
60        leftDialog.setVisible(true);
61
62        dialog = new CustomDialog(leftDialog);
63
64        if (setModal) { dialog.setModal(true); }
65        else if (modalityType != null) {
66            dialog.setModalityType(modalityType);
67        }
68
69        dialog.setSize(200, 100);
70        dialog.setLocation(150, 50);
71
72        rightFrame = new TestFrame();
73        rightFrame.setSize(200, 100);
74        rightFrame.setLocation(250, 50);
75
76        if (setModal || modalityType == Dialog.ModalityType.APPLICATION_MODAL) {
77            rightFrame.setModalExclusionType(
78                Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
79        } else if (modalityType == Dialog.ModalityType.TOOLKIT_MODAL) {
80            rightFrame.setModalExclusionType(
81                Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
82        }
83
84        dialog.setVisible(true);
85    }
86
87    public void doTest() throws Exception {
88
89        try {
90            robot.waitForIdle(delay);
91
92            dialog.clickOpenButton(robot);
93            robot.waitForIdle(delay);
94
95            rightFrame.clickCloseButton(robot);
96            robot.waitForIdle(delay);
97
98            EventQueue.invokeAndWait(() -> { leftDialog.toFront(); });
99            robot.waitForIdle(delay);
100
101            rightFrame.clickDummyButton(robot);
102
103            EventQueue.invokeAndWait(() -> { leftDialog.toFront(); });
104            robot.waitForIdle(delay);
105
106            leftDialog.clickDummyButton(robot, 7, false, "Calling toFront " +
107                    "for Dialog blocked by " + dialog.getModalityType() +
108                    "dialog brought it to the top of the modal dialog");
109
110            robot.waitForIdle(delay);
111        } finally {
112            EventQueue.invokeAndWait(this::closeAll);
113        }
114    }
115
116    private void closeAll() {
117        if (dialog != null) { dialog.dispose(); }
118        if (parent != null) { parent.dispose(); }
119        if (leftDialog != null) { leftDialog.dispose(); }
120        if (rightFrame != null) { rightFrame.dispose(); }
121    }
122
123    class CustomDialog extends TestDialog {
124
125        public CustomDialog(Dialog d) { super(d); }
126        public CustomDialog(Frame  f) { super(f); }
127
128        @Override
129        public void doOpenAction() {
130            if (rightFrame != null) {
131                rightFrame.setVisible(true);
132            }
133        }
134    }
135}
136