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