fullscreen_control_popup_unittest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017 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 "base/memory/raw_ptr.h"
  5. #include "ui/views/widget/widget.h"
  6. #include <memory>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/test_mock_time_task_runner.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "base/time/time.h"
  13. #include "components/fullscreen_control/fullscreen_control_popup.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "ui/gfx/animation/animation_test_api.h"
  16. #include "ui/views/test/widget_test.h"
  17. class FullscreenControlPopupTest : public views::test::WidgetTest {
  18. public:
  19. FullscreenControlPopupTest() {}
  20. FullscreenControlPopupTest(const FullscreenControlPopupTest&) = delete;
  21. FullscreenControlPopupTest& operator=(const FullscreenControlPopupTest&) =
  22. delete;
  23. ~FullscreenControlPopupTest() override {}
  24. // views::test::WidgetTest:
  25. void SetUp() override {
  26. views::test::WidgetTest::SetUp();
  27. parent_widget_ = CreateTopLevelNativeWidget();
  28. parent_widget_->SetBounds(gfx::Rect(100, 100, 640, 480));
  29. parent_widget_->Show();
  30. popup_ = std::make_unique<FullscreenControlPopup>(
  31. parent_widget_->GetNativeView(), base::DoNothing(), base::DoNothing());
  32. animation_api_ = std::make_unique<gfx::AnimationTestApi>(
  33. popup_->GetAnimationForTesting());
  34. }
  35. void TearDown() override {
  36. parent_widget_->CloseNow();
  37. views::test::WidgetTest::TearDown();
  38. }
  39. protected:
  40. void RunAnimationFor(base::TimeDelta duration) {
  41. base::TimeTicks now = base::TimeTicks::Now();
  42. animation_api_->SetStartTime(now);
  43. animation_api_->Step(now + duration);
  44. }
  45. void CompleteAnimation() { RunAnimationFor(base::Milliseconds(5000)); }
  46. gfx::Rect GetParentBounds() const {
  47. return parent_widget_->GetClientAreaBoundsInScreen();
  48. }
  49. gfx::Rect GetPopupBounds() const {
  50. return popup_->GetPopupWidget()->GetClientAreaBoundsInScreen();
  51. }
  52. std::unique_ptr<FullscreenControlPopup> popup_;
  53. private:
  54. std::unique_ptr<gfx::AnimationTestApi> animation_api_;
  55. raw_ptr<views::Widget> parent_widget_ = nullptr;
  56. };
  57. TEST_F(FullscreenControlPopupTest, ShowPopupAnimated) {
  58. EXPECT_FALSE(popup_->IsAnimating());
  59. EXPECT_FALSE(popup_->IsVisible());
  60. popup_->Show(GetParentBounds());
  61. EXPECT_TRUE(popup_->IsAnimating());
  62. EXPECT_TRUE(popup_->IsVisible());
  63. // The popup should be above the parent bounds when the animation is just
  64. // started.
  65. EXPECT_GT(GetParentBounds().y(), GetPopupBounds().y());
  66. CompleteAnimation();
  67. EXPECT_FALSE(popup_->IsAnimating());
  68. EXPECT_TRUE(popup_->IsVisible());
  69. int final_bottom =
  70. FullscreenControlPopup::GetButtonBottomOffset() + GetParentBounds().y();
  71. EXPECT_EQ(final_bottom, GetPopupBounds().bottom());
  72. }
  73. TEST_F(FullscreenControlPopupTest, HidePopupWhileStillShowing) {
  74. popup_->Show(GetParentBounds());
  75. RunAnimationFor(base::Milliseconds(50));
  76. EXPECT_TRUE(popup_->IsAnimating());
  77. EXPECT_TRUE(popup_->IsVisible());
  78. // The popup is partially shown.
  79. EXPECT_LT(GetParentBounds().y(), GetPopupBounds().bottom());
  80. popup_->Hide(true);
  81. EXPECT_TRUE(popup_->IsAnimating());
  82. EXPECT_TRUE(popup_->IsVisible());
  83. EXPECT_LT(GetParentBounds().y(), GetPopupBounds().bottom());
  84. CompleteAnimation();
  85. EXPECT_FALSE(popup_->IsAnimating());
  86. EXPECT_FALSE(popup_->IsVisible());
  87. EXPECT_GT(GetParentBounds().y(), GetPopupBounds().y());
  88. }
  89. TEST_F(FullscreenControlPopupTest, HidePopupWithoutAnimation) {
  90. popup_->Show(GetParentBounds());
  91. CompleteAnimation();
  92. popup_->Hide(false);
  93. EXPECT_FALSE(popup_->IsAnimating());
  94. EXPECT_FALSE(popup_->IsVisible());
  95. }