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

Side by Side Diff: ios/chrome/browser/reading_list/reading_list_model_unittest.cc

Issue 2514333003: Componentize Reading List (Closed)
Patch Set: fix 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/memory/ptr_util.h"
7 #import "base/test/ios/wait_util.h"
8 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h"
9 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 const GURL callback_url("http://example.com");
15 const std::string callback_title("test title");
16
17 class TestReadingListStorageObserver {
18 public:
19 virtual void ReadingListDidSaveEntry() = 0;
20 virtual void ReadingListDidRemoveEntry() = 0;
21 };
22
23 class TestReadingListStorage : public ReadingListModelStorage {
24 public:
25 TestReadingListStorage(TestReadingListStorageObserver* observer)
26 : read_(new std::vector<ReadingListEntry>()),
27 unread_(new std::vector<ReadingListEntry>()),
28 observer_(observer) {}
29
30 void AddSampleEntries() {
31 ReadingListEntry read_a(GURL("http://read_a.com"), "read_a");
32 ReadingListEntry read_b(GURL("http://read_b.com"), "read_b");
33 ReadingListEntry read_c(GURL("http://read_c.com"), "read_c");
34 read_->push_back(std::move(read_c));
35 read_->push_back(std::move(read_a));
36 read_->push_back(std::move(read_b));
37 ReadingListEntry unread_a(GURL("http://unread_a.com"), "unread_a");
38 ReadingListEntry unread_b(GURL("http://unread_b.com"), "unread_b");
39 ReadingListEntry unread_c(GURL("http://unread_c.com"), "unread_c");
40 ReadingListEntry unread_d(GURL("http://unread_d.com"), "unread_d");
41 unread_->push_back(std::move(unread_a));
42 unread_->push_back(std::move(unread_d));
43 unread_->push_back(std::move(unread_c));
44 unread_->push_back(std::move(unread_b));
45 }
46
47 void SetReadingListModel(ReadingListModel* model,
48 ReadingListStoreDelegate* delegate_) override {
49 delegate_->StoreLoaded(std::move(unread_), std::move(read_));
50 }
51
52 syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() override {
53 return nullptr;
54 }
55
56 std::unique_ptr<ScopedBatchUpdate> EnsureBatchCreated() override {
57 return std::unique_ptr<ScopedBatchUpdate>();
58 }
59
60 // Saves or updates an entry. If the entry is not yet in the database, it is
61 // created.
62 void SaveEntry(const ReadingListEntry& entry, bool read) override {
63 observer_->ReadingListDidSaveEntry();
64 }
65
66 // Removed an entry from the storage.
67 void RemoveEntry(const ReadingListEntry& entry) override {
68 observer_->ReadingListDidRemoveEntry();
69 }
70
71 private:
72 std::unique_ptr<std::vector<ReadingListEntry>> read_;
73 std::unique_ptr<std::vector<ReadingListEntry>> unread_;
74 TestReadingListStorageObserver* observer_;
75 };
76
77 class ReadingListModelTest : public ReadingListModelObserver,
78 public TestReadingListStorageObserver,
79 public testing::Test {
80 public:
81 ReadingListModelTest()
82 : callback_called_(false), model_(new ReadingListModelImpl()) {
83 ClearCounts();
84 model_->AddObserver(this);
85 }
86 ~ReadingListModelTest() override {}
87
88 void SetStorage(std::unique_ptr<TestReadingListStorage> storage) {
89 model_ =
90 base::MakeUnique<ReadingListModelImpl>(std::move(storage), nullptr);
91 ClearCounts();
92 model_->AddObserver(this);
93 }
94
95 void ClearCounts() {
96 observer_loaded_ = observer_started_batch_update_ =
97 observer_completed_batch_update_ = observer_deleted_ =
98 observer_remove_unread_ = observer_remove_read_ = observer_move_ =
99 observer_add_unread_ = observer_add_read_ =
100 observer_update_unread_ = observer_update_read_ =
101 observer_did_apply_ = storage_saved_ =
102 storage_removed_ = 0;
103 }
104
105 void AssertObserverCount(int observer_loaded,
106 int observer_started_batch_update,
107 int observer_completed_batch_update,
108 int observer_deleted,
109 int observer_remove_unread,
110 int observer_remove_read,
111 int observer_move,
112 int observer_add_unread,
113 int observer_add_read,
114 int observer_update_unread,
115 int observer_update_read,
116 int observer_did_apply) {
117 ASSERT_EQ(observer_loaded, observer_loaded_);
118 ASSERT_EQ(observer_started_batch_update, observer_started_batch_update_);
119 ASSERT_EQ(observer_completed_batch_update,
120 observer_completed_batch_update_);
121 ASSERT_EQ(observer_deleted, observer_deleted_);
122 ASSERT_EQ(observer_remove_unread, observer_remove_unread_);
123 ASSERT_EQ(observer_remove_read, observer_remove_read_);
124 ASSERT_EQ(observer_move, observer_move_);
125 ASSERT_EQ(observer_add_unread, observer_add_unread_);
126 ASSERT_EQ(observer_add_read, observer_add_read_);
127 ASSERT_EQ(observer_update_unread, observer_update_unread_);
128 ASSERT_EQ(observer_update_read, observer_update_read_);
129 ASSERT_EQ(observer_did_apply, observer_did_apply_);
130 }
131
132 void AssertStorageCount(int storage_saved, int storage_removed) {
133 ASSERT_EQ(storage_saved, storage_saved_);
134 ASSERT_EQ(storage_removed, storage_removed_);
135 }
136
137 // ReadingListModelObserver
138 void ReadingListModelLoaded(const ReadingListModel* model) override {
139 observer_loaded_ += 1;
140 }
141 void ReadingListModelBeganBatchUpdates(
142 const ReadingListModel* model) override {
143 observer_started_batch_update_ += 1;
144 }
145 void ReadingListModelCompletedBatchUpdates(
146 const ReadingListModel* model) override {
147 observer_completed_batch_update_ += 1;
148 }
149 void ReadingListModelBeingDeleted(const ReadingListModel* model) override {
150 observer_deleted_ += 1;
151 }
152 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model,
153 size_t index) override {
154 observer_remove_unread_ += 1;
155 }
156 void ReadingListWillMoveEntry(const ReadingListModel* model,
157 size_t index,
158 bool read) override {
159 observer_move_ += 1;
160 }
161 void ReadingListWillRemoveReadEntry(const ReadingListModel* model,
162 size_t index) override {
163 observer_remove_read_ += 1;
164 }
165 void ReadingListWillAddUnreadEntry(const ReadingListModel* model,
166 const ReadingListEntry& entry) override {
167 observer_add_unread_ += 1;
168 }
169 void ReadingListWillAddReadEntry(const ReadingListModel* model,
170 const ReadingListEntry& entry) override {
171 observer_add_read_ += 1;
172 }
173 void ReadingListWillUpdateUnreadEntry(const ReadingListModel* model,
174 size_t index) override {
175 observer_update_unread_ += 1;
176 }
177 void ReadingListWillUpdateReadEntry(const ReadingListModel* model,
178 size_t index) override {
179 observer_update_read_ += 1;
180 }
181 void ReadingListDidApplyChanges(ReadingListModel* model) override {
182 observer_did_apply_ += 1;
183 }
184
185 void ReadingListDidSaveEntry() override { storage_saved_ += 1; }
186 void ReadingListDidRemoveEntry() override { storage_removed_ += 1; }
187
188 void Callback(const ReadingListEntry& entry) {
189 EXPECT_EQ(callback_url, entry.URL());
190 EXPECT_EQ(callback_title, entry.Title());
191 callback_called_ = true;
192 }
193
194 bool CallbackCalled() { return callback_called_; }
195
196 protected:
197 int observer_loaded_;
198 int observer_started_batch_update_;
199 int observer_completed_batch_update_;
200 int observer_deleted_;
201 int observer_remove_unread_;
202 int observer_remove_read_;
203 int observer_move_;
204 int observer_add_unread_;
205 int observer_add_read_;
206 int observer_update_unread_;
207 int observer_update_read_;
208 int observer_did_apply_;
209 int storage_saved_;
210 int storage_removed_;
211 bool callback_called_;
212
213 std::unique_ptr<ReadingListModelImpl> model_;
214 };
215
216 TEST_F(ReadingListModelTest, EmptyLoaded) {
217 EXPECT_TRUE(model_->loaded());
218 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
219 EXPECT_EQ(0ul, model_->unread_size());
220 EXPECT_EQ(0ul, model_->read_size());
221 model_->Shutdown();
222 EXPECT_FALSE(model_->loaded());
223 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
224 }
225
226 TEST_F(ReadingListModelTest, ModelLoaded) {
227 ClearCounts();
228 auto storage = base::MakeUnique<TestReadingListStorage>(this);
229 storage->AddSampleEntries();
230 SetStorage(std::move(storage));
231
232 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
233 EXPECT_EQ(model_->read_size(), 3u);
234 EXPECT_EQ(model_->GetReadEntryAtIndex(0).Title(), "read_c");
235 EXPECT_EQ(model_->GetReadEntryAtIndex(1).Title(), "read_b");
236 EXPECT_EQ(model_->GetReadEntryAtIndex(2).Title(), "read_a");
237
238 EXPECT_EQ(model_->unread_size(), 4u);
239 EXPECT_EQ(model_->GetUnreadEntryAtIndex(0).Title(), "unread_d");
240 EXPECT_EQ(model_->GetUnreadEntryAtIndex(1).Title(), "unread_c");
241 EXPECT_EQ(model_->GetUnreadEntryAtIndex(2).Title(), "unread_b");
242 EXPECT_EQ(model_->GetUnreadEntryAtIndex(3).Title(), "unread_a");
243 }
244
245 TEST_F(ReadingListModelTest, AddEntry) {
246 auto storage = base::MakeUnique<TestReadingListStorage>(this);
247 SetStorage(std::move(storage));
248 ClearCounts();
249
250 const ReadingListEntry& entry =
251 model_->AddEntry(GURL("http://example.com"), "\n \tsample Test ");
252 EXPECT_EQ(GURL("http://example.com"), entry.URL());
253 EXPECT_EQ("sample Test", entry.Title());
254
255 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1);
256 AssertStorageCount(1, 0);
257 EXPECT_EQ(1ul, model_->unread_size());
258 EXPECT_EQ(0ul, model_->read_size());
259 EXPECT_TRUE(model_->HasUnseenEntries());
260
261 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0);
262 EXPECT_EQ(GURL("http://example.com"), other_entry.URL());
263 EXPECT_EQ("sample Test", other_entry.Title());
264 }
265
266 TEST_F(ReadingListModelTest, SyncAddEntry) {
267 auto storage = base::MakeUnique<TestReadingListStorage>(this);
268 SetStorage(std::move(storage));
269 auto entry =
270 base::MakeUnique<ReadingListEntry>(GURL("http://example.com"), "sample");
271 ClearCounts();
272
273 model_->SyncAddEntry(std::move(entry), true);
274 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1);
275 AssertStorageCount(0, 0);
276 ASSERT_EQ(model_->unread_size(), 0u);
277 ASSERT_EQ(model_->read_size(), 1u);
278 ClearCounts();
279 }
280
281 TEST_F(ReadingListModelTest, SyncMergeEntry) {
282 auto storage = base::MakeUnique<TestReadingListStorage>(this);
283 SetStorage(std::move(storage));
284 model_->AddEntry(GURL("http://example.com"), "sample");
285 model_->SetEntryDistilledPath(GURL("http://example.com"),
286 base::FilePath("distilled/page.html"));
287 const ReadingListEntry* local_entry =
288 model_->GetEntryFromURL(GURL("http://example.com"), nullptr);
289 int64_t local_update_time = local_entry->UpdateTime();
290
291 base::test::ios::SpinRunLoopWithMinDelay(
292 base::TimeDelta::FromMilliseconds(10));
293 auto sync_entry =
294 base::MakeUnique<ReadingListEntry>(GURL("http://example.com"), "sample");
295 ASSERT_GT(sync_entry->UpdateTime(), local_update_time);
296 int64_t sync_update_time = sync_entry->UpdateTime();
297 EXPECT_FALSE(sync_entry->DistilledURL().is_valid());
298
299 EXPECT_EQ(model_->unread_size(), 1u);
300 EXPECT_EQ(model_->read_size(), 0u);
301
302 ReadingListEntry* merged_entry =
303 model_->SyncMergeEntry(std::move(sync_entry), true);
304 EXPECT_EQ(model_->unread_size(), 0u);
305 EXPECT_EQ(model_->read_size(), 1u);
306 EXPECT_EQ(merged_entry->DistilledPath(),
307 base::FilePath("distilled/page.html"));
308 EXPECT_EQ(merged_entry->UpdateTime(), sync_update_time);
309 }
310
311 TEST_F(ReadingListModelTest, RemoveEntryByUrl) {
312 auto storage = base::MakeUnique<TestReadingListStorage>(this);
313 SetStorage(std::move(storage));
314 model_->AddEntry(GURL("http://example.com"), "sample");
315 ClearCounts();
316 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
317 nullptr);
318 EXPECT_EQ(model_->unread_size(), 1u);
319 model_->RemoveEntryByURL(GURL("http://example.com"));
320 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1);
321 AssertStorageCount(0, 1);
322 EXPECT_EQ(model_->unread_size(), 0u);
323 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
324 nullptr);
325
326 model_->AddEntry(GURL("http://example.com"), "sample");
327 model_->MarkReadByURL(GURL("http://example.com"));
328 ClearCounts();
329 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
330 nullptr);
331 EXPECT_EQ(model_->read_size(), 1u);
332 model_->RemoveEntryByURL(GURL("http://example.com"));
333 AssertObserverCount(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1);
334 AssertStorageCount(0, 1);
335 EXPECT_EQ(model_->read_size(), 0u);
336 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
337 nullptr);
338 }
339
340 TEST_F(ReadingListModelTest, RemoveSyncEntryByUrl) {
341 auto storage = base::MakeUnique<TestReadingListStorage>(this);
342 SetStorage(std::move(storage));
343 model_->AddEntry(GURL("http://example.com"), "sample");
344 ClearCounts();
345 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
346 nullptr);
347 EXPECT_EQ(model_->unread_size(), 1u);
348 model_->SyncRemoveEntry(GURL("http://example.com"));
349 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1);
350 AssertStorageCount(0, 0);
351 EXPECT_EQ(model_->unread_size(), 0u);
352 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
353 nullptr);
354
355 model_->AddEntry(GURL("http://example.com"), "sample");
356 model_->MarkReadByURL(GURL("http://example.com"));
357 ClearCounts();
358 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
359 nullptr);
360 EXPECT_EQ(model_->read_size(), 1u);
361 model_->SyncRemoveEntry(GURL("http://example.com"));
362 AssertObserverCount(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1);
363 AssertStorageCount(0, 0);
364 EXPECT_EQ(model_->read_size(), 0u);
365 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr),
366 nullptr);
367 }
368
369 TEST_F(ReadingListModelTest, ReadEntry) {
370 model_->AddEntry(GURL("http://example.com"), "sample");
371
372 ClearCounts();
373 model_->MarkReadByURL(GURL("http://example.com"));
374 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
375 EXPECT_EQ(0ul, model_->unread_size());
376 EXPECT_EQ(1ul, model_->read_size());
377 EXPECT_FALSE(model_->HasUnseenEntries());
378
379 const ReadingListEntry& other_entry = model_->GetReadEntryAtIndex(0);
380 EXPECT_EQ(GURL("http://example.com"), other_entry.URL());
381 EXPECT_EQ("sample", other_entry.Title());
382 }
383
384 TEST_F(ReadingListModelTest, EntryFromURL) {
385 GURL url1("http://example.com");
386 GURL url2("http://example2.com");
387 std::string entry1_title = "foo bar qux";
388 model_->AddEntry(url1, entry1_title);
389
390 // Check call with nullptr |read| parameter.
391 const ReadingListEntry* entry1 = model_->GetEntryFromURL(url1, nullptr);
392 EXPECT_NE(nullptr, entry1);
393 EXPECT_EQ(entry1_title, entry1->Title());
394
395 bool read;
396 entry1 = model_->GetEntryFromURL(url1, &read);
397 EXPECT_NE(nullptr, entry1);
398 EXPECT_EQ(entry1_title, entry1->Title());
399 EXPECT_EQ(read, false);
400 model_->MarkReadByURL(url1);
401 entry1 = model_->GetEntryFromURL(url1, &read);
402 EXPECT_NE(nullptr, entry1);
403 EXPECT_EQ(entry1_title, entry1->Title());
404 EXPECT_EQ(read, true);
405
406 const ReadingListEntry* entry2 = model_->GetEntryFromURL(url2, &read);
407 EXPECT_EQ(nullptr, entry2);
408 }
409
410 TEST_F(ReadingListModelTest, UnreadEntry) {
411 // Setup.
412 model_->AddEntry(GURL("http://example.com"), "sample");
413 model_->MarkReadByURL(GURL("http://example.com"));
414 ClearCounts();
415 ASSERT_EQ(0ul, model_->unread_size());
416 ASSERT_EQ(1ul, model_->read_size());
417
418 // Action.
419 model_->MarkUnreadByURL(GURL("http://example.com"));
420
421 // Tests.
422 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
423 EXPECT_EQ(1ul, model_->unread_size());
424 EXPECT_EQ(0ul, model_->read_size());
425 EXPECT_TRUE(model_->HasUnseenEntries());
426
427 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0);
428 EXPECT_EQ(GURL("http://example.com"), other_entry.URL());
429 EXPECT_EQ("sample", other_entry.Title());
430 }
431
432 TEST_F(ReadingListModelTest, BatchUpdates) {
433 auto token = model_->BeginBatchUpdates();
434 AssertObserverCount(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
435 EXPECT_TRUE(model_->IsPerformingBatchUpdates());
436
437 delete token.release();
438 AssertObserverCount(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
439 EXPECT_FALSE(model_->IsPerformingBatchUpdates());
440 }
441
442 TEST_F(ReadingListModelTest, BatchUpdatesReentrant) {
443 // When two updates happen at the same time, the notification is only sent
444 // for beginning of first update and completion of last update.
445 EXPECT_FALSE(model_->IsPerformingBatchUpdates());
446
447 auto token = model_->BeginBatchUpdates();
448 AssertObserverCount(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
449 EXPECT_TRUE(model_->IsPerformingBatchUpdates());
450
451 auto second_token = model_->BeginBatchUpdates();
452 AssertObserverCount(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
453 EXPECT_TRUE(model_->IsPerformingBatchUpdates());
454
455 delete token.release();
456 AssertObserverCount(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
457 EXPECT_TRUE(model_->IsPerformingBatchUpdates());
458
459 delete second_token.release();
460 AssertObserverCount(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
461 EXPECT_FALSE(model_->IsPerformingBatchUpdates());
462
463 // Consequent updates send notifications.
464 auto third_token = model_->BeginBatchUpdates();
465 AssertObserverCount(1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
466 EXPECT_TRUE(model_->IsPerformingBatchUpdates());
467
468 delete third_token.release();
469 AssertObserverCount(1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0);
470 EXPECT_FALSE(model_->IsPerformingBatchUpdates());
471 }
472
473 TEST_F(ReadingListModelTest, UpdateEntryTitle) {
474 const GURL gurl("http://example.com");
475 const ReadingListEntry& entry = model_->AddEntry(gurl, "sample");
476 ClearCounts();
477
478 model_->SetEntryTitle(gurl, "ping");
479 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1);
480 EXPECT_EQ("ping", entry.Title());
481 }
482
483 TEST_F(ReadingListModelTest, UpdateEntryState) {
484 const GURL gurl("http://example.com");
485 const ReadingListEntry& entry = model_->AddEntry(gurl, "sample");
486 ClearCounts();
487
488 model_->SetEntryDistilledState(gurl, ReadingListEntry::PROCESSING);
489 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1);
490 EXPECT_EQ(ReadingListEntry::PROCESSING, entry.DistilledState());
491 }
492
493 TEST_F(ReadingListModelTest, UpdateDistilledPath) {
494 const GURL gurl("http://example.com");
495 const ReadingListEntry& entry = model_->AddEntry(gurl, "sample");
496 ClearCounts();
497
498 model_->SetEntryDistilledPath(gurl, base::FilePath("distilled/page.html"));
499 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1);
500 EXPECT_EQ(ReadingListEntry::PROCESSED, entry.DistilledState());
501 EXPECT_EQ(base::FilePath("distilled/page.html"), entry.DistilledPath());
502 }
503
504 TEST_F(ReadingListModelTest, UpdateReadEntryTitle) {
505 const GURL gurl("http://example.com");
506 model_->AddEntry(gurl, "sample");
507 model_->MarkReadByURL(gurl);
508 const ReadingListEntry& entry = model_->GetReadEntryAtIndex(0);
509 ClearCounts();
510
511 model_->SetEntryTitle(gurl, "ping");
512 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1);
513 EXPECT_EQ("ping", entry.Title());
514 }
515
516 TEST_F(ReadingListModelTest, UpdateReadEntryState) {
517 const GURL gurl("http://example.com");
518 model_->AddEntry(gurl, "sample");
519 model_->MarkReadByURL(gurl);
520 const ReadingListEntry& entry = model_->GetReadEntryAtIndex(0);
521 ClearCounts();
522
523 model_->SetEntryDistilledState(gurl, ReadingListEntry::PROCESSING);
524 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1);
525 EXPECT_EQ(ReadingListEntry::PROCESSING, entry.DistilledState());
526 }
527
528 TEST_F(ReadingListModelTest, UpdateReadDistilledPath) {
529 const GURL gurl("http://example.com");
530 model_->AddEntry(gurl, "sample");
531 model_->MarkReadByURL(gurl);
532 const ReadingListEntry& entry = model_->GetReadEntryAtIndex(0);
533 ClearCounts();
534
535 model_->SetEntryDistilledPath(gurl, base::FilePath("distilled/page.html"));
536 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1);
537 EXPECT_EQ(ReadingListEntry::PROCESSED, entry.DistilledState());
538 EXPECT_EQ(GURL("chrome://offline/distilled/page.html"), entry.DistilledURL());
539 }
540
541 // Tests that the callback is called when the entry is unread.
542 TEST_F(ReadingListModelTest, CallbackEntryURLUnread) {
543 // Setup.
544 model_->AddEntry(callback_url, callback_title);
545
546 ASSERT_EQ(0UL, model_->read_size());
547 ASSERT_EQ(1UL, model_->unread_size());
548
549 // Action.
550 bool result = model_->CallbackEntryURL(
551 callback_url,
552 base::Bind(&ReadingListModelTest::Callback, base::Unretained(this)));
553
554 // Test.
555 EXPECT_TRUE(result);
556 EXPECT_TRUE(CallbackCalled());
557 }
558
559 // Tests that the callback is called when the entry is read.
560 TEST_F(ReadingListModelTest, CallbackEntryURLRead) {
561 // Setup.
562 model_->AddEntry(callback_url, callback_title);
563 model_->MarkReadByURL(callback_url);
564
565 ASSERT_EQ(1UL, model_->read_size());
566 ASSERT_EQ(0UL, model_->unread_size());
567
568 // Action.
569 bool result = model_->CallbackEntryURL(
570 callback_url,
571 base::Bind(&ReadingListModelTest::Callback, base::Unretained(this)));
572
573 // Test.
574 EXPECT_TRUE(result);
575 EXPECT_TRUE(CallbackCalled());
576 }
577
578 // Tests that the callback is not called when the entry is not present.
579 TEST_F(ReadingListModelTest, CallbackEntryURLNotPresent) {
580 // Setup.
581 const GURL gurl("http://foo.bar");
582 ASSERT_NE(gurl, callback_url);
583 model_->AddEntry(gurl, callback_title);
584
585 // Action.
586 bool result = model_->CallbackEntryURL(
587 callback_url,
588 base::Bind(&ReadingListModelTest::Callback, base::Unretained(this)));
589
590 // Test.
591 EXPECT_FALSE(result);
592 EXPECT_FALSE(CallbackCalled());
593 }
594
595 // Tests that ReadingListModel calls CallbackModelBeingDeleted when destroyed.
596 TEST_F(ReadingListModelTest, CallbackModelBeingDeleted) {
597 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
598 model_.reset();
599 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
600 }
601
602 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698