Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / storage_monitor_unittest.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "components/storage_monitor/mock_removable_storage_observer.h"
9 #include "components/storage_monitor/storage_monitor.h"
10 #include "components/storage_monitor/test_storage_monitor.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 void SetLatch(bool* called) {
16   *called = true;
17 }
18
19 }  // namespace
20
21 TEST(StorageMonitorTest, TestInitialize) {
22   TestStorageMonitor::Destroy();
23   TestStorageMonitor monitor;
24   EXPECT_FALSE(monitor.init_called());
25
26   bool initialized = false;
27   monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized));
28   EXPECT_TRUE(monitor.init_called());
29   EXPECT_FALSE(initialized);
30   monitor.MarkInitialized();
31   EXPECT_TRUE(initialized);
32 }
33
34 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) {
35   TestStorageMonitor::Destroy();
36   base::MessageLoop message_loop;
37   const base::string16 kDeviceName = base::ASCIIToUTF16("media device");
38   const std::string kDeviceId1 = "dcim:UUID:FFF0-0001";
39   const std::string kDeviceId2 = "dcim:UUID:FFF0-0002";
40   MockRemovableStorageObserver observer1;
41   MockRemovableStorageObserver observer2;
42   TestStorageMonitor monitor;
43   monitor.AddObserver(&observer1);
44   monitor.AddObserver(&observer2);
45
46   StorageInfo info(kDeviceId1, kDeviceName, FILE_PATH_LITERAL("path"),
47                    base::string16(), base::string16(), base::string16(), 0);
48   monitor.receiver()->ProcessAttach(info);
49   message_loop.RunUntilIdle();
50
51   EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id());
52   EXPECT_EQ(kDeviceName, observer1.last_attached().name());
53   EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_attached().location());
54   EXPECT_EQ(kDeviceId1, observer2.last_attached().device_id());
55   EXPECT_EQ(kDeviceName, observer2.last_attached().name());
56   EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_attached().location());
57   EXPECT_EQ(1, observer1.attach_calls());
58   EXPECT_EQ(0, observer1.detach_calls());
59
60   monitor.receiver()->ProcessDetach(kDeviceId1);
61   monitor.receiver()->ProcessDetach(kDeviceId2);
62   message_loop.RunUntilIdle();
63
64   EXPECT_EQ(kDeviceId1, observer1.last_detached().device_id());
65   EXPECT_EQ(kDeviceName, observer1.last_detached().name());
66   EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_detached().location());
67   EXPECT_EQ(kDeviceId1, observer2.last_detached().device_id());
68   EXPECT_EQ(kDeviceName, observer2.last_detached().name());
69   EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_detached().location());
70
71   EXPECT_EQ(1, observer1.attach_calls());
72   EXPECT_EQ(1, observer2.attach_calls());
73
74   // The kDeviceId2 won't be notified since it was never attached.
75   EXPECT_EQ(1, observer1.detach_calls());
76   EXPECT_EQ(1, observer2.detach_calls());
77
78   monitor.RemoveObserver(&observer1);
79   monitor.RemoveObserver(&observer2);
80 }
81
82 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) {
83   TestStorageMonitor::Destroy();
84   base::MessageLoop message_loop;
85   TestStorageMonitor monitor;
86   std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages();
87   EXPECT_EQ(0U, devices.size());
88 }
89
90 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) {
91   TestStorageMonitor::Destroy();
92   base::MessageLoop message_loop;
93   TestStorageMonitor monitor;
94   const std::string kDeviceId1 = "dcim:UUID:FFF0-0042";
95   const base::string16 kDeviceName1 = base::ASCIIToUTF16("test");
96   const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
97   StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(),
98                     base::string16(), base::string16(), base::string16(), 0);
99   monitor.receiver()->ProcessAttach(info1);
100   message_loop.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(kDeviceName1, devices[0].name());
105   EXPECT_EQ(kDevicePath1.value(), devices[0].location());
106
107   const std::string kDeviceId2 = "dcim:UUID:FFF0-0044";
108   const base::string16 kDeviceName2 = base::ASCIIToUTF16("test2");
109   const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar"));
110   StorageInfo info2(kDeviceId2, kDeviceName2, kDevicePath2.value(),
111                     base::string16(), base::string16(), base::string16(), 0);
112   monitor.receiver()->ProcessAttach(info2);
113   message_loop.RunUntilIdle();
114   devices = monitor.GetAllAvailableStorages();
115   ASSERT_EQ(2U, devices.size());
116   EXPECT_EQ(kDeviceId1, devices[0].device_id());
117   EXPECT_EQ(kDeviceName1, devices[0].name());
118   EXPECT_EQ(kDevicePath1.value(), devices[0].location());
119   EXPECT_EQ(kDeviceId2, devices[1].device_id());
120   EXPECT_EQ(kDeviceName2, devices[1].name());
121   EXPECT_EQ(kDevicePath2.value(), devices[1].location());
122
123   monitor.receiver()->ProcessDetach(kDeviceId1);
124   message_loop.RunUntilIdle();
125   devices = monitor.GetAllAvailableStorages();
126   ASSERT_EQ(1U, devices.size());
127   EXPECT_EQ(kDeviceId2, devices[0].device_id());
128   EXPECT_EQ(kDeviceName2, devices[0].name());
129   EXPECT_EQ(kDevicePath2.value(), devices[0].location());
130
131   monitor.receiver()->ProcessDetach(kDeviceId2);
132   message_loop.RunUntilIdle();
133   devices = monitor.GetAllAvailableStorages();
134   EXPECT_EQ(0U, devices.size());
135 }