Path: blob/master/test/jdk/java/awt/Component/GetScreenLocTest/GetScreenLocTest.java
41153 views
/*1* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.AWTException;24import java.awt.BorderLayout;25import java.awt.Canvas;26import java.awt.Color;27import java.awt.Dimension;28import java.awt.Frame;29import java.awt.Graphics;30import java.awt.Point;31import java.awt.Rectangle;32import java.awt.Robot;33import java.awt.Toolkit;34import java.awt.event.InputEvent;35import java.awt.event.MouseAdapter;36import java.awt.event.MouseEvent;3738/**39* @test40* @key headful41* @bug 435620242* @summary Tests that getLocationOnScreen returns valid value(WindowMaker43* only).44* @author [email protected]:45*/46public class GetScreenLocTest {47//Declare things used in the test, like buttons and labels here48static Robot robot = null;49private static class MyCanvas extends Canvas {50public Dimension getPreferredSize() {51return new Dimension(100, 100);52}53public void paint(Graphics g) {54super.paint(g);55g.setColor(Color.blue);56Rectangle r = getBounds();57g.fillRect(0, 0, r.width, r.height);58}59}60static int state = 0; // there are three states - (-1,-1),(0,0),(1,1)6162static void bigPause() {63Toolkit.getDefaultToolkit().sync();64robot.waitForIdle();65robot.delay(1000);66}6768static void doPress(Point p) {69robot.mouseMove(p.x, p.y);70robot.mousePress(InputEvent.BUTTON1_MASK);71robot.mouseRelease(InputEvent.BUTTON1_MASK);72}7374public static void main(final String[] args) throws AWTException {75robot = new Robot();76Frame bigOne = new Frame();77bigOne.setSize(200, 200);78bigOne.setLocationRelativeTo(null);79bigOne.setVisible(true);80Frame f = new Frame();81f.setLayout(new BorderLayout());82f.setSize(120, 150);83f.setLocationRelativeTo(null);84Canvas c = new MyCanvas();85f.add(c, BorderLayout.CENTER);86c.addMouseListener(new MouseAdapter() {87public void mousePressed(MouseEvent e) {88switch(state) {89case 0: // the first event should be (0,0)90if (e.getX() != 0 || e.getY() != 0) {91System.out.println("state 0: wrong location" + e);92break;93}94state++;95break;96case 1: // the second event should be (1,1)97if (e.getX() != 1 || e.getY() != 1) {98System.out.println("state 1: wrong location " + e);99break;100}101state++;102break;103case 2: // this should never happen104System.out.println("state 2: wrong location " + e);105}106}107});108f.pack();109f.setVisible(true);110bigPause();111112Point p = c.getLocationOnScreen();113doPress(p);114p.x += 1;115p.y += 1;116doPress(p);117p.x -= 2;118p.y -= 2;119doPress(p);120bigPause();121122f.dispose();123bigOne.dispose();124125// ...and at the end the state should be 2126if (state != 2) {127throw new RuntimeException("wrong state: " + state);128}129}130}131132133