OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "base/basictypes.h" | |
6 #include "sync/engine/process_updates_command.h" | |
7 #include "sync/engine/syncer_proto_util.h" | |
8 #include "sync/internal_api/public/base/model_type.h" | |
9 #include "sync/internal_api/public/test/test_entry_factory.h" | |
10 #include "sync/sessions/sync_session.h" | |
11 #include "sync/syncable/mutable_entry.h" | |
12 #include "sync/syncable/syncable_id.h" | |
13 #include "sync/syncable/syncable_proto_util.h" | |
14 #include "sync/syncable/syncable_read_transaction.h" | |
15 #include "sync/syncable/syncable_write_transaction.h" | |
16 #include "sync/test/engine/fake_model_worker.h" | |
17 #include "sync/test/engine/syncer_command_test.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace syncer { | |
21 | |
22 using sync_pb::SyncEntity; | |
23 using syncable::Id; | |
24 using syncable::MutableEntry; | |
25 using syncable::UNITTEST; | |
26 using syncable::WriteTransaction; | |
27 | |
28 namespace { | |
29 | |
30 class ProcessUpdatesCommandTest : public SyncerCommandTest { | |
31 protected: | |
32 ProcessUpdatesCommandTest() {} | |
33 virtual ~ProcessUpdatesCommandTest() {} | |
34 | |
35 virtual void SetUp() { | |
36 workers()->push_back( | |
37 make_scoped_refptr(new FakeModelWorker(GROUP_UI))); | |
38 workers()->push_back( | |
39 make_scoped_refptr(new FakeModelWorker(GROUP_DB))); | |
40 (*mutable_routing_info())[PREFERENCES] = GROUP_UI; | |
41 (*mutable_routing_info())[BOOKMARKS] = GROUP_UI; | |
42 (*mutable_routing_info())[AUTOFILL] = GROUP_DB; | |
43 SyncerCommandTest::SetUp(); | |
44 test_entry_factory_.reset(new TestEntryFactory(directory())); | |
45 } | |
46 | |
47 void CreateLocalItem(const std::string& item_id, | |
48 const std::string& parent_id, | |
49 const ModelType& type) { | |
50 WriteTransaction trans(FROM_HERE, UNITTEST, directory()); | |
51 MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, | |
52 Id::CreateFromServerId(item_id)); | |
53 ASSERT_TRUE(entry.good()); | |
54 | |
55 entry.PutBaseVersion(1); | |
56 entry.PutServerVersion(1); | |
57 entry.PutNonUniqueName(item_id); | |
58 entry.PutParentId(Id::CreateFromServerId(parent_id)); | |
59 sync_pb::EntitySpecifics default_specifics; | |
60 AddDefaultFieldValue(type, &default_specifics); | |
61 entry.PutServerSpecifics(default_specifics); | |
62 } | |
63 | |
64 SyncEntity* AddUpdate(sync_pb::GetUpdatesResponse* updates, | |
65 const std::string& id, const std::string& parent, | |
66 const ModelType& type) { | |
67 sync_pb::SyncEntity* e = updates->add_entries(); | |
68 e->set_id_string(id); | |
69 e->set_parent_id_string(parent); | |
70 e->set_non_unique_name(id); | |
71 e->set_name(id); | |
72 e->set_version(1000); | |
73 AddDefaultFieldValue(type, e->mutable_specifics()); | |
74 return e; | |
75 } | |
76 | |
77 ProcessUpdatesCommand command_; | |
78 scoped_ptr<TestEntryFactory> test_entry_factory_; | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(ProcessUpdatesCommandTest); | |
82 }; | |
83 | |
84 static const char kCacheGuid[] = "IrcjZ2jyzHDV9Io4+zKcXQ=="; | |
85 | |
86 // Test that the bookmark tag is set on newly downloaded items. | |
87 TEST_F(ProcessUpdatesCommandTest, NewBookmarkTag) { | |
88 std::string root = syncable::GetNullId().GetServerId(); | |
89 sync_pb::GetUpdatesResponse* updates = | |
90 session()->mutable_status_controller()-> | |
91 mutable_updates_response()->mutable_get_updates(); | |
92 Id server_id = Id::CreateFromServerId("b1"); | |
93 SyncEntity* e = | |
94 AddUpdate(updates, SyncableIdToProto(server_id), root, BOOKMARKS); | |
95 | |
96 e->set_originator_cache_guid( | |
97 std::string(kCacheGuid, arraysize(kCacheGuid)-1)); | |
98 Id client_id = Id::CreateFromClientString("-2"); | |
99 e->set_originator_client_item_id(client_id.GetServerId()); | |
100 e->set_position_in_parent(0); | |
101 | |
102 command_.Execute(session()); | |
103 | |
104 syncable::ReadTransaction trans(FROM_HERE, directory()); | |
105 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id); | |
106 ASSERT_TRUE(entry.good()); | |
107 EXPECT_TRUE( | |
108 UniquePosition::IsValidSuffix(entry.GetUniqueBookmarkTag())); | |
109 EXPECT_TRUE(entry.GetServerUniquePosition().IsValid()); | |
110 | |
111 // If this assertion fails, that might indicate that the algorithm used to | |
112 // generate bookmark tags has been modified. This could have implications for | |
113 // bookmark ordering. Please make sure you know what you're doing if you | |
114 // intend to make such a change. | |
115 EXPECT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", | |
116 entry.GetUniqueBookmarkTag()); | |
117 } | |
118 | |
119 TEST_F(ProcessUpdatesCommandTest, ReceiveServerCreatedBookmarkFolders) { | |
120 Id server_id = Id::CreateFromServerId("xyz"); | |
121 std::string root = syncable::GetNullId().GetServerId(); | |
122 sync_pb::GetUpdatesResponse* updates = | |
123 session()->mutable_status_controller()-> | |
124 mutable_updates_response()->mutable_get_updates(); | |
125 | |
126 // Create an update that mimics the bookmark root. | |
127 SyncEntity* e = | |
128 AddUpdate(updates, SyncableIdToProto(server_id), root, BOOKMARKS); | |
129 e->set_server_defined_unique_tag("google_chrome_bookmarks"); | |
130 e->set_folder(true); | |
131 | |
132 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e)); | |
133 | |
134 command_.Execute(session()); | |
135 | |
136 syncable::ReadTransaction trans(FROM_HERE, directory()); | |
137 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id); | |
138 ASSERT_TRUE(entry.good()); | |
139 | |
140 EXPECT_FALSE(entry.ShouldMaintainPosition()); | |
141 EXPECT_FALSE(entry.GetUniquePosition().IsValid()); | |
142 EXPECT_FALSE(entry.GetServerUniquePosition().IsValid()); | |
143 EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty()); | |
144 } | |
145 | |
146 TEST_F(ProcessUpdatesCommandTest, ReceiveNonBookmarkItem) { | |
147 Id server_id = Id::CreateFromServerId("xyz"); | |
148 std::string root = syncable::GetNullId().GetServerId(); | |
149 sync_pb::GetUpdatesResponse* updates = | |
150 session()->mutable_status_controller()-> | |
151 mutable_updates_response()->mutable_get_updates(); | |
152 | |
153 SyncEntity* e = | |
154 AddUpdate(updates, SyncableIdToProto(server_id), root, AUTOFILL); | |
155 e->set_server_defined_unique_tag("9PGRuKdX5sHyGMB17CvYTXuC43I="); | |
156 | |
157 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e)); | |
158 | |
159 command_.Execute(session()); | |
160 | |
161 syncable::ReadTransaction trans(FROM_HERE, directory()); | |
162 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id); | |
163 ASSERT_TRUE(entry.good()); | |
164 | |
165 EXPECT_FALSE(entry.ShouldMaintainPosition()); | |
166 EXPECT_FALSE(entry.GetUniquePosition().IsValid()); | |
167 EXPECT_FALSE(entry.GetServerUniquePosition().IsValid()); | |
168 EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty()); | |
169 } | |
170 | |
171 } // namespace | |
172 | |
173 } // namespace syncer | |
OLD | NEW |