| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/system_monitor.h" | 5 #include "base/system_monitor/system_monitor.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 7 |
| 8 namespace base { | 8 namespace base { |
| 9 | 9 |
| 10 class PowerTest : public SystemMonitor::PowerObserver { | 10 class PowerTest : public SystemMonitor::PowerObserver { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // Initialize a message loop for this to run on. | 48 // Initialize a message loop for this to run on. |
| 49 MessageLoop loop; | 49 MessageLoop loop; |
| 50 | 50 |
| 51 #if defined(OS_MACOSX) | 51 #if defined(OS_MACOSX) |
| 52 SystemMonitor::AllocateSystemIOPorts(); | 52 SystemMonitor::AllocateSystemIOPorts(); |
| 53 #endif | 53 #endif |
| 54 | 54 |
| 55 SystemMonitor system_monitor; | 55 SystemMonitor system_monitor; |
| 56 PowerTest test[kObservers]; | 56 PowerTest test[kObservers]; |
| 57 for (int index = 0; index < kObservers; ++index) | 57 for (int index = 0; index < kObservers; ++index) |
| 58 system_monitor.AddObserver(&test[index]); | 58 system_monitor.AddPowerObserver(&test[index]); |
| 59 | 59 |
| 60 // Send a bunch of power changes. Since the battery power hasn't | 60 // Send a bunch of power changes. Since the battery power hasn't |
| 61 // actually changed, we shouldn't get notifications. | 61 // actually changed, we shouldn't get notifications. |
| 62 for (int index = 0; index < 5; index++) { | 62 for (int index = 0; index < 5; index++) { |
| 63 system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 63 system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
| 64 EXPECT_EQ(test[0].power_state_changes(), 0); | 64 EXPECT_EQ(test[0].power_state_changes(), 0); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Sending resume when not suspended should have no effect. | 67 // Sending resume when not suspended should have no effect. |
| 68 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 68 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 83 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
| 84 loop.RunAllPending(); | 84 loop.RunAllPending(); |
| 85 EXPECT_EQ(test[0].resumes(), 1); | 85 EXPECT_EQ(test[0].resumes(), 1); |
| 86 | 86 |
| 87 // Send a duplicate resume notification. This should be suppressed. | 87 // Send a duplicate resume notification. This should be suppressed. |
| 88 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 88 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
| 89 loop.RunAllPending(); | 89 loop.RunAllPending(); |
| 90 EXPECT_EQ(test[0].resumes(), 1); | 90 EXPECT_EQ(test[0].resumes(), 1); |
| 91 } | 91 } |
| 92 | 92 |
| 93 class DevicesChangedTest : public SystemMonitor::DevicesChangedObserver { |
| 94 public: |
| 95 DevicesChangedTest() |
| 96 : changes_(0) { |
| 97 } |
| 98 |
| 99 // DevicesChangedObserver callbacks. |
| 100 virtual void OnDevicesChanged() OVERRIDE { |
| 101 changes_++; |
| 102 } |
| 103 |
| 104 // Test status counts. |
| 105 int changes() const { return changes_; } |
| 106 |
| 107 private: |
| 108 int changes_; // Count of OnDevicesChanged notifications. |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(DevicesChangedTest); |
| 111 }; |
| 112 |
| 113 TEST(SystemMonitor, DeviceChangeNotifications) { |
| 114 const int kObservers = 5; |
| 115 |
| 116 // Initialize a message loop for this to run on. |
| 117 MessageLoop loop; |
| 118 |
| 119 #if defined(OS_MACOSX) |
| 120 SystemMonitor::AllocateSystemIOPorts(); |
| 121 #endif |
| 122 |
| 123 SystemMonitor system_monitor; |
| 124 DevicesChangedTest test[kObservers]; |
| 125 for (int index = 0; index < kObservers; ++index) |
| 126 system_monitor.AddDevicesChangedObserver(&test[index]); |
| 127 |
| 128 system_monitor.ProcessDevicesChanged(); |
| 129 loop.RunAllPending(); |
| 130 EXPECT_EQ(1, test[0].changes()); |
| 131 |
| 132 system_monitor.ProcessDevicesChanged(); |
| 133 system_monitor.ProcessDevicesChanged(); |
| 134 loop.RunAllPending(); |
| 135 EXPECT_EQ(3, test[0].changes()); |
| 136 } |
| 137 |
| 93 } // namespace base | 138 } // namespace base |
| OLD | NEW |