mtp_manager_client_chromeos_unittest.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // MtpManagerClientChromeOS unit tests.
  6. #include "components/storage_monitor/mtp_manager_client_chromeos.h"
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "base/lazy_instance.h"
  11. #include "base/run_loop.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "components/storage_monitor/mock_removable_storage_observer.h"
  14. #include "components/storage_monitor/storage_info.h"
  15. #include "components/storage_monitor/storage_info_utils.h"
  16. #include "components/storage_monitor/storage_monitor.h"
  17. #include "components/storage_monitor/test_storage_monitor.h"
  18. #include "content/public/test/browser_task_environment.h"
  19. #include "services/device/public/mojom/mtp_manager.mojom.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. namespace storage_monitor {
  22. namespace {
  23. // Sample MTP device storage information.
  24. const char kStorageWithInvalidInfo[] = "usb:2,3:11111";
  25. const char kStorageWithValidInfo[] = "usb:2,2:88888";
  26. const char kStorageVendor[] = "ExampleVendor";
  27. const char16_t kStorageVendor16[] = u"ExampleVendor";
  28. const uint32_t kStorageVendorId = 0x040a;
  29. const char kStorageProduct[] = "ExampleCamera";
  30. const char16_t kStorageProduct16[] = u"ExampleCamera";
  31. const uint32_t kStorageProductId = 0x0160;
  32. const uint32_t kStorageDeviceFlags = 0x0004000;
  33. const uint32_t kStorageType = 3; // Fixed RAM
  34. const uint32_t kStorageFilesystemType = 2; // Generic Hierarchical
  35. const uint32_t kStorageAccessCapability = 0; // Read-Write
  36. const uint64_t kStorageMaxCapacity = 0x40000000; // 1G in total
  37. const uint64_t kStorageFreeSpaceInBytes = 0x20000000; // 512M bytes left
  38. const uint64_t kStorageFreeSpaceInObjects = 0x04000000; // 64M Objects left
  39. const char kStorageDescription[] = "ExampleDescription";
  40. const char kStorageVolumeIdentifier[] = "ExampleVolumeId";
  41. const char kStorageSerialNumber[] = "0123456789ABCDEF0123456789ABCDEF";
  42. base::LazyInstance<std::map<std::string, device::mojom::MtpStorageInfo>>::Leaky
  43. g_fake_storage_info_map = LAZY_INSTANCE_INITIALIZER;
  44. const device::mojom::MtpStorageInfo* GetFakeMtpStorageInfoSync(
  45. const std::string& storage_name) {
  46. // Fill the map out if it is empty.
  47. if (g_fake_storage_info_map.Get().empty()) {
  48. // Add the invalid MTP storage info.
  49. auto storage_info = device::mojom::MtpStorageInfo();
  50. storage_info.storage_name = kStorageWithInvalidInfo;
  51. g_fake_storage_info_map.Get().insert(
  52. std::make_pair(kStorageWithInvalidInfo, storage_info));
  53. // Add the valid MTP storage info.
  54. g_fake_storage_info_map.Get().insert(std::make_pair(
  55. kStorageWithValidInfo,
  56. device::mojom::MtpStorageInfo(
  57. kStorageWithValidInfo, kStorageVendor, kStorageVendorId,
  58. kStorageProduct, kStorageProductId, kStorageDeviceFlags,
  59. kStorageType, kStorageFilesystemType, kStorageAccessCapability,
  60. kStorageMaxCapacity, kStorageFreeSpaceInBytes,
  61. kStorageFreeSpaceInObjects, kStorageDescription,
  62. kStorageVolumeIdentifier, kStorageSerialNumber)));
  63. }
  64. const auto it = g_fake_storage_info_map.Get().find(storage_name);
  65. return it != g_fake_storage_info_map.Get().end() ? &it->second : nullptr;
  66. }
  67. class FakeMtpManagerClientChromeOS : public MtpManagerClientChromeOS {
  68. public:
  69. FakeMtpManagerClientChromeOS(StorageMonitor::Receiver* receiver,
  70. device::mojom::MtpManager* mtp_manager)
  71. : MtpManagerClientChromeOS(receiver, mtp_manager) {}
  72. FakeMtpManagerClientChromeOS(const FakeMtpManagerClientChromeOS&) = delete;
  73. FakeMtpManagerClientChromeOS& operator=(const FakeMtpManagerClientChromeOS&) =
  74. delete;
  75. // Notifies MtpManagerClientChromeOS about the attachment of MTP storage
  76. // device given the |storage_name|.
  77. void MtpStorageAttached(const std::string& storage_name) {
  78. auto* storage_info = GetFakeMtpStorageInfoSync(storage_name);
  79. DCHECK(storage_info);
  80. StorageAttached(storage_info->Clone());
  81. base::RunLoop().RunUntilIdle();
  82. }
  83. // Notifies MtpManagerClientChromeOS about the detachment of MTP storage
  84. // device given the |storage_name|.
  85. void MtpStorageDetached(const std::string& storage_name) {
  86. StorageDetached(storage_name);
  87. base::RunLoop().RunUntilIdle();
  88. }
  89. };
  90. } // namespace
  91. // A class to test the functionality of MtpManagerClientChromeOS member
  92. // functions.
  93. class MtpManagerClientChromeOSTest : public testing::Test {
  94. public:
  95. MtpManagerClientChromeOSTest()
  96. : task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {}
  97. MtpManagerClientChromeOSTest(const MtpManagerClientChromeOSTest&) = delete;
  98. MtpManagerClientChromeOSTest& operator=(const MtpManagerClientChromeOSTest&) =
  99. delete;
  100. ~MtpManagerClientChromeOSTest() override {}
  101. protected:
  102. void SetUp() override {
  103. mock_storage_observer_ = std::make_unique<MockRemovableStorageObserver>();
  104. TestStorageMonitor* monitor = TestStorageMonitor::CreateAndInstall();
  105. mtp_device_observer_ = std::make_unique<FakeMtpManagerClientChromeOS>(
  106. monitor->receiver(), monitor->media_transfer_protocol_manager());
  107. monitor->AddObserver(mock_storage_observer_.get());
  108. }
  109. void TearDown() override {
  110. StorageMonitor* monitor = StorageMonitor::GetInstance();
  111. monitor->RemoveObserver(mock_storage_observer_.get());
  112. mtp_device_observer_.reset();
  113. TestStorageMonitor::Destroy();
  114. }
  115. // Returns the device changed observer object.
  116. MockRemovableStorageObserver& observer() { return *mock_storage_observer_; }
  117. FakeMtpManagerClientChromeOS* mtp_device_observer() {
  118. return mtp_device_observer_.get();
  119. }
  120. private:
  121. content::BrowserTaskEnvironment task_environment_;
  122. std::unique_ptr<FakeMtpManagerClientChromeOS> mtp_device_observer_;
  123. std::unique_ptr<MockRemovableStorageObserver> mock_storage_observer_;
  124. };
  125. // Test to verify basic MTP storage attach and detach notifications.
  126. TEST_F(MtpManagerClientChromeOSTest, BasicAttachDetach) {
  127. auto* mtpStorageInfo = GetFakeMtpStorageInfoSync(kStorageWithValidInfo);
  128. std::string device_id = GetDeviceIdFromStorageInfo(*mtpStorageInfo);
  129. // Attach a MTP storage.
  130. mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
  131. EXPECT_EQ(1, observer().attach_calls());
  132. EXPECT_EQ(0, observer().detach_calls());
  133. EXPECT_EQ(device_id, observer().last_attached().device_id());
  134. EXPECT_EQ(GetDeviceLocationFromStorageName(kStorageWithValidInfo),
  135. observer().last_attached().location());
  136. EXPECT_EQ(kStorageVendor16, observer().last_attached().vendor_name());
  137. EXPECT_EQ(kStorageProduct16, observer().last_attached().model_name());
  138. // Detach the attached storage.
  139. mtp_device_observer()->MtpStorageDetached(kStorageWithValidInfo);
  140. EXPECT_EQ(1, observer().attach_calls());
  141. EXPECT_EQ(1, observer().detach_calls());
  142. EXPECT_EQ(device_id, observer().last_detached().device_id());
  143. }
  144. // When a MTP storage device with invalid storage label and id is
  145. // attached/detached, there should not be any device attach/detach
  146. // notifications.
  147. TEST_F(MtpManagerClientChromeOSTest, StorageWithInvalidInfo) {
  148. // Attach the mtp storage with invalid storage info.
  149. mtp_device_observer()->MtpStorageAttached(kStorageWithInvalidInfo);
  150. // Detach the attached storage.
  151. mtp_device_observer()->MtpStorageDetached(kStorageWithInvalidInfo);
  152. EXPECT_EQ(0, observer().attach_calls());
  153. EXPECT_EQ(0, observer().detach_calls());
  154. }
  155. } // namespace storage_monitor