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