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