Path: blob/master/test/jdk/java/awt/MouseInfo/ComponentMousePositionTest.java
41149 views
/*1* Copyright (c) 2017, 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*/2223/*24@test25@key headful26@bug 4009555 711977427@summary Test for Component's method getMousePosition()28@run main ComponentMousePositionTest29*/30import java.awt.Frame;31import java.awt.Panel;32import java.awt.Button;33import java.awt.Dimension;34import java.awt.Point;35import java.awt.Robot;36import java.awt.AWTException;3738public class ComponentMousePositionTest {3940public void changeMousePosition() {41Frame frame = new Frame();42Panel panel = new Panel();43Button button = new Button("Button");44Button overlappedButton = new Button("Overlapped button");45Dimension BUTTON_DIMENSION = new Dimension(100, 100);46Dimension FRAME_DIMENSION = new Dimension(200, 200);47Point POINT_WITHOUT_COMPONENTS = new Point(10, 10);48Point FIRST_BUTTON_LOCATION = new Point(20, 20);49Point SECOND_BUTTON_LOCATION = new Point(30, 30);5051button.setSize(BUTTON_DIMENSION);52button.setLocation(FIRST_BUTTON_LOCATION);53overlappedButton.setSize(BUTTON_DIMENSION);54overlappedButton.setLocation(SECOND_BUTTON_LOCATION);5556panel.setLayout(null);57panel.add(button);58panel.add(overlappedButton);59frame.add(panel);60Robot robot;6162try {63robot = new Robot();64frame.setSize(FRAME_DIMENSION);65frame.setVisible(true);66robot.delay(2000);6768Point p = button.getLocationOnScreen();69robot.mouseMove(p.x + button.getWidth()70/ 2, p.y + button.getHeight() / 2);71robot.waitForIdle();7273Point pMousePosition = button.getMousePosition();74if (pMousePosition == null) {75throw new RuntimeException("Test failed: "76+ "Component.getMousePosition() returned null result "77+ "inside Component");78}7980if (pMousePosition.x != button.getWidth() / 281|| pMousePosition.y != button.getHeight() / 2) {82throw new RuntimeException("Test failed: "83+ "Component.getMousePosition() returned incorrect "84+ "result inside Component");85}8687pMousePosition = overlappedButton.getMousePosition();88if (pMousePosition != null) {89throw new RuntimeException("Test failed: Overlapped component "90+ "did not return null result when a pointer was inside"91+ " the components bounds.");92}93robot.mouseMove(panel.getLocationOnScreen().x94+ POINT_WITHOUT_COMPONENTS.x,95panel.getLocationOnScreen().y96+ POINT_WITHOUT_COMPONENTS.y);97robot.waitForIdle();9899pMousePosition = button.getMousePosition();100if (pMousePosition != null) {101throw new RuntimeException("FAILED: "102+ "Component.getMousePosition() returned non-null "103+ "results outside Component");104}105106} catch (AWTException e) {107throw new RuntimeException("FAILED: AWTException by Robot" + e);108} finally {109frame.dispose();110}111}112113public static void main(String args[]) {114ComponentMousePositionTest mousePos = new ComponentMousePositionTest();115mousePos.changeMousePosition();116}117}118119120