video_detector_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2012 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/wm/video_detector.h"
  5. #include <memory>
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/test/test_window_builder.h"
  10. #include "ash/wm/window_state.h"
  11. #include "ash/wm/wm_event.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/containers/circular_deque.h"
  14. #include "base/time/time.h"
  15. #include "third_party/skia/include/core/SkColor.h"
  16. #include "ui/aura/client/aura_constants.h"
  17. #include "ui/aura/client/window_types.h"
  18. #include "ui/aura/test/test_windows.h"
  19. #include "ui/aura/window.h"
  20. #include "ui/aura/window_event_dispatcher.h"
  21. #include "ui/gfx/geometry/rect.h"
  22. namespace ash {
  23. // Implementation that just records video state changes.
  24. class TestObserver : public VideoDetector::Observer {
  25. public:
  26. TestObserver() = default;
  27. TestObserver(const TestObserver&) = delete;
  28. TestObserver& operator=(const TestObserver&) = delete;
  29. bool empty() const { return states_.empty(); }
  30. void reset() { states_.clear(); }
  31. // Pops and returns the earliest-received state.
  32. VideoDetector::State PopState() {
  33. CHECK(!states_.empty());
  34. VideoDetector::State first_state = states_.front();
  35. states_.pop_front();
  36. return first_state;
  37. }
  38. // VideoDetector::Observer implementation.
  39. void OnVideoStateChanged(VideoDetector::State state) override {
  40. states_.push_back(state);
  41. }
  42. private:
  43. // States in the order they were received.
  44. base::circular_deque<VideoDetector::State> states_;
  45. };
  46. class VideoDetectorTest : public AshTestBase {
  47. public:
  48. VideoDetectorTest() = default;
  49. VideoDetectorTest(const VideoDetectorTest&) = delete;
  50. VideoDetectorTest& operator=(const VideoDetectorTest&) = delete;
  51. ~VideoDetectorTest() override = default;
  52. void SetUp() override {
  53. AshTestBase::SetUp();
  54. observer_ = std::make_unique<TestObserver>();
  55. detector_ = Shell::Get()->video_detector();
  56. detector_->AddObserver(observer_.get());
  57. }
  58. void TearDown() override {
  59. detector_->RemoveObserver(observer_.get());
  60. AshTestBase::TearDown();
  61. }
  62. protected:
  63. // Creates and returns a new window with |bounds|.
  64. std::unique_ptr<aura::Window> CreateTestWindow(const gfx::Rect& bounds) {
  65. return TestWindowBuilder()
  66. .SetColorWindowDelegate(SK_ColorRED)
  67. .SetBounds(bounds)
  68. .AllowAllWindowStates()
  69. .Build();
  70. }
  71. VideoDetector* detector_; // not owned
  72. std::unique_ptr<TestObserver> observer_;
  73. };
  74. // Verify that the video detector can distinguish fullscreen and windowed video
  75. // activity.
  76. TEST_F(VideoDetectorTest, ReportFullscreen) {
  77. UpdateDisplay("1024x768,1024x768");
  78. std::unique_ptr<aura::Window> window =
  79. CreateTestWindow(gfx::Rect(0, 0, 1024, 768));
  80. WindowState* window_state = WindowState::Get(window.get());
  81. const WMEvent toggle_fullscreen_event(WM_EVENT_TOGGLE_FULLSCREEN);
  82. window_state->OnWMEvent(&toggle_fullscreen_event);
  83. ASSERT_TRUE(window_state->IsFullscreen());
  84. window->Focus();
  85. detector_->OnVideoActivityStarted();
  86. EXPECT_EQ(VideoDetector::State::PLAYING_FULLSCREEN, observer_->PopState());
  87. EXPECT_TRUE(observer_->empty());
  88. // Make the window non-fullscreen.
  89. observer_->reset();
  90. window_state->OnWMEvent(&toggle_fullscreen_event);
  91. ASSERT_FALSE(window_state->IsFullscreen());
  92. EXPECT_EQ(VideoDetector::State::PLAYING_WINDOWED, observer_->PopState());
  93. EXPECT_TRUE(observer_->empty());
  94. // Open a second, fullscreen window. Fullscreen video should still be reported
  95. // due to the second window being fullscreen. This avoids situations where
  96. // non-fullscreen video could be reported when multiple videos are playing in
  97. // fullscreen and non-fullscreen windows.
  98. observer_->reset();
  99. std::unique_ptr<aura::Window> other_window =
  100. CreateTestWindow(gfx::Rect(1024, 0, 1024, 768));
  101. WindowState* other_window_state = WindowState::Get(other_window.get());
  102. other_window_state->OnWMEvent(&toggle_fullscreen_event);
  103. ASSERT_TRUE(other_window_state->IsFullscreen());
  104. EXPECT_EQ(VideoDetector::State::PLAYING_FULLSCREEN, observer_->PopState());
  105. EXPECT_TRUE(observer_->empty());
  106. // Make the second window non-fullscreen and check that the observer is
  107. // immediately notified about windowed video.
  108. observer_->reset();
  109. other_window_state->OnWMEvent(&toggle_fullscreen_event);
  110. ASSERT_FALSE(other_window_state->IsFullscreen());
  111. EXPECT_EQ(VideoDetector::State::PLAYING_WINDOWED, observer_->PopState());
  112. EXPECT_TRUE(observer_->empty());
  113. }
  114. } // namespace ash