OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sync/engine/syncer_util.h" | |
6 | |
7 #include "base/rand_util.h" | |
8 #include "sync/internal_api/public/base/unique_position.h" | |
9 #include "sync/protocol/sync.pb.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace syncer { | |
13 | |
14 class GetUpdatePositionTest : public ::testing::Test { | |
15 public: | |
16 GetUpdatePositionTest() { | |
17 InitUpdate(); | |
18 | |
19 // Init test_position to some valid position value, but don't assign | |
20 // it to the update just yet. | |
21 std::string pos_suffix = UniquePosition::RandomSuffix(); | |
22 test_position = UniquePosition::InitialPosition(pos_suffix); | |
23 } | |
24 | |
25 void InitUpdate() { | |
26 update.set_id_string("I"); | |
27 update.set_parent_id_string("P"); | |
28 update.set_version(10); | |
29 update.set_mtime(100); | |
30 update.set_ctime(100); | |
31 update.set_deleted(false); | |
32 update.mutable_specifics()->mutable_bookmark()->set_title("Chrome"); | |
33 update.mutable_specifics()->mutable_bookmark()-> | |
34 set_url("https://www.chrome.com"); | |
35 } | |
36 | |
37 void InitSuffixIngredients() { | |
38 update.set_originator_cache_guid("CacheGUID"); | |
39 update.set_originator_client_item_id("OrigID"); | |
40 } | |
41 | |
42 void InitProtoPosition() { | |
43 test_position.ToProto(update.mutable_unique_position()); | |
44 } | |
45 | |
46 void InitInt64Position(int64 pos_value) { | |
47 update.set_position_in_parent(pos_value); | |
48 } | |
49 | |
50 sync_pb::SyncEntity update; | |
51 UniquePosition test_position; | |
52 }; | |
53 | |
54 // Generate a suffix from originator client GUID and client-assigned ID. These | |
55 // values should always be present in updates sent down to the client, and | |
56 // combine to create a globally unique value. | |
57 TEST_F(GetUpdatePositionTest, SuffixFromUpdate) { | |
58 InitSuffixIngredients(); | |
59 | |
60 // Expect suffix is valid and consistent. | |
61 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update); | |
62 std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update); | |
63 | |
64 EXPECT_EQ(suffix1, suffix2); | |
65 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1)); | |
66 } | |
67 | |
68 // Receive an update without the ingredients used to make a consistent suffix. | |
69 // | |
70 // The server should never send us an update like this. If it does, | |
71 // that's a bug and it needs to be fixed. Still, we'd like to not | |
72 // crash and have fairly reasonable results in this scenario. | |
73 TEST_F(GetUpdatePositionTest, SuffixFromRandom) { | |
74 // Intentonally do not call InitSuffixIngredients() | |
75 | |
76 // Expect suffix is valid but inconsistent. | |
77 std::string suffix1 = GetUniqueBookmarkTagFromUpdate(update); | |
78 std::string suffix2 = GetUniqueBookmarkTagFromUpdate(update); | |
79 | |
80 EXPECT_NE(suffix1, suffix2); | |
81 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix1)); | |
82 EXPECT_TRUE(UniquePosition::IsValidSuffix(suffix2)); | |
83 } | |
84 | |
85 TEST_F(GetUpdatePositionTest, FromInt64) { | |
86 InitSuffixIngredients(); | |
87 InitInt64Position(10); | |
88 | |
89 std::string suffix = GetUniqueBookmarkTagFromUpdate(update); | |
90 | |
91 // Expect the result is valid. | |
92 UniquePosition pos = GetUpdatePosition(update, suffix); | |
93 EXPECT_TRUE(pos.IsValid()); | |
94 | |
95 // Expect the position had some effect on ordering. | |
96 EXPECT_TRUE(pos.LessThan( | |
97 UniquePosition::FromInt64(11, UniquePosition::RandomSuffix()))); | |
98 } | |
99 | |
100 TEST_F(GetUpdatePositionTest, FromProto) { | |
101 InitSuffixIngredients(); | |
102 InitInt64Position(10); | |
103 | |
104 std::string suffix = GetUniqueBookmarkTagFromUpdate(update); | |
105 | |
106 // The proto position is not set, so we should get one based on the int64. | |
107 // It should not match the proto we defined in the test harness. | |
108 UniquePosition int64_pos = GetUpdatePosition(update, suffix); | |
109 EXPECT_FALSE(int64_pos.Equals(test_position)); | |
110 | |
111 // Move the test harness' position value into the update proto. | |
112 // Expect that it takes precedence over the int64-based position. | |
113 InitProtoPosition(); | |
114 UniquePosition pos = GetUpdatePosition(update, suffix); | |
115 EXPECT_TRUE(pos.Equals(test_position)); | |
116 } | |
117 | |
118 TEST_F(GetUpdatePositionTest, FromNothing) { | |
119 // Init none of the ingredients necessary to make a position. | |
120 // Verify we still generate a valid position locally. | |
121 | |
122 std::string suffix = GetUniqueBookmarkTagFromUpdate(update); | |
123 UniquePosition pos = GetUpdatePosition(update, suffix); | |
124 EXPECT_TRUE(pos.IsValid()); | |
125 } | |
126 | |
127 } // namespace syncer | |
OLD | NEW |