|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2023, 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 | + @bug 4101437 |
| 27 | + @summary Dialog.setLocation(int,int) works unstable when the dialog is visible |
| 28 | + @key headful |
| 29 | + @run main DialogLocationTest |
| 30 | +*/ |
| 31 | + |
| 32 | +import java.awt.AWTException; |
| 33 | +import java.awt.Container; |
| 34 | +import java.awt.Dialog; |
| 35 | +import java.awt.EventQueue; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.GraphicsEnvironment; |
| 38 | +import java.awt.Panel; |
| 39 | +import java.awt.Point; |
| 40 | +import java.awt.Rectangle; |
| 41 | +import java.awt.Robot; |
| 42 | +import java.awt.event.ComponentAdapter; |
| 43 | +import java.awt.event.ComponentEvent; |
| 44 | +import java.lang.reflect.InvocationTargetException; |
| 45 | +import java.util.Random; |
| 46 | + |
| 47 | +public class DialogLocationTest extends Panel { |
| 48 | + private volatile int count = 0; |
| 49 | + private Dialog my_dialog; |
| 50 | + private volatile boolean waitingForEvent = false; |
| 51 | + private volatile int newX, newY; |
| 52 | + Random random = new Random(); |
| 53 | + |
| 54 | + public void init() { |
| 55 | + Container f = getParent(); |
| 56 | + |
| 57 | + while (!(f instanceof Frame)) { |
| 58 | + f = f.getParent(); |
| 59 | + } |
| 60 | + |
| 61 | + my_dialog = new Dialog((Frame) f, "TestDialog"); |
| 62 | + my_dialog.setSize(150, 100); |
| 63 | + |
| 64 | + setSize(200, 200); |
| 65 | + } |
| 66 | + |
| 67 | + public void start() throws InterruptedException, |
| 68 | + InvocationTargetException { |
| 69 | + Robot robot; |
| 70 | + try { |
| 71 | + robot = new Robot(); |
| 72 | + EventQueue.invokeAndWait(() -> { |
| 73 | + my_dialog.setLocationRelativeTo(null); |
| 74 | + my_dialog.setVisible(true); |
| 75 | + }); |
| 76 | + robot.waitForIdle(); |
| 77 | + robot.delay(1000); |
| 78 | + my_dialog.addComponentListener(new CL()); |
| 79 | + setDialogLocation(my_dialog); |
| 80 | + } catch (AWTException e) { |
| 81 | + throw new RuntimeException(e); |
| 82 | + } finally { |
| 83 | + EventQueue.invokeAndWait(() -> { |
| 84 | + my_dialog.setVisible(false); |
| 85 | + my_dialog.dispose(); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void setDialogLocation(Dialog dialog) { |
| 91 | + int height, width, insetX, insetY; |
| 92 | + Point curLoc; |
| 93 | + int i; |
| 94 | + |
| 95 | + Rectangle screen = GraphicsEnvironment |
| 96 | + .getLocalGraphicsEnvironment() |
| 97 | + .getMaximumWindowBounds(); |
| 98 | + height = screen.height; |
| 99 | + width = screen.width; |
| 100 | + insetX = screen.x; |
| 101 | + insetY = screen.y; |
| 102 | + |
| 103 | + for (i = 0; i < 100; i++) { |
| 104 | + newX = random.nextInt(width - 300 - insetX) + insetX; |
| 105 | + newY = random.nextInt(height - 400 - insetY) + insetY; |
| 106 | + |
| 107 | + if (newX == 0 && newY == 0) { |
| 108 | + i--; |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + waitingForEvent = true; |
| 113 | + |
| 114 | + EventQueue.invokeLater(() -> { |
| 115 | + dialog.setLocation(newX, newY); |
| 116 | + }); |
| 117 | + |
| 118 | + while (waitingForEvent) { |
| 119 | + Thread.yield(); |
| 120 | + } |
| 121 | + |
| 122 | + curLoc = dialog.getLocation(); |
| 123 | + if (curLoc.x != newX || curLoc.y != newY) { |
| 124 | + count++; |
| 125 | + System.out.println("Failed on iteration " + i + " expect:[" + newX + "," + newY + "] reported:[" + curLoc.x + "," |
| 126 | + + curLoc.y + "] diff:[" + (curLoc.x - newX) + "," + (curLoc.y - newY) + "]"); |
| 127 | + System.out.flush(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (count > 0) { |
| 132 | + throw new RuntimeException("Dialog Location was set incorrectly"); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public class CL extends ComponentAdapter { |
| 137 | + int lastX, lastY; |
| 138 | + |
| 139 | + public void componentMoved(ComponentEvent e) { |
| 140 | + if (e.getComponent() == my_dialog) { |
| 141 | + Point eventLoc = e.getComponent().getLocation(); |
| 142 | + if (lastX != eventLoc.x || lastY != eventLoc.y) { |
| 143 | + lastX = eventLoc.x; |
| 144 | + lastY = eventLoc.y; |
| 145 | + if (newX != 0 && newY != 0 && (eventLoc.x != newX || eventLoc.y != newY)) { |
| 146 | + count++; |
| 147 | + System.out.println("Failed in componentMoved() expect:[" + newX +"," + newY + "] reported: [" + |
| 148 | + eventLoc.x + "," + eventLoc.y + "] diff [" + (eventLoc.x - newX) + "," + (eventLoc.y - newY) + "]"); |
| 149 | + System.out.flush(); |
| 150 | + } |
| 151 | + waitingForEvent = false; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public static void main(String[] args) throws InterruptedException, |
| 158 | + InvocationTargetException { |
| 159 | + Frame frame = new Frame("DialogLocationTest"); |
| 160 | + try { |
| 161 | + DialogLocationTest test = new DialogLocationTest(); |
| 162 | + EventQueue.invokeAndWait(() -> { |
| 163 | + frame.add(test); |
| 164 | + test.init(); |
| 165 | + frame.setVisible(true); |
| 166 | + }); |
| 167 | + test.start(); |
| 168 | + } finally { |
| 169 | + EventQueue.invokeLater(() -> { |
| 170 | + frame.setVisible(false); |
| 171 | + frame.dispose(); |
| 172 | + }); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
0 commit comments