occlusion_tracker_pauser_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/utility/occlusion_tracker_pauser.h"
  5. #include "ash/shell.h"
  6. #include "ash/test/ash_test_base.h"
  7. #include "base/run_loop.h"
  8. #include "ui/aura/env.h"
  9. #include "ui/aura/window_occlusion_tracker.h"
  10. #include "ui/aura/window_tree_host.h"
  11. #include "ui/compositor/compositor.h"
  12. namespace ash {
  13. namespace {
  14. class TestObserver final : public ui::CompositorAnimationObserver {
  15. public:
  16. TestObserver() = default;
  17. TestObserver(const TestObserver&) = delete;
  18. TestObserver& operator=(const TestObserver&) = delete;
  19. ~TestObserver() override = default;
  20. // ui::CompositorAnimationObserver:
  21. void OnAnimationStep(base::TimeTicks timestamp) override {}
  22. void OnCompositingShuttingDown(ui::Compositor* compositor) override {}
  23. };
  24. } // namespace
  25. class OcclusionTrackerPauserTest : public AshTestBase {
  26. public:
  27. OcclusionTrackerPauserTest()
  28. : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  29. ~OcclusionTrackerPauserTest() override = default;
  30. };
  31. TEST_F(OcclusionTrackerPauserTest, Basic) {
  32. aura::WindowOcclusionTracker* tracker =
  33. aura::Env::GetInstance()->GetWindowOcclusionTracker();
  34. ASSERT_FALSE(tracker->IsPaused());
  35. Shell::Get()->occlusion_tracker_pauser()->PauseUntilAnimationsEnd(
  36. base::TimeDelta());
  37. EXPECT_TRUE(tracker->IsPaused());
  38. auto* compositor = Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  39. TestObserver observer1, observer2;
  40. compositor->AddAnimationObserver(&observer1);
  41. EXPECT_TRUE(tracker->IsPaused());
  42. compositor->RemoveAnimationObserver(&observer1);
  43. EXPECT_FALSE(tracker->IsPaused());
  44. compositor->AddAnimationObserver(&observer1);
  45. EXPECT_FALSE(tracker->IsPaused());
  46. Shell::Get()->occlusion_tracker_pauser()->PauseUntilAnimationsEnd(
  47. base::TimeDelta());
  48. EXPECT_TRUE(tracker->IsPaused());
  49. compositor->AddAnimationObserver(&observer2);
  50. EXPECT_TRUE(tracker->IsPaused());
  51. compositor->RemoveAnimationObserver(&observer2);
  52. EXPECT_TRUE(tracker->IsPaused());
  53. compositor->RemoveAnimationObserver(&observer1);
  54. EXPECT_FALSE(tracker->IsPaused());
  55. }
  56. TEST_F(OcclusionTrackerPauserTest, MultiDisplay) {
  57. aura::WindowOcclusionTracker* tracker =
  58. aura::Env::GetInstance()->GetWindowOcclusionTracker();
  59. UpdateDisplay("800x1000, 800x1000");
  60. auto* compositor1 = Shell::GetAllRootWindows()[0]->GetHost()->compositor();
  61. auto* compositor2 = Shell::GetAllRootWindows()[1]->GetHost()->compositor();
  62. TestObserver observer1, observer2;
  63. Shell::Get()->occlusion_tracker_pauser()->PauseUntilAnimationsEnd(
  64. base::TimeDelta());
  65. EXPECT_TRUE(tracker->IsPaused());
  66. compositor1->AddAnimationObserver(&observer1);
  67. compositor2->AddAnimationObserver(&observer2);
  68. EXPECT_TRUE(tracker->IsPaused());
  69. compositor1->RemoveAnimationObserver(&observer1);
  70. EXPECT_TRUE(tracker->IsPaused());
  71. compositor2->RemoveAnimationObserver(&observer2);
  72. EXPECT_FALSE(tracker->IsPaused());
  73. // Disconnect display.
  74. Shell::Get()->occlusion_tracker_pauser()->PauseUntilAnimationsEnd(
  75. base::TimeDelta());
  76. EXPECT_TRUE(tracker->IsPaused());
  77. compositor1->AddAnimationObserver(&observer1);
  78. compositor2->AddAnimationObserver(&observer2);
  79. EXPECT_TRUE(tracker->IsPaused());
  80. compositor1->RemoveAnimationObserver(&observer1);
  81. EXPECT_TRUE(tracker->IsPaused());
  82. UpdateDisplay("800x1000");
  83. base::RunLoop().RunUntilIdle();
  84. EXPECT_FALSE(tracker->IsPaused());
  85. }
  86. TEST_F(OcclusionTrackerPauserTest, Timeout) {
  87. aura::WindowOcclusionTracker* tracker =
  88. aura::Env::GetInstance()->GetWindowOcclusionTracker();
  89. UpdateDisplay("800x1000, 800x1000");
  90. auto* compositor1 = Shell::GetAllRootWindows()[0]->GetHost()->compositor();
  91. auto* compositor2 = Shell::GetAllRootWindows()[1]->GetHost()->compositor();
  92. // Add observer to emulate animations start/end.
  93. TestObserver observer1, observer2;
  94. Shell::Get()->occlusion_tracker_pauser()->PauseUntilAnimationsEnd(
  95. base::Seconds(2));
  96. EXPECT_TRUE(tracker->IsPaused());
  97. compositor1->AddAnimationObserver(&observer1);
  98. compositor2->AddAnimationObserver(&observer2);
  99. EXPECT_TRUE(tracker->IsPaused());
  100. task_environment()->FastForwardBy(base::Seconds(1));
  101. EXPECT_TRUE(tracker->IsPaused());
  102. task_environment()->FastForwardBy(base::Seconds(2));
  103. EXPECT_FALSE(tracker->IsPaused());
  104. compositor1->RemoveAnimationObserver(&observer1);
  105. compositor2->RemoveAnimationObserver(&observer2);
  106. }
  107. } // namespace ash