Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(358)

Side by Side Diff: base/system_monitor/system_monitor_unittest.cc

Issue 10917166: Extend the capability of SystemMonitor to support watching storage free space change (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update and add unit test Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/system_monitor/system_monitor.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/test/mock_devices_changed_observer.h" 9 #include "base/test/mock_devices_changed_observer.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 29 matching lines...) Expand all
40 int power_state_changes() { return power_state_changes_; } 40 int power_state_changes() { return power_state_changes_; }
41 int suspends() { return suspends_; } 41 int suspends() { return suspends_; }
42 int resumes() { return resumes_; } 42 int resumes() { return resumes_; }
43 43
44 private: 44 private:
45 int power_state_changes_; // Count of OnPowerStateChange notifications. 45 int power_state_changes_; // Count of OnPowerStateChange notifications.
46 int suspends_; // Count of OnSuspend notifications. 46 int suspends_; // Count of OnSuspend notifications.
47 int resumes_; // Count of OnResume notifications. 47 int resumes_; // Count of OnResume notifications.
48 }; 48 };
49 49
50 class MockStorageFreeSpaceDelegateTest
51 : public SystemMonitor::StorageFreeSpaceDelegate {
52 public:
53 MockStorageFreeSpaceDelegateTest() {}
54 virtual ~MockStorageFreeSpaceDelegateTest() {}
55 MOCK_METHOD1(StartWatchingStorage, void(const FilePath& path));
56 MOCK_METHOD1(StopWatchingStorage, void(const FilePath& path));
57 DISALLOW_COPY_AND_ASSIGN(MockStorageFreeSpaceDelegateTest);
58 };
59
60 class MockStorageFreeSpaceObserver
61 : public SystemMonitor::StorageFreeSpaceObserver {
62 public:
63 MockStorageFreeSpaceObserver() {}
64 virtual ~MockStorageFreeSpaceObserver() {}
65 MOCK_METHOD2(OnFreeSpaceChanged,
66 void(const FilePath& path, int64 available_bytes));
67 MOCK_METHOD1(OnFreeSpaceChangeError,
68 void(const FilePath& path));
69 DISALLOW_COPY_AND_ASSIGN(MockStorageFreeSpaceObserver);
70 };
71
50 class SystemMonitorTest : public testing::Test { 72 class SystemMonitorTest : public testing::Test {
51 protected: 73 protected:
52 SystemMonitorTest() { 74 SystemMonitorTest() {
53 #if defined(OS_MACOSX) 75 #if defined(OS_MACOSX)
54 // This needs to happen before SystemMonitor's ctor. 76 // This needs to happen before SystemMonitor's ctor.
55 SystemMonitor::AllocateSystemIOPorts(); 77 SystemMonitor::AllocateSystemIOPorts();
56 #endif 78 #endif
57 system_monitor_.reset(new SystemMonitor); 79 system_monitor_.reset(new SystemMonitor(&delegate_test_));
58 } 80 }
59 virtual ~SystemMonitorTest() {} 81 virtual ~SystemMonitorTest() {}
60 82
61 MessageLoop message_loop_; 83 MessageLoop message_loop_;
62 scoped_ptr<SystemMonitor> system_monitor_; 84 scoped_ptr<SystemMonitor> system_monitor_;
85 MockStorageFreeSpaceDelegateTest delegate_test_;
63 86
64 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); 87 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest);
65 }; 88 };
66 89
67 TEST_F(SystemMonitorTest, PowerNotifications) { 90 TEST_F(SystemMonitorTest, PowerNotifications) {
68 const int kObservers = 5; 91 const int kObservers = 5;
69 92
70 PowerTest test[kObservers]; 93 PowerTest test[kObservers];
71 for (int index = 0; index < kObservers; ++index) 94 for (int index = 0; index < kObservers; ++index)
72 system_monitor_->AddPowerObserver(&test[index]); 95 system_monitor_->AddPowerObserver(&test[index]);
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 EXPECT_EQ(kDeviceId2, devices[0].device_id); 213 EXPECT_EQ(kDeviceId2, devices[0].device_id);
191 EXPECT_EQ(kDeviceName2, devices[0].name); 214 EXPECT_EQ(kDeviceName2, devices[0].name);
192 EXPECT_EQ(kDevicePath2.value(), devices[0].location); 215 EXPECT_EQ(kDevicePath2.value(), devices[0].location);
193 216
194 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); 217 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2);
195 message_loop_.RunAllPending(); 218 message_loop_.RunAllPending();
196 devices = system_monitor_->GetAttachedRemovableStorage(); 219 devices = system_monitor_->GetAttachedRemovableStorage();
197 EXPECT_EQ(0U, devices.size()); 220 EXPECT_EQ(0U, devices.size());
198 } 221 }
199 222
223 TEST_F(SystemMonitorTest, StorageFreeSpaceChangedNotification) {
224 const int kObservers = 3;
225 const FilePath kFilePath(FILE_PATH_LITERAL("one"));
226 const FilePath kInvalidFilePath(FILE_PATH_LITERAL("invalid_path"));
227
228 testing::Sequence mock_sequencer[kObservers], seq;
229 MockStorageFreeSpaceObserver observers[kObservers];
230
231 EXPECT_CALL(delegate_test_,
232 StartWatchingStorage(kFilePath))
233 .Times(1).InSequence(seq);
234 EXPECT_CALL(delegate_test_,
235 StartWatchingStorage(kInvalidFilePath))
236 .Times(1).InSequence(seq);
237 EXPECT_CALL(delegate_test_,
238 StopWatchingStorage(kFilePath))
239 .Times(1).InSequence(seq);
240 EXPECT_CALL(delegate_test_,
241 StopWatchingStorage(kInvalidFilePath))
242 .Times(0).InSequence(seq);
243
244 for (int i = 0; i < kObservers - 1; ++i) {
245 EXPECT_CALL(observers[i],
246 OnFreeSpaceChanged(kFilePath, 1000))
247 .Times(1).InSequence(mock_sequencer[i]);
248 EXPECT_CALL(observers[i],
249 OnFreeSpaceChanged(kFilePath, 2000))
250 .Times(1).InSequence(mock_sequencer[i]);
251 EXPECT_CALL(observers[i],
252 OnFreeSpaceChangeError(kFilePath))
253 .Times(0).InSequence(mock_sequencer[i]);
254 }
255 EXPECT_CALL(observers[kObservers - 1],
256 OnFreeSpaceChangeError(kInvalidFilePath))
257 .Times(1).InSequence(mock_sequencer[kObservers - 1]);
258 EXPECT_CALL(observers[0],
259 OnFreeSpaceChanged(kFilePath, 3000))
260 .Times(1).InSequence(mock_sequencer[kObservers - 1]);
261 EXPECT_CALL(observers[1],
262 OnFreeSpaceChanged(kFilePath, 3000))
263 .Times(0).InSequence(mock_sequencer[kObservers - 1]);
264
265 system_monitor_->AddStorageFreeSpaceObserver(kFilePath, &observers[0]);
266 system_monitor_->AddStorageFreeSpaceObserver(kFilePath, &observers[1]);
267
268 system_monitor_->ProcessStorageFreeSpaceChanged(kFilePath, 1000);
269 system_monitor_->ProcessStorageFreeSpaceChanged(kFilePath, 2000);
270 message_loop_.RunAllPending();
271
272 system_monitor_->AddStorageFreeSpaceObserver(kInvalidFilePath, &observers[2]);
273 system_monitor_->ProcessStorageFreeSpaceChangeError(kInvalidFilePath);
274 message_loop_.RunAllPending();
275
276 system_monitor_->RemoveStorageFreeSpaceObserver(kFilePath, &observers[1]);
277 system_monitor_->ProcessStorageFreeSpaceChanged(kFilePath, 3000);
278 message_loop_.RunAllPending();
279
280 system_monitor_->RemoveStorageFreeSpaceObserver(kFilePath, &observers[0]);
281 message_loop_.RunAllPending();
282 }
283
200 } // namespace 284 } // namespace
201 285
202 } // namespace base 286 } // namespace base
OLDNEW
« no previous file with comments | « base/system_monitor/system_monitor.cc ('k') | chrome/browser/system_monitor/removable_device_notifications_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698