suspend_unmount_manager_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2015 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 <algorithm>
  5. #include <string>
  6. #include <vector>
  7. #include "ash/components/disks/disk_mount_manager.h"
  8. #include "ash/components/disks/mock_disk_mount_manager.h"
  9. #include "ash/components/disks/suspend_unmount_manager.h"
  10. #include "chromeos/dbus/power/fake_power_manager_client.h"
  11. #include "chromeos/dbus/power_manager/suspend.pb.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace ash {
  14. namespace disks {
  15. namespace {
  16. const char kDeviceId[] = "device_id";
  17. const char kDeviceLabel[] = "device_label";
  18. const char kVendor[] = "vendor";
  19. const char kProduct[] = "product";
  20. const char kFileSystemType[] = "exfat";
  21. class FakeDiskMountManager : public MockDiskMountManager {
  22. public:
  23. void NotifyUnmountDeviceComplete(MountError error) {
  24. for (size_t i = 0; i < callbacks_.size(); i++) {
  25. std::move(callbacks_[i]).Run(error);
  26. }
  27. callbacks_.clear();
  28. }
  29. const std::vector<std::string>& unmounting_mount_paths() const {
  30. return unmounting_mount_paths_;
  31. }
  32. private:
  33. void UnmountPath(const std::string& mount_path,
  34. UnmountPathCallback callback) override {
  35. unmounting_mount_paths_.push_back(mount_path);
  36. callbacks_.push_back(std::move(callback));
  37. }
  38. std::vector<std::string> unmounting_mount_paths_;
  39. std::vector<UnmountPathCallback> callbacks_;
  40. };
  41. class SuspendUnmountManagerTest : public testing::Test {
  42. public:
  43. SuspendUnmountManagerTest() {
  44. PowerManagerClient::InitializeFake();
  45. suspend_unmount_manager_ =
  46. std::make_unique<SuspendUnmountManager>(&disk_mount_manager_);
  47. }
  48. SuspendUnmountManagerTest(const SuspendUnmountManagerTest&) = delete;
  49. SuspendUnmountManagerTest& operator=(const SuspendUnmountManagerTest&) =
  50. delete;
  51. ~SuspendUnmountManagerTest() override {
  52. suspend_unmount_manager_.reset();
  53. PowerManagerClient::Shutdown();
  54. }
  55. protected:
  56. FakeDiskMountManager disk_mount_manager_;
  57. std::unique_ptr<SuspendUnmountManager> suspend_unmount_manager_;
  58. };
  59. TEST_F(SuspendUnmountManagerTest, Basic) {
  60. const std::string kDummyMountPathUsb = "/dummy/mount/usb";
  61. const std::string kDummyMountPathSd = "/dummy/mount/sd";
  62. const std::string kDummyMountPathUnknown = "/dummy/mount/unknown";
  63. disk_mount_manager_.CreateDiskEntryForMountDevice(
  64. {"/dummy/device/usb", kDummyMountPathUsb, MountType::kDevice}, kDeviceId,
  65. kDeviceLabel, kVendor, kProduct, DeviceType::kUSB, 1024 * 1024,
  66. false /* is_parent */, false /* has_media */, false /* on_boot_device */,
  67. true /* on_removable_device */, kFileSystemType);
  68. disk_mount_manager_.CreateDiskEntryForMountDevice(
  69. {"/dummy/device/sd", kDummyMountPathSd, MountType::kDevice}, kDeviceId,
  70. kDeviceLabel, kVendor, kProduct, DeviceType::kSD, 1024 * 1024,
  71. false /* is_parent */, false /* has_media */, false /* on_boot_device */,
  72. true /* on_removable_device */, kFileSystemType);
  73. disk_mount_manager_.CreateDiskEntryForMountDevice(
  74. {"/dummy/device/unknown", kDummyMountPathUnknown, MountType::kDevice},
  75. kDeviceId, kDeviceLabel, kVendor, kProduct, DeviceType::kUnknown,
  76. 1024 * 1024, false /* is_parent */, false /* has_media */,
  77. false /* on_boot_device */, true /* on_removable_device */,
  78. kFileSystemType);
  79. disk_mount_manager_.SetupDefaultReplies();
  80. FakePowerManagerClient::Get()->SendSuspendImminent(
  81. power_manager::SuspendImminent_Reason_OTHER);
  82. EXPECT_EQ(
  83. 1,
  84. FakePowerManagerClient::Get()->num_pending_suspend_readiness_callbacks());
  85. EXPECT_EQ(2u, disk_mount_manager_.unmounting_mount_paths().size());
  86. EXPECT_EQ(1, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
  87. disk_mount_manager_.unmounting_mount_paths().end(),
  88. kDummyMountPathUsb));
  89. EXPECT_EQ(1, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
  90. disk_mount_manager_.unmounting_mount_paths().end(),
  91. kDummyMountPathSd));
  92. EXPECT_EQ(0, std::count(disk_mount_manager_.unmounting_mount_paths().begin(),
  93. disk_mount_manager_.unmounting_mount_paths().end(),
  94. kDummyMountPathUnknown));
  95. disk_mount_manager_.NotifyUnmountDeviceComplete(MountError::kNone);
  96. EXPECT_EQ(
  97. 0,
  98. FakePowerManagerClient::Get()->num_pending_suspend_readiness_callbacks());
  99. }
  100. TEST_F(SuspendUnmountManagerTest, CancelAndSuspendAgain) {
  101. const std::string kDummyMountPath = "/dummy/mount";
  102. disk_mount_manager_.CreateDiskEntryForMountDevice(
  103. {"/dummy/device", kDummyMountPath, MountType::kDevice}, kDeviceId,
  104. kDeviceLabel, kVendor, kProduct, DeviceType::kUSB, 1024 * 1024,
  105. false /* is_parent */, false /* has_media */, false /* on_boot_device */,
  106. true /* on_removable_device */, kFileSystemType);
  107. disk_mount_manager_.SetupDefaultReplies();
  108. FakePowerManagerClient::Get()->SendSuspendImminent(
  109. power_manager::SuspendImminent_Reason_OTHER);
  110. EXPECT_EQ(
  111. 1,
  112. FakePowerManagerClient::Get()->num_pending_suspend_readiness_callbacks());
  113. ASSERT_EQ(1u, disk_mount_manager_.unmounting_mount_paths().size());
  114. EXPECT_EQ(kDummyMountPath,
  115. disk_mount_manager_.unmounting_mount_paths().front());
  116. // Suspend cancelled.
  117. FakePowerManagerClient::Get()->SendSuspendDone();
  118. // Suspend again.
  119. FakePowerManagerClient::Get()->SendSuspendImminent(
  120. power_manager::SuspendImminent_Reason_OTHER);
  121. ASSERT_EQ(2u, disk_mount_manager_.unmounting_mount_paths().size());
  122. EXPECT_EQ(kDummyMountPath,
  123. disk_mount_manager_.unmounting_mount_paths().front());
  124. }
  125. } // namespace
  126. } // namespace disks
  127. } // namespace ash