OLD | NEW |
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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/ptr_util.h" |
6 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" | 7 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
| 8 #include "ios/chrome/browser/reading_list/reading_list_model_storage.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 const GURL callback_url("http://example.com"); | 13 const GURL callback_url("http://example.com"); |
12 const std::string callback_title("test title"); | 14 const std::string callback_title("test title"); |
13 | 15 |
| 16 class TestReadingListStorageObserver { |
| 17 public: |
| 18 virtual void ReadingListDidSaveEntry() = 0; |
| 19 virtual void ReadingListDidRemoveEntry() = 0; |
| 20 virtual void ReadingListDidSaveEntryFromSync() = 0; |
| 21 virtual void ReadingListDidRemoveEntryFromSync() = 0; |
| 22 }; |
| 23 |
| 24 class TestReadingListStorage : public ReadingListModelStorage { |
| 25 public: |
| 26 TestReadingListStorage(TestReadingListStorageObserver* observer) |
| 27 : read_(new std::vector<ReadingListEntry>()), |
| 28 unread_(new std::vector<ReadingListEntry>()), |
| 29 observer_(observer) {} |
| 30 |
| 31 void AddSampleEntries() { |
| 32 ReadingListEntry read_a(GURL("http://read_a.com"), "read_a"); |
| 33 ReadingListEntry read_b(GURL("http://read_b.com"), "read_b"); |
| 34 ReadingListEntry read_c(GURL("http://read_c.com"), "read_c"); |
| 35 read_->push_back(std::move(read_c)); |
| 36 read_->push_back(std::move(read_a)); |
| 37 read_->push_back(std::move(read_b)); |
| 38 ReadingListEntry unread_a(GURL("http://unread_a.com"), "unread_a"); |
| 39 ReadingListEntry unread_b(GURL("http://unread_b.com"), "unread_b"); |
| 40 ReadingListEntry unread_c(GURL("http://unread_c.com"), "unread_c"); |
| 41 ReadingListEntry unread_d(GURL("http://unread_d.com"), "unread_d"); |
| 42 unread_->push_back(std::move(unread_a)); |
| 43 unread_->push_back(std::move(unread_d)); |
| 44 unread_->push_back(std::move(unread_c)); |
| 45 unread_->push_back(std::move(unread_b)); |
| 46 } |
| 47 |
| 48 void SetReadingListModel(ReadingListModel* model, |
| 49 ReadingListStoreDelegate* delegate_) override { |
| 50 delegate_->StoreLoaded(std::move(unread_), std::move(read_)); |
| 51 } |
| 52 |
| 53 syncer::ModelTypeSyncBridge* GetModelTypeSyncBridge() override { |
| 54 return nullptr; |
| 55 } |
| 56 |
| 57 std::unique_ptr<ScopedBatchUpdate> EnsureBatchCreated() override { |
| 58 return std::unique_ptr<ScopedBatchUpdate>(); |
| 59 } |
| 60 |
| 61 // Saves or updates an entry. If the entry is not yet in the database, it is |
| 62 // created. |
| 63 void SaveEntry(const ReadingListEntry& entry, |
| 64 bool read, |
| 65 bool from_sync) override { |
| 66 if (from_sync) { |
| 67 observer_->ReadingListDidSaveEntryFromSync(); |
| 68 } else { |
| 69 observer_->ReadingListDidSaveEntry(); |
| 70 } |
| 71 } |
| 72 |
| 73 // Removed an entry from the storage. |
| 74 void RemoveEntry(const ReadingListEntry& entry, bool from_sync) override { |
| 75 if (from_sync) { |
| 76 observer_->ReadingListDidRemoveEntryFromSync(); |
| 77 } else { |
| 78 observer_->ReadingListDidRemoveEntry(); |
| 79 } |
| 80 } |
| 81 |
| 82 private: |
| 83 std::unique_ptr<std::vector<ReadingListEntry>> read_; |
| 84 std::unique_ptr<std::vector<ReadingListEntry>> unread_; |
| 85 TestReadingListStorageObserver* observer_; |
| 86 }; |
| 87 |
14 class ReadingListModelTest : public ReadingListModelObserver, | 88 class ReadingListModelTest : public ReadingListModelObserver, |
| 89 public TestReadingListStorageObserver, |
15 public testing::Test { | 90 public testing::Test { |
16 public: | 91 public: |
17 ReadingListModelTest() | 92 ReadingListModelTest() |
18 : callback_called_(false), model_(new ReadingListModelImpl()) { | 93 : callback_called_(false), model_(new ReadingListModelImpl()) { |
19 ClearCounts(); | 94 ClearCounts(); |
20 model_->AddObserver(this); | 95 model_->AddObserver(this); |
21 } | 96 } |
22 ~ReadingListModelTest() override {} | 97 ~ReadingListModelTest() override {} |
23 | 98 |
| 99 void SetStorage(std::unique_ptr<TestReadingListStorage> storage) { |
| 100 model_ = |
| 101 base::MakeUnique<ReadingListModelImpl>(std::move(storage), nullptr); |
| 102 ClearCounts(); |
| 103 model_->AddObserver(this); |
| 104 } |
| 105 |
24 void ClearCounts() { | 106 void ClearCounts() { |
25 observer_loaded_ = observer_started_batch_update_ = | 107 observer_loaded_ = observer_started_batch_update_ = |
26 observer_completed_batch_update_ = observer_deleted_ = | 108 observer_completed_batch_update_ = observer_deleted_ = |
27 observer_remove_unread_ = observer_remove_read_ = observer_move_ = | 109 observer_remove_unread_ = observer_remove_read_ = observer_move_ = |
28 observer_add_unread_ = observer_add_read_ = | 110 observer_add_unread_ = observer_add_read_ = |
29 observer_update_unread_ = observer_update_read_ = | 111 observer_update_unread_ = observer_update_read_ = |
30 observer_did_apply_ = 0; | 112 observer_did_apply_ = storage_saved_ = |
| 113 storage_removed_ = storage_saved_from_sync_ = |
| 114 storage_removed_from_sync_ = 0; |
31 } | 115 } |
32 | 116 |
33 void AssertObserverCount(int observer_loaded, | 117 void AssertObserverCount(int observer_loaded, |
34 int observer_started_batch_update, | 118 int observer_started_batch_update, |
35 int observer_completed_batch_update, | 119 int observer_completed_batch_update, |
36 int observer_deleted, | 120 int observer_deleted, |
37 int observer_remove_unread, | 121 int observer_remove_unread, |
38 int observer_remove_read, | 122 int observer_remove_read, |
39 int observer_move, | 123 int observer_move, |
40 int observer_add_unread, | 124 int observer_add_unread, |
41 int observer_add_read, | 125 int observer_add_read, |
42 int observer_update_unread, | 126 int observer_update_unread, |
43 int observer_update_read, | 127 int observer_update_read, |
44 int observer_did_apply) { | 128 int observer_did_apply) { |
45 ASSERT_EQ(observer_loaded, observer_loaded_); | 129 ASSERT_EQ(observer_loaded, observer_loaded_); |
46 ASSERT_EQ(observer_started_batch_update, observer_started_batch_update_); | 130 ASSERT_EQ(observer_started_batch_update, observer_started_batch_update_); |
47 ASSERT_EQ(observer_completed_batch_update, | 131 ASSERT_EQ(observer_completed_batch_update, |
48 observer_completed_batch_update_); | 132 observer_completed_batch_update_); |
49 ASSERT_EQ(observer_deleted, observer_deleted_); | 133 ASSERT_EQ(observer_deleted, observer_deleted_); |
50 ASSERT_EQ(observer_remove_unread, observer_remove_unread_); | 134 ASSERT_EQ(observer_remove_unread, observer_remove_unread_); |
51 ASSERT_EQ(observer_remove_read, observer_remove_read_); | 135 ASSERT_EQ(observer_remove_read, observer_remove_read_); |
52 ASSERT_EQ(observer_move, observer_move_); | 136 ASSERT_EQ(observer_move, observer_move_); |
53 ASSERT_EQ(observer_add_unread, observer_add_unread_); | 137 ASSERT_EQ(observer_add_unread, observer_add_unread_); |
54 ASSERT_EQ(observer_add_read, observer_add_read_); | 138 ASSERT_EQ(observer_add_read, observer_add_read_); |
55 ASSERT_EQ(observer_update_unread, observer_update_unread_); | 139 ASSERT_EQ(observer_update_unread, observer_update_unread_); |
56 ASSERT_EQ(observer_update_read, observer_update_read_); | 140 ASSERT_EQ(observer_update_read, observer_update_read_); |
57 ASSERT_EQ(observer_did_apply, observer_did_apply_); | 141 ASSERT_EQ(observer_did_apply, observer_did_apply_); |
58 } | 142 } |
59 | 143 |
| 144 void AssertStorageCount(int storage_saved, |
| 145 int storage_removed, |
| 146 int storage_saved_from_sync, |
| 147 int storage_removed_from_sync) { |
| 148 ASSERT_EQ(storage_saved, storage_saved_); |
| 149 ASSERT_EQ(storage_removed, storage_removed_); |
| 150 ASSERT_EQ(storage_saved_from_sync, storage_saved_from_sync_); |
| 151 ASSERT_EQ(storage_removed_from_sync, storage_removed_from_sync_); |
| 152 } |
| 153 |
60 // ReadingListModelObserver | 154 // ReadingListModelObserver |
61 void ReadingListModelLoaded(const ReadingListModel* model) override { | 155 void ReadingListModelLoaded(const ReadingListModel* model) override { |
62 observer_loaded_ += 1; | 156 observer_loaded_ += 1; |
63 } | 157 } |
64 void ReadingListModelBeganBatchUpdates( | 158 void ReadingListModelBeganBatchUpdates( |
65 const ReadingListModel* model) override { | 159 const ReadingListModel* model) override { |
66 observer_started_batch_update_ += 1; | 160 observer_started_batch_update_ += 1; |
67 } | 161 } |
68 void ReadingListModelCompletedBatchUpdates( | 162 void ReadingListModelCompletedBatchUpdates( |
69 const ReadingListModel* model) override { | 163 const ReadingListModel* model) override { |
70 observer_completed_batch_update_ += 1; | 164 observer_completed_batch_update_ += 1; |
71 } | 165 } |
72 void ReadingListModelBeingDeleted(const ReadingListModel* model) override { | 166 void ReadingListModelBeingDeleted(const ReadingListModel* model) override { |
73 observer_deleted_ += 1; | 167 observer_deleted_ += 1; |
74 } | 168 } |
75 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, | 169 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, |
76 size_t index) override { | 170 size_t index) override { |
77 observer_remove_unread_ += 1; | 171 observer_remove_unread_ += 1; |
78 } | 172 } |
79 void ReadingListWillMoveEntry(const ReadingListModel* model, | 173 void ReadingListWillMoveEntry(const ReadingListModel* model, |
80 size_t index) override { | 174 size_t index, |
| 175 bool read) override { |
81 observer_move_ += 1; | 176 observer_move_ += 1; |
82 } | 177 } |
83 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, | 178 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, |
84 size_t index) override { | 179 size_t index) override { |
85 observer_remove_read_ += 1; | 180 observer_remove_read_ += 1; |
86 } | 181 } |
87 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, | 182 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, |
88 const ReadingListEntry& entry) override { | 183 const ReadingListEntry& entry) override { |
89 observer_add_unread_ += 1; | 184 observer_add_unread_ += 1; |
90 } | 185 } |
91 void ReadingListWillAddReadEntry(const ReadingListModel* model, | 186 void ReadingListWillAddReadEntry(const ReadingListModel* model, |
92 const ReadingListEntry& entry) override { | 187 const ReadingListEntry& entry) override { |
93 observer_add_read_ += 1; | 188 observer_add_read_ += 1; |
94 } | 189 } |
95 void ReadingListWillUpdateUnreadEntry(const ReadingListModel* model, | 190 void ReadingListWillUpdateUnreadEntry(const ReadingListModel* model, |
96 size_t index) override { | 191 size_t index) override { |
97 observer_update_unread_ += 1; | 192 observer_update_unread_ += 1; |
98 } | 193 } |
99 void ReadingListWillUpdateReadEntry(const ReadingListModel* model, | 194 void ReadingListWillUpdateReadEntry(const ReadingListModel* model, |
100 size_t index) override { | 195 size_t index) override { |
101 observer_update_read_ += 1; | 196 observer_update_read_ += 1; |
102 } | 197 } |
103 void ReadingListDidApplyChanges(ReadingListModel* model) override { | 198 void ReadingListDidApplyChanges(ReadingListModel* model) override { |
104 observer_did_apply_ += 1; | 199 observer_did_apply_ += 1; |
105 } | 200 } |
| 201 |
| 202 void ReadingListDidSaveEntry() override { storage_saved_ += 1; } |
| 203 void ReadingListDidRemoveEntry() override { storage_removed_ += 1; } |
| 204 void ReadingListDidSaveEntryFromSync() override { |
| 205 storage_saved_from_sync_ += 1; |
| 206 } |
| 207 void ReadingListDidRemoveEntryFromSync() override { |
| 208 storage_removed_from_sync_ += 1; |
| 209 } |
| 210 |
106 void Callback(const ReadingListEntry& entry) { | 211 void Callback(const ReadingListEntry& entry) { |
107 EXPECT_EQ(callback_url, entry.URL()); | 212 EXPECT_EQ(callback_url, entry.URL()); |
108 EXPECT_EQ(callback_title, entry.Title()); | 213 EXPECT_EQ(callback_title, entry.Title()); |
109 callback_called_ = true; | 214 callback_called_ = true; |
110 } | 215 } |
111 | 216 |
112 bool CallbackCalled() { return callback_called_; } | 217 bool CallbackCalled() { return callback_called_; } |
113 | 218 |
114 protected: | 219 protected: |
115 int observer_loaded_; | 220 int observer_loaded_; |
116 int observer_started_batch_update_; | 221 int observer_started_batch_update_; |
117 int observer_completed_batch_update_; | 222 int observer_completed_batch_update_; |
118 int observer_deleted_; | 223 int observer_deleted_; |
119 int observer_remove_unread_; | 224 int observer_remove_unread_; |
120 int observer_remove_read_; | 225 int observer_remove_read_; |
121 int observer_move_; | 226 int observer_move_; |
122 int observer_add_unread_; | 227 int observer_add_unread_; |
123 int observer_add_read_; | 228 int observer_add_read_; |
124 int observer_update_unread_; | 229 int observer_update_unread_; |
125 int observer_update_read_; | 230 int observer_update_read_; |
126 int observer_did_apply_; | 231 int observer_did_apply_; |
| 232 int storage_saved_; |
| 233 int storage_removed_; |
| 234 int storage_saved_from_sync_; |
| 235 int storage_removed_from_sync_; |
127 bool callback_called_; | 236 bool callback_called_; |
128 | 237 |
129 std::unique_ptr<ReadingListModelImpl> model_; | 238 std::unique_ptr<ReadingListModelImpl> model_; |
130 }; | 239 }; |
131 | 240 |
132 TEST_F(ReadingListModelTest, EmptyLoaded) { | 241 TEST_F(ReadingListModelTest, EmptyLoaded) { |
133 EXPECT_TRUE(model_->loaded()); | 242 EXPECT_TRUE(model_->loaded()); |
134 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | 243 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
135 EXPECT_EQ(0ul, model_->unread_size()); | 244 EXPECT_EQ(0ul, model_->unread_size()); |
136 EXPECT_EQ(0ul, model_->read_size()); | 245 EXPECT_EQ(0ul, model_->read_size()); |
137 model_->Shutdown(); | 246 model_->Shutdown(); |
138 EXPECT_FALSE(model_->loaded()); | 247 EXPECT_FALSE(model_->loaded()); |
139 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); | 248 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); |
140 } | 249 } |
141 | 250 |
| 251 TEST_F(ReadingListModelTest, ModelLoaded) { |
| 252 ClearCounts(); |
| 253 auto storage = base::MakeUnique<TestReadingListStorage>(this); |
| 254 storage->AddSampleEntries(); |
| 255 SetStorage(std::move(storage)); |
| 256 |
| 257 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 258 EXPECT_EQ(model_->read_size(), 3u); |
| 259 EXPECT_EQ(model_->GetReadEntryAtIndex(0).Title(), "read_c"); |
| 260 EXPECT_EQ(model_->GetReadEntryAtIndex(1).Title(), "read_b"); |
| 261 EXPECT_EQ(model_->GetReadEntryAtIndex(2).Title(), "read_a"); |
| 262 |
| 263 EXPECT_EQ(model_->unread_size(), 4u); |
| 264 EXPECT_EQ(model_->GetUnreadEntryAtIndex(0).Title(), "unread_d"); |
| 265 EXPECT_EQ(model_->GetUnreadEntryAtIndex(1).Title(), "unread_c"); |
| 266 EXPECT_EQ(model_->GetUnreadEntryAtIndex(2).Title(), "unread_b"); |
| 267 EXPECT_EQ(model_->GetUnreadEntryAtIndex(3).Title(), "unread_a"); |
| 268 } |
| 269 |
142 TEST_F(ReadingListModelTest, AddEntry) { | 270 TEST_F(ReadingListModelTest, AddEntry) { |
| 271 auto storage = base::MakeUnique<TestReadingListStorage>(this); |
| 272 SetStorage(std::move(storage)); |
143 ClearCounts(); | 273 ClearCounts(); |
| 274 |
144 const ReadingListEntry& entry = | 275 const ReadingListEntry& entry = |
145 model_->AddEntry(GURL("http://example.com"), "\n \tsample Test "); | 276 model_->AddEntry(GURL("http://example.com"), "\n \tsample Test "); |
146 EXPECT_EQ(GURL("http://example.com"), entry.URL()); | 277 EXPECT_EQ(GURL("http://example.com"), entry.URL()); |
147 EXPECT_EQ("sample Test", entry.Title()); | 278 EXPECT_EQ("sample Test", entry.Title()); |
148 | 279 |
149 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1); | 280 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1); |
| 281 AssertStorageCount(1, 0, 0, 0); |
150 EXPECT_EQ(1ul, model_->unread_size()); | 282 EXPECT_EQ(1ul, model_->unread_size()); |
151 EXPECT_EQ(0ul, model_->read_size()); | 283 EXPECT_EQ(0ul, model_->read_size()); |
152 EXPECT_TRUE(model_->HasUnseenEntries()); | 284 EXPECT_TRUE(model_->HasUnseenEntries()); |
153 | 285 |
154 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0); | 286 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0); |
155 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); | 287 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); |
156 EXPECT_EQ("sample Test", other_entry.Title()); | 288 EXPECT_EQ("sample Test", other_entry.Title()); |
157 } | 289 } |
158 | 290 |
| 291 TEST_F(ReadingListModelTest, SyncAddEntry) { |
| 292 auto storage = base::MakeUnique<TestReadingListStorage>(this); |
| 293 SetStorage(std::move(storage)); |
| 294 auto entry = |
| 295 base::MakeUnique<ReadingListEntry>(GURL("http://example.com"), "sample"); |
| 296 ClearCounts(); |
| 297 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 298 model_->MarkReadByURL(GURL("http://example.com")); |
| 299 |
| 300 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2); |
| 301 AssertStorageCount(2, 0, 0, 0); |
| 302 ASSERT_EQ(model_->unread_size(), 0u); |
| 303 ASSERT_EQ(model_->read_size(), 1u); |
| 304 ClearCounts(); |
| 305 |
| 306 // This entry is older, it should not replace the old one. |
| 307 model_->SyncAddEntry(std::move(entry), false); |
| 308 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 309 AssertStorageCount(0, 0, 0, 0); |
| 310 ASSERT_EQ(model_->unread_size(), 0u); |
| 311 ASSERT_EQ(model_->read_size(), 1u); |
| 312 ClearCounts(); |
| 313 |
| 314 entry = |
| 315 base::MakeUnique<ReadingListEntry>(GURL("http://example.com"), "sample"); |
| 316 // This entry is newer, it should replace the old one and mark it unread. |
| 317 model_->SyncAddEntry(std::move(entry), false); |
| 318 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); |
| 319 AssertStorageCount(0, 0, 1, 0); |
| 320 ASSERT_EQ(model_->unread_size(), 1u); |
| 321 ASSERT_EQ(model_->read_size(), 0u); |
| 322 ClearCounts(); |
| 323 |
| 324 entry = |
| 325 base::MakeUnique<ReadingListEntry>(GURL("http://example.com"), "sample"); |
| 326 // This entry is newer, it should replace the old one and mark it read. |
| 327 model_->SyncAddEntry(std::move(entry), true); |
| 328 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); |
| 329 AssertStorageCount(0, 0, 1, 0); |
| 330 ASSERT_EQ(model_->unread_size(), 0u); |
| 331 ASSERT_EQ(model_->read_size(), 1u); |
| 332 } |
| 333 |
| 334 TEST_F(ReadingListModelTest, RemoveEntryByUrl) { |
| 335 auto storage = base::MakeUnique<TestReadingListStorage>(this); |
| 336 SetStorage(std::move(storage)); |
| 337 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 338 ClearCounts(); |
| 339 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 340 nullptr); |
| 341 EXPECT_EQ(model_->unread_size(), 1u); |
| 342 model_->RemoveEntryByURL(GURL("http://example.com")); |
| 343 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1); |
| 344 AssertStorageCount(0, 1, 0, 0); |
| 345 EXPECT_EQ(model_->unread_size(), 0u); |
| 346 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 347 nullptr); |
| 348 |
| 349 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 350 model_->MarkReadByURL(GURL("http://example.com")); |
| 351 ClearCounts(); |
| 352 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 353 nullptr); |
| 354 EXPECT_EQ(model_->read_size(), 1u); |
| 355 model_->RemoveEntryByURL(GURL("http://example.com")); |
| 356 AssertObserverCount(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1); |
| 357 AssertStorageCount(0, 1, 0, 0); |
| 358 EXPECT_EQ(model_->read_size(), 0u); |
| 359 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 360 nullptr); |
| 361 } |
| 362 |
| 363 TEST_F(ReadingListModelTest, RemoveSyncEntryByUrl) { |
| 364 auto storage = base::MakeUnique<TestReadingListStorage>(this); |
| 365 SetStorage(std::move(storage)); |
| 366 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 367 ClearCounts(); |
| 368 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 369 nullptr); |
| 370 EXPECT_EQ(model_->unread_size(), 1u); |
| 371 model_->SyncRemoveEntry(GURL("http://example.com")); |
| 372 AssertObserverCount(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1); |
| 373 AssertStorageCount(0, 0, 0, 1); |
| 374 EXPECT_EQ(model_->unread_size(), 0u); |
| 375 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 376 nullptr); |
| 377 |
| 378 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 379 model_->MarkReadByURL(GURL("http://example.com")); |
| 380 ClearCounts(); |
| 381 EXPECT_NE(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 382 nullptr); |
| 383 EXPECT_EQ(model_->read_size(), 1u); |
| 384 model_->SyncRemoveEntry(GURL("http://example.com")); |
| 385 AssertObserverCount(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1); |
| 386 AssertStorageCount(0, 0, 0, 1); |
| 387 EXPECT_EQ(model_->read_size(), 0u); |
| 388 EXPECT_EQ(model_->GetEntryFromURL(GURL("http://example.com"), nullptr), |
| 389 nullptr); |
| 390 } |
| 391 |
159 TEST_F(ReadingListModelTest, ReadEntry) { | 392 TEST_F(ReadingListModelTest, ReadEntry) { |
160 model_->AddEntry(GURL("http://example.com"), "sample"); | 393 model_->AddEntry(GURL("http://example.com"), "sample"); |
161 | 394 |
162 ClearCounts(); | 395 ClearCounts(); |
163 model_->MarkReadByURL(GURL("http://example.com")); | 396 model_->MarkReadByURL(GURL("http://example.com")); |
164 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); | 397 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); |
165 EXPECT_EQ(0ul, model_->unread_size()); | 398 EXPECT_EQ(0ul, model_->unread_size()); |
166 EXPECT_EQ(1ul, model_->read_size()); | 399 EXPECT_EQ(1ul, model_->read_size()); |
167 EXPECT_FALSE(model_->HasUnseenEntries()); | 400 EXPECT_FALSE(model_->HasUnseenEntries()); |
168 | 401 |
169 const ReadingListEntry& other_entry = model_->GetReadEntryAtIndex(0); | 402 const ReadingListEntry& other_entry = model_->GetReadEntryAtIndex(0); |
170 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); | 403 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); |
171 EXPECT_EQ("sample", other_entry.Title()); | 404 EXPECT_EQ("sample", other_entry.Title()); |
172 } | 405 } |
173 | 406 |
174 TEST_F(ReadingListModelTest, EntryFromURL) { | 407 TEST_F(ReadingListModelTest, EntryFromURL) { |
175 GURL url1("http://example.com"); | 408 GURL url1("http://example.com"); |
176 GURL url2("http://example2.com"); | 409 GURL url2("http://example2.com"); |
177 std::string entry1_title = "foo bar qux"; | 410 std::string entry1_title = "foo bar qux"; |
178 model_->AddEntry(url1, entry1_title); | 411 model_->AddEntry(url1, entry1_title); |
179 | 412 |
180 const ReadingListEntry* entry1 = model_->GetEntryFromURL(url1); | 413 // Check call with nullptr |read| parameter. |
181 EXPECT_NE(nullptr, entry1); | 414 const ReadingListEntry* entry1 = model_->GetEntryFromURL(url1, nullptr); |
182 EXPECT_EQ(entry1_title, entry1->Title()); | |
183 model_->MarkReadByURL(url1); | |
184 entry1 = model_->GetEntryFromURL(url1); | |
185 EXPECT_NE(nullptr, entry1); | 415 EXPECT_NE(nullptr, entry1); |
186 EXPECT_EQ(entry1_title, entry1->Title()); | 416 EXPECT_EQ(entry1_title, entry1->Title()); |
187 | 417 |
188 const ReadingListEntry* entry2 = model_->GetEntryFromURL(url2); | 418 bool read; |
| 419 entry1 = model_->GetEntryFromURL(url1, &read); |
| 420 EXPECT_NE(nullptr, entry1); |
| 421 EXPECT_EQ(entry1_title, entry1->Title()); |
| 422 EXPECT_EQ(read, false); |
| 423 model_->MarkReadByURL(url1); |
| 424 entry1 = model_->GetEntryFromURL(url1, &read); |
| 425 EXPECT_NE(nullptr, entry1); |
| 426 EXPECT_EQ(entry1_title, entry1->Title()); |
| 427 EXPECT_EQ(read, true); |
| 428 |
| 429 const ReadingListEntry* entry2 = model_->GetEntryFromURL(url2, &read); |
189 EXPECT_EQ(nullptr, entry2); | 430 EXPECT_EQ(nullptr, entry2); |
190 } | 431 } |
191 | 432 |
192 TEST_F(ReadingListModelTest, UnreadEntry) { | 433 TEST_F(ReadingListModelTest, UnreadEntry) { |
193 // Setup. | 434 // Setup. |
194 model_->AddEntry(GURL("http://example.com"), "sample"); | 435 model_->AddEntry(GURL("http://example.com"), "sample"); |
195 model_->MarkReadByURL(GURL("http://example.com")); | 436 model_->MarkReadByURL(GURL("http://example.com")); |
196 ClearCounts(); | 437 ClearCounts(); |
197 ASSERT_EQ(0ul, model_->unread_size()); | 438 ASSERT_EQ(0ul, model_->unread_size()); |
198 ASSERT_EQ(1ul, model_->read_size()); | 439 ASSERT_EQ(1ul, model_->read_size()); |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 616 } |
376 | 617 |
377 // Tests that ReadingListModel calls CallbackModelBeingDeleted when destroyed. | 618 // Tests that ReadingListModel calls CallbackModelBeingDeleted when destroyed. |
378 TEST_F(ReadingListModelTest, CallbackModelBeingDeleted) { | 619 TEST_F(ReadingListModelTest, CallbackModelBeingDeleted) { |
379 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | 620 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
380 model_.reset(); | 621 model_.reset(); |
381 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); | 622 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); |
382 } | 623 } |
383 | 624 |
384 } // namespace | 625 } // namespace |
OLD | NEW |