1 /*
   2  * Copyright 2012 Red Hat, Inc.  All Rights Reserved.
   3  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 /*
  27   @test
  28   @bug      7043963
  29   @summary  Tests  that the screen location of windows is
  30             updated properly after a maximize.
  31   @author   Denis Lila
  32   @library  ../../regtesthelpers
  33   @build    Util
  34   @run      main MutterMaximizeTest
  35 */
  36 
  37 import java.awt.AWTException;
  38 import java.awt.Dimension;
  39 import java.awt.Frame;
  40 import java.awt.Point;
  41 import java.awt.Robot;
  42 import java.awt.Window;
  43 import java.awt.event.InputEvent;
  44 import java.awt.event.WindowAdapter;
  45 import java.awt.event.WindowEvent;
  46 
  47 import test.java.awt.regtesthelpers.Util;
  48 
  49 @SuppressWarnings("serial")
  50 public class MutterMaximizeTest extends Frame {
  51 
  52     public static void main(String[] args) throws InterruptedException {
  53         if (Util.getWMID() != Util.MUTTER_WM) {
  54             System.out.println("This test is only useful on Mutter");
  55             return;
  56         }
  57         MutterMaximizeTest frame = new MutterMaximizeTest();
  58         frame.addWindowListener(Util.getClosingWindowAdapter());
  59 
  60         //Display the window.
  61         frame.setSize(500, 500);
  62         Util.showWindowWait(frame);
  63         runRobotTest(frame);
  64     }
  65 
  66     private static void runRobotTest(Frame frame) {
  67         try {
  68             Thread robotThread = startRegTest(frame);
  69             robotThread.start();
  70             waitForThread(robotThread);
  71         } finally {
  72             frame.dispose();
  73         }
  74     }
  75 
  76     private static void waitForThread(Thread t) {
  77         while (t.isAlive()) {
  78             try {
  79                 t.join();
  80             } catch (InterruptedException e) {
  81             }
  82         }
  83     }
  84 
  85     private static void sleepFor(long millis) {
  86         long dT = 0;
  87         long start = System.nanoTime();
  88         while (dT < millis) {
  89             try {
  90                 long toSleep = millis - dT/1000000;
  91                 if (toSleep > 0) {
  92                     Thread.sleep(toSleep);
  93                 }
  94                 // if this ends without an interrupted exception,
  95                 // that's good enough.
  96                 break;
  97             } catch (InterruptedException e) {
  98                 long now = System.nanoTime();
  99                 dT = now - start;
 100             }
 101         }
 102     }
 103 
 104     private static void rmove(Robot robot, Point p) {
 105         robot.mouseMove(p.x, p.y);
 106     }
 107     private static void rdown(Robot robot) {
 108         robot.mousePress(InputEvent.BUTTON1_MASK);
 109         robot.delay(50);
 110     }
 111     private static void rup(Robot robot) {
 112         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 113         robot.delay(50);
 114     }
 115 
 116     public static void click(Robot robot) {
 117         rdown(robot);
 118         rup(robot);
 119     }
 120 
 121     public static void doubleClick(Robot robot) {
 122         click(robot);
 123         click(robot);
 124     }
 125 
 126     private static void dragWindow(Window w, int dx, int dy, Robot robot) {
 127         Point p = Util.getTitlePoint(w);
 128         rmove(robot, p);
 129         rdown(robot);
 130         p.translate(dx, dy);
 131         rmove(robot, p);
 132         rup(robot);
 133     }
 134 
 135     // f must be visible
 136     private static Thread startRegTest(final Frame f) {
 137         Thread robot = new Thread(new Runnable() {
 138             public void run() {
 139                 Robot r = Util.createRobot();
 140                 dragWindow(f, 100, 100, r);
 141                 // wait for the location to be set.
 142                 sleepFor(2000);
 143 
 144                 final Point l2 = f.getLocationOnScreen();
 145 
 146                 // double click should maximize the frame
 147                 doubleClick(r);
 148 
 149                 // wait for location again.
 150                 sleepFor(2000);
 151                 final Point l3 = f.getLocationOnScreen();
 152                 if (l3.equals(l2)) {
 153                     throw new RuntimeException("Bad location after maximize. Window location has not moved");
 154                 }
 155             }
 156         });
 157         return robot;
 158     }
 159 }
 160