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 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 { |
| 45 protected: |
| 46 PowerMonitorTest() { |
| 47 #if defined(OS_MACOSX) |
| 48 // This needs to happen before PowerMonitor's ctor. |
| 49 PowerMonitor::AllocateSystemIOPorts(); |
| 50 #endif |
| 51 power_monitor_.reset(new PowerMonitor); |
| 52 } |
| 53 virtual ~PowerMonitorTest() {} |
| 54 |
| 55 void ProcessPowerEvent(PowerMonitor::PowerEvent event) { |
| 56 power_monitor_->ProcessPowerEvent(event); |
| 57 } |
| 58 |
| 59 MessageLoop message_loop_; |
| 60 scoped_ptr<PowerMonitor> power_monitor_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
| 63 }; |
| 64 |
| 65 TEST_F(PowerMonitorTest, PowerNotifications) { |
| 66 const int kObservers = 5; |
| 67 |
| 68 PowerTest test[kObservers]; |
| 69 for (int index = 0; index < kObservers; ++index) |
| 70 power_monitor_->AddObserver(&test[index]); |
| 71 |
| 72 // Send a bunch of power changes. Since the battery power hasn't |
| 73 // actually changed, we shouldn't get notifications. |
| 74 for (int index = 0; index < 5; index++) { |
| 75 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 76 EXPECT_EQ(test[0].power_state_changes(), 0); |
| 77 } |
| 78 |
| 79 // Sending resume when not suspended should have no effect. |
| 80 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 81 message_loop_.RunAllPending(); |
| 82 EXPECT_EQ(test[0].resumes(), 0); |
| 83 |
| 84 // Pretend we suspended. |
| 85 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
| 86 message_loop_.RunAllPending(); |
| 87 EXPECT_EQ(test[0].suspends(), 1); |
| 88 |
| 89 // Send a second suspend notification. This should be suppressed. |
| 90 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
| 91 message_loop_.RunAllPending(); |
| 92 EXPECT_EQ(test[0].suspends(), 1); |
| 93 |
| 94 // Pretend we were awakened. |
| 95 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 96 message_loop_.RunAllPending(); |
| 97 EXPECT_EQ(test[0].resumes(), 1); |
| 98 |
| 99 // Send a duplicate resume notification. This should be suppressed. |
| 100 ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 101 message_loop_.RunAllPending(); |
| 102 EXPECT_EQ(test[0].resumes(), 1); |
| 103 } |
| 104 |
| 105 } // namespace base |
OLD | NEW |