Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/power_monitor/power_monitor.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | |
| 8 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 9 #include "base/test/mock_devices_changed_observer.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 9 |
| 14 namespace base { | 10 namespace base { |
| 15 | 11 |
| 16 namespace { | 12 class PowerTest : public PowerObserver { |
| 17 | |
| 18 class PowerTest : public SystemMonitor::PowerObserver { | |
| 19 public: | 13 public: |
| 20 PowerTest() | 14 PowerTest() |
| 21 : power_state_changes_(0), | 15 : power_state_changes_(0), |
| 22 suspends_(0), | 16 suspends_(0), |
| 23 resumes_(0) { | 17 resumes_(0) { |
| 24 } | 18 } |
| 25 | 19 |
| 26 // PowerObserver callbacks. | 20 // PowerObserver callbacks. |
| 27 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { | 21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { |
| 28 power_state_changes_++; | 22 power_state_changes_++; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 40 int power_state_changes() { return power_state_changes_; } | 34 int power_state_changes() { return power_state_changes_; } |
| 41 int suspends() { return suspends_; } | 35 int suspends() { return suspends_; } |
| 42 int resumes() { return resumes_; } | 36 int resumes() { return resumes_; } |
| 43 | 37 |
| 44 private: | 38 private: |
| 45 int power_state_changes_; // Count of OnPowerStateChange notifications. | 39 int power_state_changes_; // Count of OnPowerStateChange notifications. |
| 46 int suspends_; // Count of OnSuspend notifications. | 40 int suspends_; // Count of OnSuspend notifications. |
| 47 int resumes_; // Count of OnResume notifications. | 41 int resumes_; // Count of OnResume notifications. |
| 48 }; | 42 }; |
| 49 | 43 |
| 50 class SystemMonitorTest : public testing::Test { | 44 class PowerMonitorTest : public testing::Test { |
| 51 protected: | 45 protected: |
| 52 SystemMonitorTest() { | 46 PowerMonitorTest() { |
| 53 #if defined(OS_MACOSX) | 47 #if defined(OS_MACOSX) |
| 54 // This needs to happen before SystemMonitor's ctor. | 48 // This needs to happen before PowerMonitor's ctor. |
| 55 SystemMonitor::AllocateSystemIOPorts(); | 49 PowerMonitor::AllocateSystemIOPorts(); |
| 56 #endif | 50 #endif |
| 57 system_monitor_.reset(new SystemMonitor); | 51 power_monitor_.reset(new PowerMonitor); |
| 52 power_signaler_.reset(PowerMonitor::Get()->GetSignalerOnce()); | |
|
vandebo (ex-Chrome)
2012/11/01 16:50:26
Platform init is called in the PowerMonitor constr
Hongbo Min
2012/11/02 00:29:07
Done.
| |
| 58 } | 53 } |
| 59 virtual ~SystemMonitorTest() {} | 54 virtual ~PowerMonitorTest() {} |
| 55 | |
| 56 void MakeSignalerAvailable() { | |
| 57 PowerMonitor* monitor = power_monitor_.get(); | |
| 58 monitor->signaler_available_ = false; | |
|
vandebo (ex-Chrome)
2012/11/01 16:50:26
combine lines: power_monitor->signaler_available_
Hongbo Min
2012/11/02 00:29:07
Done.
| |
| 59 } | |
| 60 | 60 |
| 61 MessageLoop message_loop_; | 61 MessageLoop message_loop_; |
| 62 scoped_ptr<SystemMonitor> system_monitor_; | 62 scoped_ptr<PowerMonitor> power_monitor_; |
| 63 scoped_ptr<PowerMonitor::Signaler> power_signaler_; | |
| 63 | 64 |
| 64 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); | 65 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 TEST_F(SystemMonitorTest, PowerNotifications) { | 68 TEST_F(PowerMonitorTest, PowerNotifications) { |
| 68 const int kObservers = 5; | 69 const int kObservers = 5; |
| 69 | 70 |
| 70 PowerTest test[kObservers]; | 71 PowerTest test[kObservers]; |
| 71 for (int index = 0; index < kObservers; ++index) | 72 for (int index = 0; index < kObservers; ++index) |
| 72 system_monitor_->AddPowerObserver(&test[index]); | 73 power_monitor_->AddObserver(&test[index]); |
| 73 | 74 |
| 74 // Send a bunch of power changes. Since the battery power hasn't | 75 // Send a bunch of power changes. Since the battery power hasn't |
| 75 // actually changed, we shouldn't get notifications. | 76 // actually changed, we shouldn't get notifications. |
| 76 for (int index = 0; index < 5; index++) { | 77 for (int index = 0; index < 5; index++) { |
| 77 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 78 power_signaler_->ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
| 78 EXPECT_EQ(test[0].power_state_changes(), 0); | 79 EXPECT_EQ(test[0].power_state_changes(), 0); |
| 79 } | 80 } |
| 80 | 81 |
| 81 // Sending resume when not suspended should have no effect. | 82 // Sending resume when not suspended should have no effect. |
| 82 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 83 power_signaler_->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 83 message_loop_.RunUntilIdle(); | 84 message_loop_.RunUntilIdle(); |
| 84 EXPECT_EQ(test[0].resumes(), 0); | 85 EXPECT_EQ(test[0].resumes(), 0); |
| 85 | 86 |
| 86 // Pretend we suspended. | 87 // Pretend we suspended. |
| 87 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 88 power_signaler_->ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
| 88 message_loop_.RunUntilIdle(); | 89 message_loop_.RunUntilIdle(); |
| 89 EXPECT_EQ(test[0].suspends(), 1); | 90 EXPECT_EQ(test[0].suspends(), 1); |
| 90 | 91 |
| 91 // Send a second suspend notification. This should be suppressed. | 92 // Send a second suspend notification. This should be suppressed. |
| 92 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 93 power_signaler_->ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
| 93 message_loop_.RunUntilIdle(); | 94 message_loop_.RunUntilIdle(); |
| 94 EXPECT_EQ(test[0].suspends(), 1); | 95 EXPECT_EQ(test[0].suspends(), 1); |
| 95 | 96 |
| 96 // Pretend we were awakened. | 97 // Pretend we were awakened. |
| 97 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 98 power_signaler_->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 98 message_loop_.RunUntilIdle(); | 99 message_loop_.RunUntilIdle(); |
| 99 EXPECT_EQ(test[0].resumes(), 1); | 100 EXPECT_EQ(test[0].resumes(), 1); |
| 100 | 101 |
| 101 // Send a duplicate resume notification. This should be suppressed. | 102 // Send a duplicate resume notification. This should be suppressed. |
| 102 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 103 power_signaler_->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
| 103 message_loop_.RunUntilIdle(); | 104 message_loop_.RunUntilIdle(); |
| 104 EXPECT_EQ(test[0].resumes(), 1); | 105 EXPECT_EQ(test[0].resumes(), 1); |
| 105 } | 106 } |
| 106 | 107 |
| 107 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { | 108 TEST_F(PowerMonitorTest, GetSignalerOnce) { |
| 108 const int kObservers = 5; | 109 MakeSignalerAvailable(); |
| 109 const string16 kDeviceName = ASCIIToUTF16("media device"); | 110 EXPECT_TRUE(NULL != power_monitor_->GetSignalerOnce()); |
|
vandebo (ex-Chrome)
2012/11/01 16:50:26
This leaks the signaler. You need the scoped_ptr l
Hongbo Min
2012/11/02 00:29:07
Done.
| |
| 110 const std::string kDeviceId1 = "1"; | 111 // The second time calling GetSignalerOnce should return NULL. |
| 111 const std::string kDeviceId2 = "2"; | 112 EXPECT_TRUE(NULL == power_monitor_->GetSignalerOnce()); |
| 112 | |
| 113 testing::Sequence mock_sequencer[kObservers]; | |
| 114 MockDevicesChangedObserver observers[kObservers]; | |
| 115 for (int index = 0; index < kObservers; ++index) { | |
| 116 system_monitor_->AddDevicesChangedObserver(&observers[index]); | |
| 117 | |
| 118 EXPECT_CALL(observers[index], | |
| 119 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN)) | |
| 120 .Times(3) | |
| 121 .InSequence(mock_sequencer[index]); | |
| 122 EXPECT_CALL(observers[index], OnRemovableStorageAttached(kDeviceId1, | |
| 123 kDeviceName, | |
| 124 testing::_)) | |
| 125 .InSequence(mock_sequencer[index]); | |
| 126 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId1)) | |
| 127 .InSequence(mock_sequencer[index]); | |
| 128 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId2)) | |
| 129 .Times(0).InSequence(mock_sequencer[index]); | |
| 130 } | |
| 131 | |
| 132 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
| 133 message_loop_.RunUntilIdle(); | |
| 134 | |
| 135 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
| 136 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | |
| 137 message_loop_.RunUntilIdle(); | |
| 138 | |
| 139 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, | |
| 140 kDeviceName, | |
| 141 FILE_PATH_LITERAL("path")); | |
| 142 message_loop_.RunUntilIdle(); | |
| 143 | |
| 144 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); | |
| 145 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); | |
| 146 message_loop_.RunUntilIdle(); | |
| 147 } | 113 } |
| 148 | 114 |
| 149 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageEmpty) { | |
| 150 std::vector<SystemMonitor::RemovableStorageInfo> devices = | |
| 151 system_monitor_->GetAttachedRemovableStorage(); | |
| 152 EXPECT_EQ(0U, devices.size()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageAttachDetach) { | |
| 156 const std::string kDeviceId1 = "42"; | |
| 157 const string16 kDeviceName1 = ASCIIToUTF16("test"); | |
| 158 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | |
| 159 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, | |
| 160 kDeviceName1, | |
| 161 kDevicePath1.value()); | |
| 162 message_loop_.RunUntilIdle(); | |
| 163 std::vector<SystemMonitor::RemovableStorageInfo> devices = | |
| 164 system_monitor_->GetAttachedRemovableStorage(); | |
| 165 ASSERT_EQ(1U, devices.size()); | |
| 166 EXPECT_EQ(kDeviceId1, devices[0].device_id); | |
| 167 EXPECT_EQ(kDeviceName1, devices[0].name); | |
| 168 EXPECT_EQ(kDevicePath1.value(), devices[0].location); | |
| 169 | |
| 170 const std::string kDeviceId2 = "44"; | |
| 171 const string16 kDeviceName2 = ASCIIToUTF16("test2"); | |
| 172 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); | |
| 173 system_monitor_->ProcessRemovableStorageAttached(kDeviceId2, | |
| 174 kDeviceName2, | |
| 175 kDevicePath2.value()); | |
| 176 message_loop_.RunUntilIdle(); | |
| 177 devices = system_monitor_->GetAttachedRemovableStorage(); | |
| 178 ASSERT_EQ(2U, devices.size()); | |
| 179 EXPECT_EQ(kDeviceId1, devices[0].device_id); | |
| 180 EXPECT_EQ(kDeviceName1, devices[0].name); | |
| 181 EXPECT_EQ(kDevicePath1.value(), devices[0].location); | |
| 182 EXPECT_EQ(kDeviceId2, devices[1].device_id); | |
| 183 EXPECT_EQ(kDeviceName2, devices[1].name); | |
| 184 EXPECT_EQ(kDevicePath2.value(), devices[1].location); | |
| 185 | |
| 186 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); | |
| 187 message_loop_.RunUntilIdle(); | |
| 188 devices = system_monitor_->GetAttachedRemovableStorage(); | |
| 189 ASSERT_EQ(1U, devices.size()); | |
| 190 EXPECT_EQ(kDeviceId2, devices[0].device_id); | |
| 191 EXPECT_EQ(kDeviceName2, devices[0].name); | |
| 192 EXPECT_EQ(kDevicePath2.value(), devices[0].location); | |
| 193 | |
| 194 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); | |
| 195 message_loop_.RunUntilIdle(); | |
| 196 devices = system_monitor_->GetAttachedRemovableStorage(); | |
| 197 EXPECT_EQ(0U, devices.size()); | |
| 198 } | |
| 199 | |
| 200 } // namespace | |
| 201 | |
| 202 } // namespace base | 115 } // namespace base |
| OLD | NEW |