1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/storage_monitor/storage_monitor.h"
7 #include "base/functional/bind.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/test/task_environment.h"
12 #include "components/storage_monitor/mock_removable_storage_observer.h"
13 #include "components/storage_monitor/test_storage_monitor.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 void SetLatch(bool* called) {
24 namespace storage_monitor {
26 TEST(StorageMonitorTest, TestInitialize) {
27 base::test::SingleThreadTaskEnvironment task_environment;
28 TestStorageMonitor::Destroy();
29 TestStorageMonitor monitor;
30 EXPECT_FALSE(monitor.init_called());
32 bool initialized = false;
33 monitor.EnsureInitialized(base::BindOnce(&SetLatch, &initialized));
34 EXPECT_TRUE(monitor.init_called());
35 EXPECT_FALSE(initialized);
36 monitor.MarkInitialized();
37 EXPECT_TRUE(initialized);
40 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) {
41 TestStorageMonitor::Destroy();
42 base::test::SingleThreadTaskEnvironment task_environment;
43 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001";
44 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002";
45 MockRemovableStorageObserver observer1;
46 MockRemovableStorageObserver observer2;
47 TestStorageMonitor monitor;
48 monitor.AddObserver(&observer1);
49 monitor.AddObserver(&observer2);
51 StorageInfo info(kDeviceId1, FILE_PATH_LITERAL("path"), std::u16string(),
52 std::u16string(), std::u16string(), 0);
53 monitor.receiver()->ProcessAttach(info);
54 base::RunLoop().RunUntilIdle();
56 EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id());
57 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_attached().location());
58 EXPECT_EQ(kDeviceId1, observer2.last_attached().device_id());
59 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_attached().location());
60 EXPECT_EQ(1, observer1.attach_calls());
61 EXPECT_EQ(0, observer1.detach_calls());
63 monitor.receiver()->ProcessDetach(kDeviceId1);
64 monitor.receiver()->ProcessDetach(kDeviceId2);
65 base::RunLoop().RunUntilIdle();
67 EXPECT_EQ(kDeviceId1, observer1.last_detached().device_id());
68 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_detached().location());
69 EXPECT_EQ(kDeviceId1, observer2.last_detached().device_id());
70 EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_detached().location());
72 EXPECT_EQ(1, observer1.attach_calls());
73 EXPECT_EQ(1, observer2.attach_calls());
75 // The kDeviceId2 won't be notified since it was never attached.
76 EXPECT_EQ(1, observer1.detach_calls());
77 EXPECT_EQ(1, observer2.detach_calls());
79 monitor.RemoveObserver(&observer1);
80 monitor.RemoveObserver(&observer2);
83 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) {
84 TestStorageMonitor::Destroy();
85 base::test::SingleThreadTaskEnvironment task_environment;
86 TestStorageMonitor monitor;
87 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages();
88 EXPECT_EQ(0U, devices.size());
91 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) {
92 TestStorageMonitor::Destroy();
93 base::test::SingleThreadTaskEnvironment task_environment;
94 TestStorageMonitor monitor;
95 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042";
96 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
97 StorageInfo info1(kDeviceId1, kDevicePath1.value(), std::u16string(),
98 std::u16string(), std::u16string(), 0);
99 monitor.receiver()->ProcessAttach(info1);
100 base::RunLoop().RunUntilIdle();
101 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages();
102 ASSERT_EQ(1U, devices.size());
103 EXPECT_EQ(kDeviceId1, devices[0].device_id());
104 EXPECT_EQ(kDevicePath1.value(), devices[0].location());
106 const std::string kDeviceId2 = "dcim:UUID:FFF0-0044";
107 const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar"));
108 StorageInfo info2(kDeviceId2, kDevicePath2.value(), std::u16string(),
109 std::u16string(), std::u16string(), 0);
110 monitor.receiver()->ProcessAttach(info2);
111 base::RunLoop().RunUntilIdle();
112 devices = monitor.GetAllAvailableStorages();
113 ASSERT_EQ(2U, devices.size());
114 EXPECT_EQ(kDeviceId1, devices[0].device_id());
115 EXPECT_EQ(kDevicePath1.value(), devices[0].location());
116 EXPECT_EQ(kDeviceId2, devices[1].device_id());
117 EXPECT_EQ(kDevicePath2.value(), devices[1].location());
119 monitor.receiver()->ProcessDetach(kDeviceId1);
120 base::RunLoop().RunUntilIdle();
121 devices = monitor.GetAllAvailableStorages();
122 ASSERT_EQ(1U, devices.size());
123 EXPECT_EQ(kDeviceId2, devices[0].device_id());
124 EXPECT_EQ(kDevicePath2.value(), devices[0].location());
126 monitor.receiver()->ProcessDetach(kDeviceId2);
127 base::RunLoop().RunUntilIdle();
128 devices = monitor.GetAllAvailableStorages();
129 EXPECT_EQ(0U, devices.size());
132 } // namespace storage_monitor