File: BlockedMouseInputTest3.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (216 lines) | stat: -rw-r--r-- 7,210 bytes parent folder | download | duplicates (12)
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
/*
 * Copyright (c) 2005, 2023, 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.
 */

/*
  @test
  @bug 6274378
  @summary Test for 6274378: Blocked mouse and keyboard input after hiding modal dialog
  @key headful
  @run main BlockedMouseInputTest3
*/

import java.awt.Button;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Robot;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class BlockedMouseInputTest3 {
    Frame frame;

    Dialog dlg1; // application-modal
    Dialog dlg2; // application-modal
    Dialog d; // toolkit-modal

    Button b1; // in dlg1
    Button b2; // in dlg2

    Robot r = null;

    volatile boolean b1pressed, b2pressed;
    volatile boolean dlg1activated, dlg2activated;
    volatile int b1Width, b1Height;
    volatile int b2Width, b2Height;
    volatile Point p1, p2;

    public static void main(String args[]) throws Exception {
        BlockedMouseInputTest3 test = new BlockedMouseInputTest3();
        test.start();
    }

    public void start() throws Exception {
        try {
            r = new Robot();
            EventQueue.invokeAndWait(() -> {
                frame = new Frame("Parent frame");
                frame.setBounds(0, 0, 200, 100);
                frame.setVisible(true);

                // create d and set it visible
                d = new Dialog(frame, "Toolkit-modal", Dialog.ModalityType.TOOLKIT_MODAL);
                d.setBounds(250, 0, 200, 100);
            });
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    d.setVisible(true);
                }
            });

            r.delay(1000);
            r.waitForIdle();

            // create dlg1 and set it visible
            // dlg1 is blocked by d

            EventQueue.invokeAndWait(() -> {
                dlg1 = new Dialog(frame, "Application-modal 1", Dialog.ModalityType.APPLICATION_MODAL);
                dlg1.setBounds(0, 150, 200, 100);
                dlg1.addWindowListener(new WindowAdapter() {
                    public void windowActivated(WindowEvent e) {
                        dlg1activated = true;
                    }
                });
                b1 = new Button("B1");
                b1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        b1pressed = true;
                    }
                });
                dlg1.add(b1);
            });
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    dlg1.setVisible(true);
                }
            });

            r.delay(1000);
            r.waitForIdle();

            // create dlg2 and set it visible
            // dlg2 is blocked by d
            EventQueue.invokeAndWait(() -> {
                dlg2 = new Dialog(frame, "Application-modal 2", Dialog.ModalityType.APPLICATION_MODAL);
                dlg2.setBounds(0, 300, 200, 100);
                dlg2.addWindowListener(new WindowAdapter() {
                    public void windowActivated(WindowEvent e) {
                        dlg2activated = true;
                    }
                });
                b2 = new Button("B2");
                b2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        b2pressed = true;
                    }
                });
                dlg2.add(b2);
            });
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    dlg2.setVisible(true);
                }
            });

            r.delay(1000);
            r.waitForIdle();


            // hide d
            // dlg2 is unblocked and dlg1 is blocked by dlg2
            EventQueue.invokeAndWait(() -> {
                d.setVisible(false);
            });

            r.delay(1000);
            r.waitForIdle();

            // values to check
            b1pressed = false;
            b2pressed = false;
            dlg1activated = false;
            dlg2activated = false;
            System.err.println(KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow());

            // check mouse events and activation
            EventQueue.invokeAndWait(() -> {
                p1 = b1.getLocationOnScreen();
                b1Width = b1.getWidth();
                b1Height = b1.getHeight();
            });
            clickPoint(r, p1.x + b1Width / 2, p1.y + b1Height / 2);

            EventQueue.invokeAndWait(() -> {
                dlg1activated = (KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() == dlg1);
            });

            EventQueue.invokeAndWait(() -> {
                p2 = b2.getLocationOnScreen();
                b2Width = b2.getWidth();
                b2Height = b2.getHeight();
            });

            clickPoint(r, p2.x + b2Width / 2, p2.y + b2Height / 2);

            EventQueue.invokeAndWait(() -> {
                dlg2activated = (KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() == dlg2);
            });

            if (dlg1activated || b1pressed || !dlg2activated || !b2pressed) {
                throw new RuntimeException("Test is FAILED");
            }
        } finally {
            EventQueue.invokeAndWait(() -> {
                if (frame != null) {
                    frame.dispose();
                }
                if (dlg1 != null) {
                    dlg1.dispose();
                }
                if (dlg2 != null) {
                    dlg2.dispose();
                }
                if (d != null) {
                    d.dispose();
                }
            });
        }
    }

    private static void clickPoint(Robot r, int x, int y) {
        r.mouseMove(x, y);
        r.delay(500);
        r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        r.delay(500);
        r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        r.delay(500);
    }
}