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. | 5 // MediaDeviceNotificationsLinux unit tests. |
6 | 6 |
7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" | 7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" |
8 | 8 |
9 #include <mntent.h> | 9 #include <mntent.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 const char kValidFS[] = "vfat"; | 31 const char kValidFS[] = "vfat"; |
32 const char kInvalidFS[] = "invalidfs"; | 32 const char kInvalidFS[] = "invalidfs"; |
33 | 33 |
34 const char kInvalidPath[] = "invalid path does not exist"; | 34 const char kInvalidPath[] = "invalid path does not exist"; |
35 | 35 |
36 const char kDevice1[] = "d1"; | 36 const char kDevice1[] = "d1"; |
37 const char kDevice2[] = "d2"; | 37 const char kDevice2[] = "d2"; |
38 const char kDevice3[] = "d3"; | 38 const char kDevice3[] = "d3"; |
39 | 39 |
| 40 const char kDeviceId1[] = "UUID:FFF0-000F"; |
| 41 const char kDeviceId2[] = "VendorModelSerial:ComName:Model2010:898989898989"; |
| 42 const char kDeviceId3[] = "VendorModelSerial:::WEM319X792"; |
| 43 |
| 44 const char kDeviceLabel1[] = "TEST_USB_MODEL_1"; |
| 45 const char kDeviceLabel2[] = "TEST_USB_MODEL_2"; |
| 46 const char kDeviceLabel3[] = "TEST_USB_MODEL_3"; |
| 47 |
40 const char kMountPointA[] = "mnt_a"; | 48 const char kMountPointA[] = "mnt_a"; |
41 const char kMountPointB[] = "mnt_b"; | 49 const char kMountPointB[] = "mnt_b"; |
42 | 50 |
| 51 bool GetDeviceInfo(const std::string& dev_path, |
| 52 std::string* id, |
| 53 string16* name) { |
| 54 std::string device_name; |
| 55 if (dev_path == kDevice1) { |
| 56 *id = std::string(kDeviceId1); |
| 57 device_name = kDeviceLabel1; |
| 58 } else if (dev_path == kDevice2) { |
| 59 *id = std::string(kDeviceId2); |
| 60 device_name = kDeviceLabel2; |
| 61 } else if (dev_path == kDevice3) { |
| 62 *id = std::string(kDeviceId3); |
| 63 device_name = kDeviceLabel3; |
| 64 } else { |
| 65 return false; |
| 66 } |
| 67 |
| 68 *name = ASCIIToUTF16(device_name); |
| 69 return true; |
| 70 } |
| 71 |
43 class MediaDeviceNotificationsLinuxTestWrapper | 72 class MediaDeviceNotificationsLinuxTestWrapper |
44 : public MediaDeviceNotificationsLinux { | 73 : public MediaDeviceNotificationsLinux { |
45 public: | 74 public: |
46 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 75 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
47 MessageLoop* message_loop) | 76 MessageLoop* message_loop) |
48 : MediaDeviceNotificationsLinux(path), | 77 : MediaDeviceNotificationsLinux(path, &GetDeviceInfo), |
49 message_loop_(message_loop) { | 78 message_loop_(message_loop) { |
50 } | 79 } |
51 | 80 |
52 private: | 81 private: |
53 // Avoids code deleting the object while there are references to it. | 82 // Avoids code deleting the object while there are references to it. |
54 // Aside from the base::RefCountedThreadSafe friend class, any attempts to | 83 // Aside from the base::RefCountedThreadSafe friend class, any attempts to |
55 // call this dtor will result in a compile-time error. | 84 // call this dtor will result in a compile-time error. |
56 ~MediaDeviceNotificationsLinuxTestWrapper() {} | 85 ~MediaDeviceNotificationsLinuxTestWrapper() {} |
57 | 86 |
58 virtual void OnFilePathChanged(const FilePath& path, bool error) OVERRIDE { | 87 virtual void OnFilePathChanged(const FilePath& path, bool error) OVERRIDE { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 258 |
230 // Simple test case where we attach and detach a media device. | 259 // Simple test case where we attach and detach a media device. |
231 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { | 260 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { |
232 testing::Sequence mock_sequence; | 261 testing::Sequence mock_sequence; |
233 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); | 262 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
234 ASSERT_FALSE(test_path.empty()); | 263 ASSERT_FALSE(test_path.empty()); |
235 MtabTestData test_data[] = { | 264 MtabTestData test_data[] = { |
236 MtabTestData(kDevice1, kInvalidPath, kValidFS), | 265 MtabTestData(kDevice1, kInvalidPath, kValidFS), |
237 MtabTestData(kDevice2, test_path.value(), kValidFS), | 266 MtabTestData(kDevice2, test_path.value(), kValidFS), |
238 }; | 267 }; |
239 const std::string kDeviceId = "0"; | |
240 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. | 268 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. |
241 EXPECT_CALL(observer(), | 269 EXPECT_CALL(observer(), |
242 OnMediaDeviceAttached(kDeviceId, | 270 OnMediaDeviceAttached(kDeviceId2, |
243 ASCIIToUTF16(kDevice2), | 271 ASCIIToUTF16(kDeviceLabel2), |
244 base::SystemMonitor::TYPE_PATH, | 272 base::SystemMonitor::TYPE_PATH, |
245 test_path.value())) | 273 test_path.value())) |
246 .InSequence(mock_sequence); | 274 .InSequence(mock_sequence); |
247 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); | 275 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
248 | 276 |
249 // |kDevice2| should be detached here. | 277 // |kDevice2| should be detached here. |
250 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId)) | 278 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId2)) |
251 .InSequence(mock_sequence); | 279 .InSequence(mock_sequence); |
252 WriteEmptyMtabAndRunLoop(); | 280 WriteEmptyMtabAndRunLoop(); |
253 } | 281 } |
254 | 282 |
255 // Only mount points with DCIM directories are recognized. | 283 // Only mount points with DCIM directories are recognized. |
256 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { | 284 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { |
257 testing::Sequence mock_sequence; | 285 testing::Sequence mock_sequence; |
258 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); | 286 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
259 ASSERT_FALSE(test_path_a.empty()); | 287 ASSERT_FALSE(test_path_a.empty()); |
260 MtabTestData test_data1[] = { | 288 MtabTestData test_data1[] = { |
261 MtabTestData(kDevice1, test_path_a.value(), kValidFS), | 289 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
262 }; | 290 }; |
263 const std::string kDeviceId = "0"; | |
264 // |kDevice1| should be attached as expected. | 291 // |kDevice1| should be attached as expected. |
265 EXPECT_CALL(observer(), | 292 EXPECT_CALL(observer(), |
266 OnMediaDeviceAttached(kDeviceId, | 293 OnMediaDeviceAttached(kDeviceId1, |
267 ASCIIToUTF16(kDevice1), | 294 ASCIIToUTF16(kDeviceLabel1), |
268 base::SystemMonitor::TYPE_PATH, | 295 base::SystemMonitor::TYPE_PATH, |
269 test_path_a.value())) | 296 test_path_a.value())) |
270 .InSequence(mock_sequence); | 297 .InSequence(mock_sequence); |
271 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); | 298 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
272 | 299 |
273 // This should do nothing, since |kMountPointB| does not have a DCIM dir. | 300 // This should do nothing, since |kMountPointB| does not have a DCIM dir. |
274 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); | 301 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
275 ASSERT_FALSE(test_path_b.empty()); | 302 ASSERT_FALSE(test_path_b.empty()); |
276 MtabTestData test_data2[] = { | 303 MtabTestData test_data2[] = { |
277 MtabTestData(kDevice2, test_path_b.value(), kValidFS), | 304 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
278 }; | 305 }; |
279 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); | 306 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
280 | 307 |
281 // |kDevice1| should be detached as expected. | 308 // |kDevice1| should be detached as expected. |
282 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId)) | 309 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId1)) |
283 .InSequence(mock_sequence); | 310 .InSequence(mock_sequence); |
284 WriteEmptyMtabAndRunLoop(); | 311 WriteEmptyMtabAndRunLoop(); |
285 } | 312 } |
286 | 313 |
287 // More complicated test case with multiple devices on multiple mount points. | 314 // More complicated test case with multiple devices on multiple mount points. |
| 315 TEST_F(MediaDeviceNotificationsLinuxTest, SwapMountPoints) { |
| 316 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
| 317 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
| 318 ASSERT_FALSE(test_path_a.empty()); |
| 319 ASSERT_FALSE(test_path_b.empty()); |
| 320 |
| 321 // Attach two devices. |
| 322 // kDevice1 -> kMountPointA |
| 323 // kDevice2 -> kMountPointB |
| 324 MtabTestData test_data1[] = { |
| 325 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
| 326 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
| 327 }; |
| 328 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _, _)).Times(2); |
| 329 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
| 330 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
| 331 |
| 332 // Detach two devices from old mount points and attach the devices at new |
| 333 // mount points. |
| 334 // kDevice1 -> kMountPointB |
| 335 // kDevice2 -> kMountPointA |
| 336 MtabTestData test_data2[] = { |
| 337 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
| 338 MtabTestData(kDevice2, test_path_a.value(), kValidFS), |
| 339 }; |
| 340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _, _)).Times(2); |
| 341 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
| 342 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2)); |
| 343 |
| 344 // Detach all devices. |
| 345 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _, _)).Times(0); |
| 346 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
| 347 WriteEmptyMtabAndRunLoop(); |
| 348 } |
| 349 |
| 350 // More complicated test case with multiple devices on multiple mount points. |
288 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { | 351 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { |
289 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); | 352 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
290 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); | 353 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
291 ASSERT_FALSE(test_path_a.empty()); | 354 ASSERT_FALSE(test_path_a.empty()); |
292 ASSERT_FALSE(test_path_b.empty()); | 355 ASSERT_FALSE(test_path_b.empty()); |
293 | 356 |
294 // Attach two devices. | 357 // Attach two devices. |
295 // kDevice1 -> kMountPointA | 358 // kDevice1 -> kMountPointA |
296 // kDevice2 -> kMountPointB | 359 // kDevice2 -> kMountPointB |
297 MtabTestData test_data1[] = { | 360 MtabTestData test_data1[] = { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 415 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
353 WriteEmptyMtabAndRunLoop(); | 416 WriteEmptyMtabAndRunLoop(); |
354 } | 417 } |
355 | 418 |
356 // More complicated test case with multiple devices on one mount point. | 419 // More complicated test case with multiple devices on one mount point. |
357 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { | 420 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { |
358 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); | 421 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
359 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); | 422 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
360 ASSERT_FALSE(test_path_a.empty()); | 423 ASSERT_FALSE(test_path_a.empty()); |
361 ASSERT_FALSE(test_path_b.empty()); | 424 ASSERT_FALSE(test_path_b.empty()); |
362 const std::string kDeviceId0 = "0"; | |
363 const std::string kDeviceId1 = "1"; | |
364 | 425 |
365 // |kDevice1| is most recently mounted at |kMountPointB|. | 426 // |kDevice1| is most recently mounted at |kMountPointB|. |
366 // kDevice1 -> kMountPointA | 427 // kDevice1 -> kMountPointA |
367 // kDevice2 -> kMountPointB | 428 // kDevice2 -> kMountPointB |
368 // kDevice1 -> kMountPointB | 429 // kDevice1 -> kMountPointB |
369 MtabTestData test_data1[] = { | 430 MtabTestData test_data1[] = { |
370 MtabTestData(kDevice1, test_path_a.value(), kValidFS), | 431 MtabTestData(kDevice1, test_path_a.value(), kValidFS), |
371 MtabTestData(kDevice2, test_path_b.value(), kValidFS), | 432 MtabTestData(kDevice2, test_path_b.value(), kValidFS), |
372 MtabTestData(kDevice1, test_path_b.value(), kValidFS), | 433 MtabTestData(kDevice1, test_path_b.value(), kValidFS), |
373 }; | 434 }; |
374 EXPECT_CALL(observer(), | 435 EXPECT_CALL(observer(), |
375 OnMediaDeviceAttached(kDeviceId0, | 436 OnMediaDeviceAttached(kDeviceId1, |
376 ASCIIToUTF16(kDevice1), | 437 ASCIIToUTF16(kDeviceLabel1), |
377 base::SystemMonitor::TYPE_PATH, | 438 base::SystemMonitor::TYPE_PATH, |
378 test_path_b.value())) | 439 test_path_b.value())) |
379 .Times(1); | 440 .Times(1); |
380 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 441 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
381 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); | 442 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
382 | 443 |
383 // Attach |kDevice3| to |kMountPointB|. | 444 // Attach |kDevice3| to |kMountPointB|. |
384 // |kDevice1| is inaccessible at its most recent mount point, so it is | 445 // |kDevice1| is inaccessible at its most recent mount point, so it is |
385 // detached and unavailable, even though it is still accessible via | 446 // detached and unavailable, even though it is still accessible via |
386 // |kMountPointA|. | 447 // |kMountPointA|. |
387 // kDevice1 -> kMountPointA | 448 // kDevice1 -> kMountPointA |
388 // kDevice2 -> kMountPointB | 449 // kDevice2 -> kMountPointB |
389 // kDevice1 -> kMountPointB | 450 // kDevice1 -> kMountPointB |
390 // kDevice3 -> kMountPointB | 451 // kDevice3 -> kMountPointB |
391 MtabTestData test_data2[] = { | 452 MtabTestData test_data2[] = { |
392 MtabTestData(kDevice3, test_path_b.value(), kValidFS), | 453 MtabTestData(kDevice3, test_path_b.value(), kValidFS), |
393 }; | 454 }; |
394 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId0)).Times(1); | 455 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId1)).Times(1); |
395 EXPECT_CALL(observer(), | 456 EXPECT_CALL(observer(), |
396 OnMediaDeviceAttached(kDeviceId1, | 457 OnMediaDeviceAttached(kDeviceId3, |
397 ASCIIToUTF16(kDevice3), | 458 ASCIIToUTF16(kDeviceLabel3), |
398 base::SystemMonitor::TYPE_PATH, | 459 base::SystemMonitor::TYPE_PATH, |
399 test_path_b.value())) | 460 test_path_b.value())) |
400 .Times(1); | 461 .Times(1); |
401 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); | 462 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
402 | 463 |
403 // Detach all devices. | 464 // Detach all devices. |
404 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _, _)).Times(0); | 465 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _, _)).Times(0); |
405 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId1)).Times(1); | 466 EXPECT_CALL(observer(), OnMediaDeviceDetached(kDeviceId3)).Times(1); |
406 WriteEmptyMtabAndRunLoop(); | 467 WriteEmptyMtabAndRunLoop(); |
407 } | 468 } |
408 | 469 |
409 } // namespace | 470 } // namespace |
410 | 471 |
411 } // namespace chrome | 472 } // namespace chrome |
OLD | NEW |