1 /*
2  * Copyright (c) 2007, 2018, 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 
24 /*
25  * @test
26  * @key headful
27  * @bug 4977491 8160767
28  * @summary State changes should always be reported as events
29  * @run main MaximizedToIconified
30  */
31 
32 /*
33  * MaximizedToIconified.java
34  *
35  * summary:  Invoking setExtendedState(ICONIFIED) on a maximized
36  *           frame should not combine the maximized and iconified
37  *           states in the newState of the state change event.
38  */
39 
40 import java.awt.Frame;
41 import java.awt.Robot;
42 import java.awt.Toolkit;
43 import java.awt.event.WindowEvent;
44 import java.awt.event.WindowStateListener;
45 
46 public class MaximizedToIconified
47 {
48     static volatile int lastFrameState = Frame.NORMAL;
49     static volatile boolean failed = false;
50     static volatile Toolkit myKit;
51     private static Robot robot;
52 
checkState(Frame frame, int state)53     private static void checkState(Frame frame, int state) {
54         frame.setExtendedState(state);
55         robot.waitForIdle();
56         robot.delay(100);
57 
58         System.out.println("state = " + state + "; getExtendedState() = " + frame.getExtendedState());
59 
60         if (failed) {
61             frame.dispose();
62             throw new RuntimeException("getOldState() != previous getNewState() in WINDOW_STATE_CHANGED event.");
63         }
64         if (lastFrameState != frame.getExtendedState()) {
65             frame.dispose();
66             throw new RuntimeException("getExtendedState() != last getNewState() in WINDOW_STATE_CHANGED event.");
67         }
68         if (frame.getExtendedState() != state) {
69             frame.dispose();
70             throw new RuntimeException("getExtendedState() != " + state + " as expected.");
71         }
72     }
73 
examineStates(int states[])74     private static void examineStates(int states[]) {
75 
76         Frame frame = new Frame("test");
77         frame.setSize(200, 200);
78         frame.setVisible(true);
79 
80         robot.waitForIdle();
81 
82         frame.addWindowStateListener(new WindowStateListener() {
83             public void windowStateChanged(WindowEvent e) {
84                 System.out.println("last = " + lastFrameState + "; getOldState() = " + e.getOldState() +
85                         "; getNewState() = " + e.getNewState());
86                 if (e.getOldState() == lastFrameState) {
87                     lastFrameState = e.getNewState();
88                 } else {
89                     System.out.println("Wrong getOldState(): expected = " + lastFrameState + "; received = " +
90                             e.getOldState());
91                     failed = true;
92                 }
93             }
94         });
95 
96         for (int state : states) {
97             if (myKit.isFrameStateSupported(state)) {
98                 checkState(frame, state);
99             } else {
100                 System.out.println("Frame state = " + state + " is NOT supported by the native system. The state is skipped.");
101             }
102         }
103 
104         if (frame != null) {
105             frame.dispose();
106         }
107     }
108 
doTest()109     private static void doTest() {
110 
111         myKit = Toolkit.getDefaultToolkit();
112 
113         // NOTE! Compound states (like MAXIMIZED_BOTH | ICONIFIED) CANNOT be used,
114         //    because Toolkit.isFrameStateSupported() method reports these states
115         //    as not supported. And such states will simply be skipped.
116         examineStates(new int[] {Frame.MAXIMIZED_BOTH, Frame.ICONIFIED, Frame.NORMAL});
117         examineStates(new int[] {Frame.ICONIFIED, Frame.MAXIMIZED_BOTH, Frame.NORMAL});
118 
119     }
120 
main( String args[] )121     public static void main( String args[] ) throws Exception
122     {
123         robot = new Robot();
124         doTest();
125 
126     }
127 
128 }
129