mock_disk_mount_manager.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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
  3. // found in the LICENSE file.
  4. #include "ash/components/disks/mock_disk_mount_manager.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "ash/components/disks/disk.h"
  9. using testing::_;
  10. using testing::AnyNumber;
  11. using testing::Invoke;
  12. using testing::ReturnRef;
  13. namespace ash {
  14. namespace disks {
  15. namespace {
  16. const char kTestSystemPath[] = "/this/system/path";
  17. const char kTestStorageDevicePath[] = "/this/system";
  18. const char kTestDevicePath[] = "/this/device/path";
  19. const char kTestMountPath[] = "/media/foofoo";
  20. const char kTestFilePath[] = "/this/file/path";
  21. const char kTestDeviceLabel[] = "A label";
  22. const char kTestDriveLabel[] = "Another label";
  23. const char kTestVendorId[] = "0123";
  24. const char kTestVendorName[] = "A vendor";
  25. const char kTestProductId[] = "abcd";
  26. const char kTestProductName[] = "A product";
  27. const char kTestUuid[] = "FFFF-FFFF";
  28. const char kTestFileSystemType[] = "vfat";
  29. std::unique_ptr<Disk::Builder> MakeDiskBuilder() {
  30. std::unique_ptr<Disk::Builder> builder = std::make_unique<Disk::Builder>();
  31. builder->SetDevicePath(kTestDevicePath)
  32. .SetFilePath(kTestFilePath)
  33. .SetDriveLabel(kTestDriveLabel)
  34. .SetVendorId(kTestVendorId)
  35. .SetVendorName(kTestVendorName)
  36. .SetProductId(kTestProductId)
  37. .SetProductName(kTestProductName)
  38. .SetFileSystemUUID(kTestUuid)
  39. .SetStorageDevicePath(kTestStorageDevicePath)
  40. .SetHasMedia(true)
  41. .SetOnRemovableDevice(true)
  42. .SetFileSystemType(kTestFileSystemType);
  43. return builder;
  44. }
  45. } // namespace
  46. void MockDiskMountManager::AddObserver(DiskMountManager::Observer* observer) {
  47. observers_.AddObserver(observer);
  48. }
  49. void MockDiskMountManager::RemoveObserver(
  50. DiskMountManager::Observer* observer) {
  51. observers_.RemoveObserver(observer);
  52. }
  53. MockDiskMountManager::MockDiskMountManager() {
  54. ON_CALL(*this, disks())
  55. .WillByDefault(Invoke(this, &MockDiskMountManager::disksInternal));
  56. ON_CALL(*this, mount_points())
  57. .WillByDefault(Invoke(this, &MockDiskMountManager::mountPointsInternal));
  58. ON_CALL(*this, FindDiskBySourcePath(_))
  59. .WillByDefault(Invoke(
  60. this, &MockDiskMountManager::FindDiskBySourcePathInternal));
  61. // Invoke doesn't handle move-only types, so use a lambda instead.
  62. ON_CALL(*this, EnsureMountInfoRefreshed(_, _))
  63. .WillByDefault([](EnsureMountInfoRefreshedCallback callback, bool force) {
  64. std::move(callback).Run(true);
  65. });
  66. }
  67. MockDiskMountManager::~MockDiskMountManager() = default;
  68. void MockDiskMountManager::NotifyDeviceInsertEvents() {
  69. std::unique_ptr<Disk> disk1_ptr = MakeDiskBuilder()
  70. ->SetDeviceType(DeviceType::kUSB)
  71. .SetSizeInBytes(4294967295U)
  72. .Build();
  73. Disk* disk1 = disk1_ptr.get();
  74. disks_.clear();
  75. disks_.insert(std::move(disk1_ptr));
  76. // Device Added
  77. NotifyDeviceChanged(DEVICE_ADDED, kTestSystemPath);
  78. // Disk Added
  79. NotifyDiskChanged(DISK_ADDED, disk1);
  80. // Disk Changed
  81. std::unique_ptr<Disk> disk2_ptr = MakeDiskBuilder()
  82. ->SetMountPath(kTestMountPath)
  83. .SetDeviceType(DeviceType::kMobile)
  84. .SetSizeInBytes(1073741824)
  85. .Build();
  86. Disk* disk2 = disk2_ptr.get();
  87. disks_.clear();
  88. disks_.insert(std::move(disk2_ptr));
  89. NotifyDiskChanged(DISK_CHANGED, disk2);
  90. }
  91. void MockDiskMountManager::NotifyDeviceRemoveEvents() {
  92. std::unique_ptr<Disk> disk_ptr = MakeDiskBuilder()
  93. ->SetMountPath(kTestMountPath)
  94. .SetDeviceLabel(kTestDeviceLabel)
  95. .SetDeviceType(DeviceType::kSD)
  96. .SetSizeInBytes(1073741824)
  97. .Build();
  98. Disk* disk = disk_ptr.get();
  99. disks_.clear();
  100. disks_.insert(std::move(disk_ptr));
  101. NotifyDiskChanged(DISK_REMOVED, disk);
  102. }
  103. void MockDiskMountManager::NotifyMountEvent(MountEvent event,
  104. MountError error_code,
  105. const MountPoint& mount_info) {
  106. for (auto& observer : observers_)
  107. observer.OnMountEvent(event, error_code, mount_info);
  108. }
  109. void MockDiskMountManager::SetupDefaultReplies() {
  110. EXPECT_CALL(*this, disks())
  111. .WillRepeatedly(ReturnRef(disks_));
  112. EXPECT_CALL(*this, mount_points())
  113. .WillRepeatedly(ReturnRef(mount_points_));
  114. EXPECT_CALL(*this, FindDiskBySourcePath(_))
  115. .Times(AnyNumber());
  116. EXPECT_CALL(*this, EnsureMountInfoRefreshed(_, _)).Times(AnyNumber());
  117. EXPECT_CALL(*this, MountPath(_, _, _, _, _, _, _)).Times(AnyNumber());
  118. EXPECT_CALL(*this, UnmountPath(_, _)).Times(AnyNumber());
  119. EXPECT_CALL(*this, RemountAllRemovableDrives(_)).Times(AnyNumber());
  120. EXPECT_CALL(*this, FormatMountedDevice(_, _, _)).Times(AnyNumber());
  121. EXPECT_CALL(*this, SinglePartitionFormatDevice(_, _, _)).Times(AnyNumber());
  122. EXPECT_CALL(*this, UnmountDeviceRecursively(_, _))
  123. .Times(AnyNumber());
  124. }
  125. void MockDiskMountManager::CreateDiskEntryForMountDevice(
  126. std::unique_ptr<Disk> disk) {
  127. disks_.insert(std::move(disk));
  128. }
  129. void MockDiskMountManager::CreateDiskEntryForMountDevice(
  130. const DiskMountManager::MountPoint& mount_info,
  131. const std::string& device_id,
  132. const std::string& device_label,
  133. const std::string& vendor_name,
  134. const std::string& product_name,
  135. DeviceType device_type,
  136. uint64_t total_size_in_bytes,
  137. bool is_parent,
  138. bool has_media,
  139. bool on_boot_device,
  140. bool on_removable_device,
  141. const std::string& file_system_type) {
  142. std::unique_ptr<Disk> disk_ptr =
  143. Disk::Builder()
  144. .SetDevicePath(mount_info.source_path)
  145. .SetMountPath(mount_info.mount_path)
  146. .SetFilePath(mount_info.source_path)
  147. .SetDeviceLabel(device_label)
  148. .SetVendorName(vendor_name)
  149. .SetProductName(product_name)
  150. .SetFileSystemUUID(device_id)
  151. .SetDeviceType(device_type)
  152. .SetSizeInBytes(total_size_in_bytes)
  153. .SetIsParent(is_parent)
  154. .SetHasMedia(has_media)
  155. .SetOnBootDevice(on_boot_device)
  156. .SetOnRemovableDevice(on_removable_device)
  157. .SetFileSystemType(file_system_type)
  158. .Build();
  159. CreateDiskEntryForMountDevice(std::move(disk_ptr));
  160. }
  161. void MockDiskMountManager::RemoveDiskEntryForMountDevice(
  162. const DiskMountManager::MountPoint& mount_info) {
  163. const auto it = disks_.find(mount_info.source_path);
  164. CHECK(it != disks_.end()) << "Cannot find " << mount_info.source_path;
  165. disks_.erase(it);
  166. }
  167. const DiskMountManager::MountPoints& MockDiskMountManager::mountPointsInternal()
  168. const {
  169. return mount_points_;
  170. }
  171. const Disk* MockDiskMountManager::FindDiskBySourcePathInternal(
  172. const std::string& source_path) const {
  173. Disks::const_iterator disk_it = disks_.find(source_path);
  174. return disk_it == disks_.end() ? nullptr : disk_it->get();
  175. }
  176. void MockDiskMountManager::NotifyDiskChanged(DiskEvent event,
  177. const Disk* disk) {
  178. for (auto& observer : observers_) {
  179. disk->is_auto_mountable() ? observer.OnAutoMountableDiskEvent(event, *disk)
  180. : observer.OnBootDeviceDiskEvent(event, *disk);
  181. }
  182. }
  183. void MockDiskMountManager::NotifyDeviceChanged(DeviceEvent event,
  184. const std::string& path) {
  185. for (auto& observer : observers_)
  186. observer.OnDeviceEvent(event, path);
  187. }
  188. } // namespace disks
  189. } // namespace ash