1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25    @bug 8032078
  26    @summary Frame.setExtendedState throws RuntimeException, if
  27             windowState=ICONIFIED|MAXIMIZED_BOTH, on OS X
  28    @author Anton Litvinov
  29 */
  30 
  31 import java.awt.*;
  32 
  33 import sun.awt.SunToolkit;
  34 
  35 public class ExceptionOnSetExtendedStateTest {
  36     private static final int[] frameStates = { Frame.NORMAL, Frame.ICONIFIED, Frame.MAXIMIZED_BOTH };
  37     private static final SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  38 
  39     private static boolean validatePlatform() {
  40         String osName = System.getProperty("os.name");
  41         if (osName == null) {
  42             throw new RuntimeException("Name of the current OS could not be retrieved.");
  43         }
  44         return osName.startsWith("Mac");
  45     }
  46 
  47     private static void testStateChange(int oldState, int newState, boolean decoratedFrame) {
  48         System.out.println(String.format(
  49             "testStateChange: oldState='%d', newState='%d', decoratedFrame='%b'",
  50             oldState, newState, decoratedFrame));
  51 
  52         Frame frame = new Frame("ExceptionOnSetExtendedStateTest");
  53         frame.setSize(200, 200);
  54         frame.setUndecorated(!decoratedFrame);
  55         frame.setVisible(true);
  56         toolkit.realSync();
  57 
  58         frame.setExtendedState(oldState);
  59         sleep(1000);
  60         frame.setExtendedState(newState);
  61 
  62         boolean stateWasNotChanged = true;
  63         int currentState = 0;
  64         for (int i = 0; (i < 3) && stateWasNotChanged; i++) {
  65             sleep(1000);
  66             currentState = frame.getExtendedState();
  67             if ((currentState == newState) ||
  68                 (((newState & Frame.ICONIFIED) != 0) && ((currentState & Frame.ICONIFIED) != 0))) {
  69                 stateWasNotChanged = false;
  70             }
  71         }
  72         frame.dispose();
  73 
  74         if (stateWasNotChanged) {
  75             throw new RuntimeException(String.format(
  76                 "Frame state was not changed. currentState='%d'", currentState));
  77         }
  78     }
  79 
  80     private static void sleep(int millis) {
  81         try {
  82             Thread.sleep(millis);
  83         } catch (Exception e) {
  84             e.printStackTrace();
  85         }
  86     }
  87 
  88     public static void main(String[] args) {
  89         if (!validatePlatform()) {
  90             System.out.println("This test is only for OS X.");
  91             return;
  92         }
  93 
  94         // Verify that changing states of decorated/undecorated frame to/from supported states
  95         // and the state bit mask ICONIFIED | MAXIMIZED_BOTH does not raise RuntimeException.
  96         for (int i = 0; i < frameStates.length; i++) {
  97             testStateChange(frameStates[i], Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, true);
  98             testStateChange(frameStates[i], Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, false);
  99             testStateChange(Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, frameStates[i], true);
 100             testStateChange(Frame.ICONIFIED | Frame.MAXIMIZED_BOTH, frameStates[i], false);
 101         }
 102     }
 103 }