1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116

ash / system / power / video_activity_notifier_unittest.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/power/video_activity_notifier.h"

#include <memory>

#include "ash/test/ash_test_base.h"
#include "ash/wm/video_detector.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"

namespace ash {

class VideoActivityNotifierTest : public AshTestBase {
 public:
  VideoActivityNotifierTest() = default;

  VideoActivityNotifierTest(const VideoActivityNotifierTest&) = delete;
  VideoActivityNotifierTest& operator=(const VideoActivityNotifierTest&) =
      delete;

  ~VideoActivityNotifierTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();
    power_client_ = static_cast<chromeos::FakePowerManagerClient*>(
        chromeos::PowerManagerClient::Get());
    detector_ = std::make_unique<VideoDetector>();
    notifier_ = std::make_unique<VideoActivityNotifier>(detector_.get());
  }

  void TearDown() override {
    notifier_.reset();
    detector_.reset();
    AshTestBase::TearDown();
  }

 protected:
  raw_ptr<chromeos::FakePowerManagerClient, DanglingUntriaged>
      power_client_;  // Not owned.

  std::unique_ptr<VideoDetector> detector_;
  std::unique_ptr<VideoActivityNotifier> notifier_;
};

// Test that powerd is notified immediately when video changes to a new playing
// state or the screen is unlocked.
TEST_F(VideoActivityNotifierTest, NotifyImmediatelyOnStateChange) {
  EXPECT_FALSE(power_client_->have_video_activity_report());

  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  EXPECT_FALSE(power_client_->PopVideoActivityReport());

  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_FULLSCREEN);
  EXPECT_TRUE(power_client_->PopVideoActivityReport());

  notifier_->OnLockStateChanged(true);
  EXPECT_FALSE(power_client_->have_video_activity_report());

  notifier_->OnLockStateChanged(false);
  EXPECT_TRUE(power_client_->PopVideoActivityReport());

  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  EXPECT_FALSE(power_client_->PopVideoActivityReport());

  notifier_->OnVideoStateChanged(VideoDetector::State::NOT_PLAYING);
  EXPECT_FALSE(power_client_->have_video_activity_report());

  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  EXPECT_FALSE(power_client_->PopVideoActivityReport());
}

// Test that powerd is notified periodically while video is ongoing.
TEST_F(VideoActivityNotifierTest, NotifyPeriodically) {
  // The timer shouldn't be running initially.
  EXPECT_FALSE(notifier_->TriggerTimeoutForTest());

  // The timer should start in response to windowed video.
  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_WINDOWED);
  EXPECT_FALSE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());
  EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  EXPECT_FALSE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());

  // After fullscreen video starts, the timer should start reporting that
  // instead.
  notifier_->OnVideoStateChanged(VideoDetector::State::PLAYING_FULLSCREEN);
  EXPECT_TRUE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());
  EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  EXPECT_TRUE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());

  // Locking the screen should stop the timer.
  notifier_->OnLockStateChanged(true);
  EXPECT_FALSE(notifier_->TriggerTimeoutForTest());
  EXPECT_FALSE(power_client_->have_video_activity_report());

  // Unlocking it should restart the timer.
  notifier_->OnLockStateChanged(false);
  EXPECT_TRUE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());
  EXPECT_TRUE(notifier_->TriggerTimeoutForTest());
  EXPECT_TRUE(power_client_->PopVideoActivityReport());
  EXPECT_FALSE(power_client_->have_video_activity_report());

  // The timer should stop when video video.
  notifier_->OnVideoStateChanged(VideoDetector::State::NOT_PLAYING);
  EXPECT_FALSE(notifier_->TriggerTimeoutForTest());
  EXPECT_FALSE(power_client_->have_video_activity_report());
}

}  // namespace ash