[CrOS perf test] Rotation in Overview Mode

The reason why it was flaky was that it label animation was
going while rotating. Wait until label is shown so that
we rotate the screen including label.

I'll file a separae bug to cancel the label animation.

Bug: 952885
Change-Id: Ic2af278d44182ba87e3e934e58fe872d5bc3e83e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1619423
Commit-Queue: Mitsuru Oshima <oshima@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661381}
diff --git a/ash/public/cpp/test/shell_test_api.h b/ash/public/cpp/test/shell_test_api.h
index 9367df6..2bbfa64 100644
--- a/ash/public/cpp/test/shell_test_api.h
+++ b/ash/public/cpp/test/shell_test_api.h
@@ -6,12 +6,17 @@
 #define ASH_PUBLIC_CPP_TEST_SHELL_TEST_API_H_
 
 #include <memory>
+#include <vector>
 
 #include "ash/ash_export.h"
 #include "ash/public/interfaces/app_list_view.mojom-forward.h"
 #include "base/callback_forward.h"
 #include "base/macros.h"
 
+namespace aura {
+class Window;
+}
+
 namespace ash {
 class DragDropController;
 class MessageCenterController;
@@ -89,6 +94,10 @@
   // state transition animation.
   void WaitForLauncherAnimationState(mojom::AppListViewState state);
 
+  // Returns the list of windows used in overview item. Returns empty
+  // if not in the overview mode.
+  std::vector<aura::Window*> GetItemWindowListInOverviewGrids();
+
  private:
   Shell* shell_;  // not owned
 
diff --git a/ash/shell_test_api.cc b/ash/shell_test_api.cc
index 3572f3a..d78b5cf 100644
--- a/ash/shell_test_api.cc
+++ b/ash/shell_test_api.cc
@@ -251,4 +251,10 @@
   run_loop.Run();
 }
 
+std::vector<aura::Window*> ShellTestApi::GetItemWindowListInOverviewGrids() {
+  return ash::Shell::Get()
+      ->overview_controller()
+      ->GetItemWindowListInOverviewGridsForTest();
+}
+
 }  // namespace ash
diff --git a/ash/wm/overview/overview_controller.cc b/ash/wm/overview/overview_controller.cc
index 4936e46..d51378a3 100644
--- a/ash/wm/overview/overview_controller.cc
+++ b/ash/wm/overview/overview_controller.cc
@@ -651,8 +651,19 @@
   std::vector<aura::Window*> windows;
   for (const std::unique_ptr<OverviewGrid>& grid :
        overview_session_->grid_list_for_testing()) {
-    for (const auto& overview_session_item : grid->window_list())
-      windows.push_back(overview_session_item->GetWindow());
+    for (const auto& overview_item : grid->window_list())
+      windows.push_back(overview_item->GetWindow());
+  }
+  return windows;
+}
+
+std::vector<aura::Window*>
+OverviewController::GetItemWindowListInOverviewGridsForTest() {
+  std::vector<aura::Window*> windows;
+  for (const std::unique_ptr<OverviewGrid>& grid :
+       overview_session_->grid_list_for_testing()) {
+    for (const auto& overview_item : grid->window_list())
+      windows.push_back(overview_item->item_widget()->GetNativeWindow());
   }
   return windows;
 }
diff --git a/ash/wm/overview/overview_controller.h b/ash/wm/overview/overview_controller.h
index 7b7cd77..d86b7fa 100644
--- a/ash/wm/overview/overview_controller.h
+++ b/ash/wm/overview/overview_controller.h
@@ -107,6 +107,7 @@
   // Gets the windows list that are shown in the overview windows grids if the
   // overview mode is active for testing.
   std::vector<aura::Window*> GetWindowsListInOverviewGridsForTest();
+  std::vector<aura::Window*> GetItemWindowListInOverviewGridsForTest();
 
  private:
   class OverviewWallpaperController;
diff --git a/chrome/browser/ui/ash/screen_rotation_interactive_uitest.cc b/chrome/browser/ui/ash/screen_rotation_interactive_uitest.cc
index e12b763..5ece549 100644
--- a/chrome/browser/ui/ash/screen_rotation_interactive_uitest.cc
+++ b/chrome/browser/ui/ash/screen_rotation_interactive_uitest.cc
@@ -4,6 +4,7 @@
 
 #include "ash/public/cpp/ash_pref_names.h"
 #include "ash/public/cpp/shell_window_ids.h"
+#include "ash/public/cpp/test/shell_test_api.h"
 #include "ash/rotator/screen_rotation_animator.h"
 #include "ash/rotator/screen_rotation_animator_observer.h"
 #include "base/macros.h"
@@ -17,6 +18,9 @@
 #include "chrome/test/base/perf/performance_test.h"
 #include "ui/aura/window.h"
 #include "ui/base/test/ui_controls.h"
+#include "ui/compositor/layer_animation_observer.h"
+
+namespace {
 
 // Test overview enter/exit animations with following conditions
 // int: number of windows : 2, 8
@@ -97,7 +101,37 @@
   DISALLOW_COPY_AND_ASSIGN(ScreenRotationWaiter);
 };
 
-IN_PROC_BROWSER_TEST_P(ScreenRotationTest, RotateInTable) {
+class WindowAnimationWaiter : public ui::LayerAnimationObserver {
+ public:
+  explicit WindowAnimationWaiter(aura::Window* window)
+      : animator_(window->layer()->GetAnimator()) {
+    animator_->AddObserver(this);
+  }
+  ~WindowAnimationWaiter() override = default;
+
+  // ui::LayerAnimationObserver:
+  void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
+    if (!animator_->is_animating()) {
+      animator_->RemoveObserver(this);
+      run_loop_.Quit();
+    }
+  }
+  void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
+  void OnLayerAnimationScheduled(
+      ui::LayerAnimationSequence* sequence) override {}
+
+  void Wait() { run_loop_.Run(); }
+
+ private:
+  ui::LayerAnimator* animator_;
+  base::RunLoop run_loop_;
+
+  DISALLOW_COPY_AND_ASSIGN(WindowAnimationWaiter);
+};
+
+}  // namespace
+
+IN_PROC_BROWSER_TEST_P(ScreenRotationTest, RotateInTablet) {
   // Browser window is used just to identify display.
   BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
   gfx::NativeWindow browser_window =
@@ -113,6 +147,36 @@
   waiter.Wait();
 }
 
+IN_PROC_BROWSER_TEST_P(ScreenRotationTest, RotateInTableOverview) {
+  // Browser window is used just to identify display.
+  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
+  gfx::NativeWindow browser_window =
+      browser_view->GetWidget()->GetNativeWindow();
+  ui_controls::SendKeyPress(browser_window, ui::VKEY_MEDIA_LAUNCH_APP1,
+                            /*control=*/false,
+                            /*shift=*/false,
+                            /*alt=*/false,
+                            /*command=*/false);
+  ash::ShellTestApi().WaitForOverviewAnimationState(
+      ash::OverviewAnimationState::kEnterAnimationComplete);
+  // Wait until the window labels are shown.
+  {
+    auto windows = ash::ShellTestApi().GetItemWindowListInOverviewGrids();
+    ASSERT_TRUE(windows.size() > 0);
+    WindowAnimationWaiter waiter(windows[0]);
+    waiter.Wait();
+  }
+
+  ScreenRotationWaiter waiter(browser_window->GetRootWindow());
+
+  ui_controls::SendKeyPress(browser_window, ui::VKEY_BROWSER_REFRESH,
+                            /*control=*/true,
+                            /*shift=*/true,
+                            /*alt=*/false,
+                            /*command=*/false);
+  waiter.Wait();
+}
+
 // TODO(oshma): Support split screen in tablet mode.
 // TODO(oshma): Support overview mode.