| 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" | |
| 7 #import "base/test/ios/wait_util.h" | |
| 8 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" | 6 #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" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 8 |
| 12 namespace { | 9 namespace { |
| 13 | 10 |
| 14 const GURL callback_url("http://example.com"); | 11 const GURL callback_url("http://example.com"); |
| 15 const std::string callback_title("test title"); | 12 const std::string callback_title("test title"); |
| 16 | 13 |
| 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, | 14 class ReadingListModelTest : public ReadingListModelObserver, |
| 78 public TestReadingListStorageObserver, | |
| 79 public testing::Test { | 15 public testing::Test { |
| 80 public: | 16 public: |
| 81 ReadingListModelTest() | 17 ReadingListModelTest() |
| 82 : callback_called_(false), model_(new ReadingListModelImpl()) { | 18 : callback_called_(false), model_(new ReadingListModelImpl()) { |
| 83 ClearCounts(); | 19 ClearCounts(); |
| 84 model_->AddObserver(this); | 20 model_->AddObserver(this); |
| 85 } | 21 } |
| 86 ~ReadingListModelTest() override {} | 22 ~ReadingListModelTest() override {} |
| 87 | 23 |
| 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() { | 24 void ClearCounts() { |
| 96 observer_loaded_ = observer_started_batch_update_ = | 25 observer_loaded_ = observer_started_batch_update_ = |
| 97 observer_completed_batch_update_ = observer_deleted_ = | 26 observer_completed_batch_update_ = observer_deleted_ = |
| 98 observer_remove_unread_ = observer_remove_read_ = observer_move_ = | 27 observer_remove_unread_ = observer_remove_read_ = observer_move_ = |
| 99 observer_add_unread_ = observer_add_read_ = | 28 observer_add_unread_ = observer_add_read_ = |
| 100 observer_update_unread_ = observer_update_read_ = | 29 observer_update_unread_ = observer_update_read_ = |
| 101 observer_did_apply_ = storage_saved_ = | 30 observer_did_apply_ = 0; |
| 102 storage_removed_ = 0; | |
| 103 } | 31 } |
| 104 | 32 |
| 105 void AssertObserverCount(int observer_loaded, | 33 void AssertObserverCount(int observer_loaded, |
| 106 int observer_started_batch_update, | 34 int observer_started_batch_update, |
| 107 int observer_completed_batch_update, | 35 int observer_completed_batch_update, |
| 108 int observer_deleted, | 36 int observer_deleted, |
| 109 int observer_remove_unread, | 37 int observer_remove_unread, |
| 110 int observer_remove_read, | 38 int observer_remove_read, |
| 111 int observer_move, | 39 int observer_move, |
| 112 int observer_add_unread, | 40 int observer_add_unread, |
| 113 int observer_add_read, | 41 int observer_add_read, |
| 114 int observer_update_unread, | 42 int observer_update_unread, |
| 115 int observer_update_read, | 43 int observer_update_read, |
| 116 int observer_did_apply) { | 44 int observer_did_apply) { |
| 117 ASSERT_EQ(observer_loaded, observer_loaded_); | 45 ASSERT_EQ(observer_loaded, observer_loaded_); |
| 118 ASSERT_EQ(observer_started_batch_update, observer_started_batch_update_); | 46 ASSERT_EQ(observer_started_batch_update, observer_started_batch_update_); |
| 119 ASSERT_EQ(observer_completed_batch_update, | 47 ASSERT_EQ(observer_completed_batch_update, |
| 120 observer_completed_batch_update_); | 48 observer_completed_batch_update_); |
| 121 ASSERT_EQ(observer_deleted, observer_deleted_); | 49 ASSERT_EQ(observer_deleted, observer_deleted_); |
| 122 ASSERT_EQ(observer_remove_unread, observer_remove_unread_); | 50 ASSERT_EQ(observer_remove_unread, observer_remove_unread_); |
| 123 ASSERT_EQ(observer_remove_read, observer_remove_read_); | 51 ASSERT_EQ(observer_remove_read, observer_remove_read_); |
| 124 ASSERT_EQ(observer_move, observer_move_); | 52 ASSERT_EQ(observer_move, observer_move_); |
| 125 ASSERT_EQ(observer_add_unread, observer_add_unread_); | 53 ASSERT_EQ(observer_add_unread, observer_add_unread_); |
| 126 ASSERT_EQ(observer_add_read, observer_add_read_); | 54 ASSERT_EQ(observer_add_read, observer_add_read_); |
| 127 ASSERT_EQ(observer_update_unread, observer_update_unread_); | 55 ASSERT_EQ(observer_update_unread, observer_update_unread_); |
| 128 ASSERT_EQ(observer_update_read, observer_update_read_); | 56 ASSERT_EQ(observer_update_read, observer_update_read_); |
| 129 ASSERT_EQ(observer_did_apply, observer_did_apply_); | 57 ASSERT_EQ(observer_did_apply, observer_did_apply_); |
| 130 } | 58 } |
| 131 | 59 |
| 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 | 60 // ReadingListModelObserver |
| 138 void ReadingListModelLoaded(const ReadingListModel* model) override { | 61 void ReadingListModelLoaded(const ReadingListModel* model) override { |
| 139 observer_loaded_ += 1; | 62 observer_loaded_ += 1; |
| 140 } | 63 } |
| 141 void ReadingListModelBeganBatchUpdates( | 64 void ReadingListModelBeganBatchUpdates( |
| 142 const ReadingListModel* model) override { | 65 const ReadingListModel* model) override { |
| 143 observer_started_batch_update_ += 1; | 66 observer_started_batch_update_ += 1; |
| 144 } | 67 } |
| 145 void ReadingListModelCompletedBatchUpdates( | 68 void ReadingListModelCompletedBatchUpdates( |
| 146 const ReadingListModel* model) override { | 69 const ReadingListModel* model) override { |
| 147 observer_completed_batch_update_ += 1; | 70 observer_completed_batch_update_ += 1; |
| 148 } | 71 } |
| 149 void ReadingListModelBeingDeleted(const ReadingListModel* model) override { | 72 void ReadingListModelBeingDeleted(const ReadingListModel* model) override { |
| 150 observer_deleted_ += 1; | 73 observer_deleted_ += 1; |
| 151 } | 74 } |
| 152 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, | 75 void ReadingListWillRemoveUnreadEntry(const ReadingListModel* model, |
| 153 size_t index) override { | 76 size_t index) override { |
| 154 observer_remove_unread_ += 1; | 77 observer_remove_unread_ += 1; |
| 155 } | 78 } |
| 156 void ReadingListWillMoveEntry(const ReadingListModel* model, | 79 void ReadingListWillMoveEntry(const ReadingListModel* model, |
| 157 size_t index, | 80 size_t index) override { |
| 158 bool read) override { | |
| 159 observer_move_ += 1; | 81 observer_move_ += 1; |
| 160 } | 82 } |
| 161 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, | 83 void ReadingListWillRemoveReadEntry(const ReadingListModel* model, |
| 162 size_t index) override { | 84 size_t index) override { |
| 163 observer_remove_read_ += 1; | 85 observer_remove_read_ += 1; |
| 164 } | 86 } |
| 165 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, | 87 void ReadingListWillAddUnreadEntry(const ReadingListModel* model, |
| 166 const ReadingListEntry& entry) override { | 88 const ReadingListEntry& entry) override { |
| 167 observer_add_unread_ += 1; | 89 observer_add_unread_ += 1; |
| 168 } | 90 } |
| 169 void ReadingListWillAddReadEntry(const ReadingListModel* model, | 91 void ReadingListWillAddReadEntry(const ReadingListModel* model, |
| 170 const ReadingListEntry& entry) override { | 92 const ReadingListEntry& entry) override { |
| 171 observer_add_read_ += 1; | 93 observer_add_read_ += 1; |
| 172 } | 94 } |
| 173 void ReadingListWillUpdateUnreadEntry(const ReadingListModel* model, | 95 void ReadingListWillUpdateUnreadEntry(const ReadingListModel* model, |
| 174 size_t index) override { | 96 size_t index) override { |
| 175 observer_update_unread_ += 1; | 97 observer_update_unread_ += 1; |
| 176 } | 98 } |
| 177 void ReadingListWillUpdateReadEntry(const ReadingListModel* model, | 99 void ReadingListWillUpdateReadEntry(const ReadingListModel* model, |
| 178 size_t index) override { | 100 size_t index) override { |
| 179 observer_update_read_ += 1; | 101 observer_update_read_ += 1; |
| 180 } | 102 } |
| 181 void ReadingListDidApplyChanges(ReadingListModel* model) override { | 103 void ReadingListDidApplyChanges(ReadingListModel* model) override { |
| 182 observer_did_apply_ += 1; | 104 observer_did_apply_ += 1; |
| 183 } | 105 } |
| 184 | |
| 185 void ReadingListDidSaveEntry() override { storage_saved_ += 1; } | |
| 186 void ReadingListDidRemoveEntry() override { storage_removed_ += 1; } | |
| 187 | |
| 188 void Callback(const ReadingListEntry& entry) { | 106 void Callback(const ReadingListEntry& entry) { |
| 189 EXPECT_EQ(callback_url, entry.URL()); | 107 EXPECT_EQ(callback_url, entry.URL()); |
| 190 EXPECT_EQ(callback_title, entry.Title()); | 108 EXPECT_EQ(callback_title, entry.Title()); |
| 191 callback_called_ = true; | 109 callback_called_ = true; |
| 192 } | 110 } |
| 193 | 111 |
| 194 bool CallbackCalled() { return callback_called_; } | 112 bool CallbackCalled() { return callback_called_; } |
| 195 | 113 |
| 196 protected: | 114 protected: |
| 197 int observer_loaded_; | 115 int observer_loaded_; |
| 198 int observer_started_batch_update_; | 116 int observer_started_batch_update_; |
| 199 int observer_completed_batch_update_; | 117 int observer_completed_batch_update_; |
| 200 int observer_deleted_; | 118 int observer_deleted_; |
| 201 int observer_remove_unread_; | 119 int observer_remove_unread_; |
| 202 int observer_remove_read_; | 120 int observer_remove_read_; |
| 203 int observer_move_; | 121 int observer_move_; |
| 204 int observer_add_unread_; | 122 int observer_add_unread_; |
| 205 int observer_add_read_; | 123 int observer_add_read_; |
| 206 int observer_update_unread_; | 124 int observer_update_unread_; |
| 207 int observer_update_read_; | 125 int observer_update_read_; |
| 208 int observer_did_apply_; | 126 int observer_did_apply_; |
| 209 int storage_saved_; | |
| 210 int storage_removed_; | |
| 211 bool callback_called_; | 127 bool callback_called_; |
| 212 | 128 |
| 213 std::unique_ptr<ReadingListModelImpl> model_; | 129 std::unique_ptr<ReadingListModelImpl> model_; |
| 214 }; | 130 }; |
| 215 | 131 |
| 216 TEST_F(ReadingListModelTest, EmptyLoaded) { | 132 TEST_F(ReadingListModelTest, EmptyLoaded) { |
| 217 EXPECT_TRUE(model_->loaded()); | 133 EXPECT_TRUE(model_->loaded()); |
| 218 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | 134 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 219 EXPECT_EQ(0ul, model_->unread_size()); | 135 EXPECT_EQ(0ul, model_->unread_size()); |
| 220 EXPECT_EQ(0ul, model_->read_size()); | 136 EXPECT_EQ(0ul, model_->read_size()); |
| 221 model_->Shutdown(); | 137 model_->Shutdown(); |
| 222 EXPECT_FALSE(model_->loaded()); | 138 EXPECT_FALSE(model_->loaded()); |
| 223 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); | 139 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); |
| 224 } | 140 } |
| 225 | 141 |
| 226 TEST_F(ReadingListModelTest, ModelLoaded) { | 142 TEST_F(ReadingListModelTest, AddEntry) { |
| 227 ClearCounts(); | 143 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 = | 144 const ReadingListEntry& entry = |
| 251 model_->AddEntry(GURL("http://example.com"), "\n \tsample Test "); | 145 model_->AddEntry(GURL("http://example.com"), "\n \tsample Test "); |
| 252 EXPECT_EQ(GURL("http://example.com"), entry.URL()); | 146 EXPECT_EQ(GURL("http://example.com"), entry.URL()); |
| 253 EXPECT_EQ("sample Test", entry.Title()); | 147 EXPECT_EQ("sample Test", entry.Title()); |
| 254 | 148 |
| 255 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1); | 149 AssertObserverCount(0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1); |
| 256 AssertStorageCount(1, 0); | |
| 257 EXPECT_EQ(1ul, model_->unread_size()); | 150 EXPECT_EQ(1ul, model_->unread_size()); |
| 258 EXPECT_EQ(0ul, model_->read_size()); | 151 EXPECT_EQ(0ul, model_->read_size()); |
| 259 EXPECT_TRUE(model_->HasUnseenEntries()); | 152 EXPECT_TRUE(model_->HasUnseenEntries()); |
| 260 | 153 |
| 261 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0); | 154 const ReadingListEntry& other_entry = model_->GetUnreadEntryAtIndex(0); |
| 262 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); | 155 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); |
| 263 EXPECT_EQ("sample Test", other_entry.Title()); | 156 EXPECT_EQ("sample Test", other_entry.Title()); |
| 264 } | 157 } |
| 265 | 158 |
| 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) { | 159 TEST_F(ReadingListModelTest, ReadEntry) { |
| 370 model_->AddEntry(GURL("http://example.com"), "sample"); | 160 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 371 | 161 |
| 372 ClearCounts(); | 162 ClearCounts(); |
| 373 model_->MarkReadByURL(GURL("http://example.com")); | 163 model_->MarkReadByURL(GURL("http://example.com")); |
| 374 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); | 164 AssertObserverCount(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); |
| 375 EXPECT_EQ(0ul, model_->unread_size()); | 165 EXPECT_EQ(0ul, model_->unread_size()); |
| 376 EXPECT_EQ(1ul, model_->read_size()); | 166 EXPECT_EQ(1ul, model_->read_size()); |
| 377 EXPECT_FALSE(model_->HasUnseenEntries()); | 167 EXPECT_FALSE(model_->HasUnseenEntries()); |
| 378 | 168 |
| 379 const ReadingListEntry& other_entry = model_->GetReadEntryAtIndex(0); | 169 const ReadingListEntry& other_entry = model_->GetReadEntryAtIndex(0); |
| 380 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); | 170 EXPECT_EQ(GURL("http://example.com"), other_entry.URL()); |
| 381 EXPECT_EQ("sample", other_entry.Title()); | 171 EXPECT_EQ("sample", other_entry.Title()); |
| 382 } | 172 } |
| 383 | 173 |
| 384 TEST_F(ReadingListModelTest, EntryFromURL) { | 174 TEST_F(ReadingListModelTest, EntryFromURL) { |
| 385 GURL url1("http://example.com"); | 175 GURL url1("http://example.com"); |
| 386 GURL url2("http://example2.com"); | 176 GURL url2("http://example2.com"); |
| 387 std::string entry1_title = "foo bar qux"; | 177 std::string entry1_title = "foo bar qux"; |
| 388 model_->AddEntry(url1, entry1_title); | 178 model_->AddEntry(url1, entry1_title); |
| 389 | 179 |
| 390 // Check call with nullptr |read| parameter. | 180 const ReadingListEntry* entry1 = model_->GetEntryFromURL(url1); |
| 391 const ReadingListEntry* entry1 = model_->GetEntryFromURL(url1, nullptr); | 181 EXPECT_NE(nullptr, entry1); |
| 182 EXPECT_EQ(entry1_title, entry1->Title()); |
| 183 model_->MarkReadByURL(url1); |
| 184 entry1 = model_->GetEntryFromURL(url1); |
| 392 EXPECT_NE(nullptr, entry1); | 185 EXPECT_NE(nullptr, entry1); |
| 393 EXPECT_EQ(entry1_title, entry1->Title()); | 186 EXPECT_EQ(entry1_title, entry1->Title()); |
| 394 | 187 |
| 395 bool read; | 188 const ReadingListEntry* entry2 = model_->GetEntryFromURL(url2); |
| 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); | 189 EXPECT_EQ(nullptr, entry2); |
| 408 } | 190 } |
| 409 | 191 |
| 410 TEST_F(ReadingListModelTest, UnreadEntry) { | 192 TEST_F(ReadingListModelTest, UnreadEntry) { |
| 411 // Setup. | 193 // Setup. |
| 412 model_->AddEntry(GURL("http://example.com"), "sample"); | 194 model_->AddEntry(GURL("http://example.com"), "sample"); |
| 413 model_->MarkReadByURL(GURL("http://example.com")); | 195 model_->MarkReadByURL(GURL("http://example.com")); |
| 414 ClearCounts(); | 196 ClearCounts(); |
| 415 ASSERT_EQ(0ul, model_->unread_size()); | 197 ASSERT_EQ(0ul, model_->unread_size()); |
| 416 ASSERT_EQ(1ul, model_->read_size()); | 198 ASSERT_EQ(1ul, model_->read_size()); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 } | 375 } |
| 594 | 376 |
| 595 // Tests that ReadingListModel calls CallbackModelBeingDeleted when destroyed. | 377 // Tests that ReadingListModel calls CallbackModelBeingDeleted when destroyed. |
| 596 TEST_F(ReadingListModelTest, CallbackModelBeingDeleted) { | 378 TEST_F(ReadingListModelTest, CallbackModelBeingDeleted) { |
| 597 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | 379 AssertObserverCount(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 598 model_.reset(); | 380 model_.reset(); |
| 599 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); | 381 AssertObserverCount(1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); |
| 600 } | 382 } |
| 601 | 383 |
| 602 } // namespace | 384 } // namespace |
| OLD | NEW |