• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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.
5 
6 #include "sync/engine/sync_thread_sync_entity.h"
7 
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"
14 
15 namespace syncer {
16 
17 // Some simple tests for the SyncThreadSyncEntity.
18 //
19 // The SyncThreadSyncEntity is an implementation detail of the
20 // NonBlockingTypeProcessorCore.  As such, it doesn't make much sense to test
21 // it exhaustively, since it already gets a lot of test coverage from the
22 // NonBlockingTypeProcessorCore unit tests.
23 //
24 // These tests are intended as a basic sanity check.  Anything more complicated
25 // would be redundant.
26 class SyncThreadSyncEntityTest : public ::testing::Test {
27  public:
SyncThreadSyncEntityTest()28   SyncThreadSyncEntityTest()
29       : kServerId("ServerID"),
30         kClientTag("some.sample.tag"),
31         kClientTagHash(syncable::GenerateSyncableHash(PREFERENCES, kClientTag)),
32         kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)),
33         kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) {
34     specifics.mutable_preference()->set_name(kClientTag);
35     specifics.mutable_preference()->set_value("pref.value");
36   }
37 
~SyncThreadSyncEntityTest()38   virtual ~SyncThreadSyncEntityTest() {}
39 
40   const std::string kServerId;
41   const std::string kClientTag;
42   const std::string kClientTagHash;
43   const base::Time kCtime;
44   const base::Time kMtime;
45   sync_pb::EntitySpecifics specifics;
46 };
47 
48 // Construct a new entity from a server update.  Then receive another update.
TEST_F(SyncThreadSyncEntityTest,FromServerUpdate)49 TEST_F(SyncThreadSyncEntityTest, FromServerUpdate) {
50   scoped_ptr<SyncThreadSyncEntity> entity(
51       SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10));
52   EXPECT_FALSE(entity->IsCommitPending());
53 
54   entity->ReceiveUpdate(20);
55   EXPECT_FALSE(entity->IsCommitPending());
56 }
57 
58 // Construct a new entity from a commit request.  Then serialize it.
TEST_F(SyncThreadSyncEntityTest,FromCommitRequest)59 TEST_F(SyncThreadSyncEntityTest, FromCommitRequest) {
60   scoped_ptr<SyncThreadSyncEntity> entity(
61       SyncThreadSyncEntity::FromCommitRequest(kServerId,
62                                               kClientTagHash,
63                                               22,
64                                               33,
65                                               kCtime,
66                                               kMtime,
67                                               kClientTag,
68                                               false,
69                                               specifics));
70 
71   ASSERT_TRUE(entity->IsCommitPending());
72   sync_pb::SyncEntity pb_entity;
73   int64 sequence_number = 0;
74   entity->PrepareCommitProto(&pb_entity, &sequence_number);
75   EXPECT_EQ(22, sequence_number);
76   EXPECT_EQ(kServerId, pb_entity.id_string());
77   EXPECT_EQ(kClientTagHash, pb_entity.client_defined_unique_tag());
78   EXPECT_EQ(33, pb_entity.version());
79   EXPECT_EQ(kCtime, ProtoTimeToTime(pb_entity.ctime()));
80   EXPECT_EQ(kMtime, ProtoTimeToTime(pb_entity.mtime()));
81   EXPECT_FALSE(pb_entity.deleted());
82   EXPECT_EQ(specifics.preference().name(),
83             pb_entity.specifics().preference().name());
84   EXPECT_EQ(specifics.preference().value(),
85             pb_entity.specifics().preference().value());
86 }
87 
88 // Start with a server initiated entity.  Commit over top of it.
TEST_F(SyncThreadSyncEntityTest,RequestCommit)89 TEST_F(SyncThreadSyncEntityTest, RequestCommit) {
90   scoped_ptr<SyncThreadSyncEntity> entity(
91       SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10));
92 
93   entity->RequestCommit(kServerId,
94                         kClientTagHash,
95                         1,
96                         10,
97                         kCtime,
98                         kMtime,
99                         kClientTag,
100                         false,
101                         specifics);
102 
103   EXPECT_TRUE(entity->IsCommitPending());
104 }
105 
106 // Start with a server initiated entity.  Fail to request a commit because of
107 // an out of date base version.
TEST_F(SyncThreadSyncEntityTest,RequestCommitFailure)108 TEST_F(SyncThreadSyncEntityTest, RequestCommitFailure) {
109   scoped_ptr<SyncThreadSyncEntity> entity(
110       SyncThreadSyncEntity::FromServerUpdate(kServerId, kClientTagHash, 10));
111   EXPECT_FALSE(entity->IsCommitPending());
112 
113   entity->RequestCommit(kServerId,
114                         kClientTagHash,
115                         23,
116                         5,  // Version 5 < 10
117                         kCtime,
118                         kMtime,
119                         kClientTag,
120                         false,
121                         specifics);
122   EXPECT_FALSE(entity->IsCommitPending());
123 }
124 
125 // Start with a pending commit.  Clobber it with an incoming update.
TEST_F(SyncThreadSyncEntityTest,UpdateClobbersCommit)126 TEST_F(SyncThreadSyncEntityTest, UpdateClobbersCommit) {
127   scoped_ptr<SyncThreadSyncEntity> entity(
128       SyncThreadSyncEntity::FromCommitRequest(kServerId,
129                                               kClientTagHash,
130                                               22,
131                                               33,
132                                               kCtime,
133                                               kMtime,
134                                               kClientTag,
135                                               false,
136                                               specifics));
137 
138   EXPECT_TRUE(entity->IsCommitPending());
139 
140   entity->ReceiveUpdate(400);  // Version 400 > 33.
141   EXPECT_FALSE(entity->IsCommitPending());
142 }
143 
144 // Start with a pending commit.  Send it a reflected update that
145 // will not override the in-progress commit.
TEST_F(SyncThreadSyncEntityTest,ReflectedUpdateDoesntClobberCommit)146 TEST_F(SyncThreadSyncEntityTest, ReflectedUpdateDoesntClobberCommit) {
147   scoped_ptr<SyncThreadSyncEntity> entity(
148       SyncThreadSyncEntity::FromCommitRequest(kServerId,
149                                               kClientTagHash,
150                                               22,
151                                               33,
152                                               kCtime,
153                                               kMtime,
154                                               kClientTag,
155                                               false,
156                                               specifics));
157 
158   EXPECT_TRUE(entity->IsCommitPending());
159 
160   entity->ReceiveUpdate(33);  // Version 33 == 33.
161   EXPECT_TRUE(entity->IsCommitPending());
162 }
163 
164 }  // namespace syncer
165