Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Side by Side Diff: components/reading_list/ios/reading_list_store_unittest.mm

Issue 2525663002: Refactor Reading List Model to use URL as key. (Closed)
Patch Set: feedback Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/reading_list/ios/reading_list_store.h" 5 #include "components/reading_list/ios/reading_list_store.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 put_multimap_.clear(); 107 put_multimap_.clear();
108 sync_add_called_ = 0; 108 sync_add_called_ = 0;
109 sync_remove_called_ = 0; 109 sync_remove_called_ = 0;
110 sync_merge_called_ = 0; 110 sync_merge_called_ = 0;
111 sync_added_.clear(); 111 sync_added_.clear();
112 sync_removed_.clear(); 112 sync_removed_.clear();
113 sync_merged_.clear(); 113 sync_merged_.clear();
114 } 114 }
115 115
116 // These three mathods handle callbacks from a ReadingListStore. 116 // These three mathods handle callbacks from a ReadingListStore.
117 void StoreLoaded(std::unique_ptr<ReadingListEntries> unread, 117 void StoreLoaded(std::unique_ptr<ReadingListEntries> entries) override {}
118 std::unique_ptr<ReadingListEntries> read) override {}
119 118
120 // Handle sync events. 119 // Handle sync events.
121 void SyncAddEntry(std::unique_ptr<ReadingListEntry> entry, 120 void SyncAddEntry(std::unique_ptr<ReadingListEntry> entry) override {
122 bool read) override {
123 sync_add_called_++; 121 sync_add_called_++;
124 sync_added_[entry->URL().spec()] = read; 122 sync_added_[entry->URL().spec()] = entry->IsRead();
125 } 123 }
126 124
127 void SyncRemoveEntry(const GURL& gurl) override { 125 void SyncRemoveEntry(const GURL& gurl) override {
128 sync_remove_called_++; 126 sync_remove_called_++;
129 sync_removed_.insert(gurl.spec()); 127 sync_removed_.insert(gurl.spec());
130 } 128 }
131 129
132 ReadingListEntry* SyncMergeEntry(std::unique_ptr<ReadingListEntry> entry, 130 ReadingListEntry* SyncMergeEntry(
133 bool read) override { 131 std::unique_ptr<ReadingListEntry> entry) override {
134 sync_merge_called_++; 132 sync_merge_called_++;
135 sync_merged_[entry->URL().spec()] = read; 133 sync_merged_[entry->URL().spec()] = entry->IsRead();
136 return model_->SyncMergeEntry(std::move(entry), read); 134 return model_->SyncMergeEntry(std::move(entry));
137 } 135 }
138 136
139 // In memory model type store needs a MessageLoop. 137 // In memory model type store needs a MessageLoop.
140 base::MessageLoop message_loop_; 138 base::MessageLoop message_loop_;
141 139
142 std::unique_ptr<syncer::ModelTypeStore> store_; 140 std::unique_ptr<syncer::ModelTypeStore> store_;
143 std::unique_ptr<ReadingListModelImpl> model_; 141 std::unique_ptr<ReadingListModelImpl> model_;
144 std::unique_ptr<ReadingListStore> reading_list_store_; 142 std::unique_ptr<ReadingListStore> reading_list_store_;
145 int put_called_; 143 int put_called_;
146 int delete_called_; 144 int delete_called_;
147 int sync_add_called_; 145 int sync_add_called_;
148 int sync_remove_called_; 146 int sync_remove_called_;
149 int sync_merge_called_; 147 int sync_merge_called_;
150 std::map<std::string, std::unique_ptr<syncer::EntityData>> put_multimap_; 148 std::map<std::string, std::unique_ptr<syncer::EntityData>> put_multimap_;
151 std::set<std::string> delete_set_; 149 std::set<std::string> delete_set_;
152 std::map<std::string, bool> sync_added_; 150 std::map<std::string, bool> sync_added_;
153 std::set<std::string> sync_removed_; 151 std::set<std::string> sync_removed_;
154 std::map<std::string, bool> sync_merged_; 152 std::map<std::string, bool> sync_merged_;
155 }; 153 };
156 154
157 TEST_F(ReadingListStoreTest, CheckEmpties) { 155 TEST_F(ReadingListStoreTest, CheckEmpties) {
158 EXPECT_EQ(0ul, model_->unread_size()); 156 EXPECT_EQ(0ul, model_->size());
159 EXPECT_EQ(0ul, model_->read_size());
160 } 157 }
161 158
162 TEST_F(ReadingListStoreTest, SaveOneRead) { 159 TEST_F(ReadingListStoreTest, SaveOneRead) {
163 ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); 160 ReadingListEntry entry(GURL("http://read.example.com/"), "read title");
164 reading_list_store_->SaveEntry(entry, true); 161 entry.SetRead(true);
162 reading_list_store_->SaveEntry(entry);
165 AssertCounts(1, 0, 0, 0, 0); 163 AssertCounts(1, 0, 0, 0, 0);
166 syncer::EntityData* data = put_multimap_["http://read.example.com/"].get(); 164 syncer::EntityData* data = put_multimap_["http://read.example.com/"].get();
167 const sync_pb::ReadingListSpecifics& specifics = 165 const sync_pb::ReadingListSpecifics& specifics =
168 data->specifics.reading_list(); 166 data->specifics.reading_list();
169 EXPECT_EQ(specifics.title(), "read title"); 167 EXPECT_EQ(specifics.title(), "read title");
170 EXPECT_EQ(specifics.url(), "http://read.example.com/"); 168 EXPECT_EQ(specifics.url(), "http://read.example.com/");
171 EXPECT_EQ(specifics.status(), sync_pb::ReadingListSpecifics::READ); 169 EXPECT_EQ(specifics.status(), sync_pb::ReadingListSpecifics::READ);
172 } 170 }
173 171
174 TEST_F(ReadingListStoreTest, SaveOneUnread) { 172 TEST_F(ReadingListStoreTest, SaveOneUnread) {
175 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); 173 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title");
176 reading_list_store_->SaveEntry(entry, false); 174 reading_list_store_->SaveEntry(entry);
177 AssertCounts(1, 0, 0, 0, 0); 175 AssertCounts(1, 0, 0, 0, 0);
178 syncer::EntityData* data = put_multimap_["http://unread.example.com/"].get(); 176 syncer::EntityData* data = put_multimap_["http://unread.example.com/"].get();
179 const sync_pb::ReadingListSpecifics& specifics = 177 const sync_pb::ReadingListSpecifics& specifics =
180 data->specifics.reading_list(); 178 data->specifics.reading_list();
181 EXPECT_EQ(specifics.title(), "unread title"); 179 EXPECT_EQ(specifics.title(), "unread title");
182 EXPECT_EQ(specifics.url(), "http://unread.example.com/"); 180 EXPECT_EQ(specifics.url(), "http://unread.example.com/");
183 EXPECT_EQ(specifics.status(), sync_pb::ReadingListSpecifics::UNREAD); 181 EXPECT_EQ(specifics.status(), sync_pb::ReadingListSpecifics::UNREAD);
184 } 182 }
185 183
186 TEST_F(ReadingListStoreTest, SyncMergeOneEntry) { 184 TEST_F(ReadingListStoreTest, SyncMergeOneEntry) {
187 syncer::EntityDataMap remote_input; 185 syncer::EntityDataMap remote_input;
188 ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); 186 ReadingListEntry entry(GURL("http://read.example.com/"), "read title");
187 entry.SetRead(true);
189 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics = 188 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics =
190 entry.AsReadingListSpecifics(true); 189 entry.AsReadingListSpecifics();
191 190
192 syncer::EntityData data; 191 syncer::EntityData data;
193 data.client_tag_hash = "http://read.example.com/"; 192 data.client_tag_hash = "http://read.example.com/";
194 *data.specifics.mutable_reading_list() = *specifics; 193 *data.specifics.mutable_reading_list() = *specifics;
195 194
196 remote_input["http://read.example.com/"] = data.PassToPtr(); 195 remote_input["http://read.example.com/"] = data.PassToPtr();
197 196
198 std::unique_ptr<syncer::MetadataChangeList> metadata_changes( 197 std::unique_ptr<syncer::MetadataChangeList> metadata_changes(
199 reading_list_store_->CreateMetadataChangeList()); 198 reading_list_store_->CreateMetadataChangeList());
200 const syncer::SyncError error = reading_list_store_->MergeSyncData( 199 const syncer::SyncError error = reading_list_store_->MergeSyncData(
201 std::move(metadata_changes), remote_input); 200 std::move(metadata_changes), remote_input);
202 AssertCounts(0, 0, 1, 0, 0); 201 AssertCounts(0, 0, 1, 0, 0);
203 EXPECT_EQ(sync_added_.size(), 1u); 202 EXPECT_EQ(sync_added_.size(), 1u);
204 EXPECT_EQ(sync_added_.count("http://read.example.com/"), 1u); 203 EXPECT_EQ(sync_added_.count("http://read.example.com/"), 1u);
205 EXPECT_EQ(sync_added_["http://read.example.com/"], true); 204 EXPECT_EQ(sync_added_["http://read.example.com/"], true);
206 } 205 }
207 206
208 TEST_F(ReadingListStoreTest, ApplySyncChangesOneAdd) { 207 TEST_F(ReadingListStoreTest, ApplySyncChangesOneAdd) {
209 syncer::EntityDataMap remote_input; 208 syncer::EntityDataMap remote_input;
210 ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); 209 ReadingListEntry entry(GURL("http://read.example.com/"), "read title");
210 entry.SetRead(true);
211 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics = 211 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics =
212 entry.AsReadingListSpecifics(true); 212 entry.AsReadingListSpecifics();
213 syncer::EntityData data; 213 syncer::EntityData data;
214 data.client_tag_hash = "http://read.example.com/"; 214 data.client_tag_hash = "http://read.example.com/";
215 *data.specifics.mutable_reading_list() = *specifics; 215 *data.specifics.mutable_reading_list() = *specifics;
216 216
217 syncer::EntityChangeList add_changes; 217 syncer::EntityChangeList add_changes;
218 218
219 add_changes.push_back(syncer::EntityChange::CreateAdd( 219 add_changes.push_back(syncer::EntityChange::CreateAdd(
220 "http://read.example.com/", data.PassToPtr())); 220 "http://read.example.com/", data.PassToPtr()));
221 syncer::SyncError error = reading_list_store_->ApplySyncChanges( 221 syncer::SyncError error = reading_list_store_->ApplySyncChanges(
222 reading_list_store_->CreateMetadataChangeList(), add_changes); 222 reading_list_store_->CreateMetadataChangeList(), add_changes);
223 AssertCounts(0, 0, 1, 0, 0); 223 AssertCounts(0, 0, 1, 0, 0);
224 EXPECT_EQ(sync_added_.size(), 1u); 224 EXPECT_EQ(sync_added_.size(), 1u);
225 EXPECT_EQ(sync_added_.count("http://read.example.com/"), 1u); 225 EXPECT_EQ(sync_added_.count("http://read.example.com/"), 1u);
226 EXPECT_EQ(sync_added_["http://read.example.com/"], true); 226 EXPECT_EQ(sync_added_["http://read.example.com/"], true);
227 } 227 }
228 228
229 TEST_F(ReadingListStoreTest, ApplySyncChangesOneMerge) { 229 TEST_F(ReadingListStoreTest, ApplySyncChangesOneMerge) {
230 syncer::EntityDataMap remote_input; 230 syncer::EntityDataMap remote_input;
231 model_->AddEntry(GURL("http://unread.example.com/"), "unread title"); 231 model_->AddEntry(GURL("http://unread.example.com/"), "unread title");
232 base::test::ios::SpinRunLoopWithMinDelay( 232 base::test::ios::SpinRunLoopWithMinDelay(
233 base::TimeDelta::FromMilliseconds(10)); 233 base::TimeDelta::FromMilliseconds(10));
234 234
235 ReadingListEntry new_entry(GURL("http://unread.example.com/"), 235 ReadingListEntry new_entry(GURL("http://unread.example.com/"),
236 "unread title"); 236 "unread title");
237 new_entry.SetRead(true);
237 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics = 238 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics =
238 new_entry.AsReadingListSpecifics(true); 239 new_entry.AsReadingListSpecifics();
239 syncer::EntityData data; 240 syncer::EntityData data;
240 data.client_tag_hash = "http://unread.example.com/"; 241 data.client_tag_hash = "http://unread.example.com/";
241 *data.specifics.mutable_reading_list() = *specifics; 242 *data.specifics.mutable_reading_list() = *specifics;
242 243
243 syncer::EntityChangeList add_changes; 244 syncer::EntityChangeList add_changes;
244 add_changes.push_back(syncer::EntityChange::CreateAdd( 245 add_changes.push_back(syncer::EntityChange::CreateAdd(
245 "http://unread.example.com/", data.PassToPtr())); 246 "http://unread.example.com/", data.PassToPtr()));
246 syncer::SyncError error = reading_list_store_->ApplySyncChanges( 247 syncer::SyncError error = reading_list_store_->ApplySyncChanges(
247 reading_list_store_->CreateMetadataChangeList(), add_changes); 248 reading_list_store_->CreateMetadataChangeList(), add_changes);
248 AssertCounts(1, 0, 0, 0, 1); 249 AssertCounts(1, 0, 0, 0, 1);
249 EXPECT_EQ(sync_merged_.size(), 1u); 250 EXPECT_EQ(sync_merged_.size(), 1u);
250 EXPECT_EQ(sync_merged_.count("http://unread.example.com/"), 1u); 251 EXPECT_EQ(sync_merged_.count("http://unread.example.com/"), 1u);
251 EXPECT_EQ(sync_merged_["http://unread.example.com/"], true); 252 EXPECT_EQ(sync_merged_["http://unread.example.com/"], true);
252 } 253 }
253 254
254 TEST_F(ReadingListStoreTest, ApplySyncChangesOneIgnored) { 255 TEST_F(ReadingListStoreTest, ApplySyncChangesOneIgnored) {
255 ReadingListEntry old_entry(GURL("http://unread.example.com/"), 256 ReadingListEntry old_entry(GURL("http://unread.example.com/"),
256 "unread title"); 257 "unread title");
257 base::test::ios::SpinRunLoopWithMinDelay( 258 base::test::ios::SpinRunLoopWithMinDelay(
258 base::TimeDelta::FromMilliseconds(10)); 259 base::TimeDelta::FromMilliseconds(10));
259 syncer::EntityDataMap remote_input; 260 syncer::EntityDataMap remote_input;
260 model_->AddEntry(GURL("http://unread.example.com/"), "unread title"); 261 model_->AddEntry(GURL("http://unread.example.com/"), "unread title");
261 AssertCounts(0, 0, 0, 0, 0); 262 AssertCounts(0, 0, 0, 0, 0);
262 263
263 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics = 264 std::unique_ptr<sync_pb::ReadingListSpecifics> specifics =
264 old_entry.AsReadingListSpecifics(true); 265 old_entry.AsReadingListSpecifics();
jif-google 2016/11/28 16:28:21 no need for old_entry.SetRead(true); ?
Olivier 2016/11/28 17:58:25 Done.
265 syncer::EntityData data; 266 syncer::EntityData data;
266 data.client_tag_hash = "http://unread.example.com/"; 267 data.client_tag_hash = "http://unread.example.com/";
267 *data.specifics.mutable_reading_list() = *specifics; 268 *data.specifics.mutable_reading_list() = *specifics;
268 269
269 syncer::EntityChangeList add_changes; 270 syncer::EntityChangeList add_changes;
270 add_changes.push_back(syncer::EntityChange::CreateAdd( 271 add_changes.push_back(syncer::EntityChange::CreateAdd(
271 "http://unread.example.com/", data.PassToPtr())); 272 "http://unread.example.com/", data.PassToPtr()));
272 syncer::SyncError error = reading_list_store_->ApplySyncChanges( 273 syncer::SyncError error = reading_list_store_->ApplySyncChanges(
273 reading_list_store_->CreateMetadataChangeList(), add_changes); 274 reading_list_store_->CreateMetadataChangeList(), add_changes);
274 AssertCounts(1, 0, 0, 0, 0); 275 AssertCounts(1, 0, 0, 0, 0);
275 EXPECT_EQ(sync_merged_.size(), 0u); 276 EXPECT_EQ(sync_merged_.size(), 0u);
276 } 277 }
277 278
278 TEST_F(ReadingListStoreTest, ApplySyncChangesOneRemove) { 279 TEST_F(ReadingListStoreTest, ApplySyncChangesOneRemove) {
279 syncer::EntityChangeList delete_changes; 280 syncer::EntityChangeList delete_changes;
280 delete_changes.push_back( 281 delete_changes.push_back(
281 syncer::EntityChange::CreateDelete("http://read.example.com/")); 282 syncer::EntityChange::CreateDelete("http://read.example.com/"));
282 syncer::SyncError error = reading_list_store_->ApplySyncChanges( 283 syncer::SyncError error = reading_list_store_->ApplySyncChanges(
283 reading_list_store_->CreateMetadataChangeList(), delete_changes); 284 reading_list_store_->CreateMetadataChangeList(), delete_changes);
284 AssertCounts(0, 0, 0, 1, 0); 285 AssertCounts(0, 0, 0, 1, 0);
285 EXPECT_EQ(sync_removed_.size(), 1u); 286 EXPECT_EQ(sync_removed_.size(), 1u);
286 EXPECT_EQ(sync_removed_.count("http://read.example.com/"), 1u); 287 EXPECT_EQ(sync_removed_.count("http://read.example.com/"), 1u);
287 } 288 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698