storage_monitor_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "components/storage_monitor/storage_monitor.h"
  5. #include "base/bind.h"
  6. #include "base/run_loop.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "base/synchronization/waitable_event.h"
  9. #include "base/test/task_environment.h"
  10. #include "components/storage_monitor/mock_removable_storage_observer.h"
  11. #include "components/storage_monitor/test_storage_monitor.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace {
  14. void SetLatch(bool* called) {
  15. *called = true;
  16. }
  17. } // namespace
  18. namespace storage_monitor {
  19. TEST(StorageMonitorTest, TestInitialize) {
  20. base::test::SingleThreadTaskEnvironment task_environment;
  21. TestStorageMonitor::Destroy();
  22. TestStorageMonitor monitor;
  23. EXPECT_FALSE(monitor.init_called());
  24. bool initialized = false;
  25. monitor.EnsureInitialized(base::BindOnce(&SetLatch, &initialized));
  26. EXPECT_TRUE(monitor.init_called());
  27. EXPECT_FALSE(initialized);
  28. monitor.MarkInitialized();
  29. EXPECT_TRUE(initialized);
  30. }
  31. TEST(StorageMonitorTest, DeviceAttachDetachNotifications) {
  32. TestStorageMonitor::Destroy();
  33. base::test::SingleThreadTaskEnvironment task_environment;
  34. const std::string kDeviceId1 = "dcim:UUID:FFF0-0001";
  35. const std::string kDeviceId2 = "dcim:UUID:FFF0-0002";
  36. MockRemovableStorageObserver observer1;
  37. MockRemovableStorageObserver observer2;
  38. TestStorageMonitor monitor;
  39. monitor.AddObserver(&observer1);
  40. monitor.AddObserver(&observer2);
  41. StorageInfo info(kDeviceId1, FILE_PATH_LITERAL("path"), std::u16string(),
  42. std::u16string(), std::u16string(), 0);
  43. monitor.receiver()->ProcessAttach(info);
  44. base::RunLoop().RunUntilIdle();
  45. EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id());
  46. EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_attached().location());
  47. EXPECT_EQ(kDeviceId1, observer2.last_attached().device_id());
  48. EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_attached().location());
  49. EXPECT_EQ(1, observer1.attach_calls());
  50. EXPECT_EQ(0, observer1.detach_calls());
  51. monitor.receiver()->ProcessDetach(kDeviceId1);
  52. monitor.receiver()->ProcessDetach(kDeviceId2);
  53. base::RunLoop().RunUntilIdle();
  54. EXPECT_EQ(kDeviceId1, observer1.last_detached().device_id());
  55. EXPECT_EQ(FILE_PATH_LITERAL("path"), observer1.last_detached().location());
  56. EXPECT_EQ(kDeviceId1, observer2.last_detached().device_id());
  57. EXPECT_EQ(FILE_PATH_LITERAL("path"), observer2.last_detached().location());
  58. EXPECT_EQ(1, observer1.attach_calls());
  59. EXPECT_EQ(1, observer2.attach_calls());
  60. // The kDeviceId2 won't be notified since it was never attached.
  61. EXPECT_EQ(1, observer1.detach_calls());
  62. EXPECT_EQ(1, observer2.detach_calls());
  63. monitor.RemoveObserver(&observer1);
  64. monitor.RemoveObserver(&observer2);
  65. }
  66. TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) {
  67. TestStorageMonitor::Destroy();
  68. base::test::SingleThreadTaskEnvironment task_environment;
  69. TestStorageMonitor monitor;
  70. std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages();
  71. EXPECT_EQ(0U, devices.size());
  72. }
  73. TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) {
  74. TestStorageMonitor::Destroy();
  75. base::test::SingleThreadTaskEnvironment task_environment;
  76. TestStorageMonitor monitor;
  77. const std::string kDeviceId1 = "dcim:UUID:FFF0-0042";
  78. const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo"));
  79. StorageInfo info1(kDeviceId1, kDevicePath1.value(), std::u16string(),
  80. std::u16string(), std::u16string(), 0);
  81. monitor.receiver()->ProcessAttach(info1);
  82. base::RunLoop().RunUntilIdle();
  83. std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages();
  84. ASSERT_EQ(1U, devices.size());
  85. EXPECT_EQ(kDeviceId1, devices[0].device_id());
  86. EXPECT_EQ(kDevicePath1.value(), devices[0].location());
  87. const std::string kDeviceId2 = "dcim:UUID:FFF0-0044";
  88. const base::FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar"));
  89. StorageInfo info2(kDeviceId2, kDevicePath2.value(), std::u16string(),
  90. std::u16string(), std::u16string(), 0);
  91. monitor.receiver()->ProcessAttach(info2);
  92. base::RunLoop().RunUntilIdle();
  93. devices = monitor.GetAllAvailableStorages();
  94. ASSERT_EQ(2U, devices.size());
  95. EXPECT_EQ(kDeviceId1, devices[0].device_id());
  96. EXPECT_EQ(kDevicePath1.value(), devices[0].location());
  97. EXPECT_EQ(kDeviceId2, devices[1].device_id());
  98. EXPECT_EQ(kDevicePath2.value(), devices[1].location());
  99. monitor.receiver()->ProcessDetach(kDeviceId1);
  100. base::RunLoop().RunUntilIdle();
  101. devices = monitor.GetAllAvailableStorages();
  102. ASSERT_EQ(1U, devices.size());
  103. EXPECT_EQ(kDeviceId2, devices[0].device_id());
  104. EXPECT_EQ(kDevicePath2.value(), devices[0].location());
  105. monitor.receiver()->ProcessDetach(kDeviceId2);
  106. base::RunLoop().RunUntilIdle();
  107. devices = monitor.GetAllAvailableStorages();
  108. EXPECT_EQ(0U, devices.size());
  109. }
  110. } // namespace storage_monitor