storage_monitor_mac_unittest.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_mac.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/files/file_util.h"
  10. #include "base/files/scoped_temp_dir.h"
  11. #include "base/mac/foundation_util.h"
  12. #include "base/run_loop.h"
  13. #include "base/strings/sys_string_conversions.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "components/storage_monitor/mock_removable_storage_observer.h"
  16. #include "components/storage_monitor/removable_device_constants.h"
  17. #include "components/storage_monitor/storage_info.h"
  18. #include "components/storage_monitor/test_storage_monitor.h"
  19. #include "content/public/browser/browser_task_traits.h"
  20. #include "content/public/browser/browser_thread.h"
  21. #include "content/public/test/browser_task_environment.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. uint64_t kTestSize = 1000000ULL;
  24. namespace storage_monitor {
  25. namespace {
  26. StorageInfo CreateStorageInfo(const std::string& device_id,
  27. const std::string& model_name,
  28. const base::FilePath& mount_point,
  29. uint64_t size_bytes) {
  30. return StorageInfo(device_id, mount_point.value(), std::u16string(),
  31. std::u16string(), base::UTF8ToUTF16(model_name),
  32. size_bytes);
  33. }
  34. } // namespace
  35. class StorageMonitorMacTest : public testing::Test {
  36. public:
  37. StorageMonitorMacTest() {}
  38. void SetUp() override {
  39. monitor_ = std::make_unique<StorageMonitorMac>();
  40. mock_storage_observer_ = std::make_unique<MockRemovableStorageObserver>();
  41. monitor_->AddObserver(mock_storage_observer_.get());
  42. unique_id_ = "test_id";
  43. mount_point_ = base::FilePath("/unused_test_directory");
  44. device_id_ = StorageInfo::MakeDeviceId(
  45. StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_);
  46. disk_info_ = CreateStorageInfo(device_id_, "",
  47. mount_point_, kTestSize);
  48. }
  49. void UpdateDisk(StorageInfo info, StorageMonitorMac::UpdateType update_type) {
  50. content::GetUIThreadTaskRunner({})->PostTask(
  51. FROM_HERE,
  52. base::BindOnce(&StorageMonitorMac::UpdateDisk,
  53. base::Unretained(monitor_.get()), update_type,
  54. base::Owned(new std::string("dummy_bsd_name")), info));
  55. base::RunLoop().RunUntilIdle();
  56. }
  57. protected:
  58. content::BrowserTaskEnvironment task_environment_;
  59. std::unique_ptr<MockRemovableStorageObserver> mock_storage_observer_;
  60. // Information about the disk.
  61. std::string unique_id_;
  62. base::FilePath mount_point_;
  63. std::string device_id_;
  64. StorageInfo disk_info_;
  65. std::unique_ptr<StorageMonitorMac> monitor_;
  66. };
  67. TEST_F(StorageMonitorMacTest, AddRemove) {
  68. UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
  69. EXPECT_EQ(1, mock_storage_observer_->attach_calls());
  70. EXPECT_EQ(0, mock_storage_observer_->detach_calls());
  71. EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
  72. EXPECT_EQ(mount_point_.value(),
  73. mock_storage_observer_->last_attached().location());
  74. UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_REMOVED);
  75. EXPECT_EQ(1, mock_storage_observer_->attach_calls());
  76. EXPECT_EQ(1, mock_storage_observer_->detach_calls());
  77. EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id());
  78. }
  79. TEST_F(StorageMonitorMacTest, UpdateVolumeName) {
  80. UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
  81. EXPECT_EQ(1, mock_storage_observer_->attach_calls());
  82. EXPECT_EQ(0, mock_storage_observer_->detach_calls());
  83. EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
  84. EXPECT_EQ(kTestSize,
  85. mock_storage_observer_->last_attached().total_size_in_bytes());
  86. EXPECT_EQ(mount_point_.value(),
  87. mock_storage_observer_->last_attached().location());
  88. StorageInfo info2 = CreateStorageInfo(
  89. device_id_, "", mount_point_, kTestSize * 2);
  90. UpdateDisk(info2, StorageMonitorMac::UPDATE_DEVICE_CHANGED);
  91. base::RunLoop().RunUntilIdle();
  92. EXPECT_EQ(1, mock_storage_observer_->detach_calls());
  93. EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id());
  94. EXPECT_EQ(2, mock_storage_observer_->attach_calls());
  95. EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
  96. EXPECT_EQ(kTestSize * 2,
  97. mock_storage_observer_->last_attached().total_size_in_bytes());
  98. EXPECT_EQ(mount_point_.value(),
  99. mock_storage_observer_->last_attached().location());
  100. }
  101. TEST_F(StorageMonitorMacTest, DCIM) {
  102. base::ScopedTempDir temp_dir;
  103. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  104. ASSERT_TRUE(
  105. base::CreateDirectory(temp_dir.GetPath().Append(kDCIMDirectoryName)));
  106. base::FilePath mount_point = temp_dir.GetPath();
  107. std::string device_id = StorageInfo::MakeDeviceId(
  108. StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_);
  109. StorageInfo info = CreateStorageInfo(device_id, "", mount_point, kTestSize);
  110. UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED);
  111. EXPECT_EQ(1, mock_storage_observer_->attach_calls());
  112. EXPECT_EQ(0, mock_storage_observer_->detach_calls());
  113. EXPECT_EQ(device_id, mock_storage_observer_->last_attached().device_id());
  114. EXPECT_EQ(mount_point.value(),
  115. mock_storage_observer_->last_attached().location());
  116. }
  117. TEST_F(StorageMonitorMacTest, GetStorageInfo) {
  118. UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
  119. EXPECT_EQ(1, mock_storage_observer_->attach_calls());
  120. EXPECT_EQ(0, mock_storage_observer_->detach_calls());
  121. EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
  122. EXPECT_EQ(mount_point_.value(),
  123. mock_storage_observer_->last_attached().location());
  124. StorageInfo info;
  125. EXPECT_TRUE(monitor_->GetStorageInfoForPath(mount_point_.AppendASCII("foo"),
  126. &info));
  127. EXPECT_EQ(device_id_, info.device_id());
  128. EXPECT_EQ(mount_point_.value(), info.location());
  129. EXPECT_EQ(kTestSize, info.total_size_in_bytes());
  130. EXPECT_FALSE(monitor_->GetStorageInfoForPath(
  131. base::FilePath("/non/matching/path"), &info));
  132. }
  133. // Test that mounting a DMG doesn't send a notification.
  134. TEST_F(StorageMonitorMacTest, DMG) {
  135. StorageInfo info = CreateStorageInfo(
  136. device_id_, "Disk Image", mount_point_, kTestSize);
  137. UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED);
  138. EXPECT_EQ(0, mock_storage_observer_->attach_calls());
  139. }
  140. } // namespace storage_monitor