Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / volume_manager_unittest.cc
blobf27aadeb8215ccf53e81c4386abe4f56dd05b98e
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/file_manager/fake_disk_mount_manager.h"
15 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
16 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
17 #include "chrome/browser/chromeos/file_system_provider/service.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "chromeos/dbus/fake_power_manager_client.h"
21 #include "chromeos/disks/disk_mount_manager.h"
22 #include "components/storage_monitor/storage_info.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace file_manager {
28 namespace {
30 class LoggingObserver : public VolumeManagerObserver {
31 public:
32 struct Event {
33 enum EventType {
34 DISK_ADDED,
35 DISK_REMOVED,
36 DEVICE_ADDED,
37 DEVICE_REMOVED,
38 VOLUME_MOUNTED,
39 VOLUME_UNMOUNTED,
40 FORMAT_STARTED,
41 FORMAT_COMPLETED,
42 } type;
44 // Available on DEVICE_ADDED, DEVICE_REMOVED, VOLUME_MOUNTED,
45 // VOLUME_UNMOUNTED, FORMAT_STARTED and FORMAT_COMPLETED.
46 std::string device_path;
48 // Available on DISK_ADDED.
49 bool mounting;
51 // Available on VOLUME_MOUNTED and VOLUME_UNMOUNTED.
52 chromeos::MountError mount_error;
54 // Available on FORMAT_STARTED and FORMAT_COMPLETED.
55 bool success;
58 LoggingObserver() {}
59 ~LoggingObserver() override {}
61 const std::vector<Event>& events() const { return events_; }
63 // VolumeManagerObserver overrides.
64 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk& disk,
65 bool mounting) override {
66 Event event;
67 event.type = Event::DISK_ADDED;
68 event.device_path = disk.device_path(); // Keep only device_path.
69 event.mounting = mounting;
70 events_.push_back(event);
73 void OnDiskRemoved(
74 const chromeos::disks::DiskMountManager::Disk& disk) override {
75 Event event;
76 event.type = Event::DISK_REMOVED;
77 event.device_path = disk.device_path(); // Keep only device_path.
78 events_.push_back(event);
81 void OnDeviceAdded(const std::string& device_path) override {
82 Event event;
83 event.type = Event::DEVICE_ADDED;
84 event.device_path = device_path;
85 events_.push_back(event);
88 void OnDeviceRemoved(const std::string& device_path) override {
89 Event event;
90 event.type = Event::DEVICE_REMOVED;
91 event.device_path = device_path;
92 events_.push_back(event);
95 void OnVolumeMounted(chromeos::MountError error_code,
96 const Volume& volume) override {
97 Event event;
98 event.type = Event::VOLUME_MOUNTED;
99 event.device_path = volume.source_path().AsUTF8Unsafe();
100 event.mount_error = error_code;
101 events_.push_back(event);
104 void OnVolumeUnmounted(chromeos::MountError error_code,
105 const Volume& volume) override {
106 Event event;
107 event.type = Event::VOLUME_UNMOUNTED;
108 event.device_path = volume.source_path().AsUTF8Unsafe();
109 event.mount_error = error_code;
110 events_.push_back(event);
113 void OnFormatStarted(const std::string& device_path, bool success) override {
114 Event event;
115 event.type = Event::FORMAT_STARTED;
116 event.device_path = device_path;
117 event.success = success;
118 events_.push_back(event);
121 void OnFormatCompleted(const std::string& device_path,
122 bool success) override {
123 Event event;
124 event.type = Event::FORMAT_COMPLETED;
125 event.device_path = device_path;
126 event.success = success;
127 events_.push_back(event);
130 private:
131 std::vector<Event> events_;
133 DISALLOW_COPY_AND_ASSIGN(LoggingObserver);
136 } // namespace
138 class VolumeManagerTest : public testing::Test {
139 protected:
140 // Helper class that contains per-profile objects.
141 class ProfileEnvironment {
142 public:
143 ProfileEnvironment(chromeos::PowerManagerClient* power_manager_client,
144 chromeos::disks::DiskMountManager* disk_manager)
145 : profile_(new TestingProfile),
146 extension_registry_(
147 new extensions::ExtensionRegistry(profile_.get())),
148 file_system_provider_service_(
149 new chromeos::file_system_provider::Service(
150 profile_.get(),
151 extension_registry_.get())),
152 volume_manager_(
153 new VolumeManager(profile_.get(),
154 NULL, // DriveIntegrationService
155 power_manager_client,
156 disk_manager,
157 file_system_provider_service_.get())) {
158 file_system_provider_service_->SetFileSystemFactoryForTesting(base::Bind(
159 &chromeos::file_system_provider::FakeProvidedFileSystem::Create));
162 Profile* profile() const { return profile_.get(); }
163 VolumeManager* volume_manager() const { return volume_manager_.get(); }
165 private:
166 scoped_ptr<TestingProfile> profile_;
167 scoped_ptr<extensions::ExtensionRegistry> extension_registry_;
168 scoped_ptr<chromeos::file_system_provider::Service>
169 file_system_provider_service_;
170 scoped_ptr<VolumeManager> volume_manager_;
173 void SetUp() override {
174 power_manager_client_.reset(new chromeos::FakePowerManagerClient);
175 disk_mount_manager_.reset(new FakeDiskMountManager);
176 main_profile_.reset(new ProfileEnvironment(power_manager_client_.get(),
177 disk_mount_manager_.get()));
180 Profile* profile() const { return main_profile_->profile(); }
181 VolumeManager* volume_manager() const {
182 return main_profile_->volume_manager();
185 content::TestBrowserThreadBundle thread_bundle_;
186 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
187 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
188 scoped_ptr<ProfileEnvironment> main_profile_;
191 TEST_F(VolumeManagerTest, OnDriveFileSystemMountAndUnmount) {
192 LoggingObserver observer;
193 volume_manager()->AddObserver(&observer);
195 volume_manager()->OnFileSystemMounted();
197 ASSERT_EQ(1U, observer.events().size());
198 LoggingObserver::Event event = observer.events()[0];
199 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
200 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
201 event.device_path);
202 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
204 volume_manager()->OnFileSystemBeingUnmounted();
206 ASSERT_EQ(2U, observer.events().size());
207 event = observer.events()[1];
208 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
209 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
210 event.device_path);
211 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
213 volume_manager()->RemoveObserver(&observer);
216 TEST_F(VolumeManagerTest, OnDriveFileSystemUnmountWithoutMount) {
217 LoggingObserver observer;
218 volume_manager()->AddObserver(&observer);
219 volume_manager()->OnFileSystemBeingUnmounted();
221 // Unmount event for non-mounted volume is not reported.
222 ASSERT_EQ(0U, observer.events().size());
223 volume_manager()->RemoveObserver(&observer);
226 TEST_F(VolumeManagerTest, OnDiskEvent_Hidden) {
227 LoggingObserver observer;
228 volume_manager()->AddObserver(&observer);
230 const bool kIsHidden = true;
231 const chromeos::disks::DiskMountManager::Disk kDisk(
232 "device1", "", "", "", "", "", "", "", "", "", "", "",
233 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
234 kIsHidden);
236 volume_manager()->OnDiskEvent(
237 chromeos::disks::DiskMountManager::DISK_ADDED, &kDisk);
238 EXPECT_EQ(0U, observer.events().size());
240 volume_manager()->OnDiskEvent(
241 chromeos::disks::DiskMountManager::DISK_REMOVED, &kDisk);
242 EXPECT_EQ(0U, observer.events().size());
244 volume_manager()->OnDiskEvent(
245 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
246 EXPECT_EQ(0U, observer.events().size());
248 volume_manager()->RemoveObserver(&observer);
251 TEST_F(VolumeManagerTest, OnDiskEvent_Added) {
252 // Enable external storage.
253 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
255 LoggingObserver observer;
256 volume_manager()->AddObserver(&observer);
258 const chromeos::disks::DiskMountManager::Disk kEmptyDevicePathDisk(
259 "", // empty device path.
260 "", "", "", "", "", "", "", "", "", "", "",
261 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
262 false);
263 volume_manager()->OnDiskEvent(
264 chromeos::disks::DiskMountManager::DISK_ADDED, &kEmptyDevicePathDisk);
265 EXPECT_EQ(0U, observer.events().size());
267 const bool kHasMedia = true;
268 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
269 "device1", "", "", "", "", "", "", "", "", "", "", "",
270 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, kHasMedia, false, false,
271 false);
272 volume_manager()->OnDiskEvent(
273 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
274 ASSERT_EQ(1U, observer.events().size());
275 const LoggingObserver::Event& event = observer.events()[0];
276 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
277 EXPECT_EQ("device1", event.device_path);
278 EXPECT_TRUE(event.mounting);
280 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
281 const FakeDiskMountManager::MountRequest& mount_request =
282 disk_mount_manager_->mount_requests()[0];
283 EXPECT_EQ("device1", mount_request.source_path);
284 EXPECT_EQ("", mount_request.source_format);
285 EXPECT_EQ("", mount_request.mount_label);
286 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
288 volume_manager()->RemoveObserver(&observer);
291 TEST_F(VolumeManagerTest, OnDiskEvent_AddedNonMounting) {
292 // Enable external storage.
293 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
295 // Device which is already mounted.
297 LoggingObserver observer;
298 volume_manager()->AddObserver(&observer);
300 const bool kHasMedia = true;
301 const chromeos::disks::DiskMountManager::Disk kMountedMediaDisk(
302 "device1", "mounted", "", "", "", "", "", "", "", "", "", "",
303 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
304 kHasMedia, false, false, false);
305 volume_manager()->OnDiskEvent(
306 chromeos::disks::DiskMountManager::DISK_ADDED, &kMountedMediaDisk);
307 ASSERT_EQ(1U, observer.events().size());
308 const LoggingObserver::Event& event = observer.events()[0];
309 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
310 EXPECT_EQ("device1", event.device_path);
311 EXPECT_FALSE(event.mounting);
313 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
315 volume_manager()->RemoveObserver(&observer);
318 // Device without media.
320 LoggingObserver observer;
321 volume_manager()->AddObserver(&observer);
323 const bool kWithoutMedia = false;
324 const chromeos::disks::DiskMountManager::Disk kNoMediaDisk(
325 "device1", "", "", "", "", "", "", "", "", "", "", "",
326 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
327 kWithoutMedia, false, false, false);
328 volume_manager()->OnDiskEvent(
329 chromeos::disks::DiskMountManager::DISK_ADDED, &kNoMediaDisk);
330 ASSERT_EQ(1U, observer.events().size());
331 const LoggingObserver::Event& event = observer.events()[0];
332 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
333 EXPECT_EQ("device1", event.device_path);
334 EXPECT_FALSE(event.mounting);
336 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
338 volume_manager()->RemoveObserver(&observer);
341 // External storage is disabled.
343 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
345 LoggingObserver observer;
346 volume_manager()->AddObserver(&observer);
348 const bool kHasMedia = true;
349 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
350 "device1", "", "", "", "", "", "", "", "", "", "", "",
351 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
352 kHasMedia, false, false, false);
353 volume_manager()->OnDiskEvent(
354 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
355 ASSERT_EQ(1U, observer.events().size());
356 const LoggingObserver::Event& event = observer.events()[0];
357 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
358 EXPECT_EQ("device1", event.device_path);
359 EXPECT_FALSE(event.mounting);
361 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
363 volume_manager()->RemoveObserver(&observer);
367 TEST_F(VolumeManagerTest, OnDiskEvent_Removed) {
368 LoggingObserver observer;
369 volume_manager()->AddObserver(&observer);
371 const chromeos::disks::DiskMountManager::Disk kMountedDisk(
372 "device1", "mount_path", "", "", "", "", "", "", "", "", "", "",
373 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
374 false);
375 volume_manager()->OnDiskEvent(
376 chromeos::disks::DiskMountManager::DISK_REMOVED, &kMountedDisk);
378 ASSERT_EQ(1U, observer.events().size());
379 const LoggingObserver::Event& event = observer.events()[0];
380 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
381 EXPECT_EQ("device1", event.device_path);
383 ASSERT_EQ(1U, disk_mount_manager_->unmount_requests().size());
384 const FakeDiskMountManager::UnmountRequest& unmount_request =
385 disk_mount_manager_->unmount_requests()[0];
386 EXPECT_EQ("mount_path", unmount_request.mount_path);
387 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_LAZY, unmount_request.options);
389 volume_manager()->RemoveObserver(&observer);
392 TEST_F(VolumeManagerTest, OnDiskEvent_RemovedNotMounted) {
393 LoggingObserver observer;
394 volume_manager()->AddObserver(&observer);
396 const chromeos::disks::DiskMountManager::Disk kNotMountedDisk(
397 "device1", "", "", "", "", "", "", "", "", "", "", "",
398 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
399 false);
400 volume_manager()->OnDiskEvent(
401 chromeos::disks::DiskMountManager::DISK_REMOVED, &kNotMountedDisk);
403 ASSERT_EQ(1U, observer.events().size());
404 const LoggingObserver::Event& event = observer.events()[0];
405 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
406 EXPECT_EQ("device1", event.device_path);
408 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
410 volume_manager()->RemoveObserver(&observer);
413 TEST_F(VolumeManagerTest, OnDiskEvent_Changed) {
414 // Changed event should cause mounting (if possible).
415 LoggingObserver observer;
416 volume_manager()->AddObserver(&observer);
418 const chromeos::disks::DiskMountManager::Disk kDisk(
419 "device1", "", "", "", "", "", "", "", "", "", "", "",
420 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false,
421 false);
422 volume_manager()->OnDiskEvent(
423 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
425 EXPECT_EQ(1U, observer.events().size());
426 EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
427 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
429 volume_manager()->RemoveObserver(&observer);
432 TEST_F(VolumeManagerTest, OnDeviceEvent_Added) {
433 LoggingObserver observer;
434 volume_manager()->AddObserver(&observer);
436 volume_manager()->OnDeviceEvent(
437 chromeos::disks::DiskMountManager::DEVICE_ADDED, "device1");
439 ASSERT_EQ(1U, observer.events().size());
440 const LoggingObserver::Event& event = observer.events()[0];
441 EXPECT_EQ(LoggingObserver::Event::DEVICE_ADDED, event.type);
442 EXPECT_EQ("device1", event.device_path);
444 volume_manager()->RemoveObserver(&observer);
447 TEST_F(VolumeManagerTest, OnDeviceEvent_Removed) {
448 LoggingObserver observer;
449 volume_manager()->AddObserver(&observer);
451 volume_manager()->OnDeviceEvent(
452 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
454 ASSERT_EQ(1U, observer.events().size());
455 const LoggingObserver::Event& event = observer.events()[0];
456 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, event.type);
457 EXPECT_EQ("device1", event.device_path);
459 volume_manager()->RemoveObserver(&observer);
462 TEST_F(VolumeManagerTest, OnDeviceEvent_Scanned) {
463 LoggingObserver observer;
464 volume_manager()->AddObserver(&observer);
466 volume_manager()->OnDeviceEvent(
467 chromeos::disks::DiskMountManager::DEVICE_SCANNED, "device1");
469 // SCANNED event is just ignored.
470 EXPECT_EQ(0U, observer.events().size());
472 volume_manager()->RemoveObserver(&observer);
475 TEST_F(VolumeManagerTest, OnMountEvent_MountingAndUnmounting) {
476 LoggingObserver observer;
477 volume_manager()->AddObserver(&observer);
479 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
480 "device1",
481 "mount1",
482 chromeos::MOUNT_TYPE_DEVICE,
483 chromeos::disks::MOUNT_CONDITION_NONE);
485 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::MOUNTING,
486 chromeos::MOUNT_ERROR_NONE,
487 kMountPoint);
489 ASSERT_EQ(1U, observer.events().size());
490 LoggingObserver::Event event = observer.events()[0];
491 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
492 EXPECT_EQ("device1", event.device_path);
493 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
495 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
496 chromeos::MOUNT_ERROR_NONE,
497 kMountPoint);
499 ASSERT_EQ(2U, observer.events().size());
500 event = observer.events()[1];
501 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
502 EXPECT_EQ("device1", event.device_path);
503 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
505 volume_manager()->RemoveObserver(&observer);
508 TEST_F(VolumeManagerTest, OnMountEvent_Remounting) {
509 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
510 new chromeos::disks::DiskMountManager::Disk(
511 "device1", "", "", "", "", "", "", "", "", "", "uuid1", "",
512 chromeos::DEVICE_TYPE_UNKNOWN, 0,
513 false, false, false, false, false, false));
514 disk_mount_manager_->AddDiskForTest(disk.release());
515 disk_mount_manager_->MountPath(
516 "device1", "", "", chromeos::MOUNT_TYPE_DEVICE);
518 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
519 "device1",
520 "mount1",
521 chromeos::MOUNT_TYPE_DEVICE,
522 chromeos::disks::MOUNT_CONDITION_NONE);
524 volume_manager()->OnMountEvent(
525 chromeos::disks::DiskMountManager::MOUNTING,
526 chromeos::MOUNT_ERROR_NONE,
527 kMountPoint);
529 LoggingObserver observer;
531 // Emulate system suspend and then resume.
533 power_manager_client_->SendSuspendImminent();
534 power_manager_client_->SendSuspendDone();
536 // After resume, the device is unmounted and then mounted.
537 volume_manager()->OnMountEvent(
538 chromeos::disks::DiskMountManager::UNMOUNTING,
539 chromeos::MOUNT_ERROR_NONE,
540 kMountPoint);
542 // Observe what happened for the mount event.
543 volume_manager()->AddObserver(&observer);
545 volume_manager()->OnMountEvent(
546 chromeos::disks::DiskMountManager::MOUNTING,
547 chromeos::MOUNT_ERROR_NONE,
548 kMountPoint);
551 ASSERT_EQ(1U, observer.events().size());
552 const LoggingObserver::Event& event = observer.events()[0];
553 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
554 EXPECT_EQ("device1", event.device_path);
555 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
557 volume_manager()->RemoveObserver(&observer);
560 TEST_F(VolumeManagerTest, OnMountEvent_UnmountingWithoutMounting) {
561 LoggingObserver observer;
562 volume_manager()->AddObserver(&observer);
564 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
565 "device1",
566 "mount1",
567 chromeos::MOUNT_TYPE_DEVICE,
568 chromeos::disks::MOUNT_CONDITION_NONE);
570 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
571 chromeos::MOUNT_ERROR_NONE,
572 kMountPoint);
574 // Unmount event for a disk not mounted in this manager is not reported.
575 ASSERT_EQ(0U, observer.events().size());
577 volume_manager()->RemoveObserver(&observer);
580 TEST_F(VolumeManagerTest, OnFormatEvent_Started) {
581 LoggingObserver observer;
582 volume_manager()->AddObserver(&observer);
584 volume_manager()->OnFormatEvent(
585 chromeos::disks::DiskMountManager::FORMAT_STARTED,
586 chromeos::FORMAT_ERROR_NONE,
587 "device1");
589 ASSERT_EQ(1U, observer.events().size());
590 const LoggingObserver::Event& event = observer.events()[0];
591 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
592 EXPECT_EQ("device1", event.device_path);
593 EXPECT_TRUE(event.success);
595 volume_manager()->RemoveObserver(&observer);
598 TEST_F(VolumeManagerTest, OnFormatEvent_StartFailed) {
599 LoggingObserver observer;
600 volume_manager()->AddObserver(&observer);
602 volume_manager()->OnFormatEvent(
603 chromeos::disks::DiskMountManager::FORMAT_STARTED,
604 chromeos::FORMAT_ERROR_UNKNOWN,
605 "device1");
607 ASSERT_EQ(1U, observer.events().size());
608 const LoggingObserver::Event& event = observer.events()[0];
609 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
610 EXPECT_EQ("device1", event.device_path);
611 EXPECT_FALSE(event.success);
613 volume_manager()->RemoveObserver(&observer);
616 TEST_F(VolumeManagerTest, OnFormatEvent_Completed) {
617 LoggingObserver observer;
618 volume_manager()->AddObserver(&observer);
620 volume_manager()->OnFormatEvent(
621 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
622 chromeos::FORMAT_ERROR_NONE,
623 "device1");
625 ASSERT_EQ(1U, observer.events().size());
626 const LoggingObserver::Event& event = observer.events()[0];
627 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
628 EXPECT_EQ("device1", event.device_path);
629 EXPECT_TRUE(event.success);
631 // When "format" is successfully done, VolumeManager requests to mount it.
632 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
633 const FakeDiskMountManager::MountRequest& mount_request =
634 disk_mount_manager_->mount_requests()[0];
635 EXPECT_EQ("device1", mount_request.source_path);
636 EXPECT_EQ("", mount_request.source_format);
637 EXPECT_EQ("", mount_request.mount_label);
638 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
640 volume_manager()->RemoveObserver(&observer);
643 TEST_F(VolumeManagerTest, OnFormatEvent_CompletedFailed) {
644 LoggingObserver observer;
645 volume_manager()->AddObserver(&observer);
647 volume_manager()->OnFormatEvent(
648 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
649 chromeos::FORMAT_ERROR_UNKNOWN,
650 "device1");
652 ASSERT_EQ(1U, observer.events().size());
653 const LoggingObserver::Event& event = observer.events()[0];
654 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
655 EXPECT_EQ("device1", event.device_path);
656 EXPECT_FALSE(event.success);
658 EXPECT_EQ(0U, disk_mount_manager_->mount_requests().size());
660 volume_manager()->RemoveObserver(&observer);
663 TEST_F(VolumeManagerTest, OnExternalStorageDisabledChanged) {
664 // Here create two mount points.
665 disk_mount_manager_->MountPath(
666 "mount1", "", "", chromeos::MOUNT_TYPE_DEVICE);
667 disk_mount_manager_->MountPath(
668 "mount2", "", "", chromeos::MOUNT_TYPE_DEVICE);
670 // Initially, there are two mount points.
671 ASSERT_EQ(2U, disk_mount_manager_->mount_points().size());
672 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
674 // Emulate to set kExternalStorageDisabled to false.
675 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
676 volume_manager()->OnExternalStorageDisabledChanged();
678 // Expect no effects.
679 EXPECT_EQ(2U, disk_mount_manager_->mount_points().size());
680 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
682 // Emulate to set kExternalStorageDisabled to true.
683 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
684 volume_manager()->OnExternalStorageDisabledChanged();
686 // The all mount points should be unmounted.
687 EXPECT_EQ(0U, disk_mount_manager_->mount_points().size());
689 EXPECT_EQ(2U, disk_mount_manager_->unmount_requests().size());
690 const FakeDiskMountManager::UnmountRequest& unmount_request1 =
691 disk_mount_manager_->unmount_requests()[0];
692 EXPECT_EQ("mount1", unmount_request1.mount_path);
694 const FakeDiskMountManager::UnmountRequest& unmount_request2 =
695 disk_mount_manager_->unmount_requests()[1];
696 EXPECT_EQ("mount2", unmount_request2.mount_path);
699 TEST_F(VolumeManagerTest, ExternalStorageDisabledPolicyMultiProfile) {
700 ProfileEnvironment secondary(power_manager_client_.get(),
701 disk_mount_manager_.get());
702 volume_manager()->Initialize();
703 secondary.volume_manager()->Initialize();
705 // Simulates the case that the main profile has kExternalStorageDisabled set
706 // as false, and the secondary profile has the config set to true.
707 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
708 secondary.profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled,
709 true);
711 LoggingObserver main_observer, secondary_observer;
712 volume_manager()->AddObserver(&main_observer);
713 secondary.volume_manager()->AddObserver(&secondary_observer);
715 // Add 1 disk.
716 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
717 "device1", "", "", "", "", "", "", "", "", "", "", "",
718 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false,
719 false);
720 volume_manager()->OnDiskEvent(
721 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
722 secondary.volume_manager()->OnDiskEvent(
723 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
725 // The profile with external storage enabled should have mounted the volume.
726 bool has_volume_mounted = false;
727 for (size_t i = 0; i < main_observer.events().size(); ++i) {
728 if (main_observer.events()[i].type ==
729 LoggingObserver::Event::VOLUME_MOUNTED)
730 has_volume_mounted = true;
732 EXPECT_TRUE(has_volume_mounted);
734 // The other profiles with external storage disabled should have not.
735 has_volume_mounted = false;
736 for (size_t i = 0; i < secondary_observer.events().size(); ++i) {
737 if (secondary_observer.events()[i].type ==
738 LoggingObserver::Event::VOLUME_MOUNTED)
739 has_volume_mounted = true;
741 EXPECT_FALSE(has_volume_mounted);
743 volume_manager()->RemoveObserver(&main_observer);
744 secondary.volume_manager()->RemoveObserver(&secondary_observer);
747 TEST_F(VolumeManagerTest, GetVolumeList) {
748 volume_manager()->Initialize(); // Adds "Downloads"
749 std::vector<base::WeakPtr<Volume>> volume_list =
750 volume_manager()->GetVolumeList();
751 ASSERT_EQ(1u, volume_list.size());
752 EXPECT_EQ("downloads:Downloads", volume_list[0]->volume_id());
753 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, volume_list[0]->type());
756 TEST_F(VolumeManagerTest, FindVolumeById) {
757 volume_manager()->Initialize(); // Adds "Downloads"
758 base::WeakPtr<Volume> bad_volume =
759 volume_manager()->FindVolumeById("nonexistent");
760 ASSERT_FALSE(bad_volume.get());
761 base::WeakPtr<Volume> good_volume =
762 volume_manager()->FindVolumeById("downloads:Downloads");
763 ASSERT_TRUE(good_volume.get());
764 EXPECT_EQ("downloads:Downloads", good_volume->volume_id());
765 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, good_volume->type());
768 TEST_F(VolumeManagerTest, ArchiveSourceFiltering) {
769 LoggingObserver observer;
770 volume_manager()->AddObserver(&observer);
772 // Mount a USB stick.
773 volume_manager()->OnMountEvent(
774 chromeos::disks::DiskMountManager::MOUNTING,
775 chromeos::MOUNT_ERROR_NONE,
776 chromeos::disks::DiskMountManager::MountPointInfo(
777 "/removable/usb",
778 "/removable/usb",
779 chromeos::MOUNT_TYPE_DEVICE,
780 chromeos::disks::MOUNT_CONDITION_NONE));
782 // Mount a zip archive in the stick.
783 volume_manager()->OnMountEvent(
784 chromeos::disks::DiskMountManager::MOUNTING,
785 chromeos::MOUNT_ERROR_NONE,
786 chromeos::disks::DiskMountManager::MountPointInfo(
787 "/removable/usb/1.zip",
788 "/archive/1",
789 chromeos::MOUNT_TYPE_ARCHIVE,
790 chromeos::disks::MOUNT_CONDITION_NONE));
791 base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("archive:1");
792 ASSERT_TRUE(volume.get());
793 EXPECT_EQ("/archive/1", volume->mount_path().AsUTF8Unsafe());
794 EXPECT_EQ(2u, observer.events().size());
796 // Mount a zip archive in the previous zip archive.
797 volume_manager()->OnMountEvent(
798 chromeos::disks::DiskMountManager::MOUNTING,
799 chromeos::MOUNT_ERROR_NONE,
800 chromeos::disks::DiskMountManager::MountPointInfo(
801 "/archive/1/2.zip",
802 "/archive/2",
803 chromeos::MOUNT_TYPE_ARCHIVE,
804 chromeos::disks::MOUNT_CONDITION_NONE));
805 base::WeakPtr<Volume> second_volume =
806 volume_manager()->FindVolumeById("archive:2");
807 ASSERT_TRUE(second_volume.get());
808 EXPECT_EQ("/archive/2", second_volume->mount_path().AsUTF8Unsafe());
809 EXPECT_EQ(3u, observer.events().size());
811 // A zip file is mounted from other profile. It must be ignored in the current
812 // VolumeManager.
813 volume_manager()->OnMountEvent(
814 chromeos::disks::DiskMountManager::MOUNTING,
815 chromeos::MOUNT_ERROR_NONE,
816 chromeos::disks::DiskMountManager::MountPointInfo(
817 "/other/profile/drive/folder/3.zip",
818 "/archive/3",
819 chromeos::MOUNT_TYPE_ARCHIVE,
820 chromeos::disks::MOUNT_CONDITION_NONE));
821 base::WeakPtr<Volume> third_volume =
822 volume_manager()->FindVolumeById("archive:3");
823 ASSERT_FALSE(third_volume.get());
824 EXPECT_EQ(3u, observer.events().size());
827 TEST_F(VolumeManagerTest, MTPPlugAndUnplug) {
828 LoggingObserver observer;
829 volume_manager()->AddObserver(&observer);
831 storage_monitor::StorageInfo info(
832 storage_monitor::StorageInfo::MakeDeviceId(
833 storage_monitor::StorageInfo::MTP_OR_PTP, "dummy-device-id"),
834 FILE_PATH_LITERAL("/dummy/device/location"),
835 base::UTF8ToUTF16("label"),
836 base::UTF8ToUTF16("vendor"),
837 base::UTF8ToUTF16("model"),
838 12345 /* size */);
840 storage_monitor::StorageInfo non_mtp_info(
841 storage_monitor::StorageInfo::MakeDeviceId(
842 storage_monitor::StorageInfo::IPHOTO, "dummy-device-id2"),
843 FILE_PATH_LITERAL("/dummy/device/location2"),
844 base::UTF8ToUTF16("label2"),
845 base::UTF8ToUTF16("vendor2"),
846 base::UTF8ToUTF16("model2"),
847 12345 /* size */);
849 // Attach
850 volume_manager()->OnRemovableStorageAttached(info);
851 ASSERT_EQ(1u, observer.events().size());
852 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, observer.events()[0].type);
854 base::WeakPtr<Volume> volume = volume_manager()->FindVolumeById("mtp:model");
855 EXPECT_EQ(VOLUME_TYPE_MTP, volume->type());
857 // Non MTP events from storage monitor are ignored.
858 volume_manager()->OnRemovableStorageAttached(non_mtp_info);
859 EXPECT_EQ(1u, observer.events().size());
861 // Detach
862 volume_manager()->OnRemovableStorageDetached(info);
863 ASSERT_EQ(2u, observer.events().size());
864 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED,
865 observer.events()[1].type);
867 EXPECT_FALSE(volume.get());
870 } // namespace file_manager