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

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/volume_manager_unittest.cc

Issue 23890002: Extract OnDiskEvent and OnFormatEvent logic part from EventRouter to VolumeManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/extensions/file_manager/volume_manager.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/chromeos/extensions/file_manager/volume_manager_observe r.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "chromeos/disks/disk_mount_manager.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace file_manager {
20 namespace {
21
22 // Fake implementation of DiskMountManager.
23 class FakeDiskMountManager : public chromeos::disks::DiskMountManager {
24 public:
25 struct MountRequest {
26 MountRequest(const std::string& source_path,
27 const std::string& source_format,
28 const std::string& mount_label,
29 chromeos::MountType type)
30 : source_path(source_path),
31 source_format(source_format),
32 mount_label(mount_label),
33 type(type) {
34 }
35
36 std::string source_path;
37 std::string source_format;
38 std::string mount_label;
39 chromeos::MountType type;
40 };
41
42 struct UnmountRequest {
43 UnmountRequest(const std::string& mount_path,
44 chromeos::UnmountOptions options)
45 : mount_path(mount_path),
46 options(options) {
47 }
48
49 std::string mount_path;
50 chromeos::UnmountOptions options;
51 };
52
53 FakeDiskMountManager() {}
54 virtual ~FakeDiskMountManager() {}
55
56 const std::vector<MountRequest>& mount_requests() const {
57 return mount_requests_;
58 }
59 const std::vector<UnmountRequest>& unmount_requests() const {
60 return unmount_requests_;
61 }
62
63 // DiskMountManager overrides.
64 virtual void AddObserver(Observer* observer) OVERRIDE {}
65 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
66 virtual const DiskMap& disks() const OVERRIDE { return disks_; }
67 virtual const Disk* FindDiskBySourcePath(
68 const std::string& source_path) const OVERRIDE {
69 return NULL;
70 }
71 virtual const MountPointMap& mount_points() const OVERRIDE {
72 return mount_points_;
73 }
74 virtual void RequestMountInfoRefresh() OVERRIDE {}
75 virtual void MountPath(const std::string& source_path,
76 const std::string& source_format,
77 const std::string& mount_label,
78 chromeos::MountType type) OVERRIDE {
79 mount_requests_.push_back(
80 MountRequest(source_path, source_format, mount_label, type));
81 }
82 virtual void UnmountPath(const std::string& mount_path,
83 chromeos::UnmountOptions options,
84 const UnmountPathCallback& callback) OVERRIDE {
85 unmount_requests_.push_back(UnmountRequest(mount_path, options));
86 // Currently |callback| is just ignored.
87 }
88 virtual void FormatMountedDevice(const std::string& mount_path) OVERRIDE {}
89 virtual void UnmountDeviceRecursively(
90 const std::string& device_path,
91 const UnmountDeviceRecursivelyCallbackType& callback) OVERRIDE {}
92
93 virtual bool AddDiskForTest(Disk* disk) OVERRIDE { return false; }
94 virtual bool AddMountPointForTest(
95 const MountPointInfo& mount_point) OVERRIDE {
96 return false;
97 }
98
99 private:
100 DiskMap disks_;
101 MountPointMap mount_points_;
102
103 std::vector<MountRequest> mount_requests_;
104 std::vector<UnmountRequest> unmount_requests_;
105
106 DISALLOW_COPY_AND_ASSIGN(FakeDiskMountManager);
107 };
108
109 class LoggingObserver : public VolumeManagerObserver {
110 public:
111 struct Event {
112 enum EventType {
113 DISK_ADDED,
114 DISK_REMOVED,
115 DEVICE_ADDED,
116 DEVICE_REMOVED,
117 FORMAT_STARTED,
118 FORMAT_COMPLETED,
119 } type;
120
121 // Available on DEVICE_ADDED, DEVICE_REMOVED, FORMAT_STARTED and
122 // FORMAT_COMPLETED.
123 std::string device_path;
124
125 // Available on DISK_ADDED.
126 bool mounting;
127
128 // Available on FORMAT_STARTED and FORMAT_COMPLETED.
129 bool success;
130 };
131
132 LoggingObserver() {}
133 virtual ~LoggingObserver() {}
134
135 const std::vector<Event>& events() const { return events_; }
136
137 // VolumeManagerObserver overrides.
138 virtual void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk& disk,
139 bool mounting) OVERRIDE {
140 Event event;
141 event.type = Event::DISK_ADDED;
142 event.device_path = disk.device_path(); // Keep only device_path.
143 event.mounting = mounting;
144 events_.push_back(event);
145 }
146
147 virtual void OnDiskRemoved(
148 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE {
149 Event event;
150 event.type = Event::DISK_REMOVED;
151 event.device_path = disk.device_path(); // Keep only device_path.
152 events_.push_back(event);
153 }
154
155 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE {
156 Event event;
157 event.type = Event::DEVICE_ADDED;
158 event.device_path = device_path;
159 events_.push_back(event);
160 }
161
162 virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE {
163 Event event;
164 event.type = Event::DEVICE_REMOVED;
165 event.device_path = device_path;
166 events_.push_back(event);
167 }
168
169 virtual void OnFormatStarted(
170 const std::string& device_path, bool success) OVERRIDE {
171 Event event;
172 event.type = Event::FORMAT_STARTED;
173 event.device_path = device_path;
174 event.success = success;
175 events_.push_back(event);
176 }
177
178 virtual void OnFormatCompleted(
179 const std::string& device_path, bool success) OVERRIDE {
180 Event event;
181 event.type = Event::FORMAT_COMPLETED;
182 event.device_path = device_path;
183 event.success = success;
184 events_.push_back(event);
185 }
186
187 private:
188 std::vector<Event> events_;
189
190 DISALLOW_COPY_AND_ASSIGN(LoggingObserver);
191 };
192
193 } // namespace
194
195 class VolumeManagerTest : public testing::Test {
196 protected:
197 VolumeManagerTest() {
198 }
199 virtual ~VolumeManagerTest() {
200 }
201
202 virtual void SetUp() OVERRIDE {
203 disk_mount_manager_.reset(new FakeDiskMountManager);
204 profile_.reset(new TestingProfile);
205 volume_manager_.reset(
206 new VolumeManager(profile_.get(), disk_mount_manager_.get()));
207 }
208
209 content::TestBrowserThreadBundle thread_bundle_;
210 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
211 scoped_ptr<TestingProfile> profile_;
212 scoped_ptr<VolumeManager> volume_manager_;
213 };
214
215 TEST_F(VolumeManagerTest, OnDiskEvent_Hidden) {
216 LoggingObserver observer;
217 volume_manager_->AddObserver(&observer);
218
219 const bool kIsHidden = true;
220 const chromeos::disks::DiskMountManager::Disk kDisk(
221 "device1", "", "", "", "", "", "", "", "", "", "", "",
222 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, kIsHidden);
223
224 volume_manager_->OnDiskEvent(
225 chromeos::disks::DiskMountManager::DISK_ADDED, &kDisk);
226 EXPECT_EQ(0U, observer.events().size());
227
228 volume_manager_->OnDiskEvent(
229 chromeos::disks::DiskMountManager::DISK_REMOVED, &kDisk);
230 EXPECT_EQ(0U, observer.events().size());
231
232 volume_manager_->OnDiskEvent(
233 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
234 EXPECT_EQ(0U, observer.events().size());
235
236 volume_manager_->RemoveObserver(&observer);
237 }
238
239 TEST_F(VolumeManagerTest, OnDiskEvent_Added) {
240 // Enable external storage.
241 profile_->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
242
243 LoggingObserver observer;
244 volume_manager_->AddObserver(&observer);
245
246 const chromeos::disks::DiskMountManager::Disk kEmptyDevicePathDisk(
247 "", // empty device path.
248 "", "", "", "", "", "", "", "", "", "", "",
249 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
250 volume_manager_->OnDiskEvent(
251 chromeos::disks::DiskMountManager::DISK_ADDED, &kEmptyDevicePathDisk);
252 EXPECT_EQ(0U, observer.events().size());
253
254 const bool kHasMedia = true;
255 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
256 "device1", "", "", "", "", "", "", "", "", "", "", "",
257 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, kHasMedia, false, false);
258 volume_manager_->OnDiskEvent(
259 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
260 ASSERT_EQ(1U, observer.events().size());
261 const LoggingObserver::Event& event = observer.events()[0];
262 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
263 EXPECT_EQ("device1", event.device_path);
264 EXPECT_TRUE(event.mounting);
265
266 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
267 const FakeDiskMountManager::MountRequest& mount_request =
268 disk_mount_manager_->mount_requests()[0];
269 EXPECT_EQ("device1", mount_request.source_path);
270 EXPECT_EQ("", mount_request.source_format);
271 EXPECT_EQ("", mount_request.mount_label);
272 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
273
274 volume_manager_->RemoveObserver(&observer);
275 }
276
277 TEST_F(VolumeManagerTest, OnDiskEvent_AddedNonMounting) {
278 // Enable external storage.
279 profile_->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
280
281 // Device which is already mounted.
282 {
283 LoggingObserver observer;
284 volume_manager_->AddObserver(&observer);
285
286 const bool kHasMedia = true;
287 const chromeos::disks::DiskMountManager::Disk kMountedMediaDisk(
288 "device1", "mounted", "", "", "", "", "", "", "", "", "", "",
289 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
290 kHasMedia, false, false);
291 volume_manager_->OnDiskEvent(
292 chromeos::disks::DiskMountManager::DISK_ADDED, &kMountedMediaDisk);
293 ASSERT_EQ(1U, observer.events().size());
294 const LoggingObserver::Event& event = observer.events()[0];
295 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
296 EXPECT_EQ("device1", event.device_path);
297 EXPECT_FALSE(event.mounting);
298
299 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
300
301 volume_manager_->RemoveObserver(&observer);
302 }
303
304 // Device without media.
305 {
306 LoggingObserver observer;
307 volume_manager_->AddObserver(&observer);
308
309 const bool kWithoutMedia = false;
310 const chromeos::disks::DiskMountManager::Disk kNoMediaDisk(
311 "device1", "", "", "", "", "", "", "", "", "", "", "",
312 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
313 kWithoutMedia, false, false);
314 volume_manager_->OnDiskEvent(
315 chromeos::disks::DiskMountManager::DISK_ADDED, &kNoMediaDisk);
316 ASSERT_EQ(1U, observer.events().size());
317 const LoggingObserver::Event& event = observer.events()[0];
318 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
319 EXPECT_EQ("device1", event.device_path);
320 EXPECT_FALSE(event.mounting);
321
322 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
323
324 volume_manager_->RemoveObserver(&observer);
325 }
326
327 // External storage is disabled.
328 {
329 profile_->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
330
331 LoggingObserver observer;
332 volume_manager_->AddObserver(&observer);
333
334 const bool kHasMedia = true;
335 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
336 "device1", "", "", "", "", "", "", "", "", "", "", "",
337 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
338 kHasMedia, false, false);
339 volume_manager_->OnDiskEvent(
340 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
341 ASSERT_EQ(1U, observer.events().size());
342 const LoggingObserver::Event& event = observer.events()[0];
343 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
344 EXPECT_EQ("device1", event.device_path);
345 EXPECT_FALSE(event.mounting);
346
347 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
348
349 volume_manager_->RemoveObserver(&observer);
350 }
351 }
352
353 TEST_F(VolumeManagerTest, OnDiskEvent_Removed) {
354 LoggingObserver observer;
355 volume_manager_->AddObserver(&observer);
356
357 const chromeos::disks::DiskMountManager::Disk kMountedDisk(
358 "device1", "mount_path", "", "", "", "", "", "", "", "", "", "",
359 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
360 volume_manager_->OnDiskEvent(
361 chromeos::disks::DiskMountManager::DISK_REMOVED, &kMountedDisk);
362
363 ASSERT_EQ(1U, observer.events().size());
364 const LoggingObserver::Event& event = observer.events()[0];
365 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
366 EXPECT_EQ("device1", event.device_path);
367
368 ASSERT_EQ(1U, disk_mount_manager_->unmount_requests().size());
369 const FakeDiskMountManager::UnmountRequest& unmount_request =
370 disk_mount_manager_->unmount_requests()[0];
371 EXPECT_EQ("mount_path", unmount_request.mount_path);
372 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_LAZY, unmount_request.options);
373
374 volume_manager_->RemoveObserver(&observer);
375 }
376
377 TEST_F(VolumeManagerTest, OnDiskEvent_RemovedNotMounted) {
378 LoggingObserver observer;
379 volume_manager_->AddObserver(&observer);
380
381 const chromeos::disks::DiskMountManager::Disk kNotMountedDisk(
382 "device1", "", "", "", "", "", "", "", "", "", "", "",
383 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
384 volume_manager_->OnDiskEvent(
385 chromeos::disks::DiskMountManager::DISK_REMOVED, &kNotMountedDisk);
386
387 ASSERT_EQ(1U, observer.events().size());
388 const LoggingObserver::Event& event = observer.events()[0];
389 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
390 EXPECT_EQ("device1", event.device_path);
391
392 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
393
394 volume_manager_->RemoveObserver(&observer);
395 }
396
397 TEST_F(VolumeManagerTest, OnDiskEvent_Changed) {
398 // Changed event is just ignored.
399 LoggingObserver observer;
400 volume_manager_->AddObserver(&observer);
401
402 const chromeos::disks::DiskMountManager::Disk kDisk(
403 "device1", "", "", "", "", "", "", "", "", "", "", "",
404 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
405 volume_manager_->OnDiskEvent(
406 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
407
408 EXPECT_EQ(0U, observer.events().size());
409 EXPECT_EQ(0U, disk_mount_manager_->mount_requests().size());
410 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
411
412 volume_manager_->RemoveObserver(&observer);
413 }
414
415 TEST_F(VolumeManagerTest, OnDeviceEvent_Added) {
416 LoggingObserver observer;
417 volume_manager_->AddObserver(&observer);
418
419 volume_manager_->OnDeviceEvent(
420 chromeos::disks::DiskMountManager::DEVICE_ADDED, "device1");
421
422 ASSERT_EQ(1U, observer.events().size());
423 const LoggingObserver::Event& event = observer.events()[0];
424 EXPECT_EQ(LoggingObserver::Event::DEVICE_ADDED, event.type);
425 EXPECT_EQ("device1", event.device_path);
426
427 volume_manager_->RemoveObserver(&observer);
428 }
429
430 TEST_F(VolumeManagerTest, OnDeviceEvent_Removed) {
431 LoggingObserver observer;
432 volume_manager_->AddObserver(&observer);
433
434 volume_manager_->OnDeviceEvent(
435 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
436
437 ASSERT_EQ(1U, observer.events().size());
438 const LoggingObserver::Event& event = observer.events()[0];
439 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, event.type);
440 EXPECT_EQ("device1", event.device_path);
441
442 volume_manager_->RemoveObserver(&observer);
443 }
444
445 TEST_F(VolumeManagerTest, OnDeviceEvent_Scanned) {
446 LoggingObserver observer;
447 volume_manager_->AddObserver(&observer);
448
449 volume_manager_->OnDeviceEvent(
450 chromeos::disks::DiskMountManager::DEVICE_SCANNED, "device1");
451
452 // SCANNED event is just ignored.
453 EXPECT_EQ(0U, observer.events().size());
454
455 volume_manager_->RemoveObserver(&observer);
456 }
457
458 TEST_F(VolumeManagerTest, OnFormatEvent_Started) {
459 LoggingObserver observer;
460 volume_manager_->AddObserver(&observer);
461
462 volume_manager_->OnFormatEvent(
463 chromeos::disks::DiskMountManager::FORMAT_STARTED,
464 chromeos::FORMAT_ERROR_NONE,
465 "device1");
466
467 ASSERT_EQ(1U, observer.events().size());
468 const LoggingObserver::Event& event = observer.events()[0];
469 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
470 EXPECT_EQ("device1", event.device_path);
471 EXPECT_TRUE(event.success);
472
473 volume_manager_->RemoveObserver(&observer);
474 }
475
476 TEST_F(VolumeManagerTest, OnFormatEvent_StartFailed) {
477 LoggingObserver observer;
478 volume_manager_->AddObserver(&observer);
479
480 volume_manager_->OnFormatEvent(
481 chromeos::disks::DiskMountManager::FORMAT_STARTED,
482 chromeos::FORMAT_ERROR_UNKNOWN,
483 "device1");
484
485 ASSERT_EQ(1U, observer.events().size());
486 const LoggingObserver::Event& event = observer.events()[0];
487 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
488 EXPECT_EQ("device1", event.device_path);
489 EXPECT_FALSE(event.success);
490
491 volume_manager_->RemoveObserver(&observer);
492 }
493
494 TEST_F(VolumeManagerTest, OnFormatEvent_Completed) {
495 LoggingObserver observer;
496 volume_manager_->AddObserver(&observer);
497
498 volume_manager_->OnFormatEvent(
499 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
500 chromeos::FORMAT_ERROR_NONE,
501 "device1");
502
503 ASSERT_EQ(1U, observer.events().size());
504 const LoggingObserver::Event& event = observer.events()[0];
505 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
506 EXPECT_EQ("device1", event.device_path);
507 EXPECT_TRUE(event.success);
508
509 // When "format" is successfully done, VolumeManager requests to mount it.
510 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
511 const FakeDiskMountManager::MountRequest& mount_request =
512 disk_mount_manager_->mount_requests()[0];
513 EXPECT_EQ("device1", mount_request.source_path);
514 EXPECT_EQ("", mount_request.source_format);
515 EXPECT_EQ("", mount_request.mount_label);
516 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
517
518 volume_manager_->RemoveObserver(&observer);
519 }
520
521 TEST_F(VolumeManagerTest, OnFormatEvent_CompletedFailed) {
522 LoggingObserver observer;
523 volume_manager_->AddObserver(&observer);
524
525 volume_manager_->OnFormatEvent(
526 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
527 chromeos::FORMAT_ERROR_UNKNOWN,
528 "device1");
529
530 ASSERT_EQ(1U, observer.events().size());
531 const LoggingObserver::Event& event = observer.events()[0];
532 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
533 EXPECT_EQ("device1", event.device_path);
534 EXPECT_FALSE(event.success);
535
536 EXPECT_EQ(0U, disk_mount_manager_->mount_requests().size());
537
538 volume_manager_->RemoveObserver(&observer);
539 }
540
541 } // namespace file_manager
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/extensions/file_manager/volume_manager_observer.h ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698