Chromium Code Reviews| 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 const UserEventSpecifics specifics1(CreateSpecifics(1u, 2u, 3u)); | |
| 171 const UserEventSpecifics specifics2(CreateSpecifics(1u, 4u, 3u)); | |
| 172 const UserEventSpecifics specifics3(CreateSpecifics(1u, 5u, 3u)); | |
| 173 | |
| 174 mapper()->ChangeId(2u, 4u); | |
| 175 bridge()->RecordUserEvent(base::MakeUnique<UserEventSpecifics>(specifics1)); | |
| 176 const std::string storage_key = processor().put_multimap().begin()->first; | |
| 177 EXPECT_EQ(1u, processor().put_multimap().size()); | |
| 178 bridge()->GetAllData(VerifyCallback({{storage_key, specifics2}})); | |
| 179 | |
| 180 // This id update is done while the event is "in flight", and should result in | |
| 181 // it being updated and re-sent to sync. | |
| 182 mapper()->ChangeId(4u, 5u); | |
| 183 EXPECT_EQ(2u, processor().put_multimap().size()); | |
| 184 bridge()->GetAllData(VerifyCallback({{storage_key, specifics3}})); | |
|
Patrick Noland
2017/06/28 22:44:37
It's kind of hard to follow the different timestam
skym
2017/07/05 19:14:28
Done.
| |
| 185 auto error_on_delete = | |
| 186 bridge()->ApplySyncChanges(bridge()->CreateMetadataChangeList(), | |
| 187 {EntityChange::CreateDelete(storage_key)}); | |
| 188 EXPECT_FALSE(error_on_delete); | |
| 189 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 0)); | |
| 190 | |
| 191 // This id update should be ignored, since we received commit confirmation | |
| 192 // above. | |
| 193 mapper()->ChangeId(5u, 6u); | |
| 194 EXPECT_EQ(2u, processor().put_multimap().size()); | |
| 195 bridge()->GetAllData(base::Bind(&VerifyDataBatchCount, 0)); | |
| 196 } | |
| 197 | |
| 144 } // namespace | 198 } // namespace |
| 145 | 199 |
| 146 } // namespace syncer | 200 } // namespace syncer |
| OLD | NEW |