1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/task/sequence_manager/thread_controller_power_monitor.h"
6 
7 #include "base/power_monitor/power_monitor.h"
8 #include "base/power_monitor/power_monitor_source.h"
9 #include "base/test/power_monitor_test.h"
10 #include "base/test/task_environment.h"
11 
12 #include "base/test/mock_callback.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 namespace sequence_manager {
17 namespace internal {
18 
19 class ThreadControllerPowerMonitorTest : public testing::Test {
20  public:
SetUp()21   void SetUp() override {
22     thread_controller_power_monitor_ =
23         std::make_unique<ThreadControllerPowerMonitor>();
24     internal::ThreadControllerPowerMonitor::OverrideUsePowerMonitorForTesting(
25         true);
26   }
27 
TearDown()28   void TearDown() override {
29     thread_controller_power_monitor_.reset();
30     internal::ThreadControllerPowerMonitor::ResetForTesting();
31   }
32 
33  protected:
34   test::SingleThreadTaskEnvironment task_environment_;
35   test::ScopedPowerMonitorTestSource power_monitor_source_;
36   std::unique_ptr<ThreadControllerPowerMonitor>
37       thread_controller_power_monitor_;
38 };
39 
TEST_F(ThreadControllerPowerMonitorTest,IsProcessInPowerSuspendState)40 TEST_F(ThreadControllerPowerMonitorTest, IsProcessInPowerSuspendState) {
41   EXPECT_FALSE(
42       thread_controller_power_monitor_->IsProcessInPowerSuspendState());
43 
44   // Before the monitor is bound to the thread, the notifications are not
45   // received.
46   power_monitor_source_.GenerateSuspendEvent();
47   EXPECT_FALSE(
48       thread_controller_power_monitor_->IsProcessInPowerSuspendState());
49   power_monitor_source_.GenerateResumeEvent();
50   EXPECT_FALSE(
51       thread_controller_power_monitor_->IsProcessInPowerSuspendState());
52 
53   thread_controller_power_monitor_->BindToCurrentThread();
54 
55   // Ensures notifications are processed.
56   power_monitor_source_.GenerateSuspendEvent();
57   EXPECT_TRUE(thread_controller_power_monitor_->IsProcessInPowerSuspendState());
58   power_monitor_source_.GenerateResumeEvent();
59   EXPECT_FALSE(
60       thread_controller_power_monitor_->IsProcessInPowerSuspendState());
61 }
62 
63 }  // namespace internal
64 }  // namespace sequence_manager
65 }  // namespace base
66