remote_device_cache_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2018 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/multidevice/remote_device_cache.h"
  5. #include <algorithm>
  6. #include "ash/components/multidevice/remote_device_test_util.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace ash::multidevice {
  9. class RemoteDeviceCacheTest : public testing::Test {
  10. public:
  11. RemoteDeviceCacheTest(const RemoteDeviceCacheTest&) = delete;
  12. RemoteDeviceCacheTest& operator=(const RemoteDeviceCacheTest&) = delete;
  13. protected:
  14. RemoteDeviceCacheTest()
  15. : test_remote_device_list_(CreateRemoteDeviceListForTest(5)),
  16. test_remote_device_ref_list_(CreateRemoteDeviceRefListForTest(5)) {}
  17. // testing::Test:
  18. void SetUp() override { cache_ = RemoteDeviceCache::Factory::Create(); }
  19. void VerifyCacheRemoteDevices(
  20. RemoteDeviceRefList expected_remote_device_ref_list) {
  21. RemoteDeviceRefList remote_device_ref_list = cache_->GetRemoteDevices();
  22. std::sort(remote_device_ref_list.begin(), remote_device_ref_list.end(),
  23. [](const auto& device_1, const auto& device_2) {
  24. return device_1 < device_2;
  25. });
  26. EXPECT_EQ(expected_remote_device_ref_list, remote_device_ref_list);
  27. }
  28. RemoteDeviceList test_remote_device_list_;
  29. const RemoteDeviceRefList test_remote_device_ref_list_;
  30. std::unique_ptr<RemoteDeviceCache> cache_;
  31. };
  32. TEST_F(RemoteDeviceCacheTest, TestNoRemoteDevices) {
  33. VerifyCacheRemoteDevices(RemoteDeviceRefList());
  34. EXPECT_EQ(absl::nullopt, cache_->GetRemoteDevice(
  35. test_remote_device_ref_list_[0].instance_id(),
  36. test_remote_device_ref_list_[0].GetDeviceId()));
  37. }
  38. TEST_F(RemoteDeviceCacheTest, TestSetAndGetRemoteDevices) {
  39. cache_->SetRemoteDevices(test_remote_device_list_);
  40. VerifyCacheRemoteDevices(test_remote_device_ref_list_);
  41. EXPECT_EQ(
  42. test_remote_device_ref_list_[0],
  43. cache_->GetRemoteDevice(test_remote_device_ref_list_[0].instance_id(),
  44. test_remote_device_ref_list_[0].GetDeviceId()));
  45. }
  46. TEST_F(RemoteDeviceCacheTest,
  47. TestSetAndGetRemoteDevices_LookupByInstanceIdOrPublicKey) {
  48. test_remote_device_list_[0].instance_id.clear();
  49. test_remote_device_list_[1].public_key.clear();
  50. cache_->SetRemoteDevices(test_remote_device_list_);
  51. GetMutableRemoteDevice(test_remote_device_ref_list_[0])->instance_id.clear();
  52. GetMutableRemoteDevice(test_remote_device_ref_list_[1])->public_key.clear();
  53. VerifyCacheRemoteDevices(test_remote_device_ref_list_);
  54. EXPECT_EQ(
  55. test_remote_device_ref_list_[0],
  56. cache_->GetRemoteDevice(absl::nullopt /* instance_id */,
  57. test_remote_device_ref_list_[0].GetDeviceId()));
  58. EXPECT_EQ(
  59. test_remote_device_ref_list_[1],
  60. cache_->GetRemoteDevice(test_remote_device_ref_list_[1].instance_id(),
  61. absl::nullopt /* legacy_device_id */));
  62. EXPECT_EQ(
  63. test_remote_device_ref_list_[2],
  64. cache_->GetRemoteDevice(absl::nullopt /* instance_id */,
  65. test_remote_device_ref_list_[2].GetDeviceId()));
  66. EXPECT_EQ(
  67. test_remote_device_ref_list_[2],
  68. cache_->GetRemoteDevice(test_remote_device_ref_list_[2].instance_id(),
  69. absl::nullopt /* legacy_device_id */));
  70. }
  71. TEST_F(RemoteDeviceCacheTest,
  72. TestSetRemoteDevices_RemoteDeviceRefsRemainValidAfterCacheRemoval) {
  73. cache_->SetRemoteDevices(test_remote_device_list_);
  74. VerifyCacheRemoteDevices(test_remote_device_ref_list_);
  75. cache_->SetRemoteDevices(RemoteDeviceList());
  76. VerifyCacheRemoteDevices(test_remote_device_ref_list_);
  77. }
  78. TEST_F(RemoteDeviceCacheTest,
  79. TestSetRemoteDevices_RemoteDeviceRefsRemainValidAfterValidCacheUpdate) {
  80. // Store the device with a last update time of 1000.
  81. RemoteDevice remote_device = CreateRemoteDeviceForTest();
  82. remote_device.last_update_time_millis = 1000;
  83. cache_->SetRemoteDevices({remote_device});
  84. RemoteDeviceRef remote_device_ref = *cache_->GetRemoteDevice(
  85. remote_device.instance_id, remote_device.GetDeviceId());
  86. EXPECT_EQ(remote_device.name, remote_device_ref.name());
  87. // Update the device's name and update time. Since the incoming remote device
  88. // has a newer update time, the entry should successfully update.
  89. remote_device.name = "new name";
  90. remote_device.last_update_time_millis = 2000;
  91. cache_->SetRemoteDevices({remote_device});
  92. EXPECT_EQ(remote_device.name, remote_device_ref.name());
  93. }
  94. // Currently disabled; will be re-enabled when https://crbug.com/856746 is
  95. // fixed.
  96. TEST_F(
  97. RemoteDeviceCacheTest,
  98. DISABLED_TestSetRemoteDevices_RemoteDeviceCacheDoesNotUpdateWithStaleRemoteDevice) {
  99. // Store the device with a last update time of 1000.
  100. RemoteDevice remote_device = CreateRemoteDeviceForTest();
  101. remote_device.last_update_time_millis = 1000;
  102. cache_->SetRemoteDevices({remote_device});
  103. RemoteDeviceRef remote_device_ref = *cache_->GetRemoteDevice(
  104. remote_device.instance_id, remote_device.GetDeviceId());
  105. EXPECT_EQ(remote_device.name, remote_device_ref.name());
  106. // Update the device's name and update time, this time reducing the
  107. // last update time to 500. Since this is less than 1000, adding the
  108. // device to the cache should not cause it to overwrite the previous
  109. // entry, since this entry is older.
  110. std::string prev_name = remote_device.name;
  111. remote_device.last_update_time_millis = 500;
  112. remote_device.name = "new name";
  113. cache_->SetRemoteDevices({remote_device});
  114. EXPECT_EQ(prev_name, remote_device_ref.name());
  115. }
  116. } // namespace ash::multidevice