1 /*
   2  * Copyright (c) 2002, 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 
  24 import java.awt.AWTException;
  25 import java.awt.BorderLayout;
  26 import java.awt.Canvas;
  27 import java.awt.Color;
  28 import java.awt.Dimension;
  29 import java.awt.Frame;
  30 import java.awt.Graphics;
  31 import java.awt.Point;
  32 import java.awt.Rectangle;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.event.InputEvent;
  36 import java.awt.event.MouseAdapter;
  37 import java.awt.event.MouseEvent;
  38 
  39 /**
  40  * @test
  41  * @key headful
  42  * @bug 4356202
  43  * @summary Tests that getLocationOnScreen returns valid value(WindowMaker
  44  *          only).
  45  * @author dom@sparc.spb.su:
  46  */
  47 public class GetScreenLocTest {
  48     //Declare things used in the test, like buttons and labels here
  49     static Robot robot = null;
  50     private static class MyCanvas extends Canvas {
  51         public Dimension getPreferredSize() {
  52             return new Dimension(100, 100);
  53         }
  54         public void paint(Graphics g) {
  55             super.paint(g);
  56             g.setColor(Color.blue);
  57             Rectangle r = getBounds();
  58             g.fillRect(0, 0, r.width, r.height);
  59         }
  60     }
  61     static int state = 0; // there are three states - (-1,-1),(0,0),(1,1)
  62 
  63     static void bigPause() {
  64         Toolkit.getDefaultToolkit().sync();
  65         robot.waitForIdle();
  66         robot.delay(1000);
  67     }
  68 
  69     static void doPress(Point p) {
  70         robot.mouseMove(p.x, p.y);
  71         robot.mousePress(InputEvent.BUTTON1_MASK);
  72         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  73     }
  74 
  75     public static void main(final String[] args) throws AWTException {
  76         robot = new Robot();
  77         Frame bigOne = new Frame();
  78         bigOne.setSize(200, 200);
  79         bigOne.setLocationRelativeTo(null);
  80         bigOne.setVisible(true);
  81         Frame f = new Frame();
  82         f.setLayout(new BorderLayout());
  83         f.setSize(120, 150);
  84         f.setLocationRelativeTo(null);
  85         Canvas c = new MyCanvas();
  86         f.add(c, BorderLayout.CENTER);
  87         c.addMouseListener(new MouseAdapter() {
  88             public void mousePressed(MouseEvent e) {
  89                 switch(state) {
  90                     case 0: // the first event should be (0,0)
  91                         if (e.getX() != 0 || e.getY() != 0) {
  92                             System.out.println("state 0: wrong location" + e);
  93                             break;
  94                         }
  95                         state++;
  96                         break;
  97                     case 1: // the second event should be (1,1)
  98                         if (e.getX() != 1 || e.getY() != 1) {
  99                             System.out.println("state 1: wrong location " + e);
 100                             break;
 101                         }
 102                         state++;
 103                         break;
 104                     case 2: // this should never happen
 105                         System.out.println("state 2: wrong location " + e);
 106                 }
 107             }
 108         });
 109         f.pack();
 110         f.setVisible(true);
 111         bigPause();
 112 
 113         Point p = c.getLocationOnScreen();
 114         doPress(p);
 115         p.x += 1;
 116         p.y += 1;
 117         doPress(p);
 118         p.x -= 2;
 119         p.y -= 2;
 120         doPress(p);
 121         bigPause();
 122 
 123         f.dispose();
 124         bigOne.dispose();
 125 
 126         // ...and at the end the state should be 2
 127         if (state != 2) {
 128             throw new RuntimeException("wrong state: " + state);
 129         }
 130     }
 131 }