OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/power_monitor/power_monitor.h" | 5 #include "base/power_monitor/power_monitor_test_base.h" |
6 | |
7 #include "base/message_loop.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
9 | 7 |
10 namespace base { | 8 namespace base { |
11 | 9 |
12 class PowerTest : public PowerObserver { | |
13 public: | |
14 PowerTest() | |
15 : power_state_changes_(0), | |
16 suspends_(0), | |
17 resumes_(0) { | |
18 } | |
19 | |
20 // PowerObserver callbacks. | |
21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { | |
22 power_state_changes_++; | |
23 } | |
24 | |
25 virtual void OnSuspend() OVERRIDE { | |
26 suspends_++; | |
27 } | |
28 | |
29 virtual void OnResume() OVERRIDE { | |
30 resumes_++; | |
31 } | |
32 | |
33 // Test status counts. | |
34 int power_state_changes() { return power_state_changes_; } | |
35 int suspends() { return suspends_; } | |
36 int resumes() { return resumes_; } | |
37 | |
38 private: | |
39 int power_state_changes_; // Count of OnPowerStateChange notifications. | |
40 int suspends_; // Count of OnSuspend notifications. | |
41 int resumes_; // Count of OnResume notifications. | |
42 }; | |
43 | |
44 class PowerMonitorTest : public testing::Test { | 10 class PowerMonitorTest : public testing::Test { |
45 protected: | 11 protected: |
46 PowerMonitorTest() { | 12 PowerMonitorTest() { |
47 #if defined(OS_MACOSX) | 13 power_monitor_source_ = new PowerMonitorTestSource(); |
48 // This needs to happen before PowerMonitor's ctor. | 14 power_monitor_.reset(new PowerMonitor(power_monitor_source_)); |
49 PowerMonitor::AllocateSystemIOPorts(); | |
50 #endif | |
51 power_monitor_.reset(new PowerMonitor); | |
52 } | 15 } |
| 16 virtual ~PowerMonitorTest() {}; |
53 | 17 |
54 void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) { | 18 PowerMonitorTestSource* source() { return power_monitor_source_; } |
55 power_monitor_->ProcessPowerEvent(event_id); | 19 PowerMonitor* monitor() { return power_monitor_.get(); } |
56 } | |
57 | 20 |
58 virtual ~PowerMonitorTest() {} | 21 private: |
59 | 22 PowerMonitorTestSource* power_monitor_source_; |
60 MessageLoop message_loop_; | |
61 scoped_ptr<PowerMonitor> power_monitor_; | 23 scoped_ptr<PowerMonitor> power_monitor_; |
62 | 24 |
63 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | 25 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
64 }; | 26 }; |
65 | 27 |
| 28 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test |
| 29 // Will cover both classes |
66 TEST_F(PowerMonitorTest, PowerNotifications) { | 30 TEST_F(PowerMonitorTest, PowerNotifications) { |
67 const int kObservers = 5; | 31 const int kObservers = 5; |
68 | 32 |
69 PowerTest test[kObservers]; | 33 PowerMonitorTestObserver observers[kObservers]; |
70 for (int index = 0; index < kObservers; ++index) | 34 for (int index = 0; index < kObservers; ++index) |
71 power_monitor_->AddObserver(&test[index]); | 35 monitor()->AddObserver(&observers[index]); |
72 | |
73 // Send a bunch of power changes. Since the battery power hasn't | |
74 // actually changed, we shouldn't get notifications. | |
75 for (int index = 0; index < 5; index++) { | |
76 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | |
77 EXPECT_EQ(test[0].power_state_changes(), 0); | |
78 } | |
79 | 36 |
80 // Sending resume when not suspended should have no effect. | 37 // Sending resume when not suspended should have no effect. |
81 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 38 source()->GenerateResumeEvent(); |
82 message_loop_.RunUntilIdle(); | 39 EXPECT_EQ(observers[0].resumes(), 0); |
83 EXPECT_EQ(test[0].resumes(), 0); | |
84 | 40 |
85 // Pretend we suspended. | 41 // Pretend we suspended. |
86 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 42 source()->GenerateSuspendEvent(); |
87 message_loop_.RunUntilIdle(); | 43 // Ensure all observers were notified of the event |
88 EXPECT_EQ(test[0].suspends(), 1); | 44 for (int index = 0; index < kObservers; ++index) |
| 45 EXPECT_EQ(observers[index].suspends(), 1); |
89 | 46 |
90 // Send a second suspend notification. This should be suppressed. | 47 // Send a second suspend notification. This should be suppressed. |
91 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 48 source()->GenerateSuspendEvent(); |
92 message_loop_.RunUntilIdle(); | 49 EXPECT_EQ(observers[0].suspends(), 1); |
93 EXPECT_EQ(test[0].suspends(), 1); | |
94 | 50 |
95 // Pretend we were awakened. | 51 // Pretend we were awakened. |
96 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 52 source()->GenerateResumeEvent(); |
97 message_loop_.RunUntilIdle(); | 53 EXPECT_EQ(observers[0].resumes(), 1); |
98 EXPECT_EQ(test[0].resumes(), 1); | |
99 | 54 |
100 // Send a duplicate resume notification. This should be suppressed. | 55 // Send a duplicate resume notification. This should be suppressed. |
101 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 56 source()->GenerateResumeEvent(); |
102 message_loop_.RunUntilIdle(); | 57 EXPECT_EQ(observers[0].resumes(), 1); |
103 EXPECT_EQ(test[0].resumes(), 1); | 58 |
| 59 // Pretend the device has gone on battery power |
| 60 source()->GeneratePowerStateEvent(true); |
| 61 EXPECT_EQ(observers[0].power_state_changes(), 1); |
| 62 EXPECT_EQ(observers[0].last_power_state(), true); |
| 63 |
| 64 // Repeated indications the device is on battery power should be suppressed. |
| 65 source()->GeneratePowerStateEvent(true); |
| 66 EXPECT_EQ(observers[0].power_state_changes(), 1); |
| 67 |
| 68 // Pretend the device has gone off battery power |
| 69 source()->GeneratePowerStateEvent(false); |
| 70 EXPECT_EQ(observers[0].power_state_changes(), 2); |
| 71 EXPECT_EQ(observers[0].last_power_state(), false); |
| 72 |
| 73 // Repeated indications the device is off battery power should be suppressed. |
| 74 source()->GeneratePowerStateEvent(false); |
| 75 EXPECT_EQ(observers[0].power_state_changes(), 2); |
104 } | 76 } |
105 | 77 |
106 } // namespace base | 78 } // namespace base |
OLD | NEW |