File: FrameStateTest.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 (324 lines) | stat: -rw-r--r-- 12,198 bytes parent folder | download | duplicates (9)
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
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 2012, 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.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import javax.swing.JPanel;
import javax.swing.Timer;

/*
 * @test
 * @bug 4157271
 * @summary Checks that when a Frame is created it honors the state it
 *          was set to. The bug was that if setState(Frame.ICONIFIED) was
 *          called before setVisible(true) the Frame would be shown in NORMAL
 *          state instead of ICONIFIED.
 * @library /java/awt/regtesthelpers
 * @build PassFailJFrame
 * @run main/manual FrameStateTest
 */

public class FrameStateTest implements ActionListener {

    private static final String INSTRUCTIONS = """
        <html><body><p>
        This test checks that when setState(Frame.ICONIFIED) is called before
        setVisible(true) the Frame is shown in the proper iconified state.
        The problem was that it did not honor the initial iconic state, but
        instead was shown in the NORMAL state.
        </p><hr/>
        Steps to try to reproduce this problem:
        <p>
        Select the different options for the Frame:
            <ul>
                <li><i>{Normal, Non-resizable}</i></li>
                <li><i>{Normal, Resizable}</i></li>
                <li><i>{Iconified, Resizable}</i></li>
                <li><i>{Iconified, Non-resizable}</i></li>
            </ul>
        After choosing the Frame's state click the
        Create Frame button.<br>
        After the Frame (Frame State Test (Window2)) comes up make sure the
        proper behavior occurred (Frame shown in proper state).<br>
        Click the Dispose button to close the Frame.<br>
        </p><hr/><p>

        Do the above steps for all the different Frame state combinations
        available.<br>
        For "Hide, Iconify and Show" case, the frame is hidden then iconified
        hence Window2 is not seen on-screen when shown as the frame is still
        in the ICONIFIED state. Window2 is visible on-screen when it is restored
        to NORMAL state as observed with "Hide, Iconify, Show and Restore" case.
        <br><br>

        If you observe the proper behavior for all the combinations,
        press PASS else FAIL.<br>
        </p><p>
        Note: In Frame State Test (Window2) you can also chose the different
        buttons to see different Frame behavior.<br>An example of a problem that
        has been seen, with the Frame non-resizable you can not iconify the Frame.
        </p>
        </body>
        </html>
        """;

    public static final int DELAY = 1000;

    Button btnCreate = new Button("Create Frame");
    Button btnDispose = new Button("Dispose Frame");
    CheckboxGroup cbgState = new CheckboxGroup();
    CheckboxGroup cbgResize = new CheckboxGroup();
    Checkbox cbIconState = new Checkbox("Frame State ICONIFIED", cbgState, true);
    Checkbox cbNormState = new Checkbox("Frame State NORMAL", cbgState, false);
    Checkbox cbNonResize = new Checkbox("Frame Non-Resizable", cbgResize, false);
    Checkbox cbResize = new Checkbox("Frame Resizable", cbgResize, true);

    CreateFrame icontst;

    public static void main(String[] args) throws Exception {
        PassFailJFrame
                .builder()
                .title("Frame State and Size Test Instructions")
                .instructions(INSTRUCTIONS)
                .testTimeOut(10)
                .rows(27)
                .columns(70)
                .logArea(6)
                .splitUIBottom(() -> new FrameStateTest().createPanel())
                .build()
                .awaitAndCheck();
    }

    public JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 3));
        btnDispose.setEnabled(false);

        panel.add(cbIconState);
        panel.add(cbResize);
        panel.add(btnCreate);
        panel.add(cbNormState);
        panel.add(cbNonResize);
        panel.add(btnDispose);

        btnDispose.addActionListener(this);
        btnCreate.addActionListener(this);

        return panel;
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == btnCreate) {
            btnCreate.setEnabled(false);
            btnDispose.setEnabled(true);
            icontst = new CreateFrame(cbIconState.getState(), cbResize.getState());
            icontst.setVisible(true);
        } else if (evt.getSource() == btnDispose) {
            btnCreate.setEnabled(true);
            btnDispose.setEnabled(false);
            icontst.dispose();
        }
    }

    static class CreateFrame extends Frame
            implements ActionListener, WindowListener {

        Button b1, b2, b3, b4, b5, b6, b7;
        boolean isResizable;
        String name = "Frame State Test";

        CreateFrame(boolean iconified, boolean resizable) {
            setTitle("Test Window (Window 2)");

            isResizable = resizable;

            PassFailJFrame.log("CREATING FRAME - Initially " +
                    ((iconified) ? "ICONIFIED" : "NORMAL (NON-ICONIFIED)") + " and " +
                    ((isResizable) ? "RESIZABLE" : "NON-RESIZABLE"));

            setLayout(new FlowLayout());
            add(b1 = new Button("Resizable"));
            add(b2 = new Button("Resize"));
            add(b3 = new Button("Iconify"));
            add(b4 = new Button("Iconify and Restore"));
            add(b5 = new Button("Hide and Show"));
            add(b6 = new Button("Hide, Iconify and Show"));
            add(b7 = new Button("Hide, Iconify, Show and Restore"));
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);
            b6.addActionListener(this);
            b7.addActionListener(this);
            addWindowListener(this);

            setBounds(100, 2, 300, 200);
            setState(iconified ? Frame.ICONIFIED : Frame.NORMAL);
            setResizable(isResizable);
            setVisible(true);
        }

        /**
         * Calls all runnables on EDT with a {@code DELAY} delay before each run.
         * @param runnables to run
         */
        private static void delayedActions(Runnable... runnables) {
            setTimer(new ArrayDeque<>(Arrays.asList(runnables)));
        }

        private static void setTimer(Deque<Runnable> deque) {
            if (deque == null || deque.isEmpty()) return;

            Timer timer = new Timer(DELAY, e -> {
                deque.pop().run();
                setTimer(deque);
            });
            timer.setRepeats(false);
            timer.start();
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b2) {
                Rectangle r = this.getBounds();
                r.width += 10;
                stateLog(" - button pressed - setting bounds on Frame to: " + r);
                setBounds(r);
                validate();
            } else if (e.getSource() == b1) {
                isResizable = !isResizable;
                stateLog(" - button pressed - setting Resizable to: " + isResizable);
                ((Frame) (b1.getParent())).setResizable(isResizable);
            } else if (e.getSource() == b3) {
                stateLog(" - button pressed - setting Iconic: ");
                ((Frame) (b1.getParent())).setState(Frame.ICONIFIED);
                stateLog();
            } else if (e.getSource() == b4) {
                stateLog(" - button pressed - setting Iconic: ");
                ((Frame) (b1.getParent())).setState(Frame.ICONIFIED);
                stateLog();
                delayedActions(() -> {
                    stateLog(" - now restoring: ");
                    ((Frame) (b1.getParent())).setState(Frame.NORMAL);
                    stateLog();
                });
            } else if (e.getSource() == b5) {
                stateLog(" - button pressed - hiding : ");
                b1.getParent().setVisible(false);
                stateLog();
                delayedActions(() -> {
                    stateLog(" - now reshowing: ");
                    b1.getParent().setVisible(true);
                    stateLog();
                });
            } else if (e.getSource() == b6) {
                stateLog(" - button pressed - hiding : ");
                b1.getParent().setVisible(false);
                stateLog();
                delayedActions(
                        () -> {
                            stateLog(" - setting Iconic: ");
                            ((Frame) (b1.getParent())).setState(Frame.ICONIFIED);
                        },
                        () -> {
                            stateLog(" - now reshowing: ");
                            b1.getParent().setVisible(true);
                            stateLog();
                        }
                );
            } else if (e.getSource() == b7) {
                stateLog(" - button pressed - hiding : ");
                b1.getParent().setVisible(false);
                stateLog();

                delayedActions(
                        () -> {
                            stateLog(" - setting Iconic: ");
                            ((Frame) (b1.getParent())).setState(Frame.ICONIFIED);
                        },
                        () -> {
                            stateLog(" - now reshowing: ");
                            b1.getParent().setVisible(true);
                            stateLog();
                        },
                        () -> {
                            stateLog(" - now restoring: ");
                            ((Frame) (b1.getParent())).setState(Frame.NORMAL);
                            stateLog();
                        }
                );
            }
        }

        public void windowActivated(WindowEvent e) {
            stateLog("Activated");
        }

        public void windowClosed(WindowEvent e) {
            stateLog("Closed");
        }

        public void windowClosing(WindowEvent e) {
            ((Window) (e.getSource())).dispose();
            stateLog("Closing");
        }

        public void windowDeactivated(WindowEvent e) {
            stateLog("Deactivated");
        }

        public void windowDeiconified(WindowEvent e) {
            stateLog("Deiconified");
        }

        public void windowIconified(WindowEvent e) {
            stateLog("Iconified");
        }

        public void windowOpened(WindowEvent e) {
            stateLog("Opened");
        }

        public void stateLog(String message) {
            PassFailJFrame
                .log("[Current State = %d] %s %s".formatted(getState(), name, message));
        }

        public void stateLog() {
            PassFailJFrame.log("[Current State = " + getState() + "]");
        }
    }
}