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