1 // Copyright 2013 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.
5 #include "sync/engine/sync_directory_update_handler.h"
7 #include "base/compiler_specific.h"
8 #include "base/message_loop/message_loop.h"
9 #include "sync/engine/syncer_proto_util.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/protocol/sync.pb.h"
12 #include "sync/sessions/status_controller.h"
13 #include "sync/syncable/directory.h"
14 #include "sync/syncable/entry.h"
15 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
16 #include "sync/syncable/syncable_proto_util.h"
17 #include "sync/syncable/syncable_read_transaction.h"
18 #include "sync/test/engine/test_directory_setter_upper.h"
19 #include "sync/test/engine/test_syncable_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 using syncable::UNITTEST;
26 class SyncDirectoryUpdateHandlerTest : public ::testing::Test {
28 virtual void SetUp() OVERRIDE {
32 virtual void TearDown() OVERRIDE {
33 dir_maker_.TearDown();
36 syncable::Directory* dir() {
37 return dir_maker_.directory();
40 scoped_ptr<sync_pb::SyncEntity> CreateUpdate(
41 const std::string& id,
42 const std::string& parent,
43 const ModelType& type);
45 // This exists mostly to give tests access to the protected member function.
46 // Warning: This takes the syncable directory lock.
47 void UpdateSyncEntities(
48 SyncDirectoryUpdateHandler* handler,
49 const SyncEntityList& applicable_updates,
50 sessions::StatusController* status);
52 // Another function to access private member functions.
53 void UpdateProgressMarkers(
54 SyncDirectoryUpdateHandler* handler,
55 const sync_pb::DataTypeProgressMarker& progress);
58 base::MessageLoop loop_; // Needed to initialize the directory.
59 TestDirectorySetterUpper dir_maker_;
62 scoped_ptr<sync_pb::SyncEntity> SyncDirectoryUpdateHandlerTest::CreateUpdate(
63 const std::string& id,
64 const std::string& parent,
65 const ModelType& type) {
66 scoped_ptr<sync_pb::SyncEntity> e(new sync_pb::SyncEntity());
68 e->set_parent_id_string(parent);
69 e->set_non_unique_name(id);
72 AddDefaultFieldValue(type, e->mutable_specifics());
76 void SyncDirectoryUpdateHandlerTest::UpdateSyncEntities(
77 SyncDirectoryUpdateHandler* handler,
78 const SyncEntityList& applicable_updates,
79 sessions::StatusController* status) {
80 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, UNITTEST, dir());
81 handler->UpdateSyncEntities(&trans, applicable_updates, status);
84 void SyncDirectoryUpdateHandlerTest::UpdateProgressMarkers(
85 SyncDirectoryUpdateHandler* handler,
86 const sync_pb::DataTypeProgressMarker& progress) {
87 handler->UpdateProgressMarker(progress);
90 static const char kCacheGuid[] = "IrcjZ2jyzHDV9Io4+zKcXQ==";
92 // Test that the bookmark tag is set on newly downloaded items.
93 TEST_F(SyncDirectoryUpdateHandlerTest, NewBookmarkTag) {
94 SyncDirectoryUpdateHandler handler(dir(), BOOKMARKS);
95 sync_pb::GetUpdatesResponse gu_response;
96 sessions::StatusController status;
98 // Add a bookmark item to the update message.
99 std::string root = syncable::GetNullId().GetServerId();
100 syncable::Id server_id = syncable::Id::CreateFromServerId("b1");
101 scoped_ptr<sync_pb::SyncEntity> e =
102 CreateUpdate(SyncableIdToProto(server_id), root, BOOKMARKS);
103 e->set_originator_cache_guid(
104 std::string(kCacheGuid, arraysize(kCacheGuid)-1));
105 syncable::Id client_id = syncable::Id::CreateFromClientString("-2");
106 e->set_originator_client_item_id(client_id.GetServerId());
107 e->set_position_in_parent(0);
109 // Add it to the applicable updates list.
110 SyncEntityList bookmark_updates;
111 bookmark_updates.push_back(e.get());
113 // Process the update.
114 UpdateSyncEntities(&handler, bookmark_updates, &status);
116 syncable::ReadTransaction trans(FROM_HERE, dir());
117 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
118 ASSERT_TRUE(entry.good());
119 EXPECT_TRUE(UniquePosition::IsValidSuffix(entry.GetUniqueBookmarkTag()));
120 EXPECT_TRUE(entry.GetServerUniquePosition().IsValid());
122 // If this assertion fails, that might indicate that the algorithm used to
123 // generate bookmark tags has been modified. This could have implications for
124 // bookmark ordering. Please make sure you know what you're doing if you
125 // intend to make such a change.
126 EXPECT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", entry.GetUniqueBookmarkTag());
129 // Test the receipt of a type root node.
130 TEST_F(SyncDirectoryUpdateHandlerTest, ReceiveServerCreatedBookmarkFolders) {
131 SyncDirectoryUpdateHandler handler(dir(), BOOKMARKS);
132 sync_pb::GetUpdatesResponse gu_response;
133 sessions::StatusController status;
135 // Create an update that mimics the bookmark root.
136 syncable::Id server_id = syncable::Id::CreateFromServerId("xyz");
137 std::string root = syncable::GetNullId().GetServerId();
138 scoped_ptr<sync_pb::SyncEntity> e =
139 CreateUpdate(SyncableIdToProto(server_id), root, BOOKMARKS);
140 e->set_server_defined_unique_tag("google_chrome_bookmarks");
143 // Add it to the applicable updates list.
144 SyncEntityList bookmark_updates;
145 bookmark_updates.push_back(e.get());
147 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e));
150 UpdateSyncEntities(&handler, bookmark_updates, &status);
152 // Verify the results.
153 syncable::ReadTransaction trans(FROM_HERE, dir());
154 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
155 ASSERT_TRUE(entry.good());
157 EXPECT_FALSE(entry.ShouldMaintainPosition());
158 EXPECT_FALSE(entry.GetUniquePosition().IsValid());
159 EXPECT_FALSE(entry.GetServerUniquePosition().IsValid());
160 EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty());
163 // Test the receipt of a non-bookmark item.
164 TEST_F(SyncDirectoryUpdateHandlerTest, ReceiveNonBookmarkItem) {
165 SyncDirectoryUpdateHandler handler(dir(), AUTOFILL);
166 sync_pb::GetUpdatesResponse gu_response;
167 sessions::StatusController status;
169 std::string root = syncable::GetNullId().GetServerId();
170 syncable::Id server_id = syncable::Id::CreateFromServerId("xyz");
171 scoped_ptr<sync_pb::SyncEntity> e =
172 CreateUpdate(SyncableIdToProto(server_id), root, AUTOFILL);
173 e->set_server_defined_unique_tag("9PGRuKdX5sHyGMB17CvYTXuC43I=");
175 // Add it to the applicable updates list.
176 SyncEntityList autofill_updates;
177 autofill_updates.push_back(e.get());
179 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e));
182 UpdateSyncEntities(&handler, autofill_updates, &status);
184 syncable::ReadTransaction trans(FROM_HERE, dir());
185 syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
186 ASSERT_TRUE(entry.good());
188 EXPECT_FALSE(entry.ShouldMaintainPosition());
189 EXPECT_FALSE(entry.GetUniquePosition().IsValid());
190 EXPECT_FALSE(entry.GetServerUniquePosition().IsValid());
191 EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty());
194 // Tests the setting of progress markers.
195 TEST_F(SyncDirectoryUpdateHandlerTest, ProcessNewProgressMarkers) {
196 SyncDirectoryUpdateHandler handler(dir(), BOOKMARKS);
198 sync_pb::DataTypeProgressMarker progress;
199 progress.set_data_type_id(GetSpecificsFieldNumberFromModelType(BOOKMARKS));
200 progress.set_token("token");
202 UpdateProgressMarkers(&handler, progress);
204 sync_pb::DataTypeProgressMarker saved;
205 dir()->GetDownloadProgress(BOOKMARKS, &saved);
207 EXPECT_EQ(progress.token(), saved.token());
208 EXPECT_EQ(progress.data_type_id(), saved.data_type_id());
211 } // namespace syncer