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