OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/user_events/user_event_sync_bridge.h" | 5 #include "components/sync/user_events/user_event_sync_bridge.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "components/sync/driver/fake_sync_service.h" | 15 #include "components/sync/driver/fake_sync_service.h" |
15 #include "components/sync/model/data_batch.h" | 16 #include "components/sync/model/data_batch.h" |
16 #include "components/sync/model/model_type_store_test_util.h" | 17 #include "components/sync/model/model_type_store_test_util.h" |
17 #include "components/sync/model/recording_model_type_change_processor.h" | 18 #include "components/sync/model/recording_model_type_change_processor.h" |
18 #include "components/sync/protocol/sync.pb.h" | 19 #include "components/sync/protocol/sync.pb.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return specifics; | 70 return specifics; |
70 } | 71 } |
71 | 72 |
72 std::unique_ptr<UserEventSpecifics> SpecificsUniquePtr(int64_t event_time_usec, | 73 std::unique_ptr<UserEventSpecifics> SpecificsUniquePtr(int64_t event_time_usec, |
73 int64_t navigation_id, | 74 int64_t navigation_id, |
74 uint64_t session_id) { | 75 uint64_t session_id) { |
75 return base::MakeUnique<UserEventSpecifics>( | 76 return base::MakeUnique<UserEventSpecifics>( |
76 CreateSpecifics(event_time_usec, navigation_id, session_id)); | 77 CreateSpecifics(event_time_usec, navigation_id, session_id)); |
77 } | 78 } |
78 | 79 |
| 80 class TestGlobalIdMapper : public GlobalIdMapper { |
| 81 public: |
| 82 void AddGlobalIdChangeObserver(GlobalIdChange callback) override { |
| 83 callback_ = std::move(callback); |
| 84 } |
| 85 |
| 86 int64_t GetLatestGlobalId(int64_t global_id) override { |
| 87 auto iter = id_map_.find(global_id); |
| 88 return iter == id_map_.end() ? global_id : iter->second; |
| 89 } |
| 90 |
| 91 void ChangeId(int64_t old_id, int64_t new_id) { |
| 92 id_map_[old_id] = new_id; |
| 93 callback_.Run(old_id, new_id); |
| 94 } |
| 95 |
| 96 private: |
| 97 GlobalIdChange callback_; |
| 98 std::map<int64_t, int64_t> id_map_; |
| 99 }; |
| 100 |
79 class UserEventSyncBridgeTest : public testing::Test { | 101 class UserEventSyncBridgeTest : public testing::Test { |
80 protected: | 102 protected: |
81 UserEventSyncBridgeTest() { | 103 UserEventSyncBridgeTest() { |
82 bridge_ = base::MakeUnique<UserEventSyncBridge>( | 104 bridge_ = base::MakeUnique<UserEventSyncBridge>( |
83 ModelTypeStoreTestUtil::FactoryForInMemoryStoreForTest(), | 105 ModelTypeStoreTestUtil::FactoryForInMemoryStoreForTest(), |
84 RecordingModelTypeChangeProcessor::FactoryForBridgeTest(&processor_)); | 106 RecordingModelTypeChangeProcessor::FactoryForBridgeTest(&processor_), |
| 107 &test_global_id_mapper_); |
85 } | 108 } |
86 | 109 |
87 UserEventSyncBridge* bridge() { return bridge_.get(); } | 110 UserEventSyncBridge* bridge() { return bridge_.get(); } |
88 const RecordingModelTypeChangeProcessor& processor() { return *processor_; } | 111 const RecordingModelTypeChangeProcessor& processor() { return *processor_; } |
| 112 TestGlobalIdMapper* mapper() { return &test_global_id_mapper_; } |
89 | 113 |
90 private: | 114 private: |
91 std::unique_ptr<UserEventSyncBridge> bridge_; | 115 std::unique_ptr<UserEventSyncBridge> bridge_; |
92 RecordingModelTypeChangeProcessor* processor_; | 116 RecordingModelTypeChangeProcessor* processor_; |
| 117 TestGlobalIdMapper test_global_id_mapper_; |
93 base::MessageLoop message_loop_; | 118 base::MessageLoop message_loop_; |
94 }; | 119 }; |
95 | 120 |
96 TEST_F(UserEventSyncBridgeTest, MetadataIsInitialized) { | 121 TEST_F(UserEventSyncBridgeTest, MetadataIsInitialized) { |
97 base::RunLoop().RunUntilIdle(); | 122 base::RunLoop().RunUntilIdle(); |
98 EXPECT_TRUE(processor().metadata()->GetModelTypeState().initial_sync_done()); | 123 EXPECT_TRUE(processor().metadata()->GetModelTypeState().initial_sync_done()); |
99 } | 124 } |
100 | 125 |
101 TEST_F(UserEventSyncBridgeTest, SingleRecord) { | 126 TEST_F(UserEventSyncBridgeTest, SingleRecord) { |
102 const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u)); | 127 const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 2)); | 159 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 2)); |
135 | 160 |
136 const std::string storage_key = processor().put_multimap().begin()->first; | 161 const std::string storage_key = processor().put_multimap().begin()->first; |
137 auto error_on_delete = | 162 auto error_on_delete = |
138 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | 163 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), |
139 {EntityChange::CreateDelete(storage_key)}); | 164 {EntityChange::CreateDelete(storage_key)}); |
140 EXPECT_FALSE(error_on_delete); | 165 EXPECT_FALSE(error_on_delete); |
141 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 1)); | 166 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 1)); |
142 } | 167 } |
143 | 168 |
| 169 TEST_F(UserEventSyncBridgeTest, HandleGlobalIdChange) { |
| 170 int64_t first_id = 11; |
| 171 int64_t second_id = 12; |
| 172 int64_t third_id = 13; |
| 173 int64_t fourth_id = 14; |
| 174 |
| 175 // This id update should be applied to the event as it is initially recorded. |
| 176 mapper()->ChangeId(first_id, second_id); |
| 177 bridge()->RecordUserEvent( |
| 178 base::MakeUnique<UserEventSpecifics>(CreateSpecifics(1u, first_id, 2u))); |
| 179 const std::string storage_key = processor().put_multimap().begin()->first; |
| 180 EXPECT_EQ(1u, processor().put_multimap().size()); |
| 181 bridge()->GetAllData( |
| 182 VerifyCallback({{storage_key, CreateSpecifics(1u, second_id, 2u)}})); |
| 183 |
| 184 // This id update is done while the event is "in flight", and should result in |
| 185 // it being updated and re-sent to sync. |
| 186 mapper()->ChangeId(second_id, third_id); |
| 187 EXPECT_EQ(2u, processor().put_multimap().size()); |
| 188 bridge()->GetAllData( |
| 189 VerifyCallback({{storage_key, CreateSpecifics(1u, third_id, 2u)}})); |
| 190 auto error_on_delete = |
| 191 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), |
| 192 {EntityChange::CreateDelete(storage_key)}); |
| 193 EXPECT_FALSE(error_on_delete); |
| 194 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 0)); |
| 195 |
| 196 // This id update should be ignored, since we received commit confirmation |
| 197 // above. |
| 198 mapper()->ChangeId(third_id, fourth_id); |
| 199 EXPECT_EQ(2u, processor().put_multimap().size()); |
| 200 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 0)); |
| 201 } |
| 202 |
144 } // namespace | 203 } // namespace |
145 | 204 |
146 } // namespace syncer | 205 } // namespace syncer |
OLD | NEW |