Ignore mouse position in ShelfLayoutManager if cursor is hidden

Some decisions regarding shelf visibility are made based on the
current mouse position. It leads to counter-intuitive behavior when
mouse events are followed by touch and the cursor is hidden (see bug).

Bug: 963977
Test: modified ShelfLayoutManagerTest.AutoHide
Change-Id: I82e269db0df0429e73270e28c5f431ffc6f0ca7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1623136
Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
Commit-Queue: Vladislav Kaznacheev <kaznacheev@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662038}
diff --git a/ash/shelf/shelf_layout_manager.cc b/ash/shelf/shelf_layout_manager.cc
index 2040886..bdfbfe3 100644
--- a/ash/shelf/shelf_layout_manager.cc
+++ b/ash/shelf/shelf_layout_manager.cc
@@ -1308,6 +1308,11 @@
         0);
   }
 
+  // Do not perform any checks based on the cursor position if the mouse cursor
+  // is currently hidden.
+  if (!shelf_widget_->IsMouseEventsEnabled())
+    return SHELF_AUTO_HIDE_HIDDEN;
+
   gfx::Point cursor_position_in_screen =
       display::Screen::GetScreen()->GetCursorScreenPoint();
   // Cursor is invisible in tablet mode and plug in an external mouse in tablet
diff --git a/ash/shelf/shelf_layout_manager_unittest.cc b/ash/shelf/shelf_layout_manager_unittest.cc
index 73ebd5b..dcce7bf 100644
--- a/ash/shelf/shelf_layout_manager_unittest.cc
+++ b/ash/shelf/shelf_layout_manager_unittest.cc
@@ -1132,6 +1132,23 @@
   EXPECT_EQ(stable_work_area,
             GetPrimaryWorkAreaInsets()->ComputeStableWorkArea());
 
+  // Move the mouse to the bottom again to show the shelf.
+  generator->MoveMouseTo(0, display_bottom - 1);
+  UpdateAutoHideStateNow();
+  EXPECT_EQ(SHELF_AUTO_HIDE_SHOWN, shelf->GetAutoHideState());
+
+  // A tap on the maximized window should hide the shelf, even if the most
+  // recent mouse position was over the shelf (crbug.com/963977).
+  EXPECT_TRUE(widget->IsMouseEventsEnabled());
+  gfx::Rect window_bounds = widget->GetNativeWindow()->GetBoundsInScreen();
+  generator->GestureTapAt(window_bounds.origin() + gfx::Vector2d(10, 10));
+  EXPECT_FALSE(widget->IsMouseEventsEnabled());
+  UpdateAutoHideStateNow();
+  EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->GetAutoHideState());
+
+  // Return the mouse to the top.
+  generator->MoveMouseTo(0, 0);
+
   // Drag mouse to bottom of screen.
   generator->PressLeftButton();
   generator->MoveMouseTo(0, display_bottom - 1);