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