summaryrefslogtreecommitdiffstats
path: root/sync/engine/model_type_entity_unittest.cc
blob: 5f3e66621883dc6f2381703c070db8bbaa48b5d1 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/engine/model_type_entity.h"

#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/protocol/sync.pb.h"
#include "sync/syncable/syncable_util.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace syncer_v2 {

// Some simple sanity tests for the ModelTypeEntity.
//
// A lot of the more complicated sync logic is implemented in the
// ModelTypeProcessorImpl that owns the ModelTypeEntity.  We can't unit test it
// here.
//
// Instead, we focus on simple tests to make sure that variables are getting
// properly intialized and flags properly set.  Anything more complicated would
// be a redundant and incomplete version of the ModelTypeProcessorImpl tests.
class ModelTypeEntityTest : public ::testing::Test {
 public:
  ModelTypeEntityTest()
      : kServerId("ServerID"),
        kClientTag("sample.pref.name"),
        kClientTagHash(
            syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES,
                                                   kClientTag)),
        kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)),
        kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) {
    sync_pb::PreferenceSpecifics* pref_specifics =
        specifics.mutable_preference();
    pref_specifics->set_name(kClientTag);
    pref_specifics->set_value("pref.value");
  }

  const std::string kServerId;
  const std::string kClientTag;
  const std::string kClientTagHash;
  const base::Time kCtime;
  const base::Time kMtime;
  sync_pb::EntitySpecifics specifics;
};

TEST_F(ModelTypeEntityTest, NewLocalItem) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::NewLocalItem("asdf", specifics, kCtime));

  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_TRUE(entity->IsUnsynced());
  EXPECT_FALSE(entity->UpdateIsReflection(1));
  EXPECT_TRUE(entity->UpdateIsInConflict(1));
}

TEST_F(ModelTypeEntityTest, FromServerUpdate) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::FromServerUpdate(kServerId,
                                        kClientTagHash,
                                        kClientTag,  // As non-unique name.
                                        10,
                                        specifics,
                                        false,
                                        kCtime,
                                        kMtime,
                                        std::string()));

  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_FALSE(entity->IsUnsynced());
  EXPECT_TRUE(entity->UpdateIsReflection(9));
  EXPECT_TRUE(entity->UpdateIsReflection(10));
  EXPECT_FALSE(entity->UpdateIsReflection(11));
  EXPECT_FALSE(entity->UpdateIsInConflict(11));
}

// Tombstones should behave just like regular updates.  Mostly.  The strangest
// thing about them is that they don't have specifics, so it can be hard to
// detect their type.  Fortunately, this class doesn't care about types in
// received updates.
TEST_F(ModelTypeEntityTest, TombstoneUpdate) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::FromServerUpdate(kServerId,
                                        kClientTagHash,
                                        kClientTag,  // As non-unique name.
                                        10,
                                        sync_pb::EntitySpecifics(),
                                        true,
                                        kCtime,
                                        kMtime,
                                        std::string()));

  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_FALSE(entity->IsUnsynced());
  EXPECT_TRUE(entity->UpdateIsReflection(9));
  EXPECT_TRUE(entity->UpdateIsReflection(10));
  EXPECT_FALSE(entity->UpdateIsReflection(11));
  EXPECT_FALSE(entity->UpdateIsInConflict(11));
}

// Apply a deletion update.
TEST_F(ModelTypeEntityTest, ApplyUpdate) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::FromServerUpdate(kServerId,
                                        kClientTagHash,
                                        kClientTag,  // As non-unique name.
                                        10,
                                        specifics,
                                        false,
                                        kCtime,
                                        kMtime,
                                        std::string()));

  // A deletion update one version later.
  entity->ApplyUpdateFromServer(11,
                                true,
                                sync_pb::EntitySpecifics(),
                                kMtime + base::TimeDelta::FromSeconds(10),
                                std::string());

  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_FALSE(entity->IsUnsynced());
  EXPECT_TRUE(entity->UpdateIsReflection(11));
  EXPECT_FALSE(entity->UpdateIsReflection(12));
}

TEST_F(ModelTypeEntityTest, LocalChange) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::FromServerUpdate(kServerId,
                                        kClientTagHash,
                                        kClientTag,  // As non-unique name.
                                        10,
                                        specifics,
                                        false,
                                        kCtime,
                                        kMtime,
                                        std::string()));

  sync_pb::EntitySpecifics specifics2;
  specifics2.CopyFrom(specifics);
  specifics2.mutable_preference()->set_value("new.pref.value");

  entity->MakeLocalChange(specifics2);
  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_TRUE(entity->IsUnsynced());

  EXPECT_TRUE(entity->UpdateIsReflection(10));
  EXPECT_FALSE(entity->UpdateIsInConflict(10));

  EXPECT_FALSE(entity->UpdateIsReflection(11));
  EXPECT_TRUE(entity->UpdateIsInConflict(11));
}

TEST_F(ModelTypeEntityTest, LocalDeletion) {
  scoped_ptr<ModelTypeEntity> entity(
      ModelTypeEntity::FromServerUpdate(kServerId,
                                        kClientTagHash,
                                        kClientTag,  // As non-unique name.
                                        10,
                                        specifics,
                                        false,
                                        kCtime,
                                        kMtime,
                                        std::string()));

  entity->Delete();

  EXPECT_TRUE(entity->IsWriteRequired());
  EXPECT_TRUE(entity->IsUnsynced());

  EXPECT_TRUE(entity->UpdateIsReflection(10));
  EXPECT_FALSE(entity->UpdateIsInConflict(10));

  EXPECT_FALSE(entity->UpdateIsReflection(11));
  EXPECT_TRUE(entity->UpdateIsInConflict(11));
}

}  // namespace syncer