1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync_user_events/user_event_sync_bridge.h"
#include <map>
#include <set>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/user_event_specifics.pb.h"
#include "components/sync/test/data_type_store_test_util.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
using sync_pb::UserEventSpecifics;
using testing::_;
using testing::ElementsAre;
using testing::InvokeWithoutArgs;
using testing::IsEmpty;
using testing::IsNull;
using testing::NotNull;
using testing::Pair;
using testing::Pointee;
using testing::Return;
using testing::SaveArg;
using testing::SizeIs;
using testing::UnorderedElementsAre;
using testing::WithArg;
using WriteBatch = DataTypeStore::WriteBatch;
MATCHER_P(MatchesUserEvent, expected, "") {
if (!arg.has_user_event()) {
*result_listener << "which is not a user event";
return false;
}
const UserEventSpecifics& actual = arg.user_event();
if (actual.event_time_usec() != expected.event_time_usec()) {
return false;
}
if (actual.navigation_id() != expected.navigation_id()) {
return false;
}
if (actual.session_id() != expected.session_id()) {
return false;
}
return true;
}
UserEventSpecifics CreateSpecifics(int64_t event_time_usec,
int64_t navigation_id,
uint64_t session_id) {
UserEventSpecifics specifics;
specifics.set_event_time_usec(event_time_usec);
specifics.set_navigation_id(navigation_id);
specifics.set_session_id(session_id);
return specifics;
}
std::unique_ptr<UserEventSpecifics> SpecificsUniquePtr(int64_t event_time_usec,
int64_t navigation_id,
uint64_t session_id) {
return std::make_unique<UserEventSpecifics>(
CreateSpecifics(event_time_usec, navigation_id, session_id));
}
class TestGlobalIdMapper : public GlobalIdMapper {
public:
void AddGlobalIdChangeObserver(GlobalIdChange callback) override {
callback_ = std::move(callback);
}
int64_t GetLatestGlobalId(int64_t global_id) override {
auto iter = id_map_.find(global_id);
return iter == id_map_.end() ? global_id : iter->second;
}
void ChangeId(int64_t old_id, int64_t new_id) {
id_map_[old_id] = new_id;
callback_.Run(old_id, new_id);
}
private:
GlobalIdChange callback_;
std::map<int64_t, int64_t> id_map_;
};
class UserEventSyncBridgeTest : public testing::Test {
protected:
UserEventSyncBridgeTest() { ResetBridge(); }
void ResetBridge() {
OnceDataTypeStoreFactory store_factory;
if (bridge_) {
// Carry over the underlying store from previous bridge instances.
std::unique_ptr<DataTypeStore> store = bridge_->StealStoreForTest();
bridge_.reset();
store_factory =
DataTypeStoreTestUtil::MoveStoreToFactory(std::move(store));
} else {
store_factory = DataTypeStoreTestUtil::FactoryForInMemoryStoreForTest();
}
bridge_ = std::make_unique<UserEventSyncBridge>(
std::move(store_factory), mock_processor_.CreateForwardingProcessor(),
&test_global_id_mapper_);
}
void WaitUntilModelReadyToSync(
const GaiaId& gaia_id = GaiaId("test_account_id")) {
base::RunLoop loop;
base::RepeatingClosure quit_closure = loop.QuitClosure();
// Let the bridge initialize fully, which should run ModelReadyToSync().
ON_CALL(*processor(), ModelReadyToSync)
.WillByDefault(InvokeWithoutArgs([=]() { quit_closure.Run(); }));
loop.Run();
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(gaia_id));
}
static std::string GetStorageKey(const UserEventSpecifics& specifics) {
return UserEventSyncBridge::GetStorageKeyFromSpecificsForTest(specifics);
}
UserEventSyncBridge* bridge() { return bridge_.get(); }
MockDataTypeLocalChangeProcessor* processor() { return &mock_processor_; }
TestGlobalIdMapper* mapper() { return &test_global_id_mapper_; }
std::map<std::string, sync_pb::EntitySpecifics> GetAllDataForDebugging() {
std::unique_ptr<DataBatch> batch = bridge_->GetAllDataForDebugging();
EXPECT_NE(nullptr, batch);
std::map<std::string, sync_pb::EntitySpecifics> storage_key_to_specifics;
if (batch != nullptr) {
while (batch->HasNext()) {
auto [key, data] = batch->Next();
storage_key_to_specifics[key] = data->specifics;
}
}
return storage_key_to_specifics;
}
std::unique_ptr<sync_pb::EntitySpecifics> GetDataForCommit(
const std::string& storage_key) {
std::unique_ptr<DataBatch> batch = bridge_->GetDataForCommit({storage_key});
EXPECT_NE(nullptr, batch);
std::unique_ptr<sync_pb::EntitySpecifics> specifics;
if (batch != nullptr && batch->HasNext()) {
auto [key, data] = batch->Next();
specifics = std::make_unique<sync_pb::EntitySpecifics>(data->specifics);
EXPECT_FALSE(batch->HasNext());
}
return specifics;
}
private:
base::test::TaskEnvironment task_environment_;
testing::NiceMock<MockDataTypeLocalChangeProcessor> mock_processor_;
TestGlobalIdMapper test_global_id_mapper_;
std::unique_ptr<UserEventSyncBridge> bridge_;
};
TEST_F(UserEventSyncBridgeTest, MetadataIsInitialized) {
EXPECT_CALL(*processor(), ModelReadyToSync(NotNull()));
WaitUntilModelReadyToSync();
}
TEST_F(UserEventSyncBridgeTest, GetDataForCommit) {
WaitUntilModelReadyToSync();
const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u));
std::string storage_key;
EXPECT_CALL(*processor(), Put).WillOnce(WithArg<0>(SaveArg<0>(&storage_key)));
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics));
// Existing specifics should be returned.
EXPECT_THAT(GetDataForCommit(storage_key),
Pointee(MatchesUserEvent(specifics)));
// GetDataForCommit() should handle arbitrary storage key.
EXPECT_THAT(GetDataForCommit("bogus"), IsNull());
}
TEST_F(UserEventSyncBridgeTest, SingleRecord) {
WaitUntilModelReadyToSync();
const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u));
std::string storage_key;
EXPECT_CALL(*processor(), Put).WillOnce(WithArg<0>(SaveArg<0>(&storage_key)));
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics));
EXPECT_THAT(GetDataForCommit(storage_key),
Pointee(MatchesUserEvent(specifics)));
EXPECT_THAT(GetAllDataForDebugging(),
ElementsAre(Pair(storage_key, MatchesUserEvent(specifics))));
}
TEST_F(UserEventSyncBridgeTest, ApplyDisableSyncChanges) {
WaitUntilModelReadyToSync();
const UserEventSpecifics specifics(CreateSpecifics(1u, 2u, 3u));
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics));
ASSERT_THAT(GetAllDataForDebugging(), SizeIs(1));
bridge()->ApplyDisableSyncChanges(WriteBatch::CreateMetadataChangeList());
// The bridge may asynchronously query the store to choose what to delete.
base::RunLoop().RunUntilIdle();
EXPECT_THAT(GetAllDataForDebugging(), IsEmpty());
}
TEST_F(UserEventSyncBridgeTest, MultipleRecords) {
WaitUntilModelReadyToSync();
std::set<std::string> unique_storage_keys;
EXPECT_CALL(*processor(), Put)
.Times(4)
.WillRepeatedly(
[&unique_storage_keys](const std::string& storage_key,
std::unique_ptr<EntityData> entity_data,
MetadataChangeList* metadata_change_list) {
unique_storage_keys.insert(storage_key);
});
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 1u, 1u));
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 1u, 2u));
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 2u, 2u));
bridge()->RecordUserEvent(SpecificsUniquePtr(2u, 2u, 2u));
EXPECT_EQ(2u, unique_storage_keys.size());
EXPECT_THAT(GetAllDataForDebugging(), SizeIs(2));
}
TEST_F(UserEventSyncBridgeTest, ApplyIncrementalSyncChanges) {
WaitUntilModelReadyToSync();
std::string storage_key1;
std::string storage_key2;
EXPECT_CALL(*processor(), Put)
.WillOnce(WithArg<0>(SaveArg<0>(&storage_key1)))
.WillOnce(WithArg<0>(SaveArg<0>(&storage_key2)));
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 1u, 1u));
bridge()->RecordUserEvent(SpecificsUniquePtr(2u, 2u, 2u));
EXPECT_THAT(GetAllDataForDebugging(), SizeIs(2));
syncer::EntityChangeList entity_change_list;
entity_change_list.push_back(
EntityChange::CreateDelete(storage_key1, syncer::EntityData()));
auto error_on_delete = bridge()->ApplyIncrementalSyncChanges(
bridge()->CreateMetadataChangeList(), std::move(entity_change_list));
EXPECT_FALSE(error_on_delete);
EXPECT_THAT(GetAllDataForDebugging(), SizeIs(1));
EXPECT_THAT(GetDataForCommit(storage_key1), IsNull());
EXPECT_THAT(GetDataForCommit(storage_key2), NotNull());
}
TEST_F(UserEventSyncBridgeTest, HandleGlobalIdChange) {
WaitUntilModelReadyToSync();
int64_t first_id = 11;
int64_t second_id = 12;
int64_t third_id = 13;
int64_t fourth_id = 14;
std::string storage_key;
EXPECT_CALL(*processor(), Put).WillOnce(WithArg<0>(SaveArg<0>(&storage_key)));
// This id update should be applied to the event as it is initially
// recorded.
mapper()->ChangeId(first_id, second_id);
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, first_id, 2u));
EXPECT_THAT(GetAllDataForDebugging(),
ElementsAre(Pair(storage_key, MatchesUserEvent(CreateSpecifics(
1u, second_id, 2u)))));
// This id update is done while the event is "in flight", and should result in
// it being updated and re-sent to sync.
EXPECT_CALL(*processor(), Put(storage_key, _, _));
mapper()->ChangeId(second_id, third_id);
EXPECT_THAT(GetAllDataForDebugging(),
ElementsAre(Pair(storage_key, MatchesUserEvent(CreateSpecifics(
1u, third_id, 2u)))));
syncer::EntityChangeList entity_change_list;
entity_change_list.push_back(
EntityChange::CreateDelete(storage_key, syncer::EntityData()));
auto error_on_delete = bridge()->ApplyIncrementalSyncChanges(
bridge()->CreateMetadataChangeList(), std::move(entity_change_list));
EXPECT_FALSE(error_on_delete);
EXPECT_THAT(GetAllDataForDebugging(), IsEmpty());
// This id update should be ignored, since we received commit confirmation
// above.
EXPECT_CALL(*processor(), Put).Times(0);
mapper()->ChangeId(third_id, fourth_id);
EXPECT_THAT(GetAllDataForDebugging(), IsEmpty());
}
TEST_F(UserEventSyncBridgeTest, MulipleEventsChanging) {
WaitUntilModelReadyToSync();
int64_t first_id = 11;
int64_t second_id = 12;
int64_t third_id = 13;
int64_t fourth_id = 14;
const UserEventSpecifics specifics1 = CreateSpecifics(101u, first_id, 2u);
const UserEventSpecifics specifics2 = CreateSpecifics(102u, second_id, 4u);
const UserEventSpecifics specifics3 = CreateSpecifics(103u, third_id, 6u);
const std::string key1 = GetStorageKey(specifics1);
const std::string key2 = GetStorageKey(specifics2);
const std::string key3 = GetStorageKey(specifics3);
ASSERT_NE(key1, key2);
ASSERT_NE(key1, key3);
ASSERT_NE(key2, key3);
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics1));
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics2));
bridge()->RecordUserEvent(std::make_unique<UserEventSpecifics>(specifics3));
ASSERT_THAT(GetAllDataForDebugging(),
UnorderedElementsAre(Pair(key1, MatchesUserEvent(specifics1)),
Pair(key2, MatchesUserEvent(specifics2)),
Pair(key3, MatchesUserEvent(specifics3))));
mapper()->ChangeId(second_id, fourth_id);
EXPECT_THAT(
GetAllDataForDebugging(),
UnorderedElementsAre(
Pair(key1, MatchesUserEvent(specifics1)),
Pair(key2, MatchesUserEvent(CreateSpecifics(102u, fourth_id, 4u))),
Pair(key3, MatchesUserEvent(specifics3))));
mapper()->ChangeId(first_id, fourth_id);
mapper()->ChangeId(third_id, fourth_id);
EXPECT_THAT(
GetAllDataForDebugging(),
UnorderedElementsAre(
Pair(key1, MatchesUserEvent(CreateSpecifics(101u, fourth_id, 2u))),
Pair(key2, MatchesUserEvent(CreateSpecifics(102u, fourth_id, 4u))),
Pair(key3, MatchesUserEvent(CreateSpecifics(103u, fourth_id, 6u)))));
}
TEST_F(UserEventSyncBridgeTest, RecordBeforeMetadataLoads) {
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(false));
ON_CALL(*processor(), TrackedGaiaId()).WillByDefault(Return(GaiaId()));
bridge()->RecordUserEvent(SpecificsUniquePtr(1u, 2u, 3u));
EXPECT_CALL(*processor(), ModelReadyToSync);
WaitUntilModelReadyToSync(GaiaId("gaia_id"));
EXPECT_THAT(GetAllDataForDebugging(), IsEmpty());
}
} // namespace
} // namespace syncer
|