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/syncable/syncable_util.h"
12 #include "sync/util/time.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 // Some simple tests for the EntityTracker.
19 // The EntityTracker is an implementation detail of the ModelTypeSyncWorker.
20 // As such, it doesn't make much sense to test it exhaustively, since it
21 // already gets a lot of test coverage from the ModelTypeSyncWorker unit tests.
23 // These tests are intended as a basic sanity check. Anything more complicated
24 // would be redundant.
25 class EntityTrackerTest
: public ::testing::Test
{
28 : kServerId("ServerID"),
29 kClientTag("some.sample.tag"),
31 syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES
,
33 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)),
34 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) {
35 specifics
.mutable_preference()->set_name(kClientTag
);
36 specifics
.mutable_preference()->set_value("pref.value");
39 ~EntityTrackerTest() override
{}
41 const std::string kServerId
;
42 const std::string kClientTag
;
43 const std::string kClientTagHash
;
44 const base::Time kCtime
;
45 const base::Time kMtime
;
46 sync_pb::EntitySpecifics specifics
;
49 // Construct a new entity from a server update. Then receive another update.
50 TEST_F(EntityTrackerTest
, FromServerUpdate
) {
51 scoped_ptr
<EntityTracker
> entity(
52 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
53 EXPECT_FALSE(entity
->IsCommitPending());
55 entity
->ReceiveUpdate(20);
56 EXPECT_FALSE(entity
->IsCommitPending());
59 // Construct a new entity from a commit request. Then serialize it.
60 TEST_F(EntityTrackerTest
, FromCommitRequest
) {
61 scoped_ptr
<EntityTracker
> entity(
62 EntityTracker::FromCommitRequest(kServerId
,
72 ASSERT_TRUE(entity
->IsCommitPending());
73 sync_pb::SyncEntity pb_entity
;
74 int64 sequence_number
= 0;
75 entity
->PrepareCommitProto(&pb_entity
, &sequence_number
);
76 EXPECT_EQ(22, sequence_number
);
77 EXPECT_EQ(kServerId
, pb_entity
.id_string());
78 EXPECT_EQ(kClientTagHash
, pb_entity
.client_defined_unique_tag());
79 EXPECT_EQ(33, pb_entity
.version());
80 EXPECT_EQ(kCtime
, syncer::ProtoTimeToTime(pb_entity
.ctime()));
81 EXPECT_EQ(kMtime
, syncer::ProtoTimeToTime(pb_entity
.mtime()));
82 EXPECT_FALSE(pb_entity
.deleted());
83 EXPECT_EQ(specifics
.preference().name(),
84 pb_entity
.specifics().preference().name());
85 EXPECT_EQ(specifics
.preference().value(),
86 pb_entity
.specifics().preference().value());
89 // Start with a server initiated entity. Commit over top of it.
90 TEST_F(EntityTrackerTest
, RequestCommit
) {
91 scoped_ptr
<EntityTracker
> entity(
92 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
94 entity
->RequestCommit(kServerId
,
104 EXPECT_TRUE(entity
->IsCommitPending());
107 // Start with a server initiated entity. Fail to request a commit because of
108 // an out of date base version.
109 TEST_F(EntityTrackerTest
, RequestCommitFailure
) {
110 scoped_ptr
<EntityTracker
> entity(
111 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
112 EXPECT_FALSE(entity
->IsCommitPending());
114 entity
->RequestCommit(kServerId
,
123 EXPECT_FALSE(entity
->IsCommitPending());
126 // Start with a pending commit. Clobber it with an incoming update.
127 TEST_F(EntityTrackerTest
, UpdateClobbersCommit
) {
128 scoped_ptr
<EntityTracker
> entity(
129 EntityTracker::FromCommitRequest(kServerId
,
139 EXPECT_TRUE(entity
->IsCommitPending());
141 entity
->ReceiveUpdate(400); // Version 400 > 33.
142 EXPECT_FALSE(entity
->IsCommitPending());
145 // Start with a pending commit. Send it a reflected update that
146 // will not override the in-progress commit.
147 TEST_F(EntityTrackerTest
, ReflectedUpdateDoesntClobberCommit
) {
148 scoped_ptr
<EntityTracker
> entity(
149 EntityTracker::FromCommitRequest(kServerId
,
159 EXPECT_TRUE(entity
->IsCommitPending());
161 entity
->ReceiveUpdate(33); // Version 33 == 33.
162 EXPECT_TRUE(entity
->IsCommitPending());
165 } // namespace syncer