Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/power_monitor/power_monitor.h" | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace base { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class PowerTest : public PowerMonitor::PowerObserver { | |
| 15 public: | |
| 16 PowerTest() | |
| 17 : power_state_changes_(0), | |
| 18 suspends_(0), | |
| 19 resumes_(0) { | |
| 20 } | |
| 21 | |
| 22 // PowerObserver callbacks. | |
| 23 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { | |
| 24 power_state_changes_++; | |
| 25 } | |
| 26 | |
| 27 virtual void OnSuspend() OVERRIDE { | |
| 28 suspends_++; | |
| 29 } | |
| 30 | |
| 31 virtual void OnResume() OVERRIDE { | |
| 32 resumes_++; | |
| 33 } | |
| 34 | |
| 35 // Test status counts. | |
| 36 int power_state_changes() { return power_state_changes_; } | |
| 37 int suspends() { return suspends_; } | |
| 38 int resumes() { return resumes_; } | |
| 39 | |
| 40 private: | |
| 41 int power_state_changes_; // Count of OnPowerStateChange notifications. | |
| 42 int suspends_; // Count of OnSuspend notifications. | |
| 43 int resumes_; // Count of OnResume notifications. | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 class PowerMonitorTest : public testing::Test { | |
| 49 protected: | |
| 50 PowerMonitorTest() { | |
| 51 #if defined(OS_MACOSX) | |
| 52 // This needs to happen before PowerMonitor's ctor. | |
| 53 PowerMonitor::AllocateSystemIOPorts(); | |
| 54 #endif | |
| 55 power_monitor_ = new PowerMonitor; | |
| 56 // power_monitor_.reset(new PowerMonitor); | |
| 57 } | |
| 58 virtual ~PowerMonitorTest() {} | |
| 59 | |
| 60 MessageLoop message_loop_; | |
| 61 PowerMonitor* power_monitor_; | |
| 62 // scoped_ptr<PowerMonitor> power_monitor_; | |
|
vandebo (ex-Chrome)
2012/10/11 00:58:43
Go back to the scoped_ptr ?
Hongbo Min
2012/10/11 07:57:53
Instead create the PowerMonitor on the stack and m
| |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | |
| 65 }; | |
| 66 | |
| 67 TEST_F(PowerMonitorTest, PowerNotifications) { | |
| 68 const int kObservers = 5; | |
| 69 | |
| 70 PowerTest test[kObservers]; | |
| 71 for (int index = 0; index < kObservers; ++index) | |
| 72 power_monitor_->AddPowerObserver(&test[index]); | |
| 73 | |
| 74 // Send a bunch of power changes. Since the battery power hasn't | |
| 75 // actually changed, we shouldn't get notifications. | |
| 76 for (int index = 0; index < 5; index++) { | |
| 77 power_monitor_->HandlePowerEvent(PowerMonitor::POWER_STATE_EVENT); | |
| 78 EXPECT_EQ(test[0].power_state_changes(), 0); | |
| 79 } | |
| 80 | |
| 81 // Sending resume when not suspended should have no effect. | |
| 82 power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT); | |
| 83 message_loop_.RunAllPending(); | |
| 84 EXPECT_EQ(test[0].resumes(), 0); | |
| 85 | |
| 86 // Pretend we suspended. | |
| 87 power_monitor_->HandlePowerEvent(PowerMonitor::SUSPEND_EVENT); | |
| 88 message_loop_.RunAllPending(); | |
| 89 EXPECT_EQ(test[0].suspends(), 1); | |
| 90 | |
| 91 // Send a second suspend notification. This should be suppressed. | |
| 92 power_monitor_->HandlePowerEvent(PowerMonitor::SUSPEND_EVENT); | |
| 93 message_loop_.RunAllPending(); | |
| 94 EXPECT_EQ(test[0].suspends(), 1); | |
| 95 | |
| 96 // Pretend we were awakened. | |
| 97 power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT); | |
| 98 message_loop_.RunAllPending(); | |
| 99 EXPECT_EQ(test[0].resumes(), 1); | |
| 100 | |
| 101 // Send a duplicate resume notification. This should be suppressed. | |
| 102 power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT); | |
| 103 message_loop_.RunAllPending(); | |
| 104 EXPECT_EQ(test[0].resumes(), 1); | |
| 105 } | |
| 106 | |
| 107 } // namespace base | |
| OLD | NEW |