OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/ptr_util.h" |
| 6 #include "base/test/ios/wait_util.h" |
| 7 #include "components/leveldb_proto/testing/fake_db.h" |
| 8 #include "ios/chrome/browser/reading_list/proto/reading_list.pb.h" |
| 9 #include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
| 10 #include "ios/chrome/browser/reading_list/reading_list_store.h" |
| 11 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 typedef base::hash_map<std::string, reading_list::ReadingListLocal> EntryMap; |
| 15 |
| 16 class FakeReadingListModel : public ReadingListModelImpl { |
| 17 public: |
| 18 FakeReadingListModel() : model_loaded_called_(false) {} |
| 19 void ModelLoaded(std::unique_ptr<ReadingListEntries> unread, |
| 20 std::unique_ptr<ReadingListEntries> read) override { |
| 21 ReadingListModelImpl::ModelLoaded(std::move(unread), std::move(read)); |
| 22 model_loaded_called_ = true; |
| 23 }; |
| 24 |
| 25 bool ModelLoadedCalled() { return model_loaded_called_; } |
| 26 |
| 27 private: |
| 28 bool model_loaded_called_; |
| 29 }; |
| 30 |
| 31 class ReadingListStoreTest : public testing::Test { |
| 32 public: |
| 33 void SetUp() override { |
| 34 rl_model_ = base::MakeUnique<FakeReadingListModel>(); |
| 35 fake_db_ = new leveldb_proto::test::FakeDB<reading_list::ReadingListLocal>( |
| 36 &db_model_); |
| 37 std::unique_ptr<ReadingListDB> unique_fake_db(fake_db_); |
| 38 store_ = base::MakeUnique<ReadingListStore>( |
| 39 std::move(unique_fake_db), |
| 40 leveldb_proto::test::FakeDB< |
| 41 reading_list::ReadingListLocal>::DirectoryForTestDB()); |
| 42 store_->SetReadingListModel(rl_model_.get()); |
| 43 } |
| 44 |
| 45 void LoadDatabase() { |
| 46 store_->LoadPersistentLists(); |
| 47 fake_db_->LoadCallback(true); |
| 48 base::test::ios::WaitUntilCondition(^{ |
| 49 return rl_model_->ModelLoadedCalled(); |
| 50 }); |
| 51 } |
| 52 |
| 53 web::TestWebThreadBundle thread_bundle_; |
| 54 std::unique_ptr<FakeReadingListModel> rl_model_; |
| 55 EntryMap db_model_; |
| 56 std::unique_ptr<ReadingListStore> store_; |
| 57 // Owned by |store_|. |
| 58 leveldb_proto::test::FakeDB<reading_list::ReadingListLocal>* fake_db_; |
| 59 }; |
| 60 |
| 61 TEST_F(ReadingListStoreTest, CheckEmpties) { |
| 62 LoadDatabase(); |
| 63 EXPECT_EQ(0ul, rl_model_->unread_size()); |
| 64 EXPECT_EQ(0ul, rl_model_->read_size()); |
| 65 } |
| 66 |
| 67 TEST_F(ReadingListStoreTest, SaveOneRead) { |
| 68 LoadDatabase(); |
| 69 EXPECT_EQ(0ul, db_model_.size()); |
| 70 ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); |
| 71 store_->SaveEntry(entry, true); |
| 72 fake_db_->UpdateCallback(true); |
| 73 EXPECT_EQ(1ul, db_model_.size()); |
| 74 EntryMap::iterator it = db_model_.begin(); |
| 75 EXPECT_EQ("http://read.example.com/", it->first); |
| 76 EXPECT_TRUE(it->second.has_entry()); |
| 77 EXPECT_TRUE(it->second.entry().has_url()); |
| 78 EXPECT_EQ("http://read.example.com/", it->second.entry().url()); |
| 79 EXPECT_EQ("read title", it->second.entry().title()); |
| 80 EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::READ); |
| 81 } |
| 82 |
| 83 TEST_F(ReadingListStoreTest, LoadOneRead) { |
| 84 ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); |
| 85 db_model_["http://read.example.com/"] = (*entry.AsReadingListLocal(true)); |
| 86 LoadDatabase(); |
| 87 EXPECT_EQ(0ul, rl_model_->unread_size()); |
| 88 EXPECT_EQ(1ul, rl_model_->read_size()); |
| 89 } |
| 90 |
| 91 TEST_F(ReadingListStoreTest, SaveOneUnread) { |
| 92 LoadDatabase(); |
| 93 EXPECT_EQ(0ul, db_model_.size()); |
| 94 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
| 95 store_->SaveEntry(entry, false); |
| 96 fake_db_->UpdateCallback(true); |
| 97 EXPECT_EQ(1ul, db_model_.size()); |
| 98 EntryMap::iterator it = db_model_.begin(); |
| 99 EXPECT_EQ("http://unread.example.com/", it->first); |
| 100 EXPECT_TRUE(it->second.has_entry()); |
| 101 EXPECT_TRUE(it->second.entry().has_url()); |
| 102 EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
| 103 EXPECT_EQ("unread title", it->second.entry().title()); |
| 104 EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
| 105 } |
| 106 |
| 107 TEST_F(ReadingListStoreTest, LoadOneUnread) { |
| 108 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
| 109 db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
| 110 LoadDatabase(); |
| 111 EXPECT_EQ(1ul, rl_model_->unread_size()); |
| 112 EXPECT_EQ(0ul, rl_model_->read_size()); |
| 113 } |
| 114 |
| 115 TEST_F(ReadingListStoreTest, SaveOneModified) { |
| 116 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
| 117 db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
| 118 LoadDatabase(); |
| 119 EXPECT_EQ(1ul, db_model_.size()); |
| 120 EntryMap::iterator it = db_model_.begin(); |
| 121 EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
| 122 EXPECT_EQ("unread title", it->second.entry().title()); |
| 123 EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
| 124 |
| 125 ReadingListEntry new_entry(GURL("http://unread.example.com/"), |
| 126 "unread new title"); |
| 127 store_->SaveEntry(new_entry, false); |
| 128 fake_db_->UpdateCallback(true); |
| 129 |
| 130 EXPECT_EQ(1ul, db_model_.size()); |
| 131 it = db_model_.begin(); |
| 132 EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
| 133 EXPECT_EQ("unread new title", it->second.entry().title()); |
| 134 EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
| 135 |
| 136 store_->SaveEntry(new_entry, true); |
| 137 fake_db_->UpdateCallback(true); |
| 138 it = db_model_.begin(); |
| 139 EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
| 140 EXPECT_EQ("unread new title", it->second.entry().title()); |
| 141 EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::READ); |
| 142 } |
| 143 |
| 144 TEST_F(ReadingListStoreTest, RemoveOneEntry) { |
| 145 ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
| 146 db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
| 147 LoadDatabase(); |
| 148 |
| 149 EXPECT_EQ(1ul, db_model_.size()); |
| 150 store_->RemoveEntry(entry); |
| 151 fake_db_->UpdateCallback(true); |
| 152 EXPECT_EQ(0ul, db_model_.size()); |
| 153 } |
OLD | NEW |