| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/system_monitor/system_monitor.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 | |
| 8 namespace base { | |
| 9 | |
| 10 class PowerTest : public SystemMonitor::PowerObserver { | |
| 11 public: | |
| 12 PowerTest() | |
| 13 : battery_(false), | |
| 14 power_state_changes_(0), | |
| 15 suspends_(0), | |
| 16 resumes_(0) { | |
| 17 } | |
| 18 | |
| 19 // PowerObserver callbacks. | |
| 20 void OnPowerStateChange(bool on_battery_power) { | |
| 21 power_state_changes_++; | |
| 22 } | |
| 23 | |
| 24 void OnSuspend() { | |
| 25 suspends_++; | |
| 26 } | |
| 27 | |
| 28 void OnResume() { | |
| 29 resumes_++; | |
| 30 } | |
| 31 | |
| 32 // Test status counts. | |
| 33 bool battery() { return battery_; } | |
| 34 int power_state_changes() { return power_state_changes_; } | |
| 35 int suspends() { return suspends_; } | |
| 36 int resumes() { return resumes_; } | |
| 37 | |
| 38 private: | |
| 39 bool battery_; // Do we currently think we're on battery power. | |
| 40 int power_state_changes_; // Count of OnPowerStateChange notifications. | |
| 41 int suspends_; // Count of OnSuspend notifications. | |
| 42 int resumes_; // Count of OnResume notifications. | |
| 43 }; | |
| 44 | |
| 45 TEST(SystemMonitor, PowerNotifications) { | |
| 46 const int kObservers = 5; | |
| 47 | |
| 48 // Initialize a message loop for this to run on. | |
| 49 MessageLoop loop; | |
| 50 // Initialize time() since it registers as a SystemMonitor observer. | |
| 51 base::Time now = base::Time::Now(); | |
| 52 | |
| 53 SystemMonitor system_monitor; | |
| 54 PowerTest test[kObservers]; | |
| 55 for (int index = 0; index < kObservers; ++index) | |
| 56 system_monitor.AddObserver(&test[index]); | |
| 57 | |
| 58 // Send a bunch of power changes. Since the battery power hasn't | |
| 59 // actually changed, we shouldn't get notifications. | |
| 60 for (int index = 0; index < 5; index++) { | |
| 61 system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | |
| 62 EXPECT_EQ(test[0].power_state_changes(), 0); | |
| 63 } | |
| 64 | |
| 65 // Sending resume when not suspended should have no effect. | |
| 66 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
| 67 loop.RunAllPending(); | |
| 68 EXPECT_EQ(test[0].resumes(), 0); | |
| 69 | |
| 70 // Pretend we suspended. | |
| 71 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | |
| 72 loop.RunAllPending(); | |
| 73 EXPECT_EQ(test[0].suspends(), 1); | |
| 74 | |
| 75 // Send a second suspend notification. This should be suppressed. | |
| 76 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | |
| 77 loop.RunAllPending(); | |
| 78 EXPECT_EQ(test[0].suspends(), 1); | |
| 79 | |
| 80 // Pretend we were awakened. | |
| 81 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
| 82 loop.RunAllPending(); | |
| 83 EXPECT_EQ(test[0].resumes(), 1); | |
| 84 | |
| 85 // Send a duplicate resume notification. This should be suppressed. | |
| 86 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | |
| 87 loop.RunAllPending(); | |
| 88 EXPECT_EQ(test[0].resumes(), 1); | |
| 89 } | |
| 90 | |
| 91 } // namespace base | |
| OLD | NEW |