Fix shelf after transition to tablet mode in overview

When calculating hotseat state after transition to tablet mode, don't
transition to kShownTablet  state if overview session is active (if
there are no windows shown, the target app list visibility may
momentarily be "visible" if no app windows exist, enough for the hotseat
to end up in kShownTablet state.

BUG=b/330282602

Change-Id: Ieecabe17ee4d8687ddbb3045f4a6e1ff3ff91c5c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5437022
Reviewed-by: Ana Salazar <anasalazar@chromium.org>
Commit-Queue: Toni Barzic <tbarzic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1284658}
diff --git a/ash/shelf/shelf_layout_manager.cc b/ash/shelf/shelf_layout_manager.cc
index 8e1a467..be6a2e1 100644
--- a/ash/shelf/shelf_layout_manager.cc
+++ b/ash/shelf/shelf_layout_manager.cc
@@ -1635,14 +1635,16 @@
 HotseatState ShelfLayoutManager::CalculateHotseatState(
     ShelfVisibilityState visibility_state,
     ShelfAutoHideState auto_hide_state) const {
-  if (!Shell::Get()->IsInTabletMode() || !shelf_->IsHorizontalAlignment())
+  if (!Shell::Get()->IsInTabletMode() || !shelf_->IsHorizontalAlignment()) {
     return HotseatState::kShownClamshell;
+  }
 
   auto* app_list_controller = Shell::Get()->app_list_controller();
   // If the app list controller is null, we are probably in the middle of
   // a shutdown, let's not change the hotseat state.
-  if (!app_list_controller)
+  if (!app_list_controller) {
     return hotseat_state();
+  }
   const auto* overview_controller = Shell::Get()->overview_controller();
   const bool in_overview =
       ((overview_controller && overview_controller->InOverviewSession()) ||
@@ -1653,14 +1655,16 @@
       app_list_controller->ShouldHomeLauncherBeVisible();
 
   // TODO(https://crbug.com/1058205): Test this behavior.
-  if (ShelfConfig::Get()->is_virtual_keyboard_shown())
+  if (ShelfConfig::Get()->is_virtual_keyboard_shown()) {
     return HotseatState::kHidden;
+  }
 
   // Only force to show if there is not a pending drag operation.
   if (shelf_widget_->IsHotseatForcedShowInTabletMode() &&
-      drag_status_ == kDragNone)
+      drag_status_ == kDragNone) {
     return app_list_target_visibility ? HotseatState::kShownHomeLauncher
                                       : HotseatState::kExtended;
+  }
 
   bool in_split_view = false;
   if (in_overview) {
@@ -1673,28 +1677,33 @@
   switch (drag_status_) {
     case kDragNone:
     case kDragHomeToOverviewInProgress: {
-      if (app_list_target_visibility)
+      if (app_list_target_visibility && !in_overview) {
         return HotseatState::kShownHomeLauncher;
-
+      }
       // Show the hotseat if the shelf view's context menu is showing.
-      if (shelf_->hotseat_widget()->IsShowingShelfMenu())
-        return HotseatState::kExtended;
-
-      if (in_overview) {
-        if (in_split_view)
-          return HotseatState::kExtended;
-        // Maintain the ShownHomeLauncher state if we enter overview mode
-        // from it.
-        if (hotseat_state() == HotseatState::kShownHomeLauncher)
-          return HotseatState::kShownHomeLauncher;
+      if (shelf_->hotseat_widget()->IsShowingShelfMenu()) {
         return HotseatState::kExtended;
       }
 
-      if (state_forced_by_back_gesture_)
+      if (in_overview) {
+        if (in_split_view) {
+          return HotseatState::kExtended;
+        }
+        // Maintain the ShownHomeLauncher state if we enter overview mode
+        // from it.
+        if (hotseat_state() == HotseatState::kShownHomeLauncher) {
+          return HotseatState::kShownHomeLauncher;
+        }
         return HotseatState::kExtended;
+      }
 
-      if (visibility_state == SHELF_AUTO_HIDE)
+      if (state_forced_by_back_gesture_) {
+        return HotseatState::kExtended;
+      }
+
+      if (visibility_state == SHELF_AUTO_HIDE) {
         return HotseatState::kHidden;
+      }
 
       if (shelf_->hotseat_widget()->is_manually_extended() &&
           !should_hide_hotseat_) {
@@ -1721,17 +1730,21 @@
     case kDragCancelInProgress: {
       // If the drag being completed is not a Hotseat drag, don't change the
       // state.
-      if (!hotseat_is_in_drag_)
+      if (!hotseat_is_in_drag_) {
         return hotseat_state();
+      }
 
-      if (app_list_target_visibility)
+      if (app_list_target_visibility) {
         return HotseatState::kShownHomeLauncher;
+      }
 
-      if (in_overview && !in_split_view)
+      if (in_overview && !in_split_view) {
         return HotseatState::kExtended;
+      }
 
-      if (shelf_->hotseat_widget()->IsExtended())
+      if (shelf_->hotseat_widget()->IsExtended()) {
         return HotseatState::kExtended;
+      }
 
       // |drag_amount_| is relative to the top of the hotseat when the drag
       // begins with an extended hotseat. Correct for this to get
@@ -1752,12 +1765,14 @@
       const bool dragged_to_bezel =
           std::ceil(end_of_drag_in_screen) >= screen_bottom;
 
-      if (dragged_to_bezel)
+      if (dragged_to_bezel) {
         return HotseatState::kHidden;
+      }
 
       if (std::abs(last_drag_velocity_) >= 120) {
-        if (last_drag_velocity_ > 0)
+        if (last_drag_velocity_ > 0) {
           return HotseatState::kHidden;
+        }
         return HotseatState::kExtended;
       }
 
@@ -1766,8 +1781,9 @@
           shelf_->hotseat_widget()->GetWindowBoundsInScreen().y();
       const bool dragged_over_half_hotseat_size =
           top_of_hotseat_to_screen_bottom < hotseat_size / 2;
-      if (dragged_over_half_hotseat_size)
+      if (dragged_over_half_hotseat_size) {
         return HotseatState::kHidden;
+      }
 
       return HotseatState::kExtended;
     }
diff --git a/ash/shelf/shelf_layout_manager_unittest.cc b/ash/shelf/shelf_layout_manager_unittest.cc
index f2dc5c9..e30d4f9e 100644
--- a/ash/shelf/shelf_layout_manager_unittest.cc
+++ b/ash/shelf/shelf_layout_manager_unittest.cc
@@ -714,6 +714,17 @@
             GetShelfWidget()->GetNativeWindow()->GetTargetBounds().y());
 }
 
+TEST_F(ShelfLayoutManagerTest,
+       HotseatStateAfterTabletModeTransitionWithOverview) {
+  // Tests that the shelf is visible when in overview mode.
+  EnterOverview();
+  ASSERT_EQ(HotseatState::kShownClamshell,
+            GetShelfLayoutManager()->hotseat_state());
+
+  TabletModeControllerTestApi().EnterTabletMode();
+  EXPECT_EQ(HotseatState::kExtended, GetShelfLayoutManager()->hotseat_state());
+}
+
 // Assertions around SetAutoHideBehavior.
 TEST_F(ShelfLayoutManagerTest, SetAutoHideBehavior) {
   Shelf* shelf = GetPrimaryShelf();