Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/MouseInfo/JContainerMousePositionTest.java
47525 views
/*1* Copyright (c) 2013, 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@summary unit test for a new method in Container class: getMousePosition(boolean)26@author [email protected]: area=27@bug 400955528@run main JContainerMousePositionTest29*/3031import javax.swing.*;32import java.awt.*;33import java.util.concurrent.atomic.AtomicReference;3435// this test looks at mouse pointer when it36// 1 over component37// 2 over Container, but not over one of its child Components.38// out of bounds of Container39// two values of paramater allowChildren are considered.4041public class JContainerMousePositionTest {42//Declare things used in the test, like buttons and labels here43private static JButton jButton1;44private static JButton jButton4;45private static JFrame frame1;46private static Container contentPane;4748public static void main(final String[] args) throws Exception {49Robot robot = new Robot();50robot.setAutoDelay(200);51robot.setAutoWaitForIdle(true);5253SwingUtilities.invokeAndWait(JContainerMousePositionTest::init);5455robot.delay(500);56robot.waitForIdle();5758AtomicReference<Point> centerC4 = new AtomicReference<>();59SwingUtilities.invokeAndWait(() -> {60centerC4.set(jButton4.getLocation());61contentPane.remove(jButton4);62contentPane.validate();63contentPane.repaint();64});65robot.waitForIdle();6667AtomicReference<Rectangle> frameBounds = new AtomicReference<>();68AtomicReference<Dimension> button1Size = new AtomicReference<>();69SwingUtilities.invokeAndWait(() -> {70frameBounds.set(frame1.getBounds());71button1Size.set(jButton1.getSize());72});7374//point mouse to center of top-left Component (button1)75robot.mouseMove(frameBounds.get().x + button1Size.get().width / 2,76frameBounds.get().y + button1Size.get().height / 2);7778AtomicReference<Point> pFalse = new AtomicReference<>();79AtomicReference<Point> pTrue = new AtomicReference<>();80SwingUtilities.invokeAndWait(() -> {81pFalse.set(frame1.getMousePosition(false));82pTrue.set(frame1.getMousePosition(true));83});84robot.waitForIdle();85if (pFalse.get() != null) {86throw new RuntimeException("Test failed: Container.getMousePosition(false) returned non-null over one of children.");87}88System.out.println("Test stage completed: Container.getMousePosition(false) returned null result over child Component. Passed.");8990if (pTrue.get() == null) {91throw new RuntimeException("Test failed: Container.getMousePosition(true) returned null result over child Component");92}93System.out.println("Test stage compelted: Container.getMousePosition(true) returned non-null result over child Component. Passed.");9495//point mouse out from Container's area96robot.mouseMove(frameBounds.get().x + frameBounds.get().width + 10,97frameBounds.get().y + frameBounds.get().height + 10);98SwingUtilities.invokeAndWait(() -> {99pFalse.set(frame1.getMousePosition(false));100pTrue.set(frame1.getMousePosition(true));101});102robot.waitForIdle();103if (pFalse.get() != null || pTrue.get() != null) {104throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned incorrect result outside Container");105}106System.out.println("Test stage completed: Container.getMousePosition(boolean) returned null result outside Container. Passed.");107108//point mouse in place free from child components (right-botton component)109robot.mouseMove(frameBounds.get().x + centerC4.get().x,110frameBounds.get().y + centerC4.get().y);111112robot.delay(3000);113SwingUtilities.invokeAndWait(() -> {114pFalse.set(contentPane.getMousePosition(false));115pTrue.set(frame1.getMousePosition(true));116});117robot.waitForIdle();118119if (pFalse.get() == null || pTrue.get() == null) {120throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned null result inside Container.");121}122System.out.println("Test stage completed: Container.getMousePosition(boolean) returned non-null results inside Container. Passed.");123124if (pTrue.get().x != centerC4.get().x || pTrue.get().y != centerC4.get().y) {125throw new RuntimeException("Test failed: Container.getMousePosition(true) returned incorrect result inside Container.");126}127System.out.println("Test stage completed: Container.getMousePosition(true) returned correct result inside Container. Passed.");128129System.out.println("TEST PASSED");130}131132private static void init() {133frame1 = new JFrame("Testing getMousePosition() on LWs");134jButton1 = new JButton("C1");135jButton4 = new JButton("C4");136contentPane = frame1.getContentPane();137contentPane.setLayout(new GridLayout(2, 2, 25, 25));138contentPane.add(jButton1);139contentPane.add(new JButton("C2"));140contentPane.add(new JButton("C3"));141contentPane.add(jButton4);142frame1.setSize(200, 200);143frame1.setVisible(true);144}145}146147148149150