video_activity_notifier_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2016 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/system/power/video_activity_notifier.h"
  5. #include <memory>
  6. #include "ash/test/ash_test_base.h"
  7. #include "ash/wm/video_detector.h"
  8. #include "chromeos/dbus/power/fake_power_manager_client.h"
  9. namespace ash {
  10. class VideoActivityNotifierTest : public AshTestBase {
  11. public:
  12. VideoActivityNotifierTest() = default;
  13. VideoActivityNotifierTest(const VideoActivityNotifierTest&) = delete;
  14. VideoActivityNotifierTest& operator=(const VideoActivityNotifierTest&) =
  15. delete;
  16. ~VideoActivityNotifierTest() override = default;
  17. void SetUp() override {
  18. AshTestBase::SetUp();
  19. power_client_ = static_cast<chromeos::FakePowerManagerClient*>(
  20. chromeos::PowerManagerClient::Get());
  21. detector_ = std::make_unique<VideoDetector>();
  22. notifier_ = std::make_unique<VideoActivityNotifier>(detector_.get());
  23. }
  24. void TearDown() override {
  25. notifier_.reset();
  26. detector_.reset();
  27. AshTestBase::TearDown();
  28. }
  29. protected:
  30. chromeos::FakePowerManagerClient* power_client_; // Not owned.
  31. std::unique_ptr<VideoDetector> detector_;
  32. std::unique_ptr<VideoActivityNotifier> notifier_;
  33. };
  34. // Test that powerd is notified immediately when video changes to a new playing
  35. // state or the screen is unlocked.
  36. TEST_F(VideoActivityNotifierTest, NotifyImmediatelyOnStateChange) {
  37. EXPECT_FALSE(power_client_->have_video_activity_report());
  38. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  39. EXPECT_FALSE(power_client_->PopVideoActivityReport());
  40. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_FULLSCREEN);
  41. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  42. notifier_->OnLockStateChanged(true);
  43. EXPECT_FALSE(power_client_->have_video_activity_report());
  44. notifier_->OnLockStateChanged(false);
  45. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  46. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  47. EXPECT_FALSE(power_client_->PopVideoActivityReport());
  48. notifier_->OnVideoStateChanged(VideoDetector::State::NOT_PLAYING);
  49. EXPECT_FALSE(power_client_->have_video_activity_report());
  50. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  51. EXPECT_FALSE(power_client_->PopVideoActivityReport());
  52. }
  53. // Test that powerd is notified periodically while video is ongoing.
  54. TEST_F(VideoActivityNotifierTest, NotifyPeriodically) {
  55. // The timer shouldn't be running initially.
  56. EXPECT_FALSE(notifier_->TriggerTimeoutForTest());
  57. // The timer should start in response to windowed video.
  58. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  59. EXPECT_FALSE(power_client_->PopVideoActivityReport());
  60. EXPECT_FALSE(power_client_->have_video_activity_report());
  61. EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  62. EXPECT_FALSE(power_client_->PopVideoActivityReport());
  63. EXPECT_FALSE(power_client_->have_video_activity_report());
  64. // After fullscreen video starts, the timer should start reporting that
  65. // instead.
  66. notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_FULLSCREEN);
  67. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  68. EXPECT_FALSE(power_client_->have_video_activity_report());
  69. EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  70. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  71. EXPECT_FALSE(power_client_->have_video_activity_report());
  72. // Locking the screen should stop the timer.
  73. notifier_->OnLockStateChanged(true);
  74. EXPECT_FALSE(notifier_->TriggerTimeoutForTest());
  75. EXPECT_FALSE(power_client_->have_video_activity_report());
  76. // Unlocking it should restart the timer.
  77. notifier_->OnLockStateChanged(false);
  78. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  79. EXPECT_FALSE(power_client_->have_video_activity_report());
  80. EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  81. EXPECT_TRUE(power_client_->PopVideoActivityReport());
  82. EXPECT_FALSE(power_client_->have_video_activity_report());
  83. // The timer should stop when video video.
  84. notifier_->OnVideoStateChanged(VideoDetector::State::NOT_PLAYING);
  85. EXPECT_FALSE(notifier_->TriggerTimeoutForTest());
  86. EXPECT_FALSE(power_client_->have_video_activity_report());
  87. }
  88. } // namespace ash