OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 // MediaDeviceNotificationsLinux unit tests. |
| 6 |
| 7 #include "content/browser/media_device_notifications_linux.h" |
| 8 |
5 #include <mntent.h> | 9 #include <mntent.h> |
6 #include <stdio.h> | 10 #include <stdio.h> |
7 #include <string.h> | 11 |
| 12 #include <string> |
8 | 13 |
9 #include "base/file_util.h" | 14 #include "base/file_util.h" |
10 #include "base/logging.h" | 15 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
13 #include "base/scoped_temp_dir.h" | 18 #include "base/scoped_temp_dir.h" |
14 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
15 #include "base/test/mock_devices_changed_observer.h" | 20 #include "base/test/mock_devices_changed_observer.h" |
16 #include "content/browser/browser_thread_impl.h" | 21 #include "content/browser/browser_thread_impl.h" |
17 #include "content/browser/media_device_notifications_linux.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
19 | 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
20 using testing::_; | 28 using testing::_; |
21 | 29 |
22 namespace { | 30 const char kValidFS[] = "vfat"; |
| 31 const char kInvalidFS[] = "invalidfs"; |
23 | 32 |
24 const char* kValidFS = "vfat"; | 33 const char kInvalidPath[] = "invalid path does not exist"; |
25 const char* kInvalidFS = "invalidfs"; | |
26 | 34 |
27 const char* kInvalidPath = "invalid path does not exist"; | 35 const char kDevice1[] = "d1"; |
| 36 const char kDevice2[] = "d2"; |
| 37 const char kDevice3[] = "d3"; |
28 | 38 |
29 const char* kDevice1 = "d1"; | 39 const char kMountPointA[] = "mnt_a"; |
30 const char* kDevice2 = "d2"; | 40 const char kMountPointB[] = "mnt_b"; |
31 const char* kDevice3 = "d3"; | |
32 | 41 |
33 const char* kMountPointA = "mnt_a"; | 42 // TODO(thestig) Move this into base/string_util.h, or replace this with |
34 const char* kMountPointB = "mnt_b"; | 43 // strndup_with_new() if we find more uses for it. |
35 | 44 // Duplicate the content of |str| into a new char array. Caller takes ownership |
36 } // namespace | 45 // of the allocated array. Unlike std::string::c_str(), this returns a char* |
37 | 46 // instead of a const char*. |
38 namespace content { | 47 char* copy_string(const std::string& str) { |
| 48 const size_t len = str.length(); |
| 49 char* ret = new char[len + 1]; |
| 50 str.copy(ret, len, 0); |
| 51 ret[len] = '\0'; |
| 52 return ret; |
| 53 } |
39 | 54 |
40 class MediaDeviceNotificationsLinuxTestWrapper | 55 class MediaDeviceNotificationsLinuxTestWrapper |
41 : public MediaDeviceNotificationsLinux { | 56 : public MediaDeviceNotificationsLinux { |
42 public: | 57 public: |
43 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 58 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
44 MessageLoop* message_loop) | 59 MessageLoop* message_loop) |
45 : MediaDeviceNotificationsLinux(path), | 60 : MediaDeviceNotificationsLinux(path), |
46 message_loop_(message_loop) { | 61 message_loop_(message_loop) { |
47 } | 62 } |
48 | 63 |
49 protected: | 64 private: |
| 65 // Avoids code deleting the object while there are references to it. |
| 66 // Aside from the base::RefCountedThreadSafe friend class, any attempts to |
| 67 // call this dtor will result in a compile-time error. |
50 ~MediaDeviceNotificationsLinuxTestWrapper() {} | 68 ~MediaDeviceNotificationsLinuxTestWrapper() {} |
51 | 69 |
52 virtual void OnFilePathChanged(const FilePath& path) { | 70 virtual void OnFilePathChanged(const FilePath& path) { |
53 MediaDeviceNotificationsLinux::OnFilePathChanged(path); | 71 MediaDeviceNotificationsLinux::OnFilePathChanged(path); |
54 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 72 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
55 } | 73 } |
56 | 74 |
57 private: | |
58 MessageLoop* message_loop_; | 75 MessageLoop* message_loop_; |
59 | 76 |
60 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); | 77 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); |
61 }; | 78 }; |
62 | 79 |
63 class MediaDeviceNotificationsLinuxTest : public testing::Test { | 80 class MediaDeviceNotificationsLinuxTest : public testing::Test { |
64 public: | 81 public: |
65 struct MtabTestData { | 82 struct MtabTestData { |
66 MtabTestData(const char* mount_device, | 83 MtabTestData(const std::string& mount_device, |
67 const char* mount_point, | 84 const std::string& mount_point, |
68 const char* mount_type) | 85 const std::string& mount_type) |
69 : mount_device(mount_device), | 86 : mount_device(mount_device), |
70 mount_point(mount_point), | 87 mount_point(mount_point), |
71 mount_type(mount_type) { | 88 mount_type(mount_type) { |
72 } | 89 } |
73 | 90 |
74 const char* mount_device; | 91 const std::string mount_device; |
75 const char* mount_point; | 92 const std::string mount_point; |
76 const char* mount_type; | 93 const std::string mount_type; |
77 }; | 94 }; |
78 | 95 |
79 MediaDeviceNotificationsLinuxTest() | 96 MediaDeviceNotificationsLinuxTest() |
80 : message_loop_(MessageLoop::TYPE_IO), | 97 : message_loop_(MessageLoop::TYPE_IO), |
81 file_thread_(BrowserThread::FILE, &message_loop_) { | 98 file_thread_(BrowserThread::FILE, &message_loop_) { |
82 system_monitor_.reset(new base::SystemMonitor()); | |
83 } | 99 } |
84 virtual ~MediaDeviceNotificationsLinuxTest() {} | 100 virtual ~MediaDeviceNotificationsLinuxTest() {} |
85 | 101 |
86 protected: | 102 protected: |
87 virtual void SetUp() { | 103 virtual void SetUp() { |
88 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); | 104 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
89 system_monitor_->AddDevicesChangedObserver( | 105 system_monitor_.AddDevicesChangedObserver( |
90 mock_devices_changed_observer_.get()); | 106 mock_devices_changed_observer_.get()); |
91 | 107 |
92 // Create and set up a temp dir with files for the test. | 108 // Create and set up a temp dir with files for the test. |
93 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 109 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
94 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); | 110 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); |
95 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 111 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
96 mtab_file_ = test_dir.AppendASCII("test_mtab"); | 112 mtab_file_ = test_dir.AppendASCII("test_mtab"); |
97 struct MtabTestData initial_test_data[] = { | 113 MtabTestData initial_test_data[] = { |
98 MtabTestData("dummydevice", "dummydir", kInvalidFS), | 114 MtabTestData("dummydevice", "dummydir", kInvalidFS), |
99 }; | 115 }; |
100 WriteToMtab(initial_test_data, arraysize(initial_test_data), true); | 116 WriteToMtab(initial_test_data, |
| 117 arraysize(initial_test_data), |
| 118 true /* overwrite */); |
101 | 119 |
102 // Initialize the test subject. | 120 // Initialize the test subject. |
103 notifications_ = | 121 notifications_ = |
104 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, | 122 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, |
105 &message_loop_); | 123 &message_loop_); |
106 notifications_->Init(); | 124 notifications_->Init(); |
107 message_loop_.RunAllPending(); | 125 message_loop_.RunAllPending(); |
108 } | 126 } |
109 | 127 |
110 virtual void TearDown() { | 128 virtual void TearDown() { |
111 message_loop_.RunAllPending(); | 129 message_loop_.RunAllPending(); |
112 notifications_ = NULL; | 130 notifications_ = NULL; |
113 system_monitor_->RemoveDevicesChangedObserver( | 131 system_monitor_.RemoveDevicesChangedObserver( |
114 mock_devices_changed_observer_.get()); | 132 mock_devices_changed_observer_.get()); |
115 } | 133 } |
116 | 134 |
117 // Used to run tests. When the mtab file gets modified, the message loop | 135 // Append mtab entries from the |data| array of size |data_size| to the mtab |
118 // needs to run in order to react to the file modification. | 136 // file, and run the message loop. |
119 // See WriteToMtab for parameters. | 137 void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) { |
120 void WriteToMtabAndRunLoop(struct MtabTestData* data, | 138 WriteToMtab(data, data_size, false /* do not overwrite */); |
121 size_t data_size, | |
122 bool overwrite) { | |
123 WriteToMtab(data, data_size, overwrite); | |
124 message_loop_.Run(); | 139 message_loop_.Run(); |
125 } | 140 } |
126 | 141 |
| 142 // Overwrite the mtab file with mtab entries from the |data| array of size |
| 143 // |data_size|, and run the message loop. |
| 144 void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) { |
| 145 WriteToMtab(data, data_size, true /* overwrite */); |
| 146 message_loop_.Run(); |
| 147 } |
| 148 |
| 149 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the |
| 150 // entries in the mtab file. |
| 151 void WriteEmptyMtabAndRunLoop() { |
| 152 OverwriteMtabAndRunLoop(NULL, // No data. |
| 153 0); // No data length. |
| 154 } |
| 155 |
| 156 // Create a directory named |dir| relative to the test directory. |
| 157 // It has a DCIM directory, so MediaDeviceNotificationsLinux recognizes it as |
| 158 // a media directory. |
| 159 FilePath CreateMountPointWithDCIMDir(const std::string& dir) { |
| 160 return CreateMountPoint(dir, true /* create DCIM dir */); |
| 161 } |
| 162 |
| 163 // Create a directory named |dir| relative to the test directory. |
| 164 // It does not have a DCIM directory, so MediaDeviceNotificationsLinux does |
| 165 // not recognizes it as a media directory. |
| 166 FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { |
| 167 return CreateMountPoint(dir, false /* do not create DCIM dir */); |
| 168 } |
| 169 |
| 170 base::MockDevicesChangedObserver& observer() { |
| 171 return *mock_devices_changed_observer_; |
| 172 } |
| 173 |
| 174 private: |
127 // Create a directory named |dir| relative to the test directory. | 175 // Create a directory named |dir| relative to the test directory. |
128 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" | 176 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" |
129 // subdirectory. | 177 // subdirectory. |
130 // Returns the full path to the created directory on success, or an empty | 178 // Returns the full path to the created directory on success, or an empty |
131 // path on failure. | 179 // path on failure. |
132 FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) { | 180 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { |
133 FilePath return_path(scoped_temp_dir_.path()); | 181 FilePath return_path(scoped_temp_dir_.path()); |
134 return_path = return_path.AppendASCII(dir); | 182 return_path = return_path.AppendASCII(dir); |
135 FilePath path(return_path); | 183 FilePath path(return_path); |
136 if (with_dcim_dir) | 184 if (with_dcim_dir) |
137 path = path.AppendASCII("DCIM"); | 185 path = path.AppendASCII("DCIM"); |
138 if (!file_util::CreateDirectory(path)) | 186 if (!file_util::CreateDirectory(path)) |
139 return FilePath(); | 187 return FilePath(); |
140 return return_path; | 188 return return_path; |
141 } | 189 } |
142 | 190 |
143 base::MockDevicesChangedObserver& observer() { | |
144 return *mock_devices_changed_observer_; | |
145 } | |
146 | |
147 private: | |
148 // Write the test mtab data to |mtab_file_|. | 191 // Write the test mtab data to |mtab_file_|. |
149 // |data| is an array of mtab entries. | 192 // |data| is an array of mtab entries. |
150 // |data_size| is the array size of |data|. | 193 // |data_size| is the array size of |data|. |
151 // |overwrite| specifies whether to overwrite |mtab_file_|. | 194 // |overwrite| specifies whether to overwrite |mtab_file_|. |
152 void WriteToMtab(struct MtabTestData* data, | 195 void WriteToMtab(const MtabTestData* data, |
153 size_t data_size, | 196 size_t data_size, |
154 bool overwrite) { | 197 bool overwrite) { |
155 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); | 198 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); |
156 ASSERT_TRUE(file); | 199 ASSERT_TRUE(file); |
157 | 200 |
158 struct mntent entry; | 201 mntent entry; |
159 entry.mnt_opts = strdup("rw"); | 202 scoped_array<char> mount_opts(copy_string("rw")); |
| 203 entry.mnt_opts = mount_opts.get(); |
160 entry.mnt_freq = 0; | 204 entry.mnt_freq = 0; |
161 entry.mnt_passno = 0; | 205 entry.mnt_passno = 0; |
162 for (size_t i = 0; i < data_size; ++i) { | 206 for (size_t i = 0; i < data_size; ++i) { |
163 entry.mnt_fsname = strdup(data[i].mount_device); | 207 scoped_array<char> mount_device(copy_string(data[i].mount_device)); |
164 entry.mnt_dir = strdup(data[i].mount_point); | 208 scoped_array<char> mount_point(copy_string(data[i].mount_point)); |
165 entry.mnt_type = strdup(data[i].mount_type); | 209 scoped_array<char> mount_type(copy_string(data[i].mount_type)); |
166 int add_result = addmntent(file, &entry); | 210 entry.mnt_fsname = mount_device.get(); |
167 ASSERT_EQ(0, add_result); | 211 entry.mnt_dir = mount_point.get(); |
168 free(entry.mnt_fsname); | 212 entry.mnt_type = mount_type.get(); |
169 free(entry.mnt_dir); | 213 ASSERT_EQ(0, addmntent(file, &entry)); |
170 free(entry.mnt_type); | |
171 } | 214 } |
172 free(entry.mnt_opts); | 215 ASSERT_EQ(1, endmntent(file)); |
173 int end_result = endmntent(file); | |
174 ASSERT_EQ(1, end_result); | |
175 } | 216 } |
176 | 217 |
177 // The message loop and file thread to run tests on. | 218 // The message loop and file thread to run tests on. |
178 MessageLoop message_loop_; | 219 MessageLoop message_loop_; |
179 BrowserThreadImpl file_thread_; | 220 BrowserThreadImpl file_thread_; |
180 | 221 |
181 // SystemMonitor and DevicesChangedObserver to hook together to test. | 222 // SystemMonitor and DevicesChangedObserver to hook together to test. |
182 scoped_ptr<base::SystemMonitor> system_monitor_; | 223 base::SystemMonitor system_monitor_; |
183 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; | 224 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
184 | 225 |
185 // Temporary directory for created test data. | 226 // Temporary directory for created test data. |
186 ScopedTempDir scoped_temp_dir_; | 227 ScopedTempDir scoped_temp_dir_; |
187 // Path to the test mtab file. | 228 // Path to the test mtab file. |
188 FilePath mtab_file_; | 229 FilePath mtab_file_; |
189 | 230 |
190 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; | 231 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; |
191 | 232 |
192 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); | 233 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); |
193 }; | 234 }; |
194 | 235 |
| 236 // Simple test case where we attach and detach a media device. |
195 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { | 237 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { |
196 testing::Sequence mock_sequence; | 238 testing::Sequence mock_sequence; |
197 FilePath test_path = CreateMountPoint(kMountPointA, true); | 239 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
198 ASSERT_FALSE(test_path.empty()); | 240 ASSERT_FALSE(test_path.empty()); |
199 struct MtabTestData test_data[] = { | 241 MtabTestData test_data[] = { |
200 MtabTestData(kDevice1, kInvalidPath, kValidFS), | 242 MtabTestData(kDevice1, kInvalidPath, kValidFS), |
201 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS), | 243 MtabTestData(kDevice2, test_path.value(), kValidFS), |
202 }; | 244 }; |
| 245 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. |
203 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) | 246 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) |
204 .InSequence(mock_sequence); | 247 .InSequence(mock_sequence); |
205 WriteToMtabAndRunLoop(test_data, arraysize(test_data), false); | 248 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
206 | 249 |
| 250 // |kDevice2| should be detached here. |
207 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 251 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
208 WriteToMtabAndRunLoop(NULL, 0, true); | 252 WriteEmptyMtabAndRunLoop(); |
209 } | 253 } |
210 | 254 |
211 // Only mount points with DCIM directories are recognized. | 255 // Only mount points with DCIM directories are recognized. |
212 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { | 256 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { |
213 testing::Sequence mock_sequence; | 257 testing::Sequence mock_sequence; |
214 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 258 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
215 ASSERT_FALSE(test_pathA.empty()); | 259 ASSERT_FALSE(test_path_a.empty()); |
216 struct MtabTestData test_data1[] = { | 260 MtabTestData test_data1[] = { |
217 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 261 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
218 }; | 262 }; |
219 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathA)) | 263 // |kDevice1| should be attached as expected. |
| 264 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_a)) |
220 .InSequence(mock_sequence); | 265 .InSequence(mock_sequence); |
221 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 266 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
222 | 267 |
223 FilePath test_pathB = CreateMountPoint(kMountPointB, false); | 268 // This should do nothing, since |kMountPointB| does not have a DCIM dir. |
224 ASSERT_FALSE(test_pathB.empty()); | 269 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
225 struct MtabTestData test_data2[] = { | 270 ASSERT_FALSE(test_path_b.empty()); |
226 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 271 MtabTestData test_data2[] = { |
| 272 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
227 }; | 273 }; |
228 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 274 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
229 | 275 |
| 276 // |kDevice1| should be detached as expected. |
230 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 277 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
231 WriteToMtabAndRunLoop(NULL, 0, true); | 278 WriteEmptyMtabAndRunLoop(); |
232 } | 279 } |
233 | 280 |
| 281 // More complicated test case with multiple devices on multiple mount points. |
234 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { | 282 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { |
235 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 283 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
236 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 284 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
237 ASSERT_FALSE(test_pathA.empty()); | 285 ASSERT_FALSE(test_path_a.empty()); |
238 ASSERT_FALSE(test_pathB.empty()); | 286 ASSERT_FALSE(test_path_b.empty()); |
239 | 287 |
240 // Attach two devices. | 288 // Attach two devices. |
241 // kDevice1 -> kMountPointA | 289 // kDevice1 -> kMountPointA |
242 // kDevice2 -> kMountPointB | 290 // kDevice2 -> kMountPointB |
243 struct MtabTestData test_data1[] = { | 291 MtabTestData test_data1[] = { |
244 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 292 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
245 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 293 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
246 }; | 294 }; |
247 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 295 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
248 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 296 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
249 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 297 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
250 | 298 |
251 // Attach |kDevice1| to |kMountPointB|. | 299 // Attach |kDevice1| to |kMountPointB|. |
252 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been | 300 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been |
253 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. | 301 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. |
254 // kDevice1 -> kMountPointA | 302 // kDevice1 -> kMountPointA |
255 // kDevice2 -> kMountPointB | 303 // kDevice2 -> kMountPointB |
256 // kDevice1 -> kMountPointB | 304 // kDevice1 -> kMountPointB |
257 struct MtabTestData test_data2[] = { | 305 MtabTestData test_data2[] = { |
258 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 306 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
259 }; | 307 }; |
260 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 308 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
261 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 309 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
262 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 310 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
263 | 311 |
264 // Attach |kDevice2| to |kMountPointA|. | 312 // Attach |kDevice2| to |kMountPointA|. |
265 // kDevice1 -> kMountPointA | 313 // kDevice1 -> kMountPointA |
266 // kDevice2 -> kMountPointB | 314 // kDevice2 -> kMountPointB |
267 // kDevice1 -> kMountPointB | 315 // kDevice1 -> kMountPointB |
268 // kDevice2 -> kMountPointA | 316 // kDevice2 -> kMountPointA |
269 struct MtabTestData test_data3[] = { | 317 MtabTestData test_data3[] = { |
270 MtabTestData(kDevice2, test_pathA.value().c_str(), kValidFS), | 318 MtabTestData(kDevice2, test_path_a.value(), kValidFS), |
271 }; | 319 }; |
272 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 320 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
273 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 321 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
274 WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false); | 322 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); |
275 | 323 |
276 // Detach |kDevice2| from |kMountPointA|. | 324 // Detach |kDevice2| from |kMountPointA|. |
277 // kDevice1 -> kMountPointA | 325 // kDevice1 -> kMountPointA |
278 // kDevice2 -> kMountPointB | 326 // kDevice2 -> kMountPointB |
279 // kDevice1 -> kMountPointB | 327 // kDevice1 -> kMountPointB |
280 struct MtabTestData test_data4[] = { | 328 MtabTestData test_data4[] = { |
281 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 329 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
282 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 330 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
283 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 331 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
284 }; | 332 }; |
285 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 333 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
286 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 334 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
287 WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true); | 335 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4)); |
288 | 336 |
289 // Detach |kDevice1| from |kMountPointB|. | 337 // Detach |kDevice1| from |kMountPointB|. |
290 // kDevice1 -> kMountPointA | 338 // kDevice1 -> kMountPointA |
291 // kDevice2 -> kMountPointB | 339 // kDevice2 -> kMountPointB |
292 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
293 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 341 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
294 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 342 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
295 | 343 |
296 // Detach all devices. | 344 // Detach all devices. |
297 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 345 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
298 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 346 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
299 WriteToMtabAndRunLoop(NULL, 0, true); | 347 WriteEmptyMtabAndRunLoop(); |
300 } | 348 } |
301 | 349 |
| 350 // More complicated test case with multiple devices on one mount point. |
302 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { | 351 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { |
303 testing::Sequence mock_sequence; | 352 testing::Sequence mock_sequence; |
304 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 353 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
305 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 354 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
306 ASSERT_FALSE(test_pathA.empty()); | 355 ASSERT_FALSE(test_path_a.empty()); |
307 ASSERT_FALSE(test_pathB.empty()); | 356 ASSERT_FALSE(test_path_b.empty()); |
308 | 357 |
309 // |kDevice1| is most recently mounted at |kMountPointB|. | 358 // |kDevice1| is most recently mounted at |kMountPointB|. |
310 // kDevice1 -> kMountPointA | 359 // kDevice1 -> kMountPointA |
311 // kDevice2 -> kMountPointB | 360 // kDevice2 -> kMountPointB |
312 // kDevice1 -> kMountPointB | 361 // kDevice1 -> kMountPointB |
313 struct MtabTestData test_data1[] = { | 362 MtabTestData test_data1[] = { |
314 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 363 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
315 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 364 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
316 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 365 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
317 }; | 366 }; |
318 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathB)) | 367 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_b)) |
319 .Times(1); | 368 .Times(1); |
320 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 369 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
321 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 370 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
322 | 371 |
323 // Attach |kDevice3| to |kMountPointB|. | 372 // Attach |kDevice3| to |kMountPointB|. |
324 // |kDevice1| is inaccessible at its most recent mount point, so it is | 373 // |kDevice1| is inaccessible at its most recent mount point, so it is |
325 // detached and unavailable, even though it is still accessible via | 374 // detached and unavailable, even though it is still accessible via |
326 // |kMountPointA|. | 375 // |kMountPointA|. |
327 // kDevice1 -> kMountPointA | 376 // kDevice1 -> kMountPointA |
328 // kDevice2 -> kMountPointB | 377 // kDevice2 -> kMountPointB |
329 // kDevice1 -> kMountPointB | 378 // kDevice1 -> kMountPointB |
330 // kDevice3 -> kMountPointB | 379 // kDevice3 -> kMountPointB |
331 struct MtabTestData test_data2[] = { | 380 MtabTestData test_data2[] = { |
332 MtabTestData(kDevice3, test_pathB.value().c_str(), kValidFS), | 381 MtabTestData(kDevice3, test_path_b.value(), kValidFS), |
333 }; | 382 }; |
334 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); | 383 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); |
335 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_pathB)) | 384 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_path_b)) |
336 .Times(1); | 385 .Times(1); |
337 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 386 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
338 | 387 |
339 // Detach all devices. | 388 // Detach all devices. |
340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 389 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
341 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); | 390 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); |
342 WriteToMtabAndRunLoop(NULL, 0, true); | 391 WriteEmptyMtabAndRunLoop(); |
343 } | 392 } |
344 | 393 |
| 394 } // namespace |
| 395 |
345 } // namespace content | 396 } // namespace content |
OLD | NEW |