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.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/power_monitor/power_monitor_device_source.h" |
| 9 #include "base/power_monitor/power_monitor_source.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 class PowerTest : public PowerObserver { | 14 class PowerTest : public PowerObserver { |
13 public: | 15 public: |
14 PowerTest() | 16 PowerTest() |
15 : power_state_changes_(0), | 17 : power_state_changes_(0), |
16 suspends_(0), | 18 suspends_(0), |
17 resumes_(0) { | 19 resumes_(0) { |
(...skipping 21 matching lines...) Expand all Loading... |
39 int power_state_changes_; // Count of OnPowerStateChange notifications. | 41 int power_state_changes_; // Count of OnPowerStateChange notifications. |
40 int suspends_; // Count of OnSuspend notifications. | 42 int suspends_; // Count of OnSuspend notifications. |
41 int resumes_; // Count of OnResume notifications. | 43 int resumes_; // Count of OnResume notifications. |
42 }; | 44 }; |
43 | 45 |
44 class PowerMonitorTest : public testing::Test { | 46 class PowerMonitorTest : public testing::Test { |
45 protected: | 47 protected: |
46 PowerMonitorTest() { | 48 PowerMonitorTest() { |
47 #if defined(OS_MACOSX) | 49 #if defined(OS_MACOSX) |
48 // This needs to happen before PowerMonitor's ctor. | 50 // This needs to happen before PowerMonitor's ctor. |
49 PowerMonitor::AllocateSystemIOPorts(); | 51 PowerMonitorDeviceSource::AllocateSystemIOPorts(); |
50 #endif | 52 #endif |
51 power_monitor_.reset(new PowerMonitor); | 53 power_monitor_.reset(new PowerMonitor); |
52 } | 54 } |
53 | 55 |
54 void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) { | 56 void ProcessPowerEvent(PowerMonitorSource::PowerEvent event_id) { |
55 power_monitor_->ProcessPowerEvent(event_id); | 57 PowerMonitorSource::ProcessPowerEvent(event_id); |
56 } | 58 } |
57 | 59 |
58 virtual ~PowerMonitorTest() {} | 60 virtual ~PowerMonitorTest() {} |
59 | 61 |
60 MessageLoop message_loop_; | 62 MessageLoop message_loop_; |
61 scoped_ptr<PowerMonitor> power_monitor_; | 63 scoped_ptr<PowerMonitor> power_monitor_; |
62 | 64 |
63 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | 65 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
64 }; | 66 }; |
65 | 67 |
66 TEST_F(PowerMonitorTest, PowerNotifications) { | 68 TEST_F(PowerMonitorTest, PowerNotifications) { |
67 const int kObservers = 5; | 69 const int kObservers = 5; |
68 | 70 |
69 PowerTest test[kObservers]; | 71 PowerTest test[kObservers]; |
70 for (int index = 0; index < kObservers; ++index) | 72 for (int index = 0; index < kObservers; ++index) |
71 power_monitor_->AddObserver(&test[index]); | 73 power_monitor_->AddObserver(&test[index]); |
72 | 74 |
73 // Send a bunch of power changes. Since the battery power hasn't | 75 // Send a bunch of power changes. Since the battery power hasn't |
74 // actually changed, we shouldn't get notifications. | 76 // actually changed, we shouldn't get notifications. |
75 for (int index = 0; index < 5; index++) { | 77 for (int index = 0; index < 5; index++) { |
76 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); | 78 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); |
77 EXPECT_EQ(test[0].power_state_changes(), 0); | 79 EXPECT_EQ(test[0].power_state_changes(), 0); |
78 } | 80 } |
79 | 81 |
80 // Sending resume when not suspended should have no effect. | 82 // Sending resume when not suspended should have no effect. |
81 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 83 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
82 message_loop_.RunUntilIdle(); | 84 message_loop_.RunUntilIdle(); |
83 EXPECT_EQ(test[0].resumes(), 0); | 85 EXPECT_EQ(test[0].resumes(), 0); |
84 | 86 |
85 // Pretend we suspended. | 87 // Pretend we suspended. |
86 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 88 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
87 message_loop_.RunUntilIdle(); | 89 message_loop_.RunUntilIdle(); |
88 EXPECT_EQ(test[0].suspends(), 1); | 90 EXPECT_EQ(test[0].suspends(), 1); |
89 | 91 |
90 // Send a second suspend notification. This should be suppressed. | 92 // Send a second suspend notification. This should be suppressed. |
91 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 93 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
92 message_loop_.RunUntilIdle(); | 94 message_loop_.RunUntilIdle(); |
93 EXPECT_EQ(test[0].suspends(), 1); | 95 EXPECT_EQ(test[0].suspends(), 1); |
94 | 96 |
95 // Pretend we were awakened. | 97 // Pretend we were awakened. |
96 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 98 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
97 message_loop_.RunUntilIdle(); | 99 message_loop_.RunUntilIdle(); |
98 EXPECT_EQ(test[0].resumes(), 1); | 100 EXPECT_EQ(test[0].resumes(), 1); |
99 | 101 |
100 // Send a duplicate resume notification. This should be suppressed. | 102 // Send a duplicate resume notification. This should be suppressed. |
101 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 103 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
102 message_loop_.RunUntilIdle(); | 104 message_loop_.RunUntilIdle(); |
103 EXPECT_EQ(test[0].resumes(), 1); | 105 EXPECT_EQ(test[0].resumes(), 1); |
104 } | 106 } |
105 | 107 |
106 } // namespace base | 108 } // namespace base |
OLD | NEW |