File: ModalExcludedTest.java

package info (click to toggle)
openjdk-26 26~12ea-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 826,896 kB
  • sloc: java: 5,602,163; cpp: 1,336,664; xml: 1,321,153; ansic: 488,421; asm: 404,003; objc: 21,113; sh: 15,220; javascript: 13,281; python: 8,323; makefile: 2,519; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (315 lines) | stat: -rw-r--r-- 11,524 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.JobAttributes;
import java.awt.PageAttributes;
import java.awt.Panel;
import java.awt.PrintJob;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

import sun.awt.SunToolkit;

/*
 * @test
 * @bug 4813288 4866704
 * @summary Test for "modal exclusion" functionality
 * @library /java/awt/regtesthelpers /test/lib
 * @build PassFailJFrame
 * @modules java.desktop/sun.awt
 * @run main/manual ModalExcludedTest
 */

public class ModalExcludedTest {
    private static final String INSTRUCTIONS = """
            1. Press 'Modal dialog w/o modal excluded' button below
                A window, a modeless dialog and a modal dialog will appear
                Make sure the frame and the modeless dialog are inaccessible,
                i.e. receive no mouse and keyboard events. MousePressed and
                KeyPressed events are logged in the text area - use it
                to watch events
            Close all 3 windows

            2. Press 'Modal dialog w/ modal excluded' button below
                Again, 3 windows will appear (frame, dialog, modal dialog),
                but the frame and the dialog would be modal excluded, i.e.
                behave the same way as there is no modal dialog shown. Verify
                this by pressing mouse buttons and typing any keys. The
                RootFrame would be modal blocked - verify this too
            Close all 3 windows

            3. Repeat step 2 for file and print dialogs using appropriate
                buttons below

            Notes: if there is no printer installed in the system you may not
                get any print dialogs
            """;

    public static void main(String[] args) throws Exception {
        PassFailJFrame.builder()
                .title("ModalExcludedTest")
                .instructions(INSTRUCTIONS)
                .rows(10)
                .columns(35)
                .testUI(ModalExcludedTest::createGUIs)
                .build()
                .awaitAndCheck();
    }

    public static Frame createGUIs() {
        final Frame f = new Frame("RootFrame");
        f.setBounds(0, 0, 480, 500);
        f.setLayout(new BorderLayout());

        final TextArea messages = new TextArea();

        final WindowListener wl = new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                if (ev.getSource() instanceof Window) {
                    ((Window) ev.getSource()).dispose();
                }
            }
        };
        final MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent ev) {
                messages.append(ev + "\n");
            }
        };
        final KeyListener kl = new KeyAdapter() {
            public void keyPressed(KeyEvent ev) {
                messages.append(ev + "\n");
            }
        };

        if (!SunToolkit.isModalExcludedSupported()) {
            throw new jtreg.SkippedException("Modal exclude is not supported on this platform.");
        }

        messages.addMouseListener(ml);
        messages.addKeyListener(kl);
        f.add(messages, BorderLayout.CENTER);

        Panel buttons = new Panel();
        buttons.setLayout(new GridLayout(6, 1));

        Button b = new Button("Modal dialog w/o modal excluded");
        b.addActionListener(ev -> {
            Frame ff = new Frame("Non-modal-excluded frame");
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            ff.setVisible(true);

            Dialog dd = new Dialog(ff, "Non-modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);

            Dialog d = new Dialog(f, "Modal dialog", true);
            d.setBounds(600, 200, 200, 100);
            d.addWindowListener(wl);
            d.addMouseListener(ml);
            d.addKeyListener(kl);
            d.setVisible(true);
        });
        buttons.add(b);

        Button c = new Button("Modal dialog w/ modal excluded");
        c.addActionListener(ev -> {
            JFrame ff = new JFrame("Modal-excluded frame");
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            JMenuBar mb = new JMenuBar();
            JMenu m = new JMenu("Test menu");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            m.add("Test menu item");
            mb.add(m);
            ff.setJMenuBar(mb);
            // 1: set visible
            ff.setVisible(true);

            Dialog dd = new Dialog(ff, "Modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);

            // 2: set modal excluded
            SunToolkit.setModalExcluded(ff);

            Dialog d = new Dialog(f, "Modal dialog", true);
            d.setBounds(600, 200, 200, 100);
            d.addWindowListener(wl);
            d.addMouseListener(ml);
            d.addKeyListener(kl);
            d.setVisible(true);
        });
        buttons.add(c);

        Button c1 = new Button("Modal dialog before modal excluded");
        c1.addActionListener(ev -> {
            // 1: create dialog
            Dialog d = new Dialog(f, "Modal dialog", true);
            d.setBounds(600, 200, 200, 100);
            d.addWindowListener(wl);
            d.addMouseListener(ml);
            d.addKeyListener(kl);

            // 2: create frame
            Frame ff = new Frame("Modal-excluded frame");
            // 3: set modal excluded
            SunToolkit.setModalExcluded(ff);
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            // 4: show frame
            ff.setVisible(true);

            Dialog dd = new Dialog(ff, "Modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);

            // 5: show dialog
            d.setVisible(true);
        });
        buttons.add(c1);

        Button d = new Button("File dialog w/ modal excluded");
        d.addActionListener(ev -> {
            Frame ff = new Frame("Modal-excluded frame");
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            // 1: set modal excluded (peer is not created yet)
            SunToolkit.setModalExcluded(ff);
            // 2: set visible
            ff.setVisible(true);

            Dialog dd = new Dialog(ff, "Modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);
            SunToolkit.setModalExcluded(dd);

            Dialog d1 = new FileDialog(f, "File dialog");
            d1.setVisible(true);
        });
        buttons.add(d);

        Button e = new Button("Native print dialog w/ modal excluded");
        e.addActionListener(ev -> {
            Frame ff = new Frame("Modal-excluded frame");
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            ff.setVisible(true);
            SunToolkit.setModalExcluded(ff);

            Dialog dd = new Dialog(ff, "Modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);

            JobAttributes jobAttributes = new JobAttributes();
            jobAttributes.setDialog(JobAttributes.DialogType.NATIVE);
            PageAttributes pageAttributes = new PageAttributes();
            PrintJob job = Toolkit.getDefaultToolkit().getPrintJob(f, "Test", jobAttributes, pageAttributes);
        });
        buttons.add(e);

        Button g = new Button("Common print dialog w/ modal excluded");
        g.addActionListener(ev -> {
            Frame ff = new Frame("Modal-excluded frame");
            ff.setBounds(400, 0, 200, 100);
            ff.addWindowListener(wl);
            ff.addMouseListener(ml);
            ff.addKeyListener(kl);
            ff.setVisible(true);
            SunToolkit.setModalExcluded(ff);
            ff.dispose();
            // modal excluded must still be alive
            ff.setVisible(true);

            Dialog dd = new Dialog(ff, "Modal-excluded dialog", false);
            dd.setBounds(500, 100, 200, 100);
            dd.addWindowListener(wl);
            dd.addMouseListener(ml);
            dd.addKeyListener(kl);
            dd.setVisible(true);

            JobAttributes jobAttributes = new JobAttributes();
            jobAttributes.setDialog(JobAttributes.DialogType.COMMON);
            PageAttributes pageAttributes = new PageAttributes();
            PrintJob job = Toolkit.getDefaultToolkit().getPrintJob(f, "Test", jobAttributes, pageAttributes);
        });
        buttons.add(g);

        f.add(buttons, BorderLayout.SOUTH);
        return f;
    }
}