Path: blob/master/test/jdk/java/awt/MouseInfo/JContainerMousePositionTest.java
41149 views
/*1* Copyright (c) 2013, 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@summary unit test for a new method in Container class: getMousePosition(boolean)27@author [email protected]: area=28@bug 400955529@run main JContainerMousePositionTest30*/3132import javax.swing.*;33import java.awt.*;34import java.util.concurrent.atomic.AtomicReference;3536// this test looks at mouse pointer when it37// 1 over component38// 2 over Container, but not over one of its child Components.39// out of bounds of Container40// two values of paramater allowChildren are considered.4142public class JContainerMousePositionTest {43//Declare things used in the test, like buttons and labels here44private static JButton jButton1;45private static JButton jButton4;46private static JFrame frame1;47private static Container contentPane;4849public static void main(final String[] args) throws Exception {50Robot robot = new Robot();51robot.setAutoDelay(200);52robot.setAutoWaitForIdle(true);5354SwingUtilities.invokeAndWait(JContainerMousePositionTest::init);5556robot.delay(500);57robot.waitForIdle();5859AtomicReference<Point> centerC4 = new AtomicReference<>();60SwingUtilities.invokeAndWait(() -> {61centerC4.set(jButton4.getLocation());62contentPane.remove(jButton4);63contentPane.validate();64contentPane.repaint();65});66robot.waitForIdle();6768AtomicReference<Rectangle> frameBounds = new AtomicReference<>();69AtomicReference<Insets> frameInsets = new AtomicReference<>();70AtomicReference<Dimension> button1Size = new AtomicReference<>();71SwingUtilities.invokeAndWait(() -> {72frameBounds.set(frame1.getBounds());73frameInsets.set(frame1.getInsets());74button1Size.set(jButton1.getSize());75});7677//point mouse to center of top-left Component (button1)78robot.mouseMove(frameBounds.get().x + frameInsets.get().left +79button1Size.get().width / 2,80frameBounds.get().y + frameInsets.get().top +81button1Size.get().height / 2);8283AtomicReference<Point> pFalse = new AtomicReference<>();84AtomicReference<Point> pTrue = new AtomicReference<>();85SwingUtilities.invokeAndWait(() -> {86pFalse.set(frame1.getMousePosition(false));87pTrue.set(frame1.getMousePosition(true));88});89robot.waitForIdle();90if (pFalse.get() != null) {91throw new RuntimeException("Test failed: Container.getMousePosition(false) returned non-null over one of children.");92}93System.out.println("Test stage completed: Container.getMousePosition(false) returned null result over child Component. Passed.");9495if (pTrue.get() == null) {96throw new RuntimeException("Test failed: Container.getMousePosition(true) returned null result over child Component");97}98System.out.println("Test stage compelted: Container.getMousePosition(true) returned non-null result over child Component. Passed.");99100//point mouse out from Container's area101robot.mouseMove(frameBounds.get().x + frameBounds.get().width + 10,102frameBounds.get().y + frameBounds.get().height + 10);103SwingUtilities.invokeAndWait(() -> {104pFalse.set(frame1.getMousePosition(false));105pTrue.set(frame1.getMousePosition(true));106});107robot.waitForIdle();108if (pFalse.get() != null || pTrue.get() != null) {109throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned incorrect result outside Container");110}111System.out.println("Test stage completed: Container.getMousePosition(boolean) returned null result outside Container. Passed.");112113//point mouse in place free from child components (right-botton component)114robot.mouseMove(frameBounds.get().x + frameInsets.get().left +115centerC4.get().x,116frameBounds.get().y + frameInsets.get().top +117centerC4.get().y);118119robot.waitForIdle();120SwingUtilities.invokeAndWait(() -> {121pFalse.set(contentPane.getMousePosition(false));122pTrue.set(frame1.getMousePosition(true));123});124robot.waitForIdle();125126if (pFalse.get() == null || pTrue.get() == null) {127throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned null result inside Container.");128}129System.out.println("Test stage completed: Container.getMousePosition(boolean) returned non-null results inside Container. Passed.");130131if (pTrue.get().x != frameInsets.get().left + centerC4.get().x ||132pTrue.get().y != frameInsets.get().top + centerC4.get().y) {133throw new RuntimeException("Test failed: Container.getMousePosition(true) returned incorrect result inside Container.");134}135System.out.println("Test stage completed: Container.getMousePosition(true) returned correct result inside Container. Passed.");136137System.out.println("TEST PASSED");138}139140private static void init() {141frame1 = new JFrame("Testing getMousePosition() on LWs");142jButton1 = new JButton("C1");143jButton4 = new JButton("C4");144contentPane = frame1.getContentPane();145contentPane.setLayout(new GridLayout(2, 2, 25, 25));146contentPane.add(jButton1);147contentPane.add(new JButton("C2"));148contentPane.add(new JButton("C3"));149contentPane.add(jButton4);150frame1.setSize(200, 200);151frame1.setVisible(true);152}153}154155156