1 // Copyright 2013 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
5 #include "base/macros.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/power_monitor/power_monitor.h"
8 #include "base/test/power_monitor_test_base.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
13 class PowerMonitorTest : public testing::Test {
14 protected:
PowerMonitorTest()15 PowerMonitorTest() {
16 power_monitor_source_ = new PowerMonitorTestSource();
17 power_monitor_.reset(new PowerMonitor(
18 std::unique_ptr<PowerMonitorSource>(power_monitor_source_)));
19 }
20 ~PowerMonitorTest() override = default;
21
source()22 PowerMonitorTestSource* source() { return power_monitor_source_; }
monitor()23 PowerMonitor* monitor() { return power_monitor_.get(); }
24
25 private:
26 base::MessageLoop message_loop_;
27 PowerMonitorTestSource* power_monitor_source_;
28 std::unique_ptr<PowerMonitor> power_monitor_;
29
30 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
31 };
32
33 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test
34 // Will cover both classes
TEST_F(PowerMonitorTest,PowerNotifications)35 TEST_F(PowerMonitorTest, PowerNotifications) {
36 const int kObservers = 5;
37
38 PowerMonitorTestObserver observers[kObservers];
39 for (int index = 0; index < kObservers; ++index)
40 monitor()->AddObserver(&observers[index]);
41
42 // Sending resume when not suspended should have no effect.
43 source()->GenerateResumeEvent();
44 EXPECT_EQ(observers[0].resumes(), 0);
45
46 // Pretend we suspended.
47 source()->GenerateSuspendEvent();
48 // Ensure all observers were notified of the event
49 for (int index = 0; index < kObservers; ++index)
50 EXPECT_EQ(observers[index].suspends(), 1);
51
52 // Send a second suspend notification. This should be suppressed.
53 source()->GenerateSuspendEvent();
54 EXPECT_EQ(observers[0].suspends(), 1);
55
56 // Pretend we were awakened.
57 source()->GenerateResumeEvent();
58 EXPECT_EQ(observers[0].resumes(), 1);
59
60 // Send a duplicate resume notification. This should be suppressed.
61 source()->GenerateResumeEvent();
62 EXPECT_EQ(observers[0].resumes(), 1);
63
64 // Pretend the device has gone on battery power
65 source()->GeneratePowerStateEvent(true);
66 EXPECT_EQ(observers[0].power_state_changes(), 1);
67 EXPECT_EQ(observers[0].last_power_state(), true);
68
69 // Repeated indications the device is on battery power should be suppressed.
70 source()->GeneratePowerStateEvent(true);
71 EXPECT_EQ(observers[0].power_state_changes(), 1);
72
73 // Pretend the device has gone off battery power
74 source()->GeneratePowerStateEvent(false);
75 EXPECT_EQ(observers[0].power_state_changes(), 2);
76 EXPECT_EQ(observers[0].last_power_state(), false);
77
78 // Repeated indications the device is off battery power should be suppressed.
79 source()->GeneratePowerStateEvent(false);
80 EXPECT_EQ(observers[0].power_state_changes(), 2);
81 }
82
83 } // namespace base
84