sessions_global_id_mapper_unittest.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/sync_sessions/sessions_global_id_mapper.h"
  5. #include "base/test/mock_callback.h"
  6. #include "base/time/time.h"
  7. #include "testing/gmock/include/gmock/gmock.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace sync_sessions {
  10. namespace {
  11. const base::Time kTime1 = base::Time::FromInternalValue(110);
  12. const base::Time kTime2 = base::Time::FromInternalValue(120);
  13. const base::Time kTime3 = base::Time::FromInternalValue(130);
  14. const base::Time kTime4 = base::Time::FromInternalValue(140);
  15. const base::Time kTime5 = base::Time::FromInternalValue(150);
  16. // Tests that GetLatestGlobalId returns correct mappings for updated global_ids.
  17. TEST(SessionsGlobalIdMapperTest, GetLatestGlobalId) {
  18. SessionsGlobalIdMapper mapper;
  19. mapper.TrackNavigationId(kTime1, /*unique_id=*/1);
  20. mapper.TrackNavigationId(kTime2, /*unique_id=*/2);
  21. mapper.TrackNavigationId(kTime3, /*unique_id=*/2);
  22. mapper.TrackNavigationId(kTime4, /*unique_id=*/2);
  23. EXPECT_EQ(kTime1.ToInternalValue(),
  24. mapper.GetLatestGlobalId(kTime1.ToInternalValue()));
  25. EXPECT_EQ(kTime4.ToInternalValue(),
  26. mapper.GetLatestGlobalId(kTime2.ToInternalValue()));
  27. EXPECT_EQ(kTime4.ToInternalValue(),
  28. mapper.GetLatestGlobalId(kTime3.ToInternalValue()));
  29. EXPECT_EQ(kTime4.ToInternalValue(),
  30. mapper.GetLatestGlobalId(kTime4.ToInternalValue()));
  31. // kTime5 is not mapped, so itself should be returned.
  32. EXPECT_EQ(kTime5.ToInternalValue(),
  33. mapper.GetLatestGlobalId(kTime5.ToInternalValue()));
  34. }
  35. // Tests that the global_id mapping is eventually dropped after we reach our
  36. // threshold for the amount to remember.
  37. TEST(SessionsGlobalIdMapperTest, Cleanup) {
  38. SessionsGlobalIdMapper mapper;
  39. base::Time current_time = kTime1;
  40. mapper.TrackNavigationId(current_time, /*unique_id=*/1);
  41. for (int i = 0; i < 105; i++) {
  42. current_time =
  43. base::Time::FromInternalValue(current_time.ToInternalValue() + 1);
  44. mapper.TrackNavigationId(current_time, /*unique_id=*/1);
  45. }
  46. // Threshold is 100, kTime1 should be dropped, kTime1+10 should not.
  47. EXPECT_EQ(kTime1.ToInternalValue(),
  48. mapper.GetLatestGlobalId(kTime1.ToInternalValue()));
  49. EXPECT_EQ(current_time.ToInternalValue(),
  50. mapper.GetLatestGlobalId(10 + kTime1.ToInternalValue()));
  51. }
  52. // Tests that subscribers to AddGlobalIdChangeObserver are notified when a
  53. // global_id is noticed to have been changed.
  54. TEST(SessionsGlobalIdMapperTest, AddObserver) {
  55. SessionsGlobalIdMapper mapper;
  56. mapper.TrackNavigationId(kTime1, /*unique_id=*/1);
  57. base::MockCallback<syncer::GlobalIdChange> mock_callback;
  58. EXPECT_CALL(mock_callback, Run).Times(0);
  59. mapper.AddGlobalIdChangeObserver(mock_callback.Get());
  60. EXPECT_CALL(mock_callback,
  61. Run(kTime1.ToInternalValue(), kTime2.ToInternalValue()))
  62. .Times(0);
  63. }
  64. } // namespace
  65. } // namespace sync_sessions