2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "sync/engine/entity_tracker.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/time/time.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/internal_api/public/non_blocking_sync_common.h"
12 #include "sync/syncable/syncable_util.h"
13 #include "sync/util/time.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 // Some simple tests for the EntityTracker.
20 // The EntityTracker is an implementation detail of the ModelTypeWorker.
21 // As such, it doesn't make much sense to test it exhaustively, since it
22 // already gets a lot of test coverage from the ModelTypeWorker unit tests.
24 // These tests are intended as a basic sanity check. Anything more complicated
25 // would be redundant.
26 class EntityTrackerTest
: public ::testing::Test
{
29 : kServerId("ServerID"),
30 kClientTag("some.sample.tag"),
32 syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES
,
34 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)),
35 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) {
36 specifics
.mutable_preference()->set_name(kClientTag
);
37 specifics
.mutable_preference()->set_value("pref.value");
40 ~EntityTrackerTest() override
{}
42 CommitRequestData
MakeCommitRequestData(int64 sequence_number
,
44 CommitRequestData data
;
46 data
.client_tag_hash
= kClientTagHash
;
47 data
.sequence_number
= sequence_number
;
48 data
.base_version
= base_version
;
51 data
.non_unique_name
= kClientTag
;
53 data
.specifics
= specifics
;
57 UpdateResponseData
MakeUpdateResponseData(int64 response_version
) {
58 UpdateResponseData data
;
60 data
.client_tag_hash
= kClientTagHash
;
61 data
.response_version
= response_version
;
65 const std::string kServerId
;
66 const std::string kClientTag
;
67 const std::string kClientTagHash
;
68 const base::Time kCtime
;
69 const base::Time kMtime
;
70 sync_pb::EntitySpecifics specifics
;
73 // Construct a new entity from a server update. Then receive another update.
74 TEST_F(EntityTrackerTest
, FromUpdateResponse
) {
75 scoped_ptr
<EntityTracker
> entity(
76 EntityTracker::FromUpdateResponse(MakeUpdateResponseData(10)));
77 EXPECT_FALSE(entity
->HasPendingCommit());
79 entity
->ReceiveUpdate(20);
80 EXPECT_FALSE(entity
->HasPendingCommit());
83 // Construct a new entity from a commit request. Then serialize it.
84 TEST_F(EntityTrackerTest
, FromCommitRequest
) {
85 const int64 kSequenceNumber
= 22;
86 const int64 kBaseVersion
= 33;
87 CommitRequestData data
= MakeCommitRequestData(kSequenceNumber
, kBaseVersion
);
88 scoped_ptr
<EntityTracker
> entity(EntityTracker::FromCommitRequest(data
));
89 entity
->RequestCommit(data
);
91 ASSERT_TRUE(entity
->HasPendingCommit());
92 sync_pb::SyncEntity pb_entity
;
93 int64 sequence_number
= 0;
94 entity
->PrepareCommitProto(&pb_entity
, &sequence_number
);
95 EXPECT_EQ(kSequenceNumber
, sequence_number
);
96 EXPECT_EQ(kServerId
, pb_entity
.id_string());
97 EXPECT_EQ(kClientTagHash
, pb_entity
.client_defined_unique_tag());
98 EXPECT_EQ(kBaseVersion
, pb_entity
.version());
99 EXPECT_EQ(kCtime
, syncer::ProtoTimeToTime(pb_entity
.ctime()));
100 EXPECT_EQ(kMtime
, syncer::ProtoTimeToTime(pb_entity
.mtime()));
101 EXPECT_FALSE(pb_entity
.deleted());
102 EXPECT_EQ(specifics
.preference().name(),
103 pb_entity
.specifics().preference().name());
104 EXPECT_EQ(specifics
.preference().value(),
105 pb_entity
.specifics().preference().value());
108 // Start with a server initiated entity. Commit over top of it.
109 TEST_F(EntityTrackerTest
, RequestCommit
) {
110 scoped_ptr
<EntityTracker
> entity(
111 EntityTracker::FromUpdateResponse(MakeUpdateResponseData(10)));
113 entity
->RequestCommit(MakeCommitRequestData(1, 10));
115 EXPECT_TRUE(entity
->HasPendingCommit());
118 // Start with a server initiated entity. Fail to request a commit because of
119 // an out of date base version.
120 TEST_F(EntityTrackerTest
, RequestCommitFailure
) {
121 scoped_ptr
<EntityTracker
> entity(
122 EntityTracker::FromUpdateResponse(MakeUpdateResponseData(10)));
123 EXPECT_FALSE(entity
->HasPendingCommit());
125 entity
->RequestCommit(MakeCommitRequestData(23, 5 /* base_version 5 < 10 */));
126 EXPECT_FALSE(entity
->HasPendingCommit());
129 // Start with a pending commit. Clobber it with an incoming update.
130 TEST_F(EntityTrackerTest
, UpdateClobbersCommit
) {
131 CommitRequestData data
= MakeCommitRequestData(22, 33);
132 scoped_ptr
<EntityTracker
> entity(EntityTracker::FromCommitRequest(data
));
133 entity
->RequestCommit(data
);
135 EXPECT_TRUE(entity
->HasPendingCommit());
137 entity
->ReceiveUpdate(400); // Version 400 > 33.
138 EXPECT_FALSE(entity
->HasPendingCommit());
141 // Start with a pending commit. Send it a reflected update that
142 // will not override the in-progress commit.
143 TEST_F(EntityTrackerTest
, ReflectedUpdateDoesntClobberCommit
) {
144 CommitRequestData data
= MakeCommitRequestData(22, 33);
145 scoped_ptr
<EntityTracker
> entity(EntityTracker::FromCommitRequest(data
));
146 entity
->RequestCommit(data
);
148 EXPECT_TRUE(entity
->HasPendingCommit());
150 entity
->ReceiveUpdate(33); // Version 33 == 33.
151 EXPECT_TRUE(entity
->HasPendingCommit());
154 } // namespace syncer