Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / volume_manager_unittest.cc
blob0ad31617c24df9d433f8e7f0ab2da395cd73b20c
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/prefs/pref_service.h"
12 #include "chrome/browser/chromeos/file_manager/fake_disk_mount_manager.h"
13 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
14 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h"
15 #include "chrome/browser/chromeos/file_system_provider/service.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chromeos/dbus/fake_power_manager_client.h"
19 #include "chromeos/disks/disk_mount_manager.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace file_manager {
25 namespace {
27 class LoggingObserver : public VolumeManagerObserver {
28 public:
29 struct Event {
30 enum EventType {
31 DISK_ADDED,
32 DISK_REMOVED,
33 DEVICE_ADDED,
34 DEVICE_REMOVED,
35 VOLUME_MOUNTED,
36 VOLUME_UNMOUNTED,
37 FORMAT_STARTED,
38 FORMAT_COMPLETED,
39 } type;
41 // Available on DEVICE_ADDED, DEVICE_REMOVED, VOLUME_MOUNTED,
42 // VOLUME_UNMOUNTED, FORMAT_STARTED and FORMAT_COMPLETED.
43 std::string device_path;
45 // Available on DISK_ADDED.
46 bool mounting;
48 // Available on DEVICE_REMOVED;
49 bool hard_unplugged;
51 // Available on VOLUME_MOUNTED and VOLUME_UNMOUNTED.
52 chromeos::MountError mount_error;
54 // Available on VOLUME_MOUNTED.
55 bool is_remounting;
57 // Available on FORMAT_STARTED and FORMAT_COMPLETED.
58 bool success;
61 LoggingObserver() {}
62 virtual ~LoggingObserver() {}
64 const std::vector<Event>& events() const { return events_; }
66 // VolumeManagerObserver overrides.
67 virtual void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk& disk,
68 bool mounting) OVERRIDE {
69 Event event;
70 event.type = Event::DISK_ADDED;
71 event.device_path = disk.device_path(); // Keep only device_path.
72 event.mounting = mounting;
73 events_.push_back(event);
76 virtual void OnDiskRemoved(
77 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE {
78 Event event;
79 event.type = Event::DISK_REMOVED;
80 event.device_path = disk.device_path(); // Keep only device_path.
81 events_.push_back(event);
84 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE {
85 Event event;
86 event.type = Event::DEVICE_ADDED;
87 event.device_path = device_path;
88 events_.push_back(event);
91 virtual void OnDeviceRemoved(const std::string& device_path,
92 bool hard_unplugged) OVERRIDE {
93 Event event;
94 event.type = Event::DEVICE_REMOVED;
95 event.device_path = device_path;
96 event.hard_unplugged = hard_unplugged;
97 events_.push_back(event);
100 virtual void OnVolumeMounted(chromeos::MountError error_code,
101 const VolumeInfo& volume_info,
102 bool is_remounting) OVERRIDE {
103 Event event;
104 event.type = Event::VOLUME_MOUNTED;
105 event.device_path = volume_info.source_path.AsUTF8Unsafe();
106 event.mount_error = error_code;
107 event.is_remounting = is_remounting;
108 events_.push_back(event);
111 virtual void OnVolumeUnmounted(chromeos::MountError error_code,
112 const VolumeInfo& volume_info) OVERRIDE {
113 Event event;
114 event.type = Event::VOLUME_UNMOUNTED;
115 event.device_path = volume_info.source_path.AsUTF8Unsafe();
116 event.mount_error = error_code;
117 events_.push_back(event);
120 virtual void OnFormatStarted(
121 const std::string& device_path, bool success) OVERRIDE {
122 Event event;
123 event.type = Event::FORMAT_STARTED;
124 event.device_path = device_path;
125 event.success = success;
126 events_.push_back(event);
129 virtual void OnFormatCompleted(
130 const std::string& device_path, bool success) OVERRIDE {
131 Event event;
132 event.type = Event::FORMAT_COMPLETED;
133 event.device_path = device_path;
134 event.success = success;
135 events_.push_back(event);
138 private:
139 std::vector<Event> events_;
141 DISALLOW_COPY_AND_ASSIGN(LoggingObserver);
144 } // namespace
146 class VolumeManagerTest : public testing::Test {
147 protected:
148 // Helper class that contains per-profile objects.
149 class ProfileEnvironment {
150 public:
151 ProfileEnvironment(chromeos::PowerManagerClient* power_manager_client,
152 chromeos::disks::DiskMountManager* disk_manager)
153 : profile_(new TestingProfile),
154 extension_registry_(
155 new extensions::ExtensionRegistry(profile_.get())),
156 file_system_provider_service_(
157 new chromeos::file_system_provider::Service(
158 profile_.get(),
159 extension_registry_.get())),
160 volume_manager_(
161 new VolumeManager(profile_.get(),
162 NULL, // DriveIntegrationService
163 power_manager_client,
164 disk_manager,
165 file_system_provider_service_.get())) {
166 file_system_provider_service_->SetFileSystemFactoryForTests(base::Bind(
167 &chromeos::file_system_provider::FakeProvidedFileSystem::Create));
170 Profile* profile() const { return profile_.get(); }
171 VolumeManager* volume_manager() const { return volume_manager_.get(); }
173 private:
174 scoped_ptr<TestingProfile> profile_;
175 scoped_ptr<extensions::ExtensionRegistry> extension_registry_;
176 scoped_ptr<chromeos::file_system_provider::Service>
177 file_system_provider_service_;
178 scoped_ptr<VolumeManager> volume_manager_;
181 virtual void SetUp() OVERRIDE {
182 power_manager_client_.reset(new chromeos::FakePowerManagerClient);
183 disk_mount_manager_.reset(new FakeDiskMountManager);
184 main_profile_.reset(new ProfileEnvironment(power_manager_client_.get(),
185 disk_mount_manager_.get()));
188 Profile* profile() const { return main_profile_->profile(); }
189 VolumeManager* volume_manager() const {
190 return main_profile_->volume_manager();
193 content::TestBrowserThreadBundle thread_bundle_;
194 scoped_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
195 scoped_ptr<FakeDiskMountManager> disk_mount_manager_;
196 scoped_ptr<ProfileEnvironment> main_profile_;
199 TEST_F(VolumeManagerTest, OnDriveFileSystemMountAndUnmount) {
200 LoggingObserver observer;
201 volume_manager()->AddObserver(&observer);
203 volume_manager()->OnFileSystemMounted();
205 ASSERT_EQ(1U, observer.events().size());
206 LoggingObserver::Event event = observer.events()[0];
207 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
208 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
209 event.device_path);
210 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
211 EXPECT_FALSE(event.is_remounting);
213 volume_manager()->OnFileSystemBeingUnmounted();
215 ASSERT_EQ(2U, observer.events().size());
216 event = observer.events()[1];
217 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
218 EXPECT_EQ(drive::util::GetDriveMountPointPath(profile()).AsUTF8Unsafe(),
219 event.device_path);
220 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
222 volume_manager()->RemoveObserver(&observer);
225 TEST_F(VolumeManagerTest, OnDriveFileSystemUnmountWithoutMount) {
226 LoggingObserver observer;
227 volume_manager()->AddObserver(&observer);
228 volume_manager()->OnFileSystemBeingUnmounted();
230 // Unmount event for non-mounted volume is not reported.
231 ASSERT_EQ(0U, observer.events().size());
232 volume_manager()->RemoveObserver(&observer);
235 TEST_F(VolumeManagerTest, OnDiskEvent_Hidden) {
236 LoggingObserver observer;
237 volume_manager()->AddObserver(&observer);
239 const bool kIsHidden = true;
240 const chromeos::disks::DiskMountManager::Disk kDisk(
241 "device1", "", "", "", "", "", "", "", "", "", "", "",
242 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, kIsHidden);
244 volume_manager()->OnDiskEvent(
245 chromeos::disks::DiskMountManager::DISK_ADDED, &kDisk);
246 EXPECT_EQ(0U, observer.events().size());
248 volume_manager()->OnDiskEvent(
249 chromeos::disks::DiskMountManager::DISK_REMOVED, &kDisk);
250 EXPECT_EQ(0U, observer.events().size());
252 volume_manager()->OnDiskEvent(
253 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
254 EXPECT_EQ(0U, observer.events().size());
256 volume_manager()->RemoveObserver(&observer);
259 TEST_F(VolumeManagerTest, OnDiskEvent_Added) {
260 // Enable external storage.
261 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
263 LoggingObserver observer;
264 volume_manager()->AddObserver(&observer);
266 const chromeos::disks::DiskMountManager::Disk kEmptyDevicePathDisk(
267 "", // empty device path.
268 "", "", "", "", "", "", "", "", "", "", "",
269 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
270 volume_manager()->OnDiskEvent(
271 chromeos::disks::DiskMountManager::DISK_ADDED, &kEmptyDevicePathDisk);
272 EXPECT_EQ(0U, observer.events().size());
274 const bool kHasMedia = true;
275 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
276 "device1", "", "", "", "", "", "", "", "", "", "", "",
277 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, kHasMedia, false, false);
278 volume_manager()->OnDiskEvent(
279 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
280 ASSERT_EQ(1U, observer.events().size());
281 const LoggingObserver::Event& event = observer.events()[0];
282 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
283 EXPECT_EQ("device1", event.device_path);
284 EXPECT_TRUE(event.mounting);
286 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
287 const FakeDiskMountManager::MountRequest& mount_request =
288 disk_mount_manager_->mount_requests()[0];
289 EXPECT_EQ("device1", mount_request.source_path);
290 EXPECT_EQ("", mount_request.source_format);
291 EXPECT_EQ("", mount_request.mount_label);
292 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
294 volume_manager()->RemoveObserver(&observer);
297 TEST_F(VolumeManagerTest, OnDiskEvent_AddedNonMounting) {
298 // Enable external storage.
299 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
301 // Device which is already mounted.
303 LoggingObserver observer;
304 volume_manager()->AddObserver(&observer);
306 const bool kHasMedia = true;
307 const chromeos::disks::DiskMountManager::Disk kMountedMediaDisk(
308 "device1", "mounted", "", "", "", "", "", "", "", "", "", "",
309 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
310 kHasMedia, false, false);
311 volume_manager()->OnDiskEvent(
312 chromeos::disks::DiskMountManager::DISK_ADDED, &kMountedMediaDisk);
313 ASSERT_EQ(1U, observer.events().size());
314 const LoggingObserver::Event& event = observer.events()[0];
315 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
316 EXPECT_EQ("device1", event.device_path);
317 EXPECT_FALSE(event.mounting);
319 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
321 volume_manager()->RemoveObserver(&observer);
324 // Device without media.
326 LoggingObserver observer;
327 volume_manager()->AddObserver(&observer);
329 const bool kWithoutMedia = false;
330 const chromeos::disks::DiskMountManager::Disk kNoMediaDisk(
331 "device1", "", "", "", "", "", "", "", "", "", "", "",
332 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
333 kWithoutMedia, false, false);
334 volume_manager()->OnDiskEvent(
335 chromeos::disks::DiskMountManager::DISK_ADDED, &kNoMediaDisk);
336 ASSERT_EQ(1U, observer.events().size());
337 const LoggingObserver::Event& event = observer.events()[0];
338 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
339 EXPECT_EQ("device1", event.device_path);
340 EXPECT_FALSE(event.mounting);
342 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
344 volume_manager()->RemoveObserver(&observer);
347 // External storage is disabled.
349 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
351 LoggingObserver observer;
352 volume_manager()->AddObserver(&observer);
354 const bool kHasMedia = true;
355 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
356 "device1", "", "", "", "", "", "", "", "", "", "", "",
357 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false,
358 kHasMedia, false, false);
359 volume_manager()->OnDiskEvent(
360 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
361 ASSERT_EQ(1U, observer.events().size());
362 const LoggingObserver::Event& event = observer.events()[0];
363 EXPECT_EQ(LoggingObserver::Event::DISK_ADDED, event.type);
364 EXPECT_EQ("device1", event.device_path);
365 EXPECT_FALSE(event.mounting);
367 ASSERT_EQ(0U, disk_mount_manager_->mount_requests().size());
369 volume_manager()->RemoveObserver(&observer);
373 TEST_F(VolumeManagerTest, OnDiskEvent_Removed) {
374 LoggingObserver observer;
375 volume_manager()->AddObserver(&observer);
377 const chromeos::disks::DiskMountManager::Disk kMountedDisk(
378 "device1", "mount_path", "", "", "", "", "", "", "", "", "", "",
379 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
380 volume_manager()->OnDiskEvent(
381 chromeos::disks::DiskMountManager::DISK_REMOVED, &kMountedDisk);
383 ASSERT_EQ(1U, observer.events().size());
384 const LoggingObserver::Event& event = observer.events()[0];
385 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
386 EXPECT_EQ("device1", event.device_path);
388 ASSERT_EQ(1U, disk_mount_manager_->unmount_requests().size());
389 const FakeDiskMountManager::UnmountRequest& unmount_request =
390 disk_mount_manager_->unmount_requests()[0];
391 EXPECT_EQ("mount_path", unmount_request.mount_path);
392 EXPECT_EQ(chromeos::UNMOUNT_OPTIONS_LAZY, unmount_request.options);
394 volume_manager()->RemoveObserver(&observer);
397 TEST_F(VolumeManagerTest, OnDiskEvent_RemovedNotMounted) {
398 LoggingObserver observer;
399 volume_manager()->AddObserver(&observer);
401 const chromeos::disks::DiskMountManager::Disk kNotMountedDisk(
402 "device1", "", "", "", "", "", "", "", "", "", "", "",
403 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false);
404 volume_manager()->OnDiskEvent(
405 chromeos::disks::DiskMountManager::DISK_REMOVED, &kNotMountedDisk);
407 ASSERT_EQ(1U, observer.events().size());
408 const LoggingObserver::Event& event = observer.events()[0];
409 EXPECT_EQ(LoggingObserver::Event::DISK_REMOVED, event.type);
410 EXPECT_EQ("device1", event.device_path);
412 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
414 volume_manager()->RemoveObserver(&observer);
417 TEST_F(VolumeManagerTest, OnDiskEvent_Changed) {
418 // Changed event should cause mounting (if possible).
419 LoggingObserver observer;
420 volume_manager()->AddObserver(&observer);
422 const chromeos::disks::DiskMountManager::Disk kDisk(
423 "device1", "", "", "", "", "", "", "", "", "", "", "",
424 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false);
425 volume_manager()->OnDiskEvent(
426 chromeos::disks::DiskMountManager::DISK_CHANGED, &kDisk);
428 EXPECT_EQ(1U, observer.events().size());
429 EXPECT_EQ(1U, disk_mount_manager_->mount_requests().size());
430 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
432 volume_manager()->RemoveObserver(&observer);
435 TEST_F(VolumeManagerTest, OnDeviceEvent_Added) {
436 LoggingObserver observer;
437 volume_manager()->AddObserver(&observer);
439 volume_manager()->OnDeviceEvent(
440 chromeos::disks::DiskMountManager::DEVICE_ADDED, "device1");
442 ASSERT_EQ(1U, observer.events().size());
443 const LoggingObserver::Event& event = observer.events()[0];
444 EXPECT_EQ(LoggingObserver::Event::DEVICE_ADDED, event.type);
445 EXPECT_EQ("device1", event.device_path);
447 volume_manager()->RemoveObserver(&observer);
450 TEST_F(VolumeManagerTest, OnDeviceEvent_Removed) {
451 LoggingObserver observer;
452 volume_manager()->AddObserver(&observer);
454 volume_manager()->OnDeviceEvent(
455 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
457 ASSERT_EQ(1U, observer.events().size());
458 const LoggingObserver::Event& event = observer.events()[0];
459 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, event.type);
460 EXPECT_EQ("device1", event.device_path);
462 volume_manager()->RemoveObserver(&observer);
465 TEST_F(VolumeManagerTest, OnDeviceEvent_Scanned) {
466 LoggingObserver observer;
467 volume_manager()->AddObserver(&observer);
469 volume_manager()->OnDeviceEvent(
470 chromeos::disks::DiskMountManager::DEVICE_SCANNED, "device1");
472 // SCANNED event is just ignored.
473 EXPECT_EQ(0U, observer.events().size());
475 volume_manager()->RemoveObserver(&observer);
478 TEST_F(VolumeManagerTest, OnMountEvent_MountingAndUnmounting) {
479 LoggingObserver observer;
480 volume_manager()->AddObserver(&observer);
482 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
483 "device1",
484 "mount1",
485 chromeos::MOUNT_TYPE_DEVICE,
486 chromeos::disks::MOUNT_CONDITION_NONE);
488 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::MOUNTING,
489 chromeos::MOUNT_ERROR_NONE,
490 kMountPoint);
492 ASSERT_EQ(1U, observer.events().size());
493 LoggingObserver::Event event = observer.events()[0];
494 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
495 EXPECT_EQ("device1", event.device_path);
496 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
497 EXPECT_FALSE(event.is_remounting);
499 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
500 chromeos::MOUNT_ERROR_NONE,
501 kMountPoint);
503 ASSERT_EQ(2U, observer.events().size());
504 event = observer.events()[1];
505 EXPECT_EQ(LoggingObserver::Event::VOLUME_UNMOUNTED, event.type);
506 EXPECT_EQ("device1", event.device_path);
507 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
509 volume_manager()->RemoveObserver(&observer);
512 TEST_F(VolumeManagerTest, OnMountEvent_Remounting) {
513 scoped_ptr<chromeos::disks::DiskMountManager::Disk> disk(
514 new chromeos::disks::DiskMountManager::Disk(
515 "device1", "", "", "", "", "", "", "", "", "", "uuid1", "",
516 chromeos::DEVICE_TYPE_UNKNOWN, 0,
517 false, false, false, false, false));
518 disk_mount_manager_->AddDiskForTest(disk.release());
519 disk_mount_manager_->MountPath(
520 "device1", "", "", chromeos::MOUNT_TYPE_DEVICE);
522 // Emulate system suspend and then resume.
524 power_manager_client_->SendSuspendImminent();
525 power_manager_client_->SendSuspendDone();
527 // After resume, the device is unmounted and then mounted.
528 disk_mount_manager_->UnmountPath(
529 "device1", chromeos::UNMOUNT_OPTIONS_NONE,
530 chromeos::disks::DiskMountManager::UnmountPathCallback());
531 disk_mount_manager_->MountPath(
532 "device1", "", "", chromeos::MOUNT_TYPE_DEVICE);
535 LoggingObserver observer;
536 volume_manager()->AddObserver(&observer);
538 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
539 "device1",
540 "mount1",
541 chromeos::MOUNT_TYPE_DEVICE,
542 chromeos::disks::MOUNT_CONDITION_NONE);
544 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::MOUNTING,
545 chromeos::MOUNT_ERROR_NONE,
546 kMountPoint);
548 ASSERT_EQ(1U, observer.events().size());
549 const LoggingObserver::Event& event = observer.events()[0];
550 EXPECT_EQ(LoggingObserver::Event::VOLUME_MOUNTED, event.type);
551 EXPECT_EQ("device1", event.device_path);
552 EXPECT_EQ(chromeos::MOUNT_ERROR_NONE, event.mount_error);
553 EXPECT_TRUE(event.is_remounting);
555 volume_manager()->RemoveObserver(&observer);
558 TEST_F(VolumeManagerTest, OnMountEvent_UnmountingWithoutMounting) {
559 LoggingObserver observer;
560 volume_manager()->AddObserver(&observer);
562 const chromeos::disks::DiskMountManager::MountPointInfo kMountPoint(
563 "device1",
564 "mount1",
565 chromeos::MOUNT_TYPE_DEVICE,
566 chromeos::disks::MOUNT_CONDITION_NONE);
568 volume_manager()->OnMountEvent(chromeos::disks::DiskMountManager::UNMOUNTING,
569 chromeos::MOUNT_ERROR_NONE,
570 kMountPoint);
572 // Unmount event for a disk not mounted in this manager is not reported.
573 ASSERT_EQ(0U, observer.events().size());
575 volume_manager()->RemoveObserver(&observer);
578 TEST_F(VolumeManagerTest, OnFormatEvent_Started) {
579 LoggingObserver observer;
580 volume_manager()->AddObserver(&observer);
582 volume_manager()->OnFormatEvent(
583 chromeos::disks::DiskMountManager::FORMAT_STARTED,
584 chromeos::FORMAT_ERROR_NONE,
585 "device1");
587 ASSERT_EQ(1U, observer.events().size());
588 const LoggingObserver::Event& event = observer.events()[0];
589 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
590 EXPECT_EQ("device1", event.device_path);
591 EXPECT_TRUE(event.success);
593 volume_manager()->RemoveObserver(&observer);
596 TEST_F(VolumeManagerTest, OnFormatEvent_StartFailed) {
597 LoggingObserver observer;
598 volume_manager()->AddObserver(&observer);
600 volume_manager()->OnFormatEvent(
601 chromeos::disks::DiskMountManager::FORMAT_STARTED,
602 chromeos::FORMAT_ERROR_UNKNOWN,
603 "device1");
605 ASSERT_EQ(1U, observer.events().size());
606 const LoggingObserver::Event& event = observer.events()[0];
607 EXPECT_EQ(LoggingObserver::Event::FORMAT_STARTED, event.type);
608 EXPECT_EQ("device1", event.device_path);
609 EXPECT_FALSE(event.success);
611 volume_manager()->RemoveObserver(&observer);
614 TEST_F(VolumeManagerTest, OnFormatEvent_Completed) {
615 LoggingObserver observer;
616 volume_manager()->AddObserver(&observer);
618 volume_manager()->OnFormatEvent(
619 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
620 chromeos::FORMAT_ERROR_NONE,
621 "device1");
623 ASSERT_EQ(1U, observer.events().size());
624 const LoggingObserver::Event& event = observer.events()[0];
625 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
626 EXPECT_EQ("device1", event.device_path);
627 EXPECT_TRUE(event.success);
629 // When "format" is successfully done, VolumeManager requests to mount it.
630 ASSERT_EQ(1U, disk_mount_manager_->mount_requests().size());
631 const FakeDiskMountManager::MountRequest& mount_request =
632 disk_mount_manager_->mount_requests()[0];
633 EXPECT_EQ("device1", mount_request.source_path);
634 EXPECT_EQ("", mount_request.source_format);
635 EXPECT_EQ("", mount_request.mount_label);
636 EXPECT_EQ(chromeos::MOUNT_TYPE_DEVICE, mount_request.type);
638 volume_manager()->RemoveObserver(&observer);
641 TEST_F(VolumeManagerTest, OnFormatEvent_CompletedFailed) {
642 LoggingObserver observer;
643 volume_manager()->AddObserver(&observer);
645 volume_manager()->OnFormatEvent(
646 chromeos::disks::DiskMountManager::FORMAT_COMPLETED,
647 chromeos::FORMAT_ERROR_UNKNOWN,
648 "device1");
650 ASSERT_EQ(1U, observer.events().size());
651 const LoggingObserver::Event& event = observer.events()[0];
652 EXPECT_EQ(LoggingObserver::Event::FORMAT_COMPLETED, event.type);
653 EXPECT_EQ("device1", event.device_path);
654 EXPECT_FALSE(event.success);
656 EXPECT_EQ(0U, disk_mount_manager_->mount_requests().size());
658 volume_manager()->RemoveObserver(&observer);
661 TEST_F(VolumeManagerTest, OnExternalStorageDisabledChanged) {
662 // Here create two mount points.
663 disk_mount_manager_->MountPath(
664 "mount1", "", "", chromeos::MOUNT_TYPE_DEVICE);
665 disk_mount_manager_->MountPath(
666 "mount2", "", "", chromeos::MOUNT_TYPE_DEVICE);
668 // Initially, there are two mount points.
669 ASSERT_EQ(2U, disk_mount_manager_->mount_points().size());
670 ASSERT_EQ(0U, disk_mount_manager_->unmount_requests().size());
672 // Emulate to set kExternalStorageDisabled to false.
673 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
674 volume_manager()->OnExternalStorageDisabledChanged();
676 // Expect no effects.
677 EXPECT_EQ(2U, disk_mount_manager_->mount_points().size());
678 EXPECT_EQ(0U, disk_mount_manager_->unmount_requests().size());
680 // Emulate to set kExternalStorageDisabled to true.
681 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, true);
682 volume_manager()->OnExternalStorageDisabledChanged();
684 // The all mount points should be unmounted.
685 EXPECT_EQ(0U, disk_mount_manager_->mount_points().size());
687 EXPECT_EQ(2U, disk_mount_manager_->unmount_requests().size());
688 const FakeDiskMountManager::UnmountRequest& unmount_request1 =
689 disk_mount_manager_->unmount_requests()[0];
690 EXPECT_EQ("mount1", unmount_request1.mount_path);
692 const FakeDiskMountManager::UnmountRequest& unmount_request2 =
693 disk_mount_manager_->unmount_requests()[1];
694 EXPECT_EQ("mount2", unmount_request2.mount_path);
697 TEST_F(VolumeManagerTest, ExternalStorageDisabledPolicyMultiProfile) {
698 ProfileEnvironment secondary(power_manager_client_.get(),
699 disk_mount_manager_.get());
700 volume_manager()->Initialize();
701 secondary.volume_manager()->Initialize();
703 // Simulates the case that the main profile has kExternalStorageDisabled set
704 // as false, and the secondary profile has the config set to true.
705 profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled, false);
706 secondary.profile()->GetPrefs()->SetBoolean(prefs::kExternalStorageDisabled,
707 true);
709 LoggingObserver main_observer, secondary_observer;
710 volume_manager()->AddObserver(&main_observer);
711 secondary.volume_manager()->AddObserver(&secondary_observer);
713 // Add 1 disk.
714 const chromeos::disks::DiskMountManager::Disk kMediaDisk(
715 "device1", "", "", "", "", "", "", "", "", "", "", "",
716 chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, true, false, false);
717 volume_manager()->OnDiskEvent(
718 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
719 secondary.volume_manager()->OnDiskEvent(
720 chromeos::disks::DiskMountManager::DISK_ADDED, &kMediaDisk);
722 // The profile with external storage enabled should have mounted the volume.
723 bool has_volume_mounted = false;
724 for (size_t i = 0; i < main_observer.events().size(); ++i) {
725 if (main_observer.events()[i].type ==
726 LoggingObserver::Event::VOLUME_MOUNTED)
727 has_volume_mounted = true;
729 EXPECT_TRUE(has_volume_mounted);
731 // The other profiles with external storage disabled should have not.
732 has_volume_mounted = false;
733 for (size_t i = 0; i < secondary_observer.events().size(); ++i) {
734 if (secondary_observer.events()[i].type ==
735 LoggingObserver::Event::VOLUME_MOUNTED)
736 has_volume_mounted = true;
738 EXPECT_FALSE(has_volume_mounted);
740 volume_manager()->RemoveObserver(&main_observer);
741 secondary.volume_manager()->RemoveObserver(&secondary_observer);
744 TEST_F(VolumeManagerTest, GetVolumeInfoList) {
745 volume_manager()->Initialize(); // Adds "Downloads"
746 std::vector<VolumeInfo> info_list = volume_manager()->GetVolumeInfoList();
747 ASSERT_EQ(1u, info_list.size());
748 EXPECT_EQ("downloads:Downloads", info_list[0].volume_id);
749 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, info_list[0].type);
752 TEST_F(VolumeManagerTest, FindVolumeInfoById) {
753 volume_manager()->Initialize(); // Adds "Downloads"
754 VolumeInfo volume_info;
755 ASSERT_FALSE(volume_manager()->FindVolumeInfoById(
756 "nonexistent", &volume_info));
757 ASSERT_TRUE(volume_manager()->FindVolumeInfoById(
758 "downloads:Downloads", &volume_info));
759 EXPECT_EQ("downloads:Downloads", volume_info.volume_id);
760 EXPECT_EQ(VOLUME_TYPE_DOWNLOADS_DIRECTORY, volume_info.type);
763 TEST_F(VolumeManagerTest, ArchiveSourceFiltering) {
764 LoggingObserver observer;
765 volume_manager()->AddObserver(&observer);
767 // Mount a USB stick.
768 volume_manager()->OnMountEvent(
769 chromeos::disks::DiskMountManager::MOUNTING,
770 chromeos::MOUNT_ERROR_NONE,
771 chromeos::disks::DiskMountManager::MountPointInfo(
772 "/removable/usb",
773 "/removable/usb",
774 chromeos::MOUNT_TYPE_DEVICE,
775 chromeos::disks::MOUNT_CONDITION_NONE));
777 // Mount a zip archive in the stick.
778 volume_manager()->OnMountEvent(
779 chromeos::disks::DiskMountManager::MOUNTING,
780 chromeos::MOUNT_ERROR_NONE,
781 chromeos::disks::DiskMountManager::MountPointInfo(
782 "/removable/usb/1.zip",
783 "/archive/1",
784 chromeos::MOUNT_TYPE_ARCHIVE,
785 chromeos::disks::MOUNT_CONDITION_NONE));
786 VolumeInfo volume_info;
787 ASSERT_TRUE(volume_manager()->FindVolumeInfoById("archive:1", &volume_info));
788 EXPECT_EQ("/archive/1", volume_info.mount_path.AsUTF8Unsafe());
789 EXPECT_EQ(2u, observer.events().size());
791 // Mount a zip archive in the previous zip archive.
792 volume_manager()->OnMountEvent(
793 chromeos::disks::DiskMountManager::MOUNTING,
794 chromeos::MOUNT_ERROR_NONE,
795 chromeos::disks::DiskMountManager::MountPointInfo(
796 "/archive/1/2.zip",
797 "/archive/2",
798 chromeos::MOUNT_TYPE_ARCHIVE,
799 chromeos::disks::MOUNT_CONDITION_NONE));
800 ASSERT_TRUE(volume_manager()->FindVolumeInfoById("archive:2", &volume_info));
801 EXPECT_EQ("/archive/2", volume_info.mount_path.AsUTF8Unsafe());
802 EXPECT_EQ(3u, observer.events().size());
804 // A zip file is mounted from other profile. It must be ignored in the current
805 // VolumeManager.
806 volume_manager()->OnMountEvent(
807 chromeos::disks::DiskMountManager::MOUNTING,
808 chromeos::MOUNT_ERROR_NONE,
809 chromeos::disks::DiskMountManager::MountPointInfo(
810 "/other/profile/drive/folder/3.zip",
811 "/archive/3",
812 chromeos::MOUNT_TYPE_ARCHIVE,
813 chromeos::disks::MOUNT_CONDITION_NONE));
814 EXPECT_FALSE(volume_manager()->FindVolumeInfoById("archive:3", &volume_info));
815 EXPECT_EQ(3u, observer.events().size());
818 TEST_F(VolumeManagerTest, HardUnplugged) {
819 LoggingObserver observer;
820 volume_manager()->AddObserver(&observer);
821 volume_manager()->OnDeviceEvent(
822 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
824 // Disk that has a mount path is removed.
825 chromeos::disks::DiskMountManager::Disk disk("device1",
826 "/mount/path",
835 "uuid1",
836 "device1",
837 chromeos::DEVICE_TYPE_UNKNOWN,
839 false,
840 false,
841 false,
842 false,
843 false);
844 disk_mount_manager_->InvokeDiskEventForTest(
845 chromeos::disks::DiskMountManager::DISK_REMOVED, &disk);
847 volume_manager()->OnDeviceEvent(
848 chromeos::disks::DiskMountManager::DEVICE_REMOVED, "device1");
850 EXPECT_EQ(2u, observer.events().size());
851 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, observer.events()[0].type);
852 EXPECT_EQ(LoggingObserver::Event::DEVICE_REMOVED, observer.events()[1].type);
853 EXPECT_FALSE(observer.events()[0].hard_unplugged);
854 EXPECT_TRUE(observer.events()[1].hard_unplugged);
857 } // namespace file_manager