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

base / task / sequence_manager / thread_controller_power_monitor_unittest.cc [blame]

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

#include "base/task/sequence_manager/thread_controller_power_monitor.h"

#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_source.h"
#include "base/test/power_monitor_test.h"
#include "base/test/task_environment.h"

#include "base/test/mock_callback.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {
namespace sequence_manager {
namespace internal {

class ThreadControllerPowerMonitorTest : public testing::Test {
 public:
  void SetUp() override {
    thread_controller_power_monitor_ =
        std::make_unique<ThreadControllerPowerMonitor>();
    internal::ThreadControllerPowerMonitor::OverrideUsePowerMonitorForTesting(
        true);
  }

  void TearDown() override {
    thread_controller_power_monitor_.reset();
    internal::ThreadControllerPowerMonitor::ResetForTesting();
  }

 protected:
  test::SingleThreadTaskEnvironment task_environment_;
  test::ScopedPowerMonitorTestSource power_monitor_source_;
  std::unique_ptr<ThreadControllerPowerMonitor>
      thread_controller_power_monitor_;
};

TEST_F(ThreadControllerPowerMonitorTest, IsProcessInPowerSuspendState) {
  EXPECT_FALSE(
      thread_controller_power_monitor_->IsProcessInPowerSuspendState());

  // Before the monitor is bound to the thread, the notifications are not
  // received.
  power_monitor_source_.GenerateSuspendEvent();
  EXPECT_FALSE(
      thread_controller_power_monitor_->IsProcessInPowerSuspendState());
  power_monitor_source_.GenerateResumeEvent();
  EXPECT_FALSE(
      thread_controller_power_monitor_->IsProcessInPowerSuspendState());

  thread_controller_power_monitor_->BindToCurrentThread();

  // Ensures notifications are processed.
  power_monitor_source_.GenerateSuspendEvent();
  EXPECT_TRUE(thread_controller_power_monitor_->IsProcessInPowerSuspendState());
  power_monitor_source_.GenerateResumeEvent();
  EXPECT_FALSE(
      thread_controller_power_monitor_->IsProcessInPowerSuspendState());
}

}  // namespace internal
}  // namespace sequence_manager
}  // namespace base