| 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( |
| 49 PowerMonitor::AllocateSystemIOPorts(); | 15 scoped_ptr<PowerMonitorSource>(power_monitor_source_))); |
| 50 #endif | |
| 51 power_monitor_.reset(new PowerMonitor); | |
| 52 } | 16 } |
| 17 virtual ~PowerMonitorTest() {}; |
| 53 | 18 |
| 54 void ProcessPowerEvent(PowerMonitor::PowerEvent event_id) { | 19 PowerMonitorTestSource* source() { return power_monitor_source_; } |
| 55 power_monitor_->ProcessPowerEvent(event_id); | 20 PowerMonitor* monitor() { return power_monitor_.get(); } |
| 56 } | |
| 57 | 21 |
| 58 virtual ~PowerMonitorTest() {} | 22 private: |
| 59 | 23 PowerMonitorTestSource* power_monitor_source_; |
| 60 MessageLoop message_loop_; | |
| 61 scoped_ptr<PowerMonitor> power_monitor_; | 24 scoped_ptr<PowerMonitor> power_monitor_; |
| 62 | 25 |
| 63 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | 26 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
| 64 }; | 27 }; |
| 65 | 28 |
| 29 // PowerMonitorSource is tightly coupled with the PowerMonitor, so this test |
| 30 // Will cover both classes |
| 66 TEST_F(PowerMonitorTest, PowerNotifications) { | 31 TEST_F(PowerMonitorTest, PowerNotifications) { |
| 67 const int kObservers = 5; | 32 const int kObservers = 5; |
| 68 | 33 |
| 69 PowerTest test[kObservers]; | 34 PowerMonitorTestObserver observers[kObservers]; |
| 70 for (int index = 0; index < kObservers; ++index) | 35 for (int index = 0; index < kObservers; ++index) |
| 71 power_monitor_->AddObserver(&test[index]); | 36 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 | 37 |
| 80 // Sending resume when not suspended should have no effect. | 38 // Sending resume when not suspended should have no effect. |
| 81 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 39 source()->GenerateResumeEvent(); |
| 82 message_loop_.RunUntilIdle(); | 40 EXPECT_EQ(observers[0].resumes(), 0); |
| 83 EXPECT_EQ(test[0].resumes(), 0); | |
| 84 | 41 |
| 85 // Pretend we suspended. | 42 // Pretend we suspended. |
| 86 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 43 source()->GenerateSuspendEvent(); |
| 87 message_loop_.RunUntilIdle(); | 44 // Ensure all observers were notified of the event |
| 88 EXPECT_EQ(test[0].suspends(), 1); | 45 for (int index = 0; index < kObservers; ++index) |
| 46 EXPECT_EQ(observers[index].suspends(), 1); |
| 89 | 47 |
| 90 // Send a second suspend notification. This should be suppressed. | 48 // Send a second suspend notification. This should be suppressed. |
| 91 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); | 49 source()->GenerateSuspendEvent(); |
| 92 message_loop_.RunUntilIdle(); | 50 EXPECT_EQ(observers[0].suspends(), 1); |
| 93 EXPECT_EQ(test[0].suspends(), 1); | |
| 94 | 51 |
| 95 // Pretend we were awakened. | 52 // Pretend we were awakened. |
| 96 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 53 source()->GenerateResumeEvent(); |
| 97 message_loop_.RunUntilIdle(); | 54 EXPECT_EQ(observers[0].resumes(), 1); |
| 98 EXPECT_EQ(test[0].resumes(), 1); | |
| 99 | 55 |
| 100 // Send a duplicate resume notification. This should be suppressed. | 56 // Send a duplicate resume notification. This should be suppressed. |
| 101 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); | 57 source()->GenerateResumeEvent(); |
| 102 message_loop_.RunUntilIdle(); | 58 EXPECT_EQ(observers[0].resumes(), 1); |
| 103 EXPECT_EQ(test[0].resumes(), 1); | 59 |
| 60 // Pretend the device has gone on battery power |
| 61 source()->GeneratePowerStateEvent(true); |
| 62 EXPECT_EQ(observers[0].power_state_changes(), 1); |
| 63 EXPECT_EQ(observers[0].last_power_state(), true); |
| 64 |
| 65 // Repeated indications the device is on battery power should be suppressed. |
| 66 source()->GeneratePowerStateEvent(true); |
| 67 EXPECT_EQ(observers[0].power_state_changes(), 1); |
| 68 |
| 69 // Pretend the device has gone off battery power |
| 70 source()->GeneratePowerStateEvent(false); |
| 71 EXPECT_EQ(observers[0].power_state_changes(), 2); |
| 72 EXPECT_EQ(observers[0].last_power_state(), false); |
| 73 |
| 74 // Repeated indications the device is off battery power should be suppressed. |
| 75 source()->GeneratePowerStateEvent(false); |
| 76 EXPECT_EQ(observers[0].power_state_changes(), 2); |
| 104 } | 77 } |
| 105 | 78 |
| 106 } // namespace base | 79 } // namespace base |
| OLD | NEW |