Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / volume_manager_unittest.cc
blob1d75ccf508cc2b4ffea12a48da15f3a0b9b99e1c
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.
5 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/drive/file_system_util.h"
15 #include "chrome/browser/chromeos/file_manager/fake_disk_mount_manager.h"
16 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
17 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
18 #include "chrome/browser/chromeos/file_system_provider/service.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chromeos/dbus/fake_power_manager_client.h"
22 #include "chromeos/disks/disk_mount_manager.h"
23 #include "components/storage_monitor/storage_info.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "device/media_transfer_protocol/mtp_storage_info.pb.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 namespace file_manager {
30 namespace {
32 class LoggingObserver : public VolumeManagerObserver {
33 public:
34 struct Event {
35 enum EventType {
36 DISK_ADDED,
37 DISK_REMOVED,
38 DEVICE_ADDED,
39 DEVICE_REMOVED,
40 VOLUME_MOUNTED,
41 VOLUME_UNMOUNTED,
42 FORMAT_STARTED,
43 FORMAT_COMPLETED,
44 } type;
46 // Available on DEVICE_ADDED, DEVICE_REMOVED, VOLUME_MOUNTED,
47 // VOLUME_UNMOUNTED, FORMAT_STARTED and FORMAT_COMPLETED.
48 std::string device_path;
50 // Available on DISK_ADDED.
51 bool mounting;
53 // Available on VOLUME_MOUNTED and VOLUME_UNMOUNTED.
54 chromeos::MountError mount_error;
56 // Available on FORMAT_STARTED and FORMAT_COMPLETED.
57 bool success;
60 LoggingObserver() {}
61 ~LoggingObserver() override {}
63 const std::vector<Event>& events() const { return events_; }
65 // VolumeManagerObserver overrides.
66 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk& disk,
67 bool mounting) override {
68 Event event;
69 event.type = Event::DISK_ADDED;
70 event.device_path = disk.device_path(); // Keep only device_path.
71 event.mounting = mounting;
72 events_.push_back(event);
75 void OnDiskRemoved(
76 const chromeos::disks::DiskMountManager::Disk& disk) override {
77 Event event;
78 event.type = Event::DISK_REMOVED;
79 event.device_path = disk.device_path(); // Keep only device_path.
80 events_.push_back(event);
83 void OnDeviceAdded(const std::string& device_path) override {
84 Event event;
85 event.type = Event::DEVICE_ADDED;
86 event.device_path = device_path;
87 events_.push_back(event);
90 void OnDeviceRemoved(const std::string& device_path) override {
91 Event event;
92 event.type = Event::DEVICE_REMOVED;
93 event.device_path = device_path;
94 events_.push_back(event);
97 void OnVolumeMounted(chromeos::MountError error_code,
98 const Volume& volume) override {
99 Event event;
100 event.type = Event::VOLUME_MOUNTED;
101 event.device_path = volume.source_path().AsUTF8Unsafe();
102 event.mount_error = error_code;
103 events_.push_back(event);
106 void OnVolumeUnmounted(chromeos::MountError error_code,
107 const Volume& volume) override {
108 Event event;
109 event.type = Event::VOLUME_UNMOUNTED;
110 event.device_path = volume.source_path().AsUTF8Unsafe();
111 event.mount_error = error_code;
112 events_.push_back(event);
115 void OnFormatStarted(const std::string& device_path, bool success) override {
116 Event event;
117 event.type = Event::FORMAT_STARTED;
118 event.device_path = device_path;
119 event.success = success;
120 events_.push_back(event);
123 void OnFormatCompleted(const std::string& device_path,
124 bool success) override {
125 Event event;
126 event.type = Event::FORMAT_COMPLETED;
127 event.device_path = device_path;
128 event.success = success;
129 events_.push_back(event);
132 private:
133 std::vector<Event> events_;
135 DISALLOW_COPY_AND_ASSIGN(LoggingObserver);
138 } // namespace
140 class VolumeManagerTest : public testing::Test {
141 protected:
142 // Helper class that contains per-profile objects.
143 class ProfileEnvironment {
144 public:
145 ProfileEnvironment(chromeos::PowerManagerClient* power_manager_client,
146 chromeos::disks::DiskMountManager* disk_manager)
147 : profile_(new TestingProfile),
148 extension_registry_(
149 new extensions::ExtensionRegistry(profile_.get())),
150 file_system_provider_service_(
151 new chromeos::file_system_provider::Service(
152 profile_.get(),
153 extension_registry_.get())),
154 volume_manager_(new VolumeManager(
155 profile_.get(),
156 NULL, // DriveIntegrationService
157 power_manager_client,
158 disk_manager,
159 file_system_provider_service_.get(),
160 base::Bind(&ProfileEnvironment::GetFakeMtpStorageInfo,
161 base::Unretained(this)))) {
162 file_system_provider_service_->SetFileSystemFactoryForTesting(base::Bind(
163 &chromeos::file_system_provider::FakeProvidedFileSystem::Create));
166 Profile* profile() const { return profile_.get(); }
167 VolumeManager* volume_manager() const { return volume_manager_.get(); }
169 private:
170 const MtpStorageInfo* GetFakeMtpStorageInfo(
171 const std::string& /*storage_name*/) {
172 return &fake_mtp_storage_info_;
175 scoped_ptr<TestingProfile> profile_;
176 scoped_ptr<extensions::ExtensionRegistry> extension_registry_;
177 scoped_ptr<chromeos::file_system_provider::Service>
178 file_system_provider_service_;
179 scoped_ptr<VolumeManager> volume_manager_;
180 const MtpStorageInfo fake_mtp_storage_info_;
183 void SetUp() override {
184 power_manager_client_.reset(new chromeos::FakePowerManagerClient);
185 disk_mount_manager_.reset(new FakeDiskMountManager);
186 main_profile_.reset(new ProfileEnvironment(power_manager_client_.get(),
187 disk_mount_manager_.get()));
190 Profile* profile() const { return main_profile_->profile(); }
191 VolumeManager* volume_manager() const {
192 return main_profile_->volume_manager();
195 content::TestBrowserThreadBundle thread_bundle_;
196 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
197 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
198 scoped_ptr<ProfileEnvironment> main_profile_;
201 TEST_F(VolumeManagerTest, OnDriveFileSystemMountAndUnmount) {
202 LoggingObserver observer;
203 volume_manager()->AddObserver(&observer);
205 volume_manager()->OnFileSystemMounted();
207 ASSERT_EQ(1U, observer.events().size());
208 LoggingObserver::Event event = observer.events()[0];
209 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
210 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
211 event.device_path);
212 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
214 volume_manager()->OnFileSystemBeingUnmounted();
216 ASSERT_EQ(2U, observer.events().size());
217 event = observer.events()[1];
218 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
219 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
220 event.device_path);
221 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
223 volume_manager()->RemoveObserver(&observer);
226 TEST_F(VolumeManagerTest, OnDriveFileSystemUnmountWithoutMount) {
227 LoggingObserver observer;
228 volume_manager()->AddObserver(&observer);
229 volume_manager()->OnFileSystemBeingUnmounted();
231 // Unmount event for non-mounted volume is not reported.
232 ASSERT_EQ(0U, observer.events().size());
233 volume_manager()->RemoveObserver(&observer);
236 TEST_F(VolumeManagerTest, OnDiskEvent_Hidden) {
237 LoggingObserver observer;
238 volume_manager()->AddObserver(&observer);
240 const bool kIsHidden = true;
241 const chromeos::disks::DiskMountManager::Disk kDisk(
242 "device1", "", "", "", "", "", "", "", "", "", "", "",
243 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
244 kIsHidden);
246 volume_manager()->OnDiskEvent(
247 chromeos::disks::DiskMountManager::DISK_ADDED, &kDisk);
248 EXPECT_EQ(0U, observer.events().size());
250 volume_manager()->OnDiskEvent(
251 chromeos::disks::DiskMountManager::DISK_REMOVED, &kDisk);
252 EXPECT_EQ(0U, observer.events().size());
254 volume_manager()->OnDiskEvent(
255 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
256 EXPECT_EQ(0U, observer.events().size());
258 volume_manager()->RemoveObserver(&observer);
261 TEST_F(VolumeManagerTest, OnDiskEvent_Added) {
262 // Enable external storage.
263 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
265 LoggingObserver observer;
266 volume_manager()->AddObserver(&observer);
268 const chromeos::disks::DiskMountManager::Disk kEmptyDevicePathDisk(
269 "", // empty device path.
270 "", "", "", "", "", "", "", "", "", "", "",
271 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
272 false);
273 volume_manager()->OnDiskEvent(
274 chromeos::disks::DiskMountManager::DISK_ADDED, &kEmptyDevicePathDisk);
275 EXPECT_EQ(0U, observer.events().size());
277 const bool kHasMedia = true;
278 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
279 "device1", "", "", "", "", "", "", "", "", "", "", "",
280 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, kHasMedia, false, false,
281 false);
282 volume_manager()->OnDiskEvent(
283 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
284 ASSERT_EQ(1U, observer.events().size());
285 const LoggingObserver::Event& event = observer.events()[0];
286 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
287 EXPECT_EQ("device1", event.device_path);
288 EXPECT_TRUE(event.mounting);
290 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
291 const FakeDiskMountManager::MountRequest& mount_request =
292 disk_mount_manager_->mount_requests()[0];
293 EXPECT_EQ("device1", mount_request.source_path);
294 EXPECT_EQ("", mount_request.source_format);
295 EXPECT_EQ("", mount_request.mount_label);
296 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
298 volume_manager()->RemoveObserver(&observer);
301 TEST_F(VolumeManagerTest, OnDiskEvent_AddedNonMounting) {
302 // Enable external storage.
303 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
305 // Device which is already mounted.
307 LoggingObserver observer;
308 volume_manager()->AddObserver(&observer);
310 const bool kHasMedia = true;
311 const chromeos::disks::DiskMountManager::Disk kMountedMediaDisk(
312 "device1", "mounted", "", "", "", "", "", "", "", "", "", "",
313 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
314 kHasMedia, false, false, false);
315 volume_manager()->OnDiskEvent(
316 chromeos::disks::DiskMountManager::DISK_ADDED, &kMountedMediaDisk);
317 ASSERT_EQ(1U, observer.events().size());
318 const LoggingObserver::Event& event = observer.events()[0];
319 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
320 EXPECT_EQ("device1", event.device_path);
321 EXPECT_FALSE(event.mounting);
323 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
325 volume_manager()->RemoveObserver(&observer);
328 // Device without media.
330 LoggingObserver observer;
331 volume_manager()->AddObserver(&observer);
333 const bool kWithoutMedia = false;
334 const chromeos::disks::DiskMountManager::Disk kNoMediaDisk(
335 "device1", "", "", "", "", "", "", "", "", "", "", "",
336 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
337 kWithoutMedia, false, false, false);
338 volume_manager()->OnDiskEvent(
339 chromeos::disks::DiskMountManager::DISK_ADDED, &kNoMediaDisk);
340 ASSERT_EQ(1U, observer.events().size());
341 const LoggingObserver::Event& event = observer.events()[0];
342 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
343 EXPECT_EQ("device1", event.device_path);
344 EXPECT_FALSE(event.mounting);
346 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
348 volume_manager()->RemoveObserver(&observer);
351 // External storage is disabled.
353 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
355 LoggingObserver observer;
356 volume_manager()->AddObserver(&observer);
358 const bool kHasMedia = true;
359 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
360 "device1", "", "", "", "", "", "", "", "", "", "", "",
361 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
362 kHasMedia, false, false, false);
363 volume_manager()->OnDiskEvent(
364 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
365 ASSERT_EQ(1U, observer.events().size());
366 const LoggingObserver::Event& event = observer.events()[0];
367 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
368 EXPECT_EQ("device1", event.device_path);
369 EXPECT_FALSE(event.mounting);
371 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
373 volume_manager()->RemoveObserver(&observer);
377 TEST_F(VolumeManagerTest, OnDiskEvent_Removed) {
378 LoggingObserver observer;
379 volume_manager()->AddObserver(&observer);
381 const chromeos::disks::DiskMountManager::Disk kMountedDisk(
382 "device1", "mount_path", "", "", "", "", "", "", "", "", "", "",
383 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
384 false);
385 volume_manager()->OnDiskEvent(
386 chromeos::disks::DiskMountManager::DISK_REMOVED, &kMountedDisk);
388 ASSERT_EQ(1U, observer.events().size());
389 const LoggingObserver::Event& event = observer.events()[0];
390 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
391 EXPECT_EQ("device1", event.device_path);
393 ASSERT_EQ(1U, disk_mount_manager_->unmount_requests().size());
394 const FakeDiskMountManager::UnmountRequest& unmount_request =
395 disk_mount_manager_->unmount_requests()[0];
396 EXPECT_EQ("mount_path", unmount_request.mount_path);
397 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_LAZY, unmount_request.options);
399 volume_manager()->RemoveObserver(&observer);
402 TEST_F(VolumeManagerTest, OnDiskEvent_RemovedNotMounted) {
403 LoggingObserver observer;
404 volume_manager()->AddObserver(&observer);
406 const chromeos::disks::DiskMountManager::Disk kNotMountedDisk(
407 "device1", "", "", "", "", "", "", "", "", "", "", "",
408 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
409 false);
410 volume_manager()->OnDiskEvent(
411 chromeos::disks::DiskMountManager::DISK_REMOVED, &kNotMountedDisk);
413 ASSERT_EQ(1U, observer.events().size());
414 const LoggingObserver::Event& event = observer.events()[0];
415 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
416 EXPECT_EQ("device1", event.device_path);
418 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
420 volume_manager()->RemoveObserver(&observer);
423 TEST_F(VolumeManagerTest, OnDiskEvent_Changed) {
424 // Changed event should cause mounting (if possible).
425 LoggingObserver observer;
426 volume_manager()->AddObserver(&observer);
428 const chromeos::disks::DiskMountManager::Disk kDisk(
429 "device1", "", "", "", "", "", "", "", "", "", "", "",
430 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false,
431 false);
432 volume_manager()->OnDiskEvent(
433 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
435 EXPECT_EQ(1U, observer.events().size());
436 EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
437 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
439 volume_manager()->RemoveObserver(&observer);
442 TEST_F(VolumeManagerTest, OnDeviceEvent_Added) {
443 LoggingObserver observer;
444 volume_manager()->AddObserver(&observer);
446 volume_manager()->OnDeviceEvent(
447 chromeos::disks::DiskMountManager::DEVICE_ADDED, "device1");
449 ASSERT_EQ(1U, observer.events().size());
450 const LoggingObserver::Event& event = observer.events()[0];
451 EXPECT_EQ(LoggingObserver::Event::DEVICE_ADDED, event.type);
452 EXPECT_EQ("device1", event.device_path);
454 volume_manager()->RemoveObserver(&observer);
457 TEST_F(VolumeManagerTest, OnDeviceEvent_Removed) {
458 LoggingObserver observer;
459 volume_manager()->AddObserver(&observer);
461 volume_manager()->OnDeviceEvent(
462 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
464 ASSERT_EQ(1U, observer.events().size());
465 const LoggingObserver::Event& event = observer.events()[0];
466 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, event.type);
467 EXPECT_EQ("device1", event.device_path);
469 volume_manager()->RemoveObserver(&observer);
472 TEST_F(VolumeManagerTest, OnDeviceEvent_Scanned) {
473 LoggingObserver observer;
474 volume_manager()->AddObserver(&observer);
476 volume_manager()->OnDeviceEvent(
477 chromeos::disks::DiskMountManager::DEVICE_SCANNED, "device1");
479 // SCANNED event is just ignored.
480 EXPECT_EQ(0U, observer.events().size());
482 volume_manager()->RemoveObserver(&observer);
485 TEST_F(VolumeManagerTest, OnMountEvent_MountingAndUnmounting) {
486 LoggingObserver observer;
487 volume_manager()->AddObserver(&observer);
489 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
490 "device1",
491 "mount1",
492 chromeos::MOUNT_TYPE_DEVICE,
493 chromeos::disks::MOUNT_CONDITION_NONE);
495 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::MOUNTING,
496 chromeos::MOUNT_ERROR_NONE,
497 kMountPoint);
499 ASSERT_EQ(1U, observer.events().size());
500 LoggingObserver::Event event = observer.events()[0];
501 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
502 EXPECT_EQ("device1", event.device_path);
503 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
505 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
506 chromeos::MOUNT_ERROR_NONE,
507 kMountPoint);
509 ASSERT_EQ(2U, observer.events().size());
510 event = observer.events()[1];
511 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
512 EXPECT_EQ("device1", event.device_path);
513 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
515 volume_manager()->RemoveObserver(&observer);
518 TEST_F(VolumeManagerTest, OnMountEvent_Remounting) {
519 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
520 new chromeos::disks::DiskMountManager::Disk(
521 "device1", "", "", "", "", "", "", "", "", "", "uuid1", "",
522 chromeos::DEVICE_TYPE_UNKNOWN, 0,
523 false, false, false, false, false, false));
524 disk_mount_manager_->AddDiskForTest(disk.release());
525 disk_mount_manager_->MountPath(
526 "device1", "", "", chromeos::MOUNT_TYPE_DEVICE);
528 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
529 "device1",
530 "mount1",
531 chromeos::MOUNT_TYPE_DEVICE,
532 chromeos::disks::MOUNT_CONDITION_NONE);
534 volume_manager()->OnMountEvent(
535 chromeos::disks::DiskMountManager::MOUNTING,
536 chromeos::MOUNT_ERROR_NONE,
537 kMountPoint);
539 LoggingObserver observer;
541 // Emulate system suspend and then resume.
543 power_manager_client_->SendSuspendImminent();
544 power_manager_client_->SendSuspendDone();
546 // After resume, the device is unmounted and then mounted.
547 volume_manager()->OnMountEvent(
548 chromeos::disks::DiskMountManager::UNMOUNTING,
549 chromeos::MOUNT_ERROR_NONE,
550 kMountPoint);
552 // Observe what happened for the mount event.
553 volume_manager()->AddObserver(&observer);
555 volume_manager()->OnMountEvent(
556 chromeos::disks::DiskMountManager::MOUNTING,
557 chromeos::MOUNT_ERROR_NONE,
558 kMountPoint);
561 ASSERT_EQ(1U, observer.events().size());
562 const LoggingObserver::Event& event = observer.events()[0];
563 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
564 EXPECT_EQ("device1", event.device_path);
565 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
567 volume_manager()->RemoveObserver(&observer);
570 TEST_F(VolumeManagerTest, OnMountEvent_UnmountingWithoutMounting) {
571 LoggingObserver observer;
572 volume_manager()->AddObserver(&observer);
574 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
575 "device1",
576 "mount1",
577 chromeos::MOUNT_TYPE_DEVICE,
578 chromeos::disks::MOUNT_CONDITION_NONE);
580 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
581 chromeos::MOUNT_ERROR_NONE,
582 kMountPoint);
584 // Unmount event for a disk not mounted in this manager is not reported.
585 ASSERT_EQ(0U, observer.events().size());
587 volume_manager()->RemoveObserver(&observer);
590 TEST_F(VolumeManagerTest, OnFormatEvent_Started) {
591 LoggingObserver observer;
592 volume_manager()->AddObserver(&observer);
594 volume_manager()->OnFormatEvent(
595 chromeos::disks::DiskMountManager::FORMAT_STARTED,
596 chromeos::FORMAT_ERROR_NONE,
597 "device1");
599 ASSERT_EQ(1U, observer.events().size());
600 const LoggingObserver::Event& event = observer.events()[0];
601 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
602 EXPECT_EQ("device1", event.device_path);
603 EXPECT_TRUE(event.success);
605 volume_manager()->RemoveObserver(&observer);
608 TEST_F(VolumeManagerTest, OnFormatEvent_StartFailed) {
609 LoggingObserver observer;
610 volume_manager()->AddObserver(&observer);
612 volume_manager()->OnFormatEvent(
613 chromeos::disks::DiskMountManager::FORMAT_STARTED,
614 chromeos::FORMAT_ERROR_UNKNOWN,
615 "device1");
617 ASSERT_EQ(1U, observer.events().size());
618 const LoggingObserver::Event& event = observer.events()[0];
619 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
620 EXPECT_EQ("device1", event.device_path);
621 EXPECT_FALSE(event.success);
623 volume_manager()->RemoveObserver(&observer);
626 TEST_F(VolumeManagerTest, OnFormatEvent_Completed) {
627 LoggingObserver observer;
628 volume_manager()->AddObserver(&observer);
630 volume_manager()->OnFormatEvent(
631 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
632 chromeos::FORMAT_ERROR_NONE,
633 "device1");
635 ASSERT_EQ(1U, observer.events().size());
636 const LoggingObserver::Event& event = observer.events()[0];
637 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
638 EXPECT_EQ("device1", event.device_path);
639 EXPECT_TRUE(event.success);
641 // When "format" is successfully done, VolumeManager requests to mount it.
642 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
643 const FakeDiskMountManager::MountRequest& mount_request =
644 disk_mount_manager_->mount_requests()[0];
645 EXPECT_EQ("device1", mount_request.source_path);
646 EXPECT_EQ("", mount_request.source_format);
647 EXPECT_EQ("", mount_request.mount_label);
648 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
650 volume_manager()->RemoveObserver(&observer);
653 TEST_F(VolumeManagerTest, OnFormatEvent_CompletedFailed) {
654 LoggingObserver observer;
655 volume_manager()->AddObserver(&observer);
657 volume_manager()->OnFormatEvent(
658 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
659 chromeos::FORMAT_ERROR_UNKNOWN,
660 "device1");
662 ASSERT_EQ(1U, observer.events().size());
663 const LoggingObserver::Event& event = observer.events()[0];
664 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
665 EXPECT_EQ("device1", event.device_path);
666 EXPECT_FALSE(event.success);
668 EXPECT_EQ(0U, disk_mount_manager_->mount_requests().size());
670 volume_manager()->RemoveObserver(&observer);
673 TEST_F(VolumeManagerTest, OnExternalStorageDisabledChanged) {
674 // Here create two mount points.
675 disk_mount_manager_->MountPath(
676 "mount1", "", "", chromeos::MOUNT_TYPE_DEVICE);
677 disk_mount_manager_->MountPath(
678 "mount2", "", "", chromeos::MOUNT_TYPE_DEVICE);
680 // Initially, there are two mount points.
681 ASSERT_EQ(2U, disk_mount_manager_->mount_points().size());
682 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
684 // Emulate to set kExternalStorageDisabled to false.
685 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
686 volume_manager()->OnExternalStorageDisabledChanged();
688 // Expect no effects.
689 EXPECT_EQ(2U, disk_mount_manager_->mount_points().size());
690 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
692 // Emulate to set kExternalStorageDisabled to true.
693 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
694 volume_manager()->OnExternalStorageDisabledChanged();
696 // The all mount points should be unmounted.
697 EXPECT_EQ(0U, disk_mount_manager_->mount_points().size());
699 EXPECT_EQ(2U, disk_mount_manager_->unmount_requests().size());
700 const FakeDiskMountManager::UnmountRequest& unmount_request1 =
701 disk_mount_manager_->unmount_requests()[0];
702 EXPECT_EQ("mount1", unmount_request1.mount_path);
704 const FakeDiskMountManager::UnmountRequest& unmount_request2 =
705 disk_mount_manager_->unmount_requests()[1];
706 EXPECT_EQ("mount2", unmount_request2.mount_path);
709 TEST_F(VolumeManagerTest, ExternalStorageDisabledPolicyMultiProfile) {
710 ProfileEnvironment secondary(power_manager_client_.get(),
711 disk_mount_manager_.get());
712 volume_manager()->Initialize();
713 secondary.volume_manager()->Initialize();
715 // Simulates the case that the main profile has kExternalStorageDisabled set
716 // as false, and the secondary profile has the config set to true.
717 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
718 secondary.profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled,
719 true);
721 LoggingObserver main_observer, secondary_observer;
722 volume_manager()->AddObserver(&main_observer);
723 secondary.volume_manager()->AddObserver(&secondary_observer);
725 // Add 1 disk.
726 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
727 "device1", "", "", "", "", "", "", "", "", "", "", "",
728 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false,
729 false);
730 volume_manager()->OnDiskEvent(
731 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
732 secondary.volume_manager()->OnDiskEvent(
733 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
735 // The profile with external storage enabled should have mounted the volume.
736 bool has_volume_mounted = false;
737 for (size_t i = 0; i < main_observer.events().size(); ++i) {
738 if (main_observer.events()[i].type ==
739 LoggingObserver::Event::VOLUME_MOUNTED)
740 has_volume_mounted = true;
742 EXPECT_TRUE(has_volume_mounted);
744 // The other profiles with external storage disabled should have not.
745 has_volume_mounted = false;
746 for (size_t i = 0; i < secondary_observer.events().size(); ++i) {
747 if (secondary_observer.events()[i].type ==
748 LoggingObserver::Event::VOLUME_MOUNTED)
749 has_volume_mounted = true;
751 EXPECT_FALSE(has_volume_mounted);
753 volume_manager()->RemoveObserver(&main_observer);
754 secondary.volume_manager()->RemoveObserver(&secondary_observer);
757 TEST_F(VolumeManagerTest, GetVolumeList) {
758 volume_manager()->Initialize(); // Adds "Downloads"
759 std::vector<base::WeakPtr<Volume>> volume_list =
760 volume_manager()->GetVolumeList();
761 ASSERT_EQ(1u, volume_list.size());
762 EXPECT_EQ("downloads:Downloads", volume_list[0]->volume_id());
763 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, volume_list[0]->type());
766 TEST_F(VolumeManagerTest, FindVolumeById) {
767 volume_manager()->Initialize(); // Adds "Downloads"
768 base::WeakPtr<Volume> bad_volume =
769 volume_manager()->FindVolumeById("nonexistent");
770 ASSERT_FALSE(bad_volume.get());
771 base::WeakPtr<Volume> good_volume =
772 volume_manager()->FindVolumeById("downloads:Downloads");
773 ASSERT_TRUE(good_volume.get());
774 EXPECT_EQ("downloads:Downloads", good_volume->volume_id());
775 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, good_volume->type());
778 TEST_F(VolumeManagerTest, ArchiveSourceFiltering) {
779 LoggingObserver observer;
780 volume_manager()->AddObserver(&observer);
782 // Mount a USB stick.
783 volume_manager()->OnMountEvent(
784 chromeos::disks::DiskMountManager::MOUNTING,
785 chromeos::MOUNT_ERROR_NONE,
786 chromeos::disks::DiskMountManager::MountPointInfo(
787 "/removable/usb",
788 "/removable/usb",
789 chromeos::MOUNT_TYPE_DEVICE,
790 chromeos::disks::MOUNT_CONDITION_NONE));
792 // Mount a zip archive in the stick.
793 volume_manager()->OnMountEvent(
794 chromeos::disks::DiskMountManager::MOUNTING,
795 chromeos::MOUNT_ERROR_NONE,
796 chromeos::disks::DiskMountManager::MountPointInfo(
797 "/removable/usb/1.zip",
798 "/archive/1",
799 chromeos::MOUNT_TYPE_ARCHIVE,
800 chromeos::disks::MOUNT_CONDITION_NONE));
801 base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("archive:1");
802 ASSERT_TRUE(volume.get());
803 EXPECT_EQ("/archive/1", volume->mount_path().AsUTF8Unsafe());
804 EXPECT_EQ(2u, observer.events().size());
806 // Mount a zip archive in the previous zip archive.
807 volume_manager()->OnMountEvent(
808 chromeos::disks::DiskMountManager::MOUNTING,
809 chromeos::MOUNT_ERROR_NONE,
810 chromeos::disks::DiskMountManager::MountPointInfo(
811 "/archive/1/2.zip",
812 "/archive/2",
813 chromeos::MOUNT_TYPE_ARCHIVE,
814 chromeos::disks::MOUNT_CONDITION_NONE));
815 base::WeakPtr<Volume> second_volume =
816 volume_manager()->FindVolumeById("archive:2");
817 ASSERT_TRUE(second_volume.get());
818 EXPECT_EQ("/archive/2", second_volume->mount_path().AsUTF8Unsafe());
819 EXPECT_EQ(3u, observer.events().size());
821 // A zip file is mounted from other profile. It must be ignored in the current
822 // VolumeManager.
823 volume_manager()->OnMountEvent(
824 chromeos::disks::DiskMountManager::MOUNTING,
825 chromeos::MOUNT_ERROR_NONE,
826 chromeos::disks::DiskMountManager::MountPointInfo(
827 "/other/profile/drive/folder/3.zip",
828 "/archive/3",
829 chromeos::MOUNT_TYPE_ARCHIVE,
830 chromeos::disks::MOUNT_CONDITION_NONE));
831 base::WeakPtr<Volume> third_volume =
832 volume_manager()->FindVolumeById("archive:3");
833 ASSERT_FALSE(third_volume.get());
834 EXPECT_EQ(3u, observer.events().size());
837 TEST_F(VolumeManagerTest, MTPPlugAndUnplug) {
838 LoggingObserver observer;
839 volume_manager()->AddObserver(&observer);
841 storage_monitor::StorageInfo info(
842 storage_monitor::StorageInfo::MakeDeviceId(
843 storage_monitor::StorageInfo::MTP_OR_PTP, "dummy-device-id"),
844 FILE_PATH_LITERAL("/dummy/device/location"),
845 base::UTF8ToUTF16("label"),
846 base::UTF8ToUTF16("vendor"),
847 base::UTF8ToUTF16("model"),
848 12345 /* size */);
850 storage_monitor::StorageInfo non_mtp_info(
851 storage_monitor::StorageInfo::MakeDeviceId(
852 storage_monitor::StorageInfo::IPHOTO, "dummy-device-id2"),
853 FILE_PATH_LITERAL("/dummy/device/location2"),
854 base::UTF8ToUTF16("label2"),
855 base::UTF8ToUTF16("vendor2"),
856 base::UTF8ToUTF16("model2"),
857 12345 /* size */);
859 // Attach
860 volume_manager()->OnRemovableStorageAttached(info);
861 ASSERT_EQ(1u, observer.events().size());
862 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, observer.events()[0].type);
864 base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("mtp:model");
865 EXPECT_EQ(VOLUME_TYPE_MTP, volume->type());
867 // Non MTP events from storage monitor are ignored.
868 volume_manager()->OnRemovableStorageAttached(non_mtp_info);
869 EXPECT_EQ(1u, observer.events().size());
871 // Detach
872 volume_manager()->OnRemovableStorageDetached(info);
873 ASSERT_EQ(2u, observer.events().size());
874 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED,
875 observer.events()[1].type);
877 EXPECT_FALSE(volume.get());
880 } // namespace file_manager