Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/internal_api/public/processor_entity_tracker.h" | 5 #include "sync/internal_api/public/processor_entity_tracker.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
| 15 #include "sync/internal_api/public/non_blocking_sync_common.h" | 15 #include "sync/internal_api/public/non_blocking_sync_common.h" |
| 16 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
| 17 #include "sync/syncable/syncable_util.h" | 17 #include "sync/syncable/syncable_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace syncer_v2 { | 20 namespace syncer_v2 { |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 const std::string kTag1 = "tag1"; | |
| 25 const std::string kTag2 = "tag2"; | |
| 26 const std::string kTag3 = "tag3"; | |
| 27 const std::string kValue1 = "value1"; | |
| 28 const std::string kValue2 = "value2"; | |
| 29 const std::string kValue3 = "value3"; | |
| 30 | |
| 31 std::string GenerateTagHash(const std::string& tag) { | |
| 32 return syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES, tag); | |
| 33 } | |
| 34 | |
| 35 sync_pb::EntitySpecifics GenerateSpecifics(const std::string& tag, | |
| 36 const std::string& value) { | |
| 37 sync_pb::EntitySpecifics specifics; | |
| 38 specifics.mutable_preference()->set_name(tag); | |
| 39 specifics.mutable_preference()->set_value(value); | |
| 40 return specifics; | |
| 41 } | |
| 42 } | |
|
pavely
2016/04/26 20:59:39
nit: new line before second } and add " // namesp
maxbogue
2016/04/27 17:28:52
Done.
| |
| 43 | |
| 22 // Some simple sanity tests for the ProcessorEntityTracker. | 44 // Some simple sanity tests for the ProcessorEntityTracker. |
| 23 // | 45 // |
| 24 // A lot of the more complicated sync logic is implemented in the | 46 // A lot of the more complicated sync logic is implemented in the |
| 25 // SharedModelTypeProcessor that owns the ProcessorEntityTracker. We can't unit | 47 // SharedModelTypeProcessor that owns the ProcessorEntityTracker. We can't unit |
| 26 // test it here. | 48 // test it here. |
| 27 // | 49 // |
| 28 // Instead, we focus on simple tests to make sure that variables are getting | 50 // Instead, we focus on simple tests to make sure that variables are getting |
| 29 // properly intialized and flags properly set. Anything more complicated would | 51 // properly intialized and flags properly set. Anything more complicated would |
| 30 // be a redundant and incomplete version of the SharedModelTypeProcessor tests. | 52 // be a redundant and incomplete version of the SharedModelTypeProcessor tests. |
| 31 class ProcessorEntityTrackerTest : public ::testing::Test { | 53 class ProcessorEntityTrackerTest : public ::testing::Test { |
| 32 public: | 54 public: |
| 33 ProcessorEntityTrackerTest() | 55 ProcessorEntityTrackerTest() |
| 34 : kServerId("ServerID"), | 56 : kServerId("ServerID"), |
| 35 kClientTag("sample.pref.name"), | 57 kClientTag("sample.pref.name"), |
| 36 kClientTagHash(GetSyncableHash(kClientTag)), | 58 kClientTagHash(GenerateTagHash(kClientTag)), |
| 37 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)), | 59 kCtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(10)), |
| 38 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)) { | 60 kMtime(base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)), |
| 39 sync_pb::PreferenceSpecifics* pref_specifics = | 61 specifics(GenerateSpecifics(kClientTag, kValue1)) {} |
| 40 specifics.mutable_preference(); | |
| 41 pref_specifics->set_name(kClientTag); | |
| 42 pref_specifics->set_value("pref.value"); | |
| 43 } | |
| 44 | |
| 45 static std::string GetSyncableHash(const std::string& tag) { | |
| 46 return syncer::syncable::GenerateSyncableHash(syncer::PREFERENCES, tag); | |
| 47 } | |
| 48 | 62 |
| 49 std::unique_ptr<ProcessorEntityTracker> NewLocalItem(const std::string& tag) { | 63 std::unique_ptr<ProcessorEntityTracker> NewLocalItem(const std::string& tag) { |
| 50 return std::unique_ptr<ProcessorEntityTracker>( | 64 return std::unique_ptr<ProcessorEntityTracker>( |
| 51 ProcessorEntityTracker::CreateNew(tag, GetSyncableHash(tag), "", | 65 ProcessorEntityTracker::CreateNew(tag, GenerateTagHash(tag), "", |
| 52 kCtime)); | 66 kCtime)); |
| 53 } | 67 } |
| 54 | 68 |
| 55 std::unique_ptr<ProcessorEntityTracker> NewLocalItem( | 69 std::unique_ptr<ProcessorEntityTracker> NewLocalItem( |
| 56 const std::string& tag, | 70 const std::string& tag, |
| 57 const sync_pb::EntitySpecifics& specifics) { | 71 const sync_pb::EntitySpecifics& specifics) { |
| 58 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem(tag)); | 72 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem(tag)); |
| 59 MakeLocalChange(entity.get(), specifics); | 73 MakeLocalChange(entity.get(), specifics); |
| 60 return entity; | 74 return entity; |
| 61 } | 75 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 const std::string kClientTagHash; | 132 const std::string kClientTagHash; |
| 119 const base::Time kCtime; | 133 const base::Time kCtime; |
| 120 const base::Time kMtime; | 134 const base::Time kMtime; |
| 121 sync_pb::EntitySpecifics specifics; | 135 sync_pb::EntitySpecifics specifics; |
| 122 }; | 136 }; |
| 123 | 137 |
| 124 TEST_F(ProcessorEntityTrackerTest, NewItem) { | 138 TEST_F(ProcessorEntityTrackerTest, NewItem) { |
| 125 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem("asdf")); | 139 std::unique_ptr<ProcessorEntityTracker> entity(NewLocalItem("asdf")); |
| 126 | 140 |
| 127 EXPECT_EQ(entity->client_tag(), "asdf"); | 141 EXPECT_EQ(entity->client_tag(), "asdf"); |
| 128 EXPECT_EQ(entity->metadata().client_tag_hash(), GetSyncableHash("asdf")); | 142 EXPECT_EQ(entity->metadata().client_tag_hash(), GenerateTagHash("asdf")); |
| 129 | 143 |
| 130 EXPECT_FALSE(entity->HasCommitData()); | 144 EXPECT_FALSE(entity->HasCommitData()); |
| 131 EXPECT_FALSE(HasSpecificsHash(entity)); | 145 EXPECT_FALSE(HasSpecificsHash(entity)); |
| 132 | 146 |
| 133 EXPECT_FALSE(entity->IsUnsynced()); | 147 EXPECT_FALSE(entity->IsUnsynced()); |
| 134 EXPECT_FALSE(entity->UpdateIsReflection(1)); | 148 EXPECT_FALSE(entity->UpdateIsReflection(1)); |
| 135 } | 149 } |
| 136 | 150 |
| 137 TEST_F(ProcessorEntityTrackerTest, NewLocalItem) { | 151 TEST_F(ProcessorEntityTrackerTest, NewLocalItem) { |
| 138 std::unique_ptr<ProcessorEntityTracker> entity( | 152 std::unique_ptr<ProcessorEntityTracker> entity( |
| 139 NewLocalItem("asdf", specifics)); | 153 NewLocalItem("asdf", specifics)); |
| 140 | 154 |
| 141 EXPECT_EQ(1, entity->metadata().sequence_number()); | 155 EXPECT_EQ(1, entity->metadata().sequence_number()); |
| 142 EXPECT_EQ(0, entity->metadata().acked_sequence_number()); | 156 EXPECT_EQ(0, entity->metadata().acked_sequence_number()); |
| 143 EXPECT_EQ(kUncommittedVersion, entity->metadata().server_version()); | 157 EXPECT_EQ(kUncommittedVersion, entity->metadata().server_version()); |
| 144 EXPECT_TRUE(entity->HasCommitData()); | 158 EXPECT_TRUE(entity->HasCommitData()); |
| 145 EXPECT_TRUE(HasSpecificsHash(entity)); | 159 EXPECT_TRUE(HasSpecificsHash(entity)); |
| 146 EXPECT_TRUE(entity->IsUnsynced()); | 160 EXPECT_TRUE(entity->IsUnsynced()); |
| 147 EXPECT_FALSE(entity->UpdateIsReflection(1)); | 161 EXPECT_FALSE(entity->UpdateIsReflection(1)); |
| 148 | 162 |
| 149 entity->ReceiveCommitResponse("id", 1, 1); | 163 CommitResponseData data; |
| 164 data.id = "id"; | |
| 165 data.client_tag_hash = entity->metadata().client_tag_hash(); | |
| 166 data.sequence_number = 1; | |
| 167 data.response_version = 1; | |
| 168 data.specifics_hash = entity->metadata().specifics_hash(); | |
| 169 entity->ReceiveCommitResponse(data); | |
| 150 | 170 |
| 151 EXPECT_EQ(1, entity->metadata().sequence_number()); | 171 EXPECT_EQ(1, entity->metadata().sequence_number()); |
| 152 EXPECT_EQ(1, entity->metadata().acked_sequence_number()); | 172 EXPECT_EQ(1, entity->metadata().acked_sequence_number()); |
| 153 EXPECT_EQ(1, entity->metadata().server_version()); | 173 EXPECT_EQ(1, entity->metadata().server_version()); |
| 154 EXPECT_FALSE(entity->HasCommitData()); | 174 EXPECT_FALSE(entity->HasCommitData()); |
| 155 EXPECT_TRUE(HasSpecificsHash(entity)); | 175 EXPECT_TRUE(HasSpecificsHash(entity)); |
| 176 EXPECT_TRUE(entity->metadata().base_specifics_hash().empty()); | |
|
pavely
2016/04/26 20:59:39
Consider adding test for calling MakeLocalChange m
maxbogue
2016/04/27 17:28:53
As discussed offline, I will be adding these in my
| |
| 156 EXPECT_FALSE(entity->IsUnsynced()); | 177 EXPECT_FALSE(entity->IsUnsynced()); |
| 157 EXPECT_TRUE(entity->UpdateIsReflection(1)); | 178 EXPECT_TRUE(entity->UpdateIsReflection(1)); |
| 158 } | 179 } |
| 159 | 180 |
| 160 TEST_F(ProcessorEntityTrackerTest, FromServerUpdate) { | 181 TEST_F(ProcessorEntityTrackerTest, FromServerUpdate) { |
| 161 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem()); | 182 std::unique_ptr<ProcessorEntityTracker> entity(NewServerItem()); |
| 162 | 183 |
| 163 EXPECT_EQ(entity->client_tag(), kClientTag); | 184 EXPECT_EQ(entity->client_tag(), kClientTag); |
| 164 EXPECT_EQ(entity->metadata().client_tag_hash(), kClientTagHash); | 185 EXPECT_EQ(entity->metadata().client_tag_hash(), kClientTagHash); |
| 165 EXPECT_FALSE(HasSpecificsHash(entity)); | 186 EXPECT_FALSE(HasSpecificsHash(entity)); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 entity->Delete(); | 299 entity->Delete(); |
| 279 | 300 |
| 280 entity->InitializeCommitRequestData(&commit_request); | 301 entity->InitializeCommitRequestData(&commit_request); |
| 281 const EntityData& data3 = commit_request.entity.value(); | 302 const EntityData& data3 = commit_request.entity.value(); |
| 282 | 303 |
| 283 EXPECT_EQ(3, commit_request.sequence_number); | 304 EXPECT_EQ(3, commit_request.sequence_number); |
| 284 EXPECT_TRUE(data3.is_deleted()); | 305 EXPECT_TRUE(data3.is_deleted()); |
| 285 } | 306 } |
| 286 | 307 |
| 287 } // namespace syncer_v2 | 308 } // namespace syncer_v2 |
| OLD | NEW |