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