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 @key headful 27 @summary unit test for a new method in Container class: getMousePosition(boolean) 28 @author dav@sparc.spb.su: area= 29 @bug 4009555 30 @run main JContainerMousePositionTest 31 */ 32 33 import javax.swing.*; 34 import java.awt.*; 35 import java.util.concurrent.atomic.AtomicReference; 36 37 // this test looks at mouse pointer when it 38 // 1 over component 39 // 2 over Container, but not over one of its child Components. 40 // out of bounds of Container 41 // two values of paramater allowChildren are considered. 42 43 public class JContainerMousePositionTest { 44 //Declare things used in the test, like buttons and labels here 45 private static JButton jButton1; 46 private static JButton jButton4; 47 private static JFrame frame1; 48 private static Container contentPane; 49 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 } 147 } 148 149