1 /*
   2  * Copyright (c) 2015, 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 import java.awt.*;
  24 
  25 /**
  26  * @test
  27  * @key headful
  28  * @bug 8065739
  29  * @summary Moved window is maximazed to new screen
  30  * @author Alexandr Scherbatiy
  31  *
  32  * @run main MaximizedMovedWindow
  33  */
  34 
  35 public class MaximizedMovedWindow {
  36 
  37     public static void main(String[] args) throws Exception {
  38 
  39         //Supported platforms are Windows and OS X.
  40         String os = System.getProperty("os.name").toLowerCase();
  41         if (!os.contains("os x")) {
  42             return;
  43         }
  44 
  45         if (!Toolkit.getDefaultToolkit().
  46                 isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
  47             return;
  48         }
  49 
  50         GraphicsEnvironment ge = GraphicsEnvironment.
  51                 getLocalGraphicsEnvironment();
  52 
  53         if (ge.isHeadlessInstance()) {
  54             return;
  55         }
  56 
  57         GraphicsDevice[] devices = ge.getScreenDevices();
  58 
  59         if (devices.length < 2) {
  60             return;
  61         }
  62 
  63         Frame frame = null;
  64         try {
  65 
  66             GraphicsConfiguration gc1 = devices[0].getDefaultConfiguration();
  67             GraphicsConfiguration gc2 = devices[1].getDefaultConfiguration();
  68 
  69             Robot robot = new Robot();
  70             robot.setAutoDelay(50);
  71 
  72             frame = new Frame();
  73             Rectangle maxArea1 = getMaximizedScreenArea(gc1);
  74             frame.setBounds(getSmallerRectangle(maxArea1));
  75             frame.setVisible(true);
  76             robot.waitForIdle();
  77 
  78             frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  79             robot.waitForIdle();
  80             robot.delay(1000);
  81 
  82             Rectangle bounds = frame.getBounds();
  83             if (!bounds.equals(maxArea1)) {
  84                 throw new RuntimeException("The bounds of the Frame do not equal"
  85                         + " to screen 1 size");
  86             }
  87 
  88             frame.setExtendedState(Frame.NORMAL);
  89             robot.waitForIdle();
  90             robot.delay(1000);
  91 
  92             Rectangle maxArea2 = getMaximizedScreenArea(gc2);
  93             frame.setBounds(getSmallerRectangle(maxArea2));
  94             robot.waitForIdle();
  95             robot.delay(1000);
  96 
  97             frame.setExtendedState(Frame.MAXIMIZED_BOTH);
  98             robot.waitForIdle();
  99             robot.delay(1000);
 100 
 101             bounds = frame.getBounds();
 102             if (!bounds.equals(maxArea2)) {
 103                 throw new RuntimeException("The bounds of the Frame do not equal"
 104                         + " to screen 2 size");
 105             }
 106         } finally {
 107             if (frame != null) {
 108                 frame.dispose();
 109             }
 110         }
 111     }
 112 
 113     static Rectangle getSmallerRectangle(Rectangle rect) {
 114         return new Rectangle(
 115                 rect.x + rect.width / 6,
 116                 rect.y + rect.height / 6,
 117                 rect.width / 3,
 118                 rect.height / 3);
 119     }
 120     static Rectangle getMaximizedScreenArea(GraphicsConfiguration gc) {
 121         Rectangle bounds = gc.getBounds();
 122         Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
 123         return new Rectangle(
 124                 bounds.x + insets.left,
 125                 bounds.y + insets.top,
 126                 bounds.width - insets.left - insets.right,
 127                 bounds.height - insets.top - insets.bottom);
 128     }
 129 }