1 /*
   2  * Copyright 2011 Red Hat, Inc.  All Rights Reserved.
   3  * Copyright (c) 2011, 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      6666666
  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.pack();
  62         frame.setSize(500, 500);
  63         Util.showWindowWait(frame);
  64         runRobotTest(frame);
  65     }
  66 
  67     private static void runRobotTest(Frame frame) {
  68         try {
  69             Thread robotThread = startRegTest(frame);
  70             robotThread.start();
  71             waitForThread(robotThread);
  72         } finally {
  73             frame.dispose();
  74         }
  75     }
  76 
  77     private static void waitForThread(Thread t) {
  78         while (t.isAlive()) {
  79             try {
  80                 t.join();
  81             } catch (InterruptedException e) {
  82             }
  83         }
  84     }
  85 
  86     private static void sleepFor(long millis) {
  87         long dT = 0;
  88         long start = System.nanoTime();
  89         while (dT < millis) {
  90             try {
  91                 long toSleep = millis - dT/1000000;
  92                 if (toSleep > 0) {
  93                     Thread.sleep(toSleep);
  94                 }
  95                 // if this ends without an interrupted exception,
  96                 // that's good enough.
  97                 break;
  98             } catch (InterruptedException e) {
  99                 long now = System.nanoTime();
 100                 dT = now - start;
 101             }
 102         }
 103     }
 104 
 105     private static void rmove(Robot robot, Point p) {
 106         robot.mouseMove(p.x, p.y);
 107     }
 108     private static void rdown(Robot robot) {
 109         robot.mousePress(InputEvent.BUTTON1_MASK);
 110         robot.delay(50);
 111     }
 112     private static void rup(Robot robot) {
 113         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 114         robot.delay(50);
 115     }
 116 
 117     public static void click(Robot robot) {
 118         rdown(robot);
 119         rup(robot);
 120     }
 121 
 122     public static void doubleClick(Robot robot) {
 123         click(robot);
 124         click(robot);
 125     }
 126 
 127     private static void dragWindow(Window w, int dx, int dy, Robot robot) {
 128         Point p = Util.getTitlePoint(w);
 129         rmove(robot, p);
 130         rdown(robot);
 131         p.translate(dx, dy);
 132         rmove(robot, p);
 133         rup(robot);
 134     }
 135 
 136     // f must be visible
 137     private static Thread startRegTest(final Frame f) {
 138         Thread robot = new Thread(new Runnable() {
 139             public void run() {
 140                 Robot r = Util.createRobot();
 141                 dragWindow(f, 100, 100, r);
 142                 // wait for the location to be set.
 143                 sleepFor(2000);
 144 
 145                 final Point l2 = f.getLocationOnScreen();
 146 
 147                 // double click should maximize the frame
 148                 doubleClick(r);
 149 
 150                 // wait for location again.
 151                 sleepFor(2000);
 152                 final Point l3 = f.getLocationOnScreen();
 153                 if (l3.equals(l2)) {
 154                     throw new RuntimeException("Bad location after maximize. Window location has not moved");
 155                 }
 156             }
 157         });
 158         return robot;
 159     }
 160 }
 161