| 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 14 matching lines...) Expand all Loading... |
| 25 int resumes() { return resumes_; } | 25 int resumes() { return resumes_; } |
| 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 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | 36 base::SystemMonitor* monitor = base::SystemMonitor::Get(); |
| 36 PowerTest test; | 37 PowerTest test[kObservers]; |
| 37 monitor->AddObserver(&test); | 38 for (int index = 0; index < kObservers; ++index) |
| 39 monitor->AddObserver(&test[index]); |
| 38 | 40 |
| 39 // Send a bunch of power changes. Since the battery power hasn't | 41 // Send a bunch of power changes. Since the battery power hasn't |
| 40 // actually changed, we shouldn't get notifications. | 42 // actually changed, we shouldn't get notifications. |
| 41 for (int index = 0; index < 5; index++) { | 43 for (int index = 0; index < 5; index++) { |
| 42 monitor->ProcessPowerMessage(base::SystemMonitor::PowerStateEvent); | 44 monitor->ProcessPowerMessage(base::SystemMonitor::POWER_STATE_EVENT); |
| 43 EXPECT_EQ(test.power_state_changes(), 0); | 45 EXPECT_EQ(test[0].power_state_changes(), 0); |
| 44 } | 46 } |
| 45 | 47 |
| 46 // Sending resume when not suspended should have no effect. | 48 // Sending resume when not suspended should have no effect. |
| 47 monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent); | 49 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 48 EXPECT_EQ(test.resumes(), 0); | 50 EXPECT_EQ(test[0].resumes(), 0); |
| 49 | 51 |
| 50 // Pretend we suspended. | 52 // Pretend we suspended. |
| 51 monitor->ProcessPowerMessage(base::SystemMonitor::SuspendEvent); | 53 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); |
| 52 EXPECT_EQ(test.suspends(), 1); | 54 EXPECT_EQ(test[0].suspends(), 1); |
| 53 | 55 |
| 54 // Send a second suspend notification. This should be suppressed. | 56 // Send a second suspend notification. This should be suppressed. |
| 55 monitor->ProcessPowerMessage(base::SystemMonitor::SuspendEvent); | 57 monitor->ProcessPowerMessage(base::SystemMonitor::SUSPEND_EVENT); |
| 56 EXPECT_EQ(test.suspends(), 1); | 58 EXPECT_EQ(test[0].suspends(), 1); |
| 57 | 59 |
| 58 // Pretend we were awakened. | 60 // Pretend we were awakened. |
| 59 monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent); | 61 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 60 EXPECT_EQ(test.resumes(), 1); | 62 EXPECT_EQ(test[0].resumes(), 1); |
| 61 | 63 |
| 62 // Send a duplicate resume notification. This should be suppressed. | 64 // Send a duplicate resume notification. This should be suppressed. |
| 63 monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent); | 65 monitor->ProcessPowerMessage(base::SystemMonitor::RESUME_EVENT); |
| 64 EXPECT_EQ(test.resumes(), 1); | 66 EXPECT_EQ(test[0].resumes(), 1); |
| 65 } | 67 } |
| 66 | 68 |
| OLD | NEW |