author | serb |
Thu, 17 May 2018 14:41:23 -0700 | |
changeset 50343 | 80a5ff734fcd |
child 59189 | b5cdba232fca |
permissions | -rw-r--r-- |
50343
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
1 |
/* |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
4 |
* |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
8 |
* |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
13 |
* accompanied this code). |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
14 |
* |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
18 |
* |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
21 |
* questions. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
22 |
*/ |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
23 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
24 |
import java.awt.Frame; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
25 |
import java.awt.GraphicsDevice; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
26 |
import java.awt.GraphicsEnvironment; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
27 |
import java.awt.Point; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
28 |
import java.awt.Rectangle; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
29 |
import java.awt.Robot; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
30 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
31 |
/** |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
32 |
* @test |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
33 |
* @key headful |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
34 |
* @bug 8201364 |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
35 |
* @summary Component.getLocation() should returns correct location if |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
36 |
* Component.setBounds() was ignored by the OS |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
37 |
*/ |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
38 |
public final class LocationAtScreenCorner { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
39 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
40 |
public static void main(final String[] args) throws Exception { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
41 |
Robot robot = new Robot(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
42 |
Frame frame = new Frame(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
43 |
frame.setSize(200, 200); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
44 |
frame.setLocationRelativeTo(null); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
45 |
frame.setVisible(true); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
46 |
robot.waitForIdle(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
47 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
48 |
GraphicsEnvironment lge = |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
49 |
GraphicsEnvironment.getLocalGraphicsEnvironment(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
50 |
GraphicsDevice[] devices = lge.getScreenDevices(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
51 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
52 |
// The Component.setBounds() for corners of the screen can be ignored by |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
53 |
// OS because of menubar, taskbar, dock etc. But in this case |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
54 |
// getLocation() and getLocationOnScreen() should always return the same |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
55 |
// coordinates. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
56 |
for (GraphicsDevice device : devices) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
57 |
Rectangle bounds = device.getDefaultConfiguration().getBounds(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
58 |
test(robot, frame, bounds.x, bounds.y); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
59 |
test(robot, frame, bounds.width, bounds.y); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
60 |
test(robot, frame, bounds.x, bounds.height); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
61 |
test(robot, frame, bounds.width, bounds.height); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
62 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
63 |
frame.dispose(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
64 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
65 |
|
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
66 |
private static void test(Robot robot, Frame frame, int x, int y) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
67 |
for (int i = 0; i < 10; ++i) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
68 |
// intentionally set the same coordinates a few times |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
69 |
frame.setLocation(x, y); // x and y are cached in the frame |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
70 |
int attempt = 0; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
71 |
while (true) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
72 |
robot.waitForIdle(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
73 |
// location was cached in the frame and should be updated to the |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
74 |
// real location by the native callback some time later. |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
75 |
// this is why we make a few attempts |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
76 |
Point location = frame.getLocation(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
77 |
// locationOnScreen is fetched from the peer |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
78 |
Point locationOnScreen = frame.getLocationOnScreen(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
79 |
if (location.equals(locationOnScreen)) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
80 |
break; |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
81 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
82 |
if (attempt++ > 10) { |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
83 |
frame.dispose(); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
84 |
System.err.println("Location: " + location); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
85 |
System.err.println("Location on screen: " + locationOnScreen); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
86 |
throw new RuntimeException("Wrong location"); |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
87 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
88 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
89 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
90 |
} |
80a5ff734fcd
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
serb
parents:
diff
changeset
|
91 |
} |