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
26
27public class FrameToFrontModalBlockedTest {
28
29    private volatile CustomDialog dialog;
30    private volatile TestFrame leftFrame, rightFrame;
31    private volatile Dialog hiddenDialog;
32    private volatile Frame  hiddenFrame;
33
34    private static final int delay = 500;
35    private final ExtendedRobot robot;
36
37    public enum DialogOwner {HIDDEN_DIALOG, NULL_DIALOG, HIDDEN_FRAME, NULL_FRAME, FRAME};
38
39    private DialogOwner owner;
40    boolean setModal;
41
42    Dialog.ModalityType modalityType;
43
44    private FrameToFrontModalBlockedTest(Dialog.ModalityType modType,
45                                         boolean             modal,
46                                         DialogOwner         o) throws Exception {
47        modalityType = modType;
48        setModal = modal;
49        owner = o;
50
51        robot = new ExtendedRobot();
52        EventQueue.invokeLater(this::createGUI);
53    }
54
55    public FrameToFrontModalBlockedTest(Dialog.ModalityType modalityType,
56                                        DialogOwner         o) throws Exception {
57        this(modalityType, false, o);
58    }
59
60    public FrameToFrontModalBlockedTest(DialogOwner o) throws Exception {
61        this(null, true, o);
62    }
63
64    private void createGUI() {
65
66        leftFrame = new TestFrame();
67        leftFrame.setSize(200, 100);
68        leftFrame.setLocation(50, 50);
69        leftFrame.setVisible(true);
70
71        switch (owner) {
72            case HIDDEN_DIALOG:
73                hiddenDialog = new Dialog((Frame) null);
74                dialog = new CustomDialog(hiddenDialog);
75                break;
76            case NULL_DIALOG:
77                dialog = new CustomDialog((Dialog) null);
78                break;
79            case HIDDEN_FRAME:
80                hiddenFrame = new Frame();
81                dialog = new CustomDialog(hiddenFrame);
82                break;
83            case NULL_FRAME:
84                dialog = new CustomDialog((Frame) null);
85                break;
86            case FRAME:
87                dialog = new CustomDialog(leftFrame);
88                break;
89        }
90
91        if (setModal) {
92            dialog.setModal(true);
93            modalityType = dialog.getModalityType();
94        } else if (modalityType != null) {
95            dialog.setModalityType(modalityType);
96        }
97
98        dialog.setSize(200, 100);
99        dialog.setLocation(230, 50);
100
101        rightFrame = new TestFrame();
102        rightFrame.setSize(200, 100);
103        rightFrame.setLocation(280, 50);
104
105        if  (setModal ||
106            (modalityType == Dialog.ModalityType.APPLICATION_MODAL)) {
107            rightFrame.setModalExclusionType(
108                    Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
109        }
110
111        if (modalityType == Dialog.ModalityType.TOOLKIT_MODAL) {
112            rightFrame.setModalExclusionType(
113                    Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
114        }
115
116        dialog.setVisible(true);
117    }
118
119    private void blockingTest() throws Exception {
120
121        dialog.clickOpenButton(robot);
122        robot.waitForIdle(delay);
123
124        rightFrame.clickCloseButton(robot);
125        robot.waitForIdle(delay);
126
127        EventQueue.invokeAndWait(() -> { leftFrame.toFront(); });
128        robot.waitForIdle(delay);
129
130        rightFrame.clickDummyButton(robot);
131        robot.waitForIdle(delay);
132
133        EventQueue.invokeAndWait(() -> { leftFrame.toFront(); });
134        robot.waitForIdle(delay);
135
136        leftFrame.clickDummyButton(robot, 7, false,
137            "Calling toFront for Frame blocked by " + dialog.getModalityType() +
138            " dialog brought it to the top of the modal dialog.");
139        robot.waitForIdle(delay);
140    }
141
142    private void docModalTest() throws Exception {
143
144        if (owner == DialogOwner.FRAME) { blockingTest(); }
145        else {
146            EventQueue.invokeAndWait(() -> { leftFrame.toFront(); });
147            robot.waitForIdle(delay);
148
149            leftFrame.clickDummyButton(robot);
150            robot.waitForIdle(delay);
151
152            EventQueue.invokeAndWait(() -> { dialog.toFront(); });
153            robot.waitForIdle(delay);
154
155            dialog.clickDummyButton(robot);
156            robot.waitForIdle(delay);
157        }
158
159    }
160
161
162    public void doTest() throws Exception {
163
164        try {
165
166            robot.waitForIdle(delay);
167
168            switch (modalityType) {
169                case APPLICATION_MODAL:
170                case TOOLKIT_MODAL:
171                    blockingTest();
172                    break;
173                case DOCUMENT_MODAL:
174                    docModalTest();
175                    break;
176                default:
177                    throw new RuntimeException(
178                        "improper modality type, please do not use it for this test");
179            }
180
181        } finally {
182            EventQueue.invokeAndWait(this::closeAll);
183        }
184    }
185
186    private void closeAll() {
187        if (dialog != null) { dialog.dispose(); }
188        if (leftFrame  != null) {  leftFrame.dispose(); }
189        if (rightFrame != null) { rightFrame.dispose(); }
190        if (hiddenDialog != null) { hiddenDialog.dispose(); }
191        if (hiddenFrame  != null) {  hiddenFrame.dispose(); }
192    }
193
194
195    class CustomDialog extends TestDialog {
196
197        public CustomDialog(Dialog d) { super(d); }
198        public CustomDialog(Frame  f) { super(f); }
199
200        @Override
201        public void doOpenAction() {
202            if (rightFrame != null) {
203                rightFrame.setVisible(true);
204            }
205        }
206    }
207}
208