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