changeset 6952:97de64f172be

8032078: [macosx] CPlatformWindow.setWindowState throws RuntimeException, if windowState=ICONIFIED|MAXIMIZED_BOTH Reviewed-by: pchelko, serb
author alitvinov
date Mon, 24 Feb 2014 19:17:40 +0400
parents e03ef8c99170
children 0da092934c9d
files src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java test/java/awt/Frame/ExceptionOnSetExtendedStateTest/ExceptionOnSetExtendedStateTest.java
diffstat 2 files changed, 114 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Mon Feb 24 11:59:28 2014 +0400
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Mon Feb 24 19:17:40 2014 +0400
@@ -580,7 +580,12 @@
         if (visible) {
             // Apply the extended state as expected in shared code
             if (target instanceof Frame) {
-                switch (((Frame)target).getExtendedState()) {
+                int frameState = ((Frame)target).getExtendedState();
+                if ((frameState & Frame.ICONIFIED) != 0) {
+                    // Treat all state bit masks with ICONIFIED bit as ICONIFIED state.
+                    frameState = Frame.ICONIFIED;
+                }
+                switch (frameState) {
                     case Frame.ICONIFIED:
                         CWrapper.NSWindow.miniaturize(nsWindowPtr);
                         break;
@@ -806,6 +811,10 @@
         if (prevWindowState == windowState) return;
 
         final long nsWindowPtr = getNSWindowPtr();
+        if ((windowState & Frame.ICONIFIED) != 0) {
+            // Treat all state bit masks with ICONIFIED bit as ICONIFIED state.
+            windowState = Frame.ICONIFIED;
+        }
         switch (windowState) {
             case Frame.ICONIFIED:
                 if (prevWindowState == Frame.MAXIMIZED_BOTH) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Frame/ExceptionOnSetExtendedStateTest/ExceptionOnSetExtendedStateTest.java	Mon Feb 24 19:17:40 2014 +0400
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2014, 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 8032078
+   @summary Frame.setExtendedState throws RuntimeException, if
+            windowState=ICONIFIED|MAXIMIZED_BOTH, on OS X
+   @author Anton Litvinov
+*/
+
+import java.awt.*;
+
+import sun.awt.SunToolkit;
+
+public class ExceptionOnSetExtendedStateTest {
+    private static final int[] frameStates = { Frame.NORMAL, Frame.ICONIFIED, Frame.MAXIMIZED_BOTH };
+    private static final SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
+
+    private static boolean validatePlatform() {
+        String osName = System.getProperty("os.name");
+        if (osName == null) {
+            throw new RuntimeException("Name of the current OS could not be retrieved.");
+        }
+        return osName.startsWith("Mac");
+    }
+
+    private static void testStateChange(int oldState, int newState, boolean decoratedFrame) {
+        System.out.println(String.format(
+            "testStateChange: oldState='%d', newState='%d', decoratedFrame='%b'",
+            oldState, newState, decoratedFrame));
+
+        Frame frame = new Frame("ExceptionOnSetExtendedStateTest");
+        frame.setSize(200, 200);
+        frame.setUndecorated(!decoratedFrame);
+        frame.setVisible(true);
+        toolkit.realSync();
+
+        frame.setExtendedState(oldState);
+        sleep(1000);
+        frame.setExtendedState(newState);
+
+        boolean stateWasNotChanged = true;
+        int currentState = 0;
+        for (int i = 0; (i < 3) && stateWasNotChanged; i++) {
+            sleep(1000);
+            currentState = frame.getExtendedState();
+            if ((currentState == newState) ||
+                (((newState & Frame.ICONIFIED) != 0) && ((currentState & Frame.ICONIFIED) != 0))) {
+                stateWasNotChanged = false;
+            }
+        }
+        frame.dispose();
+
+        if (stateWasNotChanged) {
+            throw new RuntimeException(String.format(
+                "Frame state was not changed. currentState='%d'", currentState));
+        }
+    }
+
+    private static void sleep(int millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void main(String[] args) {
+        if (!validatePlatform()) {
+            System.out.println("This test is only for OS X.");
+            return;
+        }
+
+        // Verify that changing states of decorated/undecorated frame to/from supported states
+        // and the state bit mask ICONIFIED | MAXIMIZED_BOTH does not raise RuntimeException.
+        for (int i = 0; i < frameStates.length; i++) {
+            testStateChange(frameStates[i], Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, true);
+            testStateChange(frameStates[i], Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, false);
+            testStateChange(Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, frameStates[i], true);
+            // Uncomment the line below, when 8026143 is ported to JDK 7.
+            //testStateChange(Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, frameStates[i], false);
+        }
+    }
+}