8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0)
authorserb
Thu, 17 May 2018 14:41:23 -0700
changeset 50343 80a5ff734fcd
parent 50342 ddbd2037f9ef
child 50344 8039dc75f125
8201364: [macosx] Component.getLocation() gives inconsistent coordinate for a component at (0,0) Reviewed-by: dmarkov
src/java.desktop/macosx/classes/sun/lwawt/LWWindowPeer.java
test/jdk/ProblemList.txt
test/jdk/java/awt/Window/LocationAtScreenCorner/LocationAtScreenCorner.java
--- a/src/java.desktop/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu May 17 11:40:55 2018 +0530
+++ b/src/java.desktop/macosx/classes/sun/lwawt/LWWindowPeer.java	Thu May 17 14:41:23 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,16 +25,56 @@
 
 package sun.lwawt;
 
-import java.awt.*;
-import java.awt.event.*;
-import java.awt.peer.*;
+import java.awt.AlphaComposite;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dialog;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Frame;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.GraphicsConfiguration;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.Insets;
+import java.awt.KeyboardFocusManager;
+import java.awt.MenuBar;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Shape;
+import java.awt.SystemColor;
+import java.awt.Toolkit;
+import java.awt.Window;
+import java.awt.event.FocusEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.WindowEvent;
+import java.awt.peer.ComponentPeer;
+import java.awt.peer.DialogPeer;
+import java.awt.peer.FramePeer;
+import java.awt.peer.KeyboardFocusManagerPeer;
+import java.awt.peer.WindowPeer;
 import java.util.List;
 
-import javax.swing.*;
+import javax.swing.JComponent;
 
-import sun.awt.*;
+import sun.awt.AWTAccessor;
 import sun.awt.AWTAccessor.ComponentAccessor;
-import sun.java2d.*;
+import sun.awt.AppContext;
+import sun.awt.CGraphicsDevice;
+import sun.awt.DisplayChangedListener;
+import sun.awt.ExtendedKeyCodes;
+import sun.awt.FullScreenCapable;
+import sun.awt.SunToolkit;
+import sun.awt.TimedWindowEvent;
+import sun.awt.UngrabEvent;
+import sun.java2d.NullSurfaceData;
+import sun.java2d.SunGraphics2D;
+import sun.java2d.SunGraphicsEnvironment;
+import sun.java2d.SurfaceData;
 import sun.java2d.loops.Blit;
 import sun.java2d.loops.CompositeType;
 import sun.java2d.pipe.Region;
@@ -661,16 +701,25 @@
      * user or window insets are changed. There's no notifyReshape() in
      * LWComponentPeer as the only components which could be resized by user are
      * top-level windows.
+     * <p>
+     * We need to update the target and post the events, if the peer was moved
+     * or resized, or if the target is out of sync with this peer.
      */
     @Override
     public void notifyReshape(int x, int y, int w, int h) {
-        Rectangle oldBounds = getBounds();
+        final Rectangle pBounds = getBounds();
         final boolean invalid = updateInsets(platformWindow.getInsets());
-        final boolean moved = (x != oldBounds.x) || (y != oldBounds.y);
-        final boolean resized = (w != oldBounds.width) || (h != oldBounds.height);
+        final boolean pMoved = (x != pBounds.x) || (y != pBounds.y);
+        final boolean pResized = (w != pBounds.width) || (h != pBounds.height);
+
+        final ComponentAccessor accessor = AWTAccessor.getComponentAccessor();
+        final Rectangle tBounds = accessor.getBounds(getTarget());
+        final boolean tMoved = (x != tBounds.x) || (y != tBounds.y);
+        final boolean tResized = (w != tBounds.width) || (h != tBounds.height);
 
         // Check if anything changed
-        if (!moved && !resized && !invalid) {
+        if (!tMoved && !tResized && !pMoved && !pResized && !invalid) {
+            // Native window(NSWindow)/LWWindowPeer/Target are in sync
             return;
         }
         // First, update peer's bounds
@@ -682,16 +731,16 @@
             setPlatformMaximizedBounds(getDefaultMaximizedBounds());
         }
 
-        if (resized || isNewDevice) {
+        if (pResized || isNewDevice) {
             replaceSurfaceData();
             updateMinimumSize();
         }
 
         // Third, COMPONENT_MOVED/COMPONENT_RESIZED/PAINT events
-        if (moved || invalid) {
+        if (tMoved || pMoved || invalid) {
             handleMove(x, y, true);
         }
-        if (resized || invalid || isNewDevice) {
+        if (tResized || pResized || invalid || isNewDevice) {
             handleResize(w, h, true);
             repaintPeer();
         }
--- a/test/jdk/ProblemList.txt	Thu May 17 11:40:55 2018 +0530
+++ b/test/jdk/ProblemList.txt	Thu May 17 14:41:23 2018 -0700
@@ -216,6 +216,7 @@
 java/awt/Window/ShapedAndTranslucentWindows/StaticallyShaped.java 8165218 macosx-all
 java/awt/Window/AlwaysOnTop/AutoTestOnTop.java 6847593 macosx-all
 java/awt/Window/GrabSequence/GrabSequence.java 6848409 macosx-all,linux-all
+java/awt/Window/LocationAtScreenCorner/LocationAtScreenCorner.java 8203371 linux-all,solaris-all
 java/awt/font/TextLayout/CombiningPerf.java 8192931 generic-all
 java/awt/font/TextLayout/TextLayoutBounds.java 8169188 generic-all
 java/awt/font/StyledMetrics/BoldSpace.java 8198422 linux-all
@@ -482,7 +483,7 @@
 java/awt/dnd/MissingDragExitEventTest/MissingDragExitEventTest.java 8030121 macosx-all
 java/awt/Choice/ChoicePopupLocation/ChoicePopupLocation.java 8202931 macosx-all,linux-all
 java/awt/Focus/NonFocusableBlockedOwnerTest/NonFocusableBlockedOwnerTest.html 7124275 macosx-all
-java/awt/Focus/TranserFocusToWindow/TranserFocusToWindow.java 6848810 macosx-all 
+java/awt/Focus/TranserFocusToWindow/TranserFocusToWindow.java 6848810 macosx-all
 java/awt/Component/NativeInLightShow/NativeInLightShow.java 8202932 linux-all
 java/awt/grab/GrabOnUnfocusableToplevel/GrabOnUnfocusableToplevel.java 8202933 linux-all
 java/awt/grab/MenuDragEvents/MenuDragEvents.html 8202934 linux-all
@@ -491,7 +492,7 @@
 java/awt/SplashScreen/MultiResolutionSplash/unix/UnixMultiResolutionSplashTest.java 8203004 linux-all
 java/awt/Robot/AcceptExtraMouseButtons/AcceptExtraMouseButtons.java 7107528 linux-all,macosx-all
 java/awt/Mouse/MouseDragEvent/MouseDraggedTest.java 8080676 linux-all
-java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersInKeyEvent.java 
+java/awt/Mouse/MouseModifiersUnitTest/MouseModifiersInKeyEvent.java
 java/awt/Mouse/TitleBarDoubleClick/TitleBarDoubleClick.html 8148041 linux-all
 java/awt/Toolkit/DesktopProperties/rfe4758438.java 8193547 linux-all
 java/awt/Toolkit/ToolkitPropertyTest/ToolkitPropertyTest_Enable.java 6847163
@@ -830,4 +831,4 @@
 jdk/jfr/event/io/TestInstrumentation.java                       8202142    generic-all
 jdk/jfr/event/sampling/TestNative.java                          8202142    generic-all
 jdk/jfr/event/os/TestSystemProcess.java                         8202835    linux-all
-jdk/jfr/event/runtime/TestBiasedLockRevocationEvents.java       8203237    generic-all
\ No newline at end of file
+jdk/jfr/event/runtime/TestBiasedLockRevocationEvents.java       8203237    generic-all
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/awt/Window/LocationAtScreenCorner/LocationAtScreenCorner.java	Thu May 17 14:41:23 2018 -0700
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Frame;
+import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Robot;
+
+/**
+ * @test
+ * @key headful
+ * @bug 8201364
+ * @summary Component.getLocation() should returns correct location if
+ *          Component.setBounds() was ignored by the OS
+ */
+public final class LocationAtScreenCorner {
+
+    public static void main(final String[] args) throws Exception {
+        Robot robot = new Robot();
+        Frame frame = new Frame();
+        frame.setSize(200, 200);
+        frame.setLocationRelativeTo(null);
+        frame.setVisible(true);
+        robot.waitForIdle();
+
+        GraphicsEnvironment lge =
+                GraphicsEnvironment.getLocalGraphicsEnvironment();
+        GraphicsDevice[] devices = lge.getScreenDevices();
+
+        // The Component.setBounds() for corners of the screen can be ignored by
+        // OS because of menubar, taskbar, dock etc. But in this case
+        // getLocation() and getLocationOnScreen() should always return the same
+        // coordinates.
+        for (GraphicsDevice device : devices) {
+            Rectangle bounds = device.getDefaultConfiguration().getBounds();
+            test(robot, frame, bounds.x, bounds.y);
+            test(robot, frame, bounds.width, bounds.y);
+            test(robot, frame, bounds.x, bounds.height);
+            test(robot, frame, bounds.width, bounds.height);
+        }
+        frame.dispose();
+    }
+
+    private static void test(Robot robot, Frame frame, int x, int y) {
+        for (int i = 0; i < 10; ++i) {
+            // intentionally set the same coordinates a few times
+            frame.setLocation(x, y); // x and y are cached in the frame
+            int attempt = 0;
+            while (true) {
+                robot.waitForIdle();
+                // location was cached in the frame and should be updated to the
+                // real location by the native callback some time later.
+                // this is why we make a few attempts
+                Point location = frame.getLocation();
+                // locationOnScreen is fetched from the peer
+                Point locationOnScreen = frame.getLocationOnScreen();
+                if (location.equals(locationOnScreen)) {
+                    break;
+                }
+                if (attempt++ > 10) {
+                    frame.dispose();
+                    System.err.println("Location: " + location);
+                    System.err.println("Location on screen: " + locationOnScreen);
+                    throw new RuntimeException("Wrong location");
+                }
+            }
+        }
+    }
+}