| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 "base/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 base::SystemMonitor::PowerObserver { |
| 9 public: | 9 public: |
| 10 PowerTest() | 10 PowerTest() |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 private: | 27 private: |
| 28 bool battery_; // Do we currently think we're on battery power. | 28 bool battery_; // Do we currently think we're on battery power. |
| 29 int power_state_changes_; // Count of OnPowerStateChange notifications. | 29 int power_state_changes_; // Count of OnPowerStateChange notifications. |
| 30 int suspends_; // Count of OnSuspend notifications. | 30 int suspends_; // Count of OnSuspend notifications. |
| 31 int resumes_; // Count of OnResume notifications. | 31 int resumes_; // Count of OnResume notifications. |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 TEST(SystemMonitor, PowerNotifications) { | 34 TEST(SystemMonitor, PowerNotifications) { |
| 35 const int kObservers = 5; | 35 const int kObservers = 5; |
| 36 |
| 37 // Initialize a message loop for this to run on. |
| 38 MessageLoop loop; |
| 39 // Initialize time() since it registers as a SystemMonitor observer. |
| 40 base::Time now = base::Time::Now(); |
| 41 |
| 36 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | 42 base::SystemMonitor* monitor = base::SystemMonitor::Get(); |
| 37 PowerTest test[kObservers]; | 43 PowerTest test[kObservers]; |
| 38 for (int index = 0; index < kObservers; ++index) | 44 for (int index = 0; index < kObservers; ++index) |
| 39 monitor->AddObserver(&test[index]); | 45 monitor->AddObserver(&test[index]); |
| 40 | 46 |
| 41 // Send a bunch of power changes. Since the battery power hasn't | 47 // Send a bunch of power changes. Since the battery power hasn't |
| 42 // actually changed, we shouldn't get notifications. | 48 // actually changed, we shouldn't get notifications. |
| 43 for (int index = 0; index < 5; index++) { | 49 for (int index = 0; index < 5; index++) { |
| 44 monitor->ProcessPowerMessage(base::SystemMonitor::POWER_STATE_EVENT); | 50 monitor->ProcessPowerMessage(base::SystemMonitor::POWER_STATE_EVENT); |
| 45 EXPECT_EQ(test[0].power_state_changes(), 0); | 51 EXPECT_EQ(test[0].power_state_changes(), 0); |
| 46 } | 52 } |
| 47 | 53 |
| 48 // Sending resume when not suspended should have no effect. | 54 // Sending resume when not suspended should have no effect. |
| 49 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); | 55 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 56 loop.RunAllPending(); |
| 50 EXPECT_EQ(test[0].resumes(), 0); | 57 EXPECT_EQ(test[0].resumes(), 0); |
| 51 | 58 |
| 52 // Pretend we suspended. | 59 // Pretend we suspended. |
| 53 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); | 60 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); |
| 61 loop.RunAllPending(); |
| 54 EXPECT_EQ(test[0].suspends(), 1); | 62 EXPECT_EQ(test[0].suspends(), 1); |
| 55 | 63 |
| 56 // Send a second suspend notification. This should be suppressed. | 64 // Send a second suspend notification. This should be suppressed. |
| 57 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); | 65 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); |
| 66 loop.RunAllPending(); |
| 58 EXPECT_EQ(test[0].suspends(), 1); | 67 EXPECT_EQ(test[0].suspends(), 1); |
| 59 | 68 |
| 60 // Pretend we were awakened. | 69 // Pretend we were awakened. |
| 61 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); | 70 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 71 loop.RunAllPending(); |
| 62 EXPECT_EQ(test[0].resumes(), 1); | 72 EXPECT_EQ(test[0].resumes(), 1); |
| 63 | 73 |
| 64 // Send a duplicate resume notification. This should be suppressed. | 74 // Send a duplicate resume notification. This should be suppressed. |
| 65 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); | 75 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 76 loop.RunAllPending(); |
| 66 EXPECT_EQ(test[0].resumes(), 1); | 77 EXPECT_EQ(test[0].resumes(), 1); |
| 67 } | 78 } |
| 68 | 79 |
| OLD | NEW |