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 */
50 public static void main(final String[] args) throws Exception {
51 Robot robot = new Robot();
52 robot.setAutoDelay(200);
53 robot.setAutoWaitForIdle(true);
54
55 SwingUtilities.invokeAndWait(JContainerMousePositionTest::init);
56
57 robot.delay(500);
58 robot.waitForIdle();
59
60 AtomicReference<Point> centerC4 = new AtomicReference<>();
61 SwingUtilities.invokeAndWait(() -> {
62 centerC4.set(jButton4.getLocation());
63 contentPane.remove(jButton4);
64 contentPane.validate();
65 contentPane.repaint();
66 });
67 robot.waitForIdle();
68
69 AtomicReference<Rectangle> frameBounds = new AtomicReference<>();
70 AtomicReference<Dimension> button1Size = new AtomicReference<>();
71 SwingUtilities.invokeAndWait(() -> {
72 frameBounds.set(frame1.getBounds());
73 button1Size.set(jButton1.getSize());
74 });
75
76 //point mouse to center of top-left Component (button1)
77 robot.mouseMove(frameBounds.get().x + button1Size.get().width / 2,
78 frameBounds.get().y + button1Size.get().height / 2);
79
80 AtomicReference<Point> pFalse = new AtomicReference<>();
81 AtomicReference<Point> pTrue = new AtomicReference<>();
82 SwingUtilities.invokeAndWait(() -> {
83 pFalse.set(frame1.getMousePosition(false));
84 pTrue.set(frame1.getMousePosition(true));
85 });
86 robot.waitForIdle();
87 if (pFalse.get() != null) {
88 throw new RuntimeException("Test failed: Container.getMousePosition(false) returned non-null over one of children.");
89 }
90 System.out.println("Test stage completed: Container.getMousePosition(false) returned null result over child Component. Passed.");
91
92 if (pTrue.get() == null) {
93 throw new RuntimeException("Test failed: Container.getMousePosition(true) returned null result over child Component");
94 }
95 System.out.println("Test stage compelted: Container.getMousePosition(true) returned non-null result over child Component. Passed.");
96
97 //point mouse out from Container's area
98 robot.mouseMove(frameBounds.get().x + frameBounds.get().width + 10,
99 frameBounds.get().y + frameBounds.get().height + 10);
100 SwingUtilities.invokeAndWait(() -> {
101 pFalse.set(frame1.getMousePosition(false));
102 pTrue.set(frame1.getMousePosition(true));
103 });
104 robot.waitForIdle();
105 if (pFalse.get() != null || pTrue.get() != null) {
106 throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned incorrect result outside Container");
107 }
108 System.out.println("Test stage completed: Container.getMousePosition(boolean) returned null result outside Container. Passed.");
109
110 //point mouse in place free from child components (right-botton component)
111 robot.mouseMove(frameBounds.get().x + centerC4.get().x,
112 frameBounds.get().y + centerC4.get().y);
113
114 robot.delay(3000);
115 SwingUtilities.invokeAndWait(() -> {
116 pFalse.set(contentPane.getMousePosition(false));
117 pTrue.set(frame1.getMousePosition(true));
118 });
119 robot.waitForIdle();
120
121 if (pFalse.get() == null || pTrue.get() == null) {
122 throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned null result inside Container.");
123 }
124 System.out.println("Test stage completed: Container.getMousePosition(boolean) returned non-null results inside Container. Passed.");
125
126 if (pTrue.get().x != centerC4.get().x || pTrue.get().y != centerC4.get().y) {
127 throw new RuntimeException("Test failed: Container.getMousePosition(true) returned incorrect result inside Container.");
128 }
129 System.out.println("Test stage completed: Container.getMousePosition(true) returned correct result inside Container. Passed.");
130
131 System.out.println("TEST PASSED");
132 }
133
134 private static void init() {
135 frame1 = new JFrame("Testing getMousePosition() on LWs");
136 jButton1 = new JButton("C1");
137 jButton4 = new JButton("C4");
138 contentPane = frame1.getContentPane();
139 contentPane.setLayout(new GridLayout(2, 2, 25, 25));
140 contentPane.add(jButton1);
141 contentPane.add(new JButton("C2"));
142 contentPane.add(new JButton("C3"));
143 contentPane.add(jButton4);
144 frame1.setSize(200, 200);
145 frame1.setVisible(true);
146 }
|
1 /*
2 * Copyright (c) 2013, 2017 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 */
50 public static void main(final String[] args) throws Exception {
51 Robot robot = new Robot();
52 robot.setAutoDelay(200);
53 robot.setAutoWaitForIdle(true);
54
55 SwingUtilities.invokeAndWait(JContainerMousePositionTest::init);
56
57 robot.delay(500);
58 robot.waitForIdle();
59
60 AtomicReference<Point> centerC4 = new AtomicReference<>();
61 SwingUtilities.invokeAndWait(() -> {
62 centerC4.set(jButton4.getLocation());
63 contentPane.remove(jButton4);
64 contentPane.validate();
65 contentPane.repaint();
66 });
67 robot.waitForIdle();
68
69 AtomicReference<Rectangle> frameBounds = new AtomicReference<>();
70 AtomicReference<Insets> frameInsets = new AtomicReference<>();
71 AtomicReference<Dimension> button1Size = new AtomicReference<>();
72 SwingUtilities.invokeAndWait(() -> {
73 frameBounds.set(frame1.getBounds());
74 frameInsets.set(frame1.getInsets());
75 button1Size.set(jButton1.getSize());
76 });
77
78 //point mouse to center of top-left Component (button1)
79 robot.mouseMove(frameBounds.get().x + frameInsets.get().left +
80 button1Size.get().width / 2,
81 frameBounds.get().y + frameInsets.get().top +
82 button1Size.get().height / 2);
83
84 AtomicReference<Point> pFalse = new AtomicReference<>();
85 AtomicReference<Point> pTrue = new AtomicReference<>();
86 SwingUtilities.invokeAndWait(() -> {
87 pFalse.set(frame1.getMousePosition(false));
88 pTrue.set(frame1.getMousePosition(true));
89 });
90 robot.waitForIdle();
91 if (pFalse.get() != null) {
92 throw new RuntimeException("Test failed: Container.getMousePosition(false) returned non-null over one of children.");
93 }
94 System.out.println("Test stage completed: Container.getMousePosition(false) returned null result over child Component. Passed.");
95
96 if (pTrue.get() == null) {
97 throw new RuntimeException("Test failed: Container.getMousePosition(true) returned null result over child Component");
98 }
99 System.out.println("Test stage compelted: Container.getMousePosition(true) returned non-null result over child Component. Passed.");
100
101 //point mouse out from Container's area
102 robot.mouseMove(frameBounds.get().x + frameBounds.get().width + 10,
103 frameBounds.get().y + frameBounds.get().height + 10);
104 SwingUtilities.invokeAndWait(() -> {
105 pFalse.set(frame1.getMousePosition(false));
106 pTrue.set(frame1.getMousePosition(true));
107 });
108 robot.waitForIdle();
109 if (pFalse.get() != null || pTrue.get() != null) {
110 throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned incorrect result outside Container");
111 }
112 System.out.println("Test stage completed: Container.getMousePosition(boolean) returned null result outside Container. Passed.");
113
114 //point mouse in place free from child components (right-botton component)
115 robot.mouseMove(frameBounds.get().x + frameInsets.get().left +
116 centerC4.get().x,
117 frameBounds.get().y + frameInsets.get().top +
118 centerC4.get().y);
119
120 robot.waitForIdle();
121 SwingUtilities.invokeAndWait(() -> {
122 pFalse.set(contentPane.getMousePosition(false));
123 pTrue.set(frame1.getMousePosition(true));
124 });
125 robot.waitForIdle();
126
127 if (pFalse.get() == null || pTrue.get() == null) {
128 throw new RuntimeException("Test failed: Container.getMousePosition(boolean) returned null result inside Container.");
129 }
130 System.out.println("Test stage completed: Container.getMousePosition(boolean) returned non-null results inside Container. Passed.");
131
132 if (pTrue.get().x != frameInsets.get().left + centerC4.get().x ||
133 pTrue.get().y != frameInsets.get().top + centerC4.get().y) {
134 throw new RuntimeException("Test failed: Container.getMousePosition(true) returned incorrect result inside Container.");
135 }
136 System.out.println("Test stage completed: Container.getMousePosition(true) returned correct result inside Container. Passed.");
137
138 System.out.println("TEST PASSED");
139 }
140
141 private static void init() {
142 frame1 = new JFrame("Testing getMousePosition() on LWs");
143 jButton1 = new JButton("C1");
144 jButton4 = new JButton("C4");
145 contentPane = frame1.getContentPane();
146 contentPane.setLayout(new GridLayout(2, 2, 25, 25));
147 contentPane.add(jButton1);
148 contentPane.add(new JButton("C2"));
149 contentPane.add(new JButton("C3"));
150 contentPane.add(jButton4);
151 frame1.setSize(200, 200);
152 frame1.setVisible(true);
153 }
|