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