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