File: ContainerMousePositionTest.java

package info (click to toggle)
openjdk-11 11.0.28%2B6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,124 kB
  • sloc: java: 5,208,481; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,729; python: 4,757; asm: 3,570; makefile: 2,965; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (172 lines) | stat: -rw-r--r-- 7,127 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
  @test
  @summary unit test for a new method in Container class: getMousePosition(boolean)
  @bug 4009555
  @key headful
*/

import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;

public class ContainerMousePositionTest {
    private Button button;
    private Frame frame;
    private Panel panel;
    private static Dimension BUTTON_DIMENSION = new Dimension(100, 100);
    private static Dimension FRAME_DIMENSION = new Dimension(200, 200);
    private static Point POINT_WITHOUT_COMPONENTS = new Point(10, 10);
    private static Point FIRST_BUTTON_LOCATION = new Point(20, 20);
    private static int DELAY = 1000;
    Robot robot;
    volatile int xPos = 0;
    volatile int yPos = 0;
    Point pMousePosition;

    public static void main(String[] args) throws Exception {
        ContainerMousePositionTest containerObj = new ContainerMousePositionTest();
        containerObj.init();
        containerObj.start();
    }

    public void init() throws Exception {
        robot = new Robot();
        EventQueue.invokeAndWait(() -> {
            button = new Button("Button");
            frame = new Frame("Testing Component.getMousePosition()");
            panel = new Panel();

            button.setSize(BUTTON_DIMENSION);
            button.setLocation(FIRST_BUTTON_LOCATION);

            panel.setLayout(null);

            panel.add(button);
            frame.add(panel);
            frame.setSize(FRAME_DIMENSION);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

    public void start() throws Exception {
        try {
            robot.delay(DELAY);
            robot.waitForIdle();

            EventQueue.invokeAndWait(() -> {
                Point p = button.getLocationOnScreen();
                xPos = p.x + button.getWidth() / 2;
                yPos = p.y + button.getHeight() / 2;
            });
            robot.mouseMove(xPos,yPos);
            robot.delay(DELAY);

            EventQueue.invokeAndWait(() -> {
                pMousePosition = frame.getMousePosition(false);
                if (pMousePosition != null) {
                    throw new RuntimeException("Test failed: Container is " +
                            "overlapped by " + " child and it should be taken " +
                            "into account");
                }
                System.out.println("Test stage completed: Container is " +
                        "overlapped by " + " child and it was taken into " +
                        "account");

                pMousePosition = frame.getMousePosition(true);
                if (pMousePosition == null) {
                    throw new RuntimeException("Test failed: Container is " +
                            "overlapped by " + " child and it should not be " +
                            "taken into account");
                }
                System.out.println("Test stage completed: Container is " +
                        "overlapped by " + " child and it should not be " +
                        "taken into account");
                xPos = panel.getLocationOnScreen().x + POINT_WITHOUT_COMPONENTS.x;
                yPos = panel.getLocationOnScreen().y + POINT_WITHOUT_COMPONENTS.y;
            });

            robot.mouseMove(xPos, yPos);

            robot.delay(DELAY);

            EventQueue.invokeAndWait(() -> {
                pMousePosition = panel.getMousePosition(true);
                if (pMousePosition == null) {
                    throw new RuntimeException("Test failed: Pointer was " +
                            "outside of " + "the component so getMousePosition()" +
                            " should not return null");
                }
                System.out.println("Test stage completed: Pointer was outside of " +
                        "the component and getMousePosition() has not returned null");

                pMousePosition = panel.getMousePosition(false);
                if (pMousePosition == null) {
                    throw new RuntimeException("Test failed: Pointer was outside of " +
                            "the component so getMousePosition() should not return null");
                }
                System.out.println("Test stage completed: Pointer was outside of " +
                        "the component and getMousePosition() has not returned null");
                xPos = frame.getLocationOnScreen().x + frame.getWidth() + POINT_WITHOUT_COMPONENTS.x;
                yPos = frame.getLocationOnScreen().y + frame.getHeight() + POINT_WITHOUT_COMPONENTS.y;
            });
            robot.mouseMove(xPos, yPos);

            robot.delay(DELAY);

            EventQueue.invokeAndWait(() -> {
                pMousePosition = frame.getMousePosition(true);
                if (pMousePosition != null) {
                    throw new RuntimeException("Test failed: Pointer was outside of " +
                            "the Frame widow and getMousePosition() should return null");
                }
                System.out.println("Test stage completed: Pointer was outside of " +
                        "the Frame widow and getMousePosition() returned null");

                pMousePosition = frame.getMousePosition(false);
                if (pMousePosition != null) {
                    throw new RuntimeException("Test failed: Pointer was outside of " +
                            "the Frame widow and getMousePosition() should return null");
                }
                System.out.println("Test stage completed: Pointer was outside of " +
                        "the Frame widow and getMousePosition() returned null");
            });
            robot.delay(DELAY);

            System.out.println("ComponentMousePositionTest PASSED.");
        } finally {
            EventQueue.invokeAndWait(() -> {
                if (frame != null) {
                    frame.dispose();
                }
            });
        }
    }
}