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