1 /*
   2  * Copyright (c) 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 8209123
  28   @summary Maximized frame is resizable on Mac but not on Windows and Ubuntu
  29   @compile MaximizedFrameResizibility.java
  30   @run main MaximizedFrameResizibility
  31 */
  32 
  33 import java.awt.Toolkit;
  34 import java.awt.Frame;
  35 import java.awt.Rectangle;
  36 import java.awt.AWTException;
  37 import java.awt.event.InputEvent;
  38 import java.awt.Robot;
  39 
  40 public class MaximizedFrameResizibility {
  41 
  42     private static Frame frame;
  43     private static Robot robot;
  44     private static boolean isProgInterruption = false;
  45     private static Thread mainThread = null;
  46     private static int sleepTime = 300000;
  47 
  48     private static void createAndShowFrame() {
  49 
  50         //The MAXIMIZED_BOTH state is not supported by the toolkit. Nothing to test.
  51         if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
  52             return;
  53         }
  54 
  55         //Case 1: Setting state to MAXIMIZED_BOTH for resizable frame.
  56         frame = new Frame("MAXIMIZED_BOTH Resizable frame");
  57         setFrameState(Frame.MAXIMIZED_BOTH);
  58         frame.setResizable(true);
  59         frame.setVisible(true);
  60         resizeFrame();
  61 
  62         //Case 2: Setting state to MAXIMIZED_BOTH from NORMAL for non-resizable frame.
  63         frame = new Frame("MAXIMIZED_BOTH non-Resizable frame");
  64         setFrameState(Frame.NORMAL);
  65         setFrameState(Frame.MAXIMIZED_BOTH);
  66         frame.setResizable(false);
  67         frame.setVisible(true);
  68         resizeFrame();
  69 
  70         //Case 3: Setting resizable frame state to MAXIMIZED_BOTH from NORMAL state.
  71         frame = new Frame("Resizable MAXIMIZED_BOTH frame");
  72         setFrameState(Frame.NORMAL);
  73         frame.setResizable(true);
  74         setFrameState(Frame.MAXIMIZED_BOTH);
  75         frame.setVisible(true);
  76         resizeFrame();
  77 
  78         //Case 4: Setting non-resizable frame state to MAXIMIZED_BOTH from NORMAL state.
  79         frame = new Frame("Non-resizable MAXIMIZED_BOTH frame");
  80         setFrameState(Frame.NORMAL);
  81         frame.setResizable(false);
  82         setFrameState(Frame.MAXIMIZED_BOTH);
  83         frame.setVisible(true);
  84         resizeFrame();
  85 
  86         cleanup();
  87     }
  88 
  89     private static void setFrameState(int frameState) {
  90         frame.setSize(200, 200);
  91 
  92         switch(frameState) {
  93             case Frame.MAXIMIZED_BOTH:
  94                 frame.setMaximizedBounds(new Rectangle(0, 0, 400, 400));
  95                 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  96                 break;
  97             case Frame.NORMAL:
  98                 frame.setExtendedState(Frame.NORMAL);
  99             default:
 100         }
 101     }
 102 
 103     private static void resizeFrame() {
 104         try {
 105             robot = new Robot();
 106             robot.delay(2000);
 107 
 108             // The initial bounds of the frame
 109             final Rectangle bounds = frame.getBounds();
 110 
 111             // Move the mouse pointer to the bottom-right corner of the frame
 112             robot.mouseMove(bounds.x + bounds.width - 2, bounds.y + bounds.height - 2);
 113             robot.waitForIdle();
 114 
 115             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 116             robot.waitForIdle();
 117             robot.mouseMove(bounds.x + bounds.width + 20, bounds.y + bounds.height + 15);
 118             robot.waitForIdle();
 119 
 120             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 121             robot.waitForIdle();
 122 
 123             // The bounds of the frame after the attempt of resizing is made
 124             final Rectangle finalBounds = frame.getBounds();
 125 
 126             int frameState = frame.getExtendedState();
 127             if ((frameState & Frame.ICONIFIED) != 0) {
 128                 // Treat all state bit masks with ICONIFIED bit as ICONIFIED state.
 129                 frameState = Frame.ICONIFIED;
 130             }
 131             switch (frameState) {
 132                 case Frame.MAXIMIZED_BOTH:
 133                     if (!finalBounds.equals(bounds)) {
 134                         throw new RuntimeException("MAXIMIZED_BOTH frame is resizable");
 135                     }
 136                     break;
 137                 case Frame.NORMAL:
 138                     if (!frame.isResizable() && !finalBounds.equals(bounds)) {
 139                         throw new RuntimeException("NORMAL non-resizable frame is resizable");
 140                     }
 141                     if (frame.isResizable() && finalBounds.equals(bounds)) {
 142                         throw new RuntimeException("NORMAL resizable frame is not resizable");
 143                     }
 144                     break;
 145                 default: //ICONIFIED
 146                     break;
 147             }
 148         } catch (AWTException e) {
 149             throw new RuntimeException("Robot creation failed");
 150         } finally {
 151             frame.dispose();
 152         }
 153     }
 154 
 155     private static void cleanup() {
 156         isProgInterruption = true;
 157         mainThread.interrupt();
 158     }
 159 
 160     public static void main(String args[]) throws InterruptedException {
 161 
 162         mainThread = Thread.currentThread();
 163         try {
 164             createAndShowFrame();
 165             mainThread.sleep(sleepTime);
 166         } catch (InterruptedException e) {
 167             if (!isProgInterruption) {
 168                 throw e;
 169             }
 170         }
 171 
 172         if (!isProgInterruption) {
 173             throw new RuntimeException("Timed out after " + sleepTime / 1000
 174                     + " seconds");
 175         }
 176     }
 177 }
 178