JContainerMousePositionTest.java revision 12677:a4299d47bd00
1/*
2 * Copyright (c) 2013, 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/*
25  @test
26  @summary unit test for a new method in Container class: getMousePosition(boolean)
27  @author dav@sparc.spb.su: area=
28  @bug 4009555
29  @run main JContainerMousePositionTest
30*/
31
32import javax.swing.*;
33import java.awt.*;
34import java.util.concurrent.atomic.AtomicReference;
35
36// this test looks at mouse pointer when it
37// 1 over component
38// 2 over Container, but not over one of its child Components.
39// out of bounds of Container
40// two values of paramater allowChildren are considered.
41
42public class JContainerMousePositionTest {
43    //Declare things used in the test, like buttons and labels here
44    private static JButton jButton1;
45    private static JButton jButton4;
46    private static JFrame frame1;
47    private static Container contentPane;
48
49    public static void main(final String[] args) throws Exception {
50        Robot robot = new Robot();
51        robot.setAutoDelay(200);
52        robot.setAutoWaitForIdle(true);
53
54        SwingUtilities.invokeAndWait(JContainerMousePositionTest::init);
55
56        robot.delay(500);
57        robot.waitForIdle();
58
59        AtomicReference<Point> centerC4 = new AtomicReference<>();
60        SwingUtilities.invokeAndWait(() -> {
61            centerC4.set(jButton4.getLocation());
62            contentPane.remove(jButton4);
63            contentPane.validate();
64            contentPane.repaint();
65        });
66        robot.waitForIdle();
67
68        AtomicReference<Rectangle> frameBounds = new AtomicReference<>();
69        AtomicReference<Dimension> button1Size = new AtomicReference<>();
70        SwingUtilities.invokeAndWait(() -> {
71            frameBounds.set(frame1.getBounds());
72            button1Size.set(jButton1.getSize());
73        });
74
75//point mouse to center of top-left Component (button1)
76        robot.mouseMove(frameBounds.get().x + button1Size.get().width / 2,
77                        frameBounds.get().y + button1Size.get().height / 2);
78
79        AtomicReference<Point> pFalse = new AtomicReference<>();
80        AtomicReference<Point> pTrue = new AtomicReference<>();
81        SwingUtilities.invokeAndWait(() -> {
82            pFalse.set(frame1.getMousePosition(false));
83            pTrue.set(frame1.getMousePosition(true));
84        });
85        robot.waitForIdle();
86        if (pFalse.get() != null) {
87            throw new RuntimeException("Test failed: Container.getMousePosition(false) returned non-null over one of children.");
88        }
89        System.out.println("Test stage completed: Container.getMousePosition(false) returned null result over child Component. Passed.");
90
91        if (pTrue.get() == null) {
92            throw new RuntimeException("Test failed: Container.getMousePosition(true) returned null result over child Component");
93        }
94        System.out.println("Test stage compelted: Container.getMousePosition(true) returned non-null result over child Component. Passed.");
95
96//point mouse out from Container's area
97        robot.mouseMove(frameBounds.get().x + frameBounds.get().width + 10,
98                        frameBounds.get().y + frameBounds.get().height + 10);
99        SwingUtilities.invokeAndWait(() -> {
100            pFalse.set(frame1.getMousePosition(false));
101            pTrue.set(frame1.getMousePosition(true));
102        });
103        robot.waitForIdle();
104        if (pFalse.get() != null || pTrue.get() != null) {
105            throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned incorrect result outside Container");
106        }
107        System.out.println("Test stage completed: Container.getMousePosition(boolean) returned null result outside Container. Passed.");
108
109//point mouse in place free from child components (right-botton component)
110        robot.mouseMove(frameBounds.get().x + centerC4.get().x,
111                        frameBounds.get().y + centerC4.get().y);
112
113        robot.delay(3000);
114        SwingUtilities.invokeAndWait(() -> {
115            pFalse.set(contentPane.getMousePosition(false));
116            pTrue.set(frame1.getMousePosition(true));
117        });
118        robot.waitForIdle();
119
120        if (pFalse.get() == null || pTrue.get() == null) {
121            throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned null result inside Container.");
122        }
123        System.out.println("Test stage completed: Container.getMousePosition(boolean) returned non-null results  inside Container. Passed.");
124
125        if (pTrue.get().x != centerC4.get().x || pTrue.get().y != centerC4.get().y) {
126            throw new RuntimeException("Test failed: Container.getMousePosition(true) returned incorrect result inside Container.");
127        }
128        System.out.println("Test stage completed: Container.getMousePosition(true) returned correct result inside Container. Passed.");
129
130        System.out.println("TEST PASSED");
131    }
132
133    private static void init() {
134        frame1 = new JFrame("Testing getMousePosition() on LWs");
135        jButton1 = new JButton("C1");
136        jButton4 = new JButton("C4");
137        contentPane = frame1.getContentPane();
138        contentPane.setLayout(new GridLayout(2, 2, 25, 25));
139        contentPane.add(jButton1);
140        contentPane.add(new JButton("C2"));
141        contentPane.add(new JButton("C3"));
142        contentPane.add(jButton4);
143        frame1.setSize(200, 200);
144        frame1.setVisible(true);
145    }
146}
147