user_event_service_impl_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2017 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_user_events/user_event_service_impl.h"
  5. #include <utility>
  6. #include <vector>
  7. #include "base/test/task_environment.h"
  8. #include "components/sync/base/model_type.h"
  9. #include "components/sync/driver/test_sync_service.h"
  10. #include "components/sync/protocol/user_event_specifics.pb.h"
  11. #include "components/sync/test/mock_model_type_change_processor.h"
  12. #include "components/sync/test/model_type_store_test_util.h"
  13. #include "components/sync_user_events/user_event_sync_bridge.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. using sync_pb::UserEventSpecifics;
  16. namespace syncer {
  17. namespace {
  18. std::unique_ptr<UserEventSpecifics> Event() {
  19. return std::make_unique<UserEventSpecifics>();
  20. }
  21. std::unique_ptr<UserEventSpecifics> AsTest(
  22. std::unique_ptr<UserEventSpecifics> specifics) {
  23. specifics->mutable_test_event();
  24. return specifics;
  25. }
  26. std::unique_ptr<UserEventSpecifics> AsGaiaPasswordReuseEvent(
  27. std::unique_ptr<UserEventSpecifics> specifics) {
  28. specifics->mutable_gaia_password_reuse_event();
  29. return specifics;
  30. }
  31. std::unique_ptr<UserEventSpecifics> AsGaiaPasswordCaptured(
  32. std::unique_ptr<UserEventSpecifics> specifics) {
  33. specifics->mutable_gaia_password_captured_event();
  34. return specifics;
  35. }
  36. std::unique_ptr<UserEventSpecifics> WithNav(
  37. std::unique_ptr<UserEventSpecifics> specifics,
  38. int64_t navigation_id = 1) {
  39. specifics->set_navigation_id(navigation_id);
  40. return specifics;
  41. }
  42. class TestGlobalIdMapper : public GlobalIdMapper {
  43. void AddGlobalIdChangeObserver(GlobalIdChange callback) override {}
  44. int64_t GetLatestGlobalId(int64_t global_id) override { return global_id; }
  45. };
  46. class UserEventServiceImplTest : public testing::Test {
  47. protected:
  48. UserEventServiceImplTest() {
  49. sync_service_.SetPreferredDataTypes(
  50. {HISTORY_DELETE_DIRECTIVES, USER_EVENTS});
  51. ON_CALL(mock_processor_, IsTrackingMetadata())
  52. .WillByDefault(testing::Return(true));
  53. ON_CALL(mock_processor_, TrackedAccountId())
  54. .WillByDefault(testing::Return("account_id"));
  55. }
  56. std::unique_ptr<UserEventSyncBridge> MakeBridge() {
  57. return std::make_unique<UserEventSyncBridge>(
  58. ModelTypeStoreTestUtil::FactoryForInMemoryStoreForTest(),
  59. mock_processor_.CreateForwardingProcessor(), &mapper_);
  60. }
  61. syncer::TestSyncService* sync_service() { return &sync_service_; }
  62. MockModelTypeChangeProcessor* mock_processor() { return &mock_processor_; }
  63. private:
  64. base::test::TaskEnvironment task_environment_;
  65. syncer::TestSyncService sync_service_;
  66. testing::NiceMock<MockModelTypeChangeProcessor> mock_processor_;
  67. TestGlobalIdMapper mapper_;
  68. };
  69. TEST_F(UserEventServiceImplTest, ShouldRecord) {
  70. UserEventServiceImpl service(MakeBridge());
  71. EXPECT_CALL(*mock_processor(), Put);
  72. service.RecordUserEvent(AsTest(Event()));
  73. }
  74. TEST_F(UserEventServiceImplTest, ShouldNotRecordWhenSyncIsNotStarted) {
  75. ON_CALL(*mock_processor(), IsTrackingMetadata())
  76. .WillByDefault(testing::Return(false));
  77. UserEventServiceImpl service(MakeBridge());
  78. // Do not record events when the engine is off.
  79. EXPECT_CALL(*mock_processor(), Put).Times(0);
  80. service.RecordUserEvent(WithNav(AsTest(Event())));
  81. service.RecordUserEvent(AsTest(Event()));
  82. }
  83. TEST_F(UserEventServiceImplTest, ShouldNotRecordEmptyEvents) {
  84. UserEventServiceImpl service(MakeBridge());
  85. // All untyped events should always be ignored.
  86. EXPECT_CALL(*mock_processor(), Put).Times(0);
  87. service.RecordUserEvent(Event());
  88. service.RecordUserEvent(WithNav(Event()));
  89. }
  90. TEST_F(UserEventServiceImplTest, ShouldRecordHasNavigationId) {
  91. UserEventServiceImpl service(MakeBridge());
  92. // Verify logic for types that might or might not have a navigation id.
  93. EXPECT_CALL(*mock_processor(), Put);
  94. service.RecordUserEvent(AsTest(Event()));
  95. EXPECT_CALL(*mock_processor(), Put);
  96. service.RecordUserEvent(WithNav(AsTest(Event())));
  97. // Verify logic for types that must have a navigation id.
  98. EXPECT_CALL(*mock_processor(), Put).Times(0);
  99. service.RecordUserEvent(AsGaiaPasswordReuseEvent(Event()));
  100. EXPECT_CALL(*mock_processor(), Put);
  101. service.RecordUserEvent(WithNav(AsGaiaPasswordReuseEvent(Event())));
  102. // Verify logic for types that cannot have a navigation id.
  103. EXPECT_CALL(*mock_processor(), Put);
  104. service.RecordUserEvent(AsGaiaPasswordCaptured(Event()));
  105. EXPECT_CALL(*mock_processor(), Put).Times(0);
  106. service.RecordUserEvent(WithNav(AsGaiaPasswordCaptured(Event())));
  107. }
  108. TEST_F(UserEventServiceImplTest, SessionIdIsDifferent) {
  109. std::vector<int64_t> put_session_ids;
  110. ON_CALL(*mock_processor(), Put)
  111. .WillByDefault([&](const std::string& storage_key,
  112. const std::unique_ptr<EntityData> entity_data,
  113. MetadataChangeList* metadata_change_list) {
  114. put_session_ids.push_back(
  115. entity_data->specifics.user_event().session_id());
  116. });
  117. UserEventServiceImpl service1(MakeBridge());
  118. service1.RecordUserEvent(AsTest(Event()));
  119. UserEventServiceImpl service2(MakeBridge());
  120. service2.RecordUserEvent(AsTest(Event()));
  121. ASSERT_EQ(2U, put_session_ids.size());
  122. EXPECT_NE(put_session_ids[0], put_session_ids[1]);
  123. }
  124. } // namespace
  125. } // namespace syncer