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 */ 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<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 } 154 }