[Home] Position app menu based on location of anchor view

If the anchor view for the app menu is at the top of the screen,
the menu should have a negative vertical offset so that it covers
the anchor view. If the anchor view is at the bottom of the screen,
the menu should have a positive vertical offset so that it covers
the anchor view.

BUG=703317

Review-Url: https://codereview.chromium.org/2761033002
Cr-Commit-Position: refs/heads/master@{#458203}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenu.java b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenu.java
index 13ce193..a1091b8 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenu.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/appmenu/AppMenu.java
@@ -281,9 +281,20 @@
             // padding of the background.
             popup.setVerticalOffset(-padding.bottom);
         } else {
-            // The menu is displayed over and below the anchored view, so shift the menu up by the
-            // height of the anchor view.
-            popup.setVerticalOffset(-mNegativeSoftwareVerticalOffset - anchorHeight);
+            int[] anchorLocationScreen = new int[2];
+            popup.getAnchorView().getLocationOnScreen(anchorLocationScreen);
+            boolean isAnchorViewAtTopOfApp = anchorLocationScreen[1] == appRect.top;
+
+            if (isAnchorViewAtTopOfApp) {
+                // When the anchor view is at the top of the screen, the menu is displayed over and
+                // below the anchored view, so shift the menu up by the height of the anchor view.
+                popup.setVerticalOffset(-mNegativeSoftwareVerticalOffset - anchorHeight);
+            } else {
+                // When the anchor view is at the bottom of the screen, the menu is displayed over
+                // and above the anchored view, so shift the menu down by the height of the anchor
+                // view.
+                popup.setVerticalOffset(-mNegativeSoftwareVerticalOffset + anchorHeight);
+            }
         }
     }