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); |
58 } | 52 } |
59 virtual ~SystemMonitorTest() {} | 53 virtual ~PowerMonitorTest() {} |
60 | 54 |
61 MessageLoop message_loop_; | 55 MessageLoop message_loop_; |
62 scoped_ptr<SystemMonitor> system_monitor_; | 56 scoped_ptr<PowerMonitor> power_monitor_; |
63 | 57 |
64 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); | 58 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
65 }; | 59 }; |
66 | 60 |
67 TEST_F(SystemMonitorTest, PowerNotifications) { | 61 TEST_F(PowerMonitorTest, PowerNotifications) { |
68 const int kObservers = 5; | 62 const int kObservers = 5; |
63 scoped_ptr<PowerMonitor::Signaler> power_signaler; | |
vandebo (ex-Chrome)
2012/10/31 17:30:34
nit: you can put this in PowerMonitorTest and init
Hongbo Min
2012/11/01 09:26:24
Done.
| |
64 | |
65 power_signaler.reset(PowerMonitor::Get()->GetSignalerOnce()); | |
69 | 66 |
70 PowerTest test[kObservers]; | 67 PowerTest test[kObservers]; |
71 for (int index = 0; index < kObservers; ++index) | 68 for (int index = 0; index < kObservers; ++index) |
72 system_monitor_->AddPowerObserver(&test[index]); | 69 power_monitor_->AddObserver(&test[index]); |
73 | 70 |
74 // Send a bunch of power changes. Since the battery power hasn't | 71 // Send a bunch of power changes. Since the battery power hasn't |
75 // actually changed, we shouldn't get notifications. | 72 // actually changed, we shouldn't get notifications. |
76 for (int index = 0; index < 5; index++) { | 73 for (int index = 0; index < 5; index++) { |
77 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 74 power_signaler->ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT); |
78 EXPECT_EQ(test[0].power_state_changes(), 0); | 75 EXPECT_EQ(test[0].power_state_changes(), 0); |
79 } | 76 } |
80 | 77 |
81 // Sending resume when not suspended should have no effect. | 78 // Sending resume when not suspended should have no effect. |
82 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 79 power_signaler->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
83 message_loop_.RunUntilIdle(); | 80 message_loop_.RunUntilIdle(); |
84 EXPECT_EQ(test[0].resumes(), 0); | 81 EXPECT_EQ(test[0].resumes(), 0); |
85 | 82 |
86 // Pretend we suspended. | 83 // Pretend we suspended. |
87 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 84 power_signaler->ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
88 message_loop_.RunUntilIdle(); | 85 message_loop_.RunUntilIdle(); |
89 EXPECT_EQ(test[0].suspends(), 1); | 86 EXPECT_EQ(test[0].suspends(), 1); |
90 | 87 |
91 // Send a second suspend notification. This should be suppressed. | 88 // Send a second suspend notification. This should be suppressed. |
92 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 89 power_signaler->ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT); |
93 message_loop_.RunUntilIdle(); | 90 message_loop_.RunUntilIdle(); |
94 EXPECT_EQ(test[0].suspends(), 1); | 91 EXPECT_EQ(test[0].suspends(), 1); |
95 | 92 |
96 // Pretend we were awakened. | 93 // Pretend we were awakened. |
97 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 94 power_signaler->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
98 message_loop_.RunUntilIdle(); | 95 message_loop_.RunUntilIdle(); |
99 EXPECT_EQ(test[0].resumes(), 1); | 96 EXPECT_EQ(test[0].resumes(), 1); |
100 | 97 |
101 // Send a duplicate resume notification. This should be suppressed. | 98 // Send a duplicate resume notification. This should be suppressed. |
102 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 99 power_signaler->ProcessPowerEvent(PowerMonitor::RESUME_EVENT); |
103 message_loop_.RunUntilIdle(); | 100 message_loop_.RunUntilIdle(); |
104 EXPECT_EQ(test[0].resumes(), 1); | 101 EXPECT_EQ(test[0].resumes(), 1); |
105 } | 102 } |
106 | 103 |
107 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { | 104 TEST_F(PowerMonitorTest, GetSignalerOnce) { |
108 const int kObservers = 5; | 105 scoped_ptr<PowerMonitor::Signaler> power_signaler; |
109 const string16 kDeviceName = ASCIIToUTF16("media device"); | 106 power_signaler.reset(PowerMonitor::Get()->GetSignalerOnce()); |
110 const std::string kDeviceId1 = "1"; | 107 EXPECT_TRUE(NULL != power_signaler.get()); |
111 const std::string kDeviceId2 = "2"; | |
112 | 108 |
113 testing::Sequence mock_sequencer[kObservers]; | 109 // The second time calling GetSignalerOnce should return NULL. |
114 MockDevicesChangedObserver observers[kObservers]; | 110 PowerMonitor::Signaler* signaler = power_monitor_->GetSignalerOnce(); |
115 for (int index = 0; index < kObservers; ++index) { | 111 EXPECT_TRUE(NULL == signaler); |
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 } | 112 } |
148 | 113 |
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 | 114 } // namespace base |
OLD | NEW |