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"),
30 kClientTagHash(syncable::GenerateSyncableHash(PREFERENCES
, kClientTag
)),
31 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)),
32 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) {
33 specifics
.mutable_preference()->set_name(kClientTag
);
34 specifics
.mutable_preference()->set_value("pref.value");
37 virtual ~EntityTrackerTest() {}
39 const std::string kServerId
;
40 const std::string kClientTag
;
41 const std::string kClientTagHash
;
42 const base::Time kCtime
;
43 const base::Time kMtime
;
44 sync_pb::EntitySpecifics specifics
;
47 // Construct a new entity from a server update. Then receive another update.
48 TEST_F(EntityTrackerTest
, FromServerUpdate
) {
49 scoped_ptr
<EntityTracker
> entity(
50 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
51 EXPECT_FALSE(entity
->IsCommitPending());
53 entity
->ReceiveUpdate(20);
54 EXPECT_FALSE(entity
->IsCommitPending());
57 // Construct a new entity from a commit request. Then serialize it.
58 TEST_F(EntityTrackerTest
, FromCommitRequest
) {
59 scoped_ptr
<EntityTracker
> entity(
60 EntityTracker::FromCommitRequest(kServerId
,
70 ASSERT_TRUE(entity
->IsCommitPending());
71 sync_pb::SyncEntity pb_entity
;
72 int64 sequence_number
= 0;
73 entity
->PrepareCommitProto(&pb_entity
, &sequence_number
);
74 EXPECT_EQ(22, sequence_number
);
75 EXPECT_EQ(kServerId
, pb_entity
.id_string());
76 EXPECT_EQ(kClientTagHash
, pb_entity
.client_defined_unique_tag());
77 EXPECT_EQ(33, pb_entity
.version());
78 EXPECT_EQ(kCtime
, ProtoTimeToTime(pb_entity
.ctime()));
79 EXPECT_EQ(kMtime
, ProtoTimeToTime(pb_entity
.mtime()));
80 EXPECT_FALSE(pb_entity
.deleted());
81 EXPECT_EQ(specifics
.preference().name(),
82 pb_entity
.specifics().preference().name());
83 EXPECT_EQ(specifics
.preference().value(),
84 pb_entity
.specifics().preference().value());
87 // Start with a server initiated entity. Commit over top of it.
88 TEST_F(EntityTrackerTest
, RequestCommit
) {
89 scoped_ptr
<EntityTracker
> entity(
90 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
92 entity
->RequestCommit(kServerId
,
102 EXPECT_TRUE(entity
->IsCommitPending());
105 // Start with a server initiated entity. Fail to request a commit because of
106 // an out of date base version.
107 TEST_F(EntityTrackerTest
, RequestCommitFailure
) {
108 scoped_ptr
<EntityTracker
> entity(
109 EntityTracker::FromServerUpdate(kServerId
, kClientTagHash
, 10));
110 EXPECT_FALSE(entity
->IsCommitPending());
112 entity
->RequestCommit(kServerId
,
121 EXPECT_FALSE(entity
->IsCommitPending());
124 // Start with a pending commit. Clobber it with an incoming update.
125 TEST_F(EntityTrackerTest
, UpdateClobbersCommit
) {
126 scoped_ptr
<EntityTracker
> entity(
127 EntityTracker::FromCommitRequest(kServerId
,
137 EXPECT_TRUE(entity
->IsCommitPending());
139 entity
->ReceiveUpdate(400); // Version 400 > 33.
140 EXPECT_FALSE(entity
->IsCommitPending());
143 // Start with a pending commit. Send it a reflected update that
144 // will not override the in-progress commit.
145 TEST_F(EntityTrackerTest
, ReflectedUpdateDoesntClobberCommit
) {
146 scoped_ptr
<EntityTracker
> entity(
147 EntityTracker::FromCommitRequest(kServerId
,
157 EXPECT_TRUE(entity
->IsCommitPending());
159 entity
->ReceiveUpdate(33); // Version 33 == 33.
160 EXPECT_TRUE(entity
->IsCommitPending());
163 } // namespace syncer