|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2024, 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 | +import java.awt.BorderLayout; |
| 25 | +import java.awt.Button; |
| 26 | +import java.awt.EventQueue; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Point; |
| 29 | +import java.awt.Robot; |
| 30 | +import java.awt.event.ComponentAdapter; |
| 31 | +import java.awt.event.ComponentEvent; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @key headful |
| 38 | + * @bug 4009555 |
| 39 | + * @summary Unit test for a new method in Container class: getMousePosition(boolean) |
| 40 | + * while Container resized. |
| 41 | + */ |
| 42 | + |
| 43 | +public class ContainerResizeMousePositionTest { |
| 44 | + private static Frame frame; |
| 45 | + private static Button button; |
| 46 | + private static Robot robot; |
| 47 | + private static volatile Point frameLocation; |
| 48 | + private static volatile Point newLoc; |
| 49 | + private static boolean testSucceeded = false; |
| 50 | + |
| 51 | + private static final CountDownLatch eventCaught = new CountDownLatch(1); |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + try { |
| 55 | + robot = new Robot(); |
| 56 | + EventQueue.invokeAndWait(() -> createAndShowUI()); |
| 57 | + robot.waitForIdle(); |
| 58 | + robot.delay(1000); |
| 59 | + testUI(); |
| 60 | + } finally { |
| 61 | + EventQueue.invokeAndWait(() -> { |
| 62 | + if (frame != null) { |
| 63 | + frame.dispose(); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void createAndShowUI() { |
| 70 | + frame = new Frame("Testing getMousePosition() after resize"); |
| 71 | + button = new Button("Button"); |
| 72 | + frame.setLayout(new BorderLayout()); |
| 73 | + frame.add(button); |
| 74 | + frame.setSize(200, 200); |
| 75 | + frame.setLocationRelativeTo(null); |
| 76 | + frame.setVisible(true); |
| 77 | + } |
| 78 | + |
| 79 | + private static void testUI() throws Exception { |
| 80 | + EventQueue.invokeAndWait(() -> { |
| 81 | + frameLocation = frame.getLocationOnScreen(); |
| 82 | + newLoc = new Point(frame.getWidth() + 10, frame.getHeight() + 10); |
| 83 | + }); |
| 84 | + |
| 85 | + robot.mouseMove(frameLocation.x + newLoc.x, frameLocation.y + newLoc.y); |
| 86 | + EventQueue.invokeAndWait(() -> { |
| 87 | + button.addComponentListener(new ResizeAdapter()); |
| 88 | + frame.setSize(frame.getWidth() * 2, frame.getHeight() * 2); |
| 89 | + frame.validate(); |
| 90 | + }); |
| 91 | + robot.waitForIdle(); |
| 92 | + robot.delay(500); |
| 93 | + |
| 94 | + if (!eventCaught.await(2, TimeUnit.SECONDS)) { |
| 95 | + throw new RuntimeException("componentResized Event isn't" |
| 96 | + + " received within a timeout"); |
| 97 | + } |
| 98 | + |
| 99 | + if (!testSucceeded) { |
| 100 | + throw new RuntimeException("Container.getMousePosition(boolean)" |
| 101 | + + " returned incorrect result while Container resized"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static class ResizeAdapter extends ComponentAdapter { |
| 106 | + int testStageCounter = 0; |
| 107 | + @Override |
| 108 | + public void componentResized(ComponentEvent e) { |
| 109 | + Point pTrue = frame.getMousePosition(true); |
| 110 | + if (frame.getMousePosition(false) == null) { |
| 111 | + testStageCounter++; |
| 112 | + System.out.println(""" |
| 113 | + TEST STAGE 1 PASSED: |
| 114 | + Container.getMousePosition(false) |
| 115 | + returned NULL over Child Component |
| 116 | + during resize. |
| 117 | + """); |
| 118 | + } |
| 119 | + if (pTrue != null) { |
| 120 | + testStageCounter++; |
| 121 | + System.out.println(""" |
| 122 | + TEST STAGE 2 PASSED: |
| 123 | + Container.getMousePosition(true) |
| 124 | + returned NON-NULL over Child Component |
| 125 | + during resize. |
| 126 | + """); |
| 127 | + } |
| 128 | + if (pTrue != null && pTrue.x == newLoc.x && pTrue.y == newLoc.y) { |
| 129 | + testStageCounter++; |
| 130 | + System.out.println(""" |
| 131 | + TEST STAGE 3 PASSED: |
| 132 | + Container.getMousePosition(true) |
| 133 | + returned correct result over Child Component |
| 134 | + during resize. |
| 135 | + """); |
| 136 | + } |
| 137 | + testSucceeded = testStageCounter == 3; |
| 138 | + eventCaught.countDown(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments