OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "sync/engine/sync_directory_commit_contribution.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "sync/sessions/status_controller.h" | |
9 #include "sync/syncable/entry.h" | |
10 #include "sync/syncable/mutable_entry.h" | |
11 #include "sync/syncable/syncable_read_transaction.h" | |
12 #include "sync/syncable/syncable_write_transaction.h" | |
13 #include "sync/test/engine/test_directory_setter_upper.h" | |
14 #include "sync/test/engine/test_id_factory.h" | |
15 #include "sync/test/engine/test_syncable_utils.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace syncer { | |
19 | |
20 class SyncDirectoryCommitContributionTest : public ::testing::Test { | |
21 public: | |
22 virtual void SetUp() OVERRIDE { | |
23 dir_maker_.SetUp(); | |
24 | |
25 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | |
26 CreateTypeRoot(&trans, dir(), PREFERENCES); | |
27 CreateTypeRoot(&trans, dir(), EXTENSIONS); | |
28 } | |
29 | |
30 virtual void TearDown() OVERRIDE { | |
31 dir_maker_.TearDown(); | |
32 } | |
33 | |
34 protected: | |
35 int64 CreateUnsyncedItem(syncable::WriteTransaction* trans, | |
36 ModelType type, | |
37 const std::string& tag) { | |
38 syncable::Entry parent_entry( | |
39 trans, | |
40 syncable::GET_BY_SERVER_TAG, | |
41 ModelTypeToRootTag(type)); | |
42 syncable::MutableEntry entry( | |
43 trans, | |
44 syncable::CREATE, | |
45 type, | |
46 parent_entry.GetId(), | |
47 tag); | |
48 entry.PutIsUnsynced(true); | |
49 return entry.GetMetahandle(); | |
50 } | |
51 | |
52 void CreateSuccessfulCommitResponse( | |
53 const sync_pb::SyncEntity& entity, | |
54 sync_pb::CommitResponse::EntryResponse* response) { | |
55 response->set_response_type(sync_pb::CommitResponse::SUCCESS); | |
56 response->set_non_unique_name(entity.name()); | |
57 response->set_version(entity.version() + 1); | |
58 response->set_parent_id_string(entity.parent_id_string()); | |
59 | |
60 if (entity.id_string()[0] == '-') // Look for the - in 'c-1234' style IDs. | |
61 response->set_id_string(id_factory_.NewServerId().GetServerId()); | |
62 else | |
63 response->set_id_string(entity.id_string()); | |
64 } | |
65 | |
66 syncable::Directory* dir() { | |
67 return dir_maker_.directory(); | |
68 } | |
69 | |
70 TestIdFactory id_factory_; | |
71 | |
72 private: | |
73 base::MessageLoop loop_; // Neeed to initialize the directory. | |
74 TestDirectorySetterUpper dir_maker_; | |
75 }; | |
76 | |
77 // Verify that the SyncDirectoryCommitContribution contains only entries of its | |
78 // specified type. | |
79 TEST_F(SyncDirectoryCommitContributionTest, GatherByTypes) { | |
80 int64 pref1; | |
81 { | |
82 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | |
83 pref1 = CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); | |
84 CreateUnsyncedItem(&trans, PREFERENCES, "pref2"); | |
85 CreateUnsyncedItem(&trans, EXTENSIONS, "extension1"); | |
86 } | |
87 | |
88 scoped_ptr<SyncDirectoryCommitContribution> cc( | |
89 SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 5)); | |
90 ASSERT_EQ(2U, cc->GetNumEntries()); | |
91 | |
92 const std::vector<int64>& metahandles = cc->metahandles_; | |
93 EXPECT_TRUE(std::find(metahandles.begin(), metahandles.end(), pref1) != | |
94 metahandles.end()); | |
95 EXPECT_TRUE(std::find(metahandles.begin(), metahandles.end(), pref1) != | |
96 metahandles.end()); | |
97 } | |
98 | |
99 // Verify that the SyncDirectoryCommitContributionTest builder function | |
100 // truncates if necessary. | |
101 TEST_F(SyncDirectoryCommitContributionTest, GatherAndTruncate) { | |
102 int64 pref1; | |
103 int64 pref2; | |
104 { | |
105 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | |
106 pref1 = CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); | |
107 pref2 = CreateUnsyncedItem(&trans, PREFERENCES, "pref2"); | |
108 CreateUnsyncedItem(&trans, EXTENSIONS, "extension1"); | |
109 } | |
110 | |
111 scoped_ptr<SyncDirectoryCommitContribution> cc( | |
112 SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 1)); | |
113 ASSERT_EQ(1U, cc->GetNumEntries()); | |
114 | |
115 int64 only_metahandle = cc->metahandles_[0]; | |
116 EXPECT_TRUE(only_metahandle == pref1 || only_metahandle == pref2); | |
117 } | |
118 | |
119 // Sanity check for building commits from SyncDirectoryCommitContributions. | |
120 // This test makes two CommitContribution objects of different types and uses | |
121 // them to initialize a commit message. Then it checks that the contents of the | |
122 // commit message match those of the directory they came from. | |
123 TEST_F(SyncDirectoryCommitContributionTest, PrepareCommit) { | |
124 { | |
125 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | |
126 CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); | |
127 CreateUnsyncedItem(&trans, PREFERENCES, "pref2"); | |
128 CreateUnsyncedItem(&trans, EXTENSIONS, "extension1"); | |
129 } | |
130 | |
131 scoped_ptr<SyncDirectoryCommitContribution> pref_cc( | |
132 SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 25)); | |
133 scoped_ptr<SyncDirectoryCommitContribution> ext_cc( | |
134 SyncDirectoryCommitContribution::Build(dir(), EXTENSIONS, 25)); | |
135 | |
136 sync_pb::ClientToServerMessage message; | |
137 pref_cc->AddToCommitMessage(&message); | |
138 ext_cc->AddToCommitMessage(&message); | |
139 | |
140 const sync_pb::CommitMessage& commit_message = message.commit(); | |
141 | |
142 std::set<syncable::Id> ids_for_commit; | |
143 ASSERT_EQ(3, commit_message.entries_size()); | |
144 for (int i = 0; i < commit_message.entries_size(); ++i) { | |
145 const sync_pb::SyncEntity& entity = commit_message.entries(i); | |
146 // The entities in this test have client-style IDs since they've never been | |
147 // committed before, so we must use CreateFromClientString to re-create them | |
148 // from the commit message. | |
149 ids_for_commit.insert(syncable::Id::CreateFromClientString( | |
150 entity.id_string())); | |
151 } | |
152 | |
153 ASSERT_EQ(3U, ids_for_commit.size()); | |
154 { | |
155 syncable::ReadTransaction trans(FROM_HERE, dir()); | |
156 for (std::set<syncable::Id>::iterator it = ids_for_commit.begin(); | |
157 it != ids_for_commit.end(); ++it) { | |
158 SCOPED_TRACE(it->value()); | |
159 syncable::Entry entry(&trans, syncable::GET_BY_ID, *it); | |
160 ASSERT_TRUE(entry.good()); | |
161 EXPECT_TRUE(entry.GetSyncing()); | |
162 } | |
163 } | |
164 } | |
165 | |
166 TEST_F(SyncDirectoryCommitContributionTest, ProcessCommitResponse) { | |
Nicolas Zea
2013/10/10 21:34:45
comment about what the test does?
rlarocque
2013/10/11 23:03:30
Done.
| |
167 int64 pref1_handle; | |
168 int64 pref2_handle; | |
169 int64 ext1_handle; | |
170 { | |
171 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir()); | |
172 pref1_handle = CreateUnsyncedItem(&trans, PREFERENCES, "pref1"); | |
173 pref2_handle = CreateUnsyncedItem(&trans, PREFERENCES, "pref2"); | |
174 ext1_handle = CreateUnsyncedItem(&trans, EXTENSIONS, "extension1"); | |
175 } | |
176 | |
177 scoped_ptr<SyncDirectoryCommitContribution> pref_cc( | |
178 SyncDirectoryCommitContribution::Build(dir(), PREFERENCES, 25)); | |
179 scoped_ptr<SyncDirectoryCommitContribution> ext_cc( | |
180 SyncDirectoryCommitContribution::Build(dir(), EXTENSIONS, 25)); | |
181 | |
182 sync_pb::ClientToServerMessage message; | |
183 pref_cc->AddToCommitMessage(&message); | |
184 ext_cc->AddToCommitMessage(&message); | |
185 | |
186 const sync_pb::CommitMessage& commit_message = message.commit(); | |
187 ASSERT_EQ(3, commit_message.entries_size()); | |
188 | |
189 sync_pb::ClientToServerResponse response; | |
190 for (int i = 0; i < commit_message.entries_size(); ++i) { | |
191 sync_pb::SyncEntity entity = commit_message.entries(i); | |
192 sync_pb::CommitResponse_EntryResponse* entry_response = | |
193 response.mutable_commit()->add_entryresponse(); | |
194 CreateSuccessfulCommitResponse(entity, entry_response); | |
195 } | |
196 | |
197 sessions::StatusController status; | |
198 | |
199 // Process these in reverse order. Just because we can. | |
200 ext_cc->ProcessCommitResponse(response, &status); | |
201 pref_cc->ProcessCommitResponse(response, &status); | |
202 | |
203 { | |
204 syncable::ReadTransaction trans(FROM_HERE, dir()); | |
205 syncable::Entry p1(&trans, syncable::GET_BY_HANDLE, pref1_handle); | |
206 EXPECT_TRUE(p1.GetId().ServerKnows()); | |
207 EXPECT_FALSE(p1.GetSyncing()); | |
208 EXPECT_LT(0, p1.GetServerVersion()); | |
209 | |
210 syncable::Entry p2(&trans, syncable::GET_BY_HANDLE, pref2_handle); | |
211 EXPECT_TRUE(p2.GetId().ServerKnows()); | |
212 EXPECT_FALSE(p2.GetSyncing()); | |
213 EXPECT_LT(0, p2.GetServerVersion()); | |
214 | |
215 syncable::Entry e1(&trans, syncable::GET_BY_HANDLE, ext1_handle); | |
216 EXPECT_TRUE(e1.GetId().ServerKnows()); | |
217 EXPECT_FALSE(e1.GetSyncing()); | |
218 EXPECT_LT(0, e1.GetServerVersion()); | |
219 } | |
220 } | |
221 | |
222 } // namespace syncer | |
OLD | NEW |