Index: ios/chrome/browser/reading_list/reading_list_store_unittest.cc |
diff --git a/ios/chrome/browser/reading_list/reading_list_store_unittest.cc b/ios/chrome/browser/reading_list/reading_list_store_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0712d4754d499b0e40d5344dca1b043ee62e622f |
--- /dev/null |
+++ b/ios/chrome/browser/reading_list/reading_list_store_unittest.cc |
@@ -0,0 +1,153 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/test/ios/wait_util.h" |
+#include "components/leveldb_proto/testing/fake_db.h" |
+#include "ios/chrome/browser/reading_list/proto/reading_list.pb.h" |
+#include "ios/chrome/browser/reading_list/reading_list_model_impl.h" |
+#include "ios/chrome/browser/reading_list/reading_list_store.h" |
+#include "ios/web/public/test/test_web_thread_bundle.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+typedef base::hash_map<std::string, reading_list::ReadingListLocal> EntryMap; |
+ |
+class FakeReadingListModel : public ReadingListModelImpl { |
+ public: |
+ FakeReadingListModel() : model_loaded_called_(false) {} |
+ void ModelLoaded(std::unique_ptr<ReadingListEntries> unread, |
+ std::unique_ptr<ReadingListEntries> read) override { |
+ ReadingListModelImpl::ModelLoaded(std::move(unread), std::move(read)); |
+ model_loaded_called_ = true; |
+ }; |
+ |
+ bool ModelLoadedCalled() { return model_loaded_called_; } |
+ |
+ private: |
+ bool model_loaded_called_; |
+}; |
+ |
+class ReadingListStoreTest : public testing::Test { |
+ public: |
+ void SetUp() override { |
+ rl_model_ = base::MakeUnique<FakeReadingListModel>(); |
+ fake_db_ = new leveldb_proto::test::FakeDB<reading_list::ReadingListLocal>( |
+ &db_model_); |
+ std::unique_ptr<ReadingListDB> unique_fake_db(fake_db_); |
+ store_ = base::MakeUnique<ReadingListStore>( |
+ std::move(unique_fake_db), |
+ leveldb_proto::test::FakeDB< |
+ reading_list::ReadingListLocal>::DirectoryForTestDB()); |
+ store_->SetReadingListModel(rl_model_.get()); |
+ } |
+ |
+ void LoadDatabase() { |
+ store_->LoadPersistentLists(); |
+ fake_db_->LoadCallback(true); |
+ base::test::ios::WaitUntilCondition(^{ |
+ return rl_model_->ModelLoadedCalled(); |
+ }); |
+ } |
+ |
+ web::TestWebThreadBundle thread_bundle_; |
+ std::unique_ptr<FakeReadingListModel> rl_model_; |
+ EntryMap db_model_; |
+ std::unique_ptr<ReadingListStore> store_; |
+ // Owned by |store_|. |
+ leveldb_proto::test::FakeDB<reading_list::ReadingListLocal>* fake_db_; |
+}; |
+ |
+TEST_F(ReadingListStoreTest, CheckEmpties) { |
+ LoadDatabase(); |
+ EXPECT_EQ(0ul, rl_model_->unread_size()); |
+ EXPECT_EQ(0ul, rl_model_->read_size()); |
+} |
+ |
+TEST_F(ReadingListStoreTest, SaveOneRead) { |
+ LoadDatabase(); |
+ EXPECT_EQ(0ul, db_model_.size()); |
+ ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); |
+ store_->SaveEntry(entry, true); |
+ fake_db_->UpdateCallback(true); |
+ EXPECT_EQ(1ul, db_model_.size()); |
+ EntryMap::iterator it = db_model_.begin(); |
+ EXPECT_EQ("http://read.example.com/", it->first); |
+ EXPECT_TRUE(it->second.has_entry()); |
+ EXPECT_TRUE(it->second.entry().has_url()); |
+ EXPECT_EQ("http://read.example.com/", it->second.entry().url()); |
+ EXPECT_EQ("read title", it->second.entry().title()); |
+ EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::READ); |
+} |
+ |
+TEST_F(ReadingListStoreTest, LoadOneRead) { |
+ ReadingListEntry entry(GURL("http://read.example.com/"), "read title"); |
+ db_model_["http://read.example.com/"] = (*entry.AsReadingListLocal(true)); |
+ LoadDatabase(); |
+ EXPECT_EQ(0ul, rl_model_->unread_size()); |
+ EXPECT_EQ(1ul, rl_model_->read_size()); |
+} |
+ |
+TEST_F(ReadingListStoreTest, SaveOneUnread) { |
+ LoadDatabase(); |
+ EXPECT_EQ(0ul, db_model_.size()); |
+ ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
+ store_->SaveEntry(entry, false); |
+ fake_db_->UpdateCallback(true); |
+ EXPECT_EQ(1ul, db_model_.size()); |
+ EntryMap::iterator it = db_model_.begin(); |
+ EXPECT_EQ("http://unread.example.com/", it->first); |
+ EXPECT_TRUE(it->second.has_entry()); |
+ EXPECT_TRUE(it->second.entry().has_url()); |
+ EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
+ EXPECT_EQ("unread title", it->second.entry().title()); |
+ EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
+} |
+ |
+TEST_F(ReadingListStoreTest, LoadOneUnread) { |
+ ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
+ db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
+ LoadDatabase(); |
+ EXPECT_EQ(1ul, rl_model_->unread_size()); |
+ EXPECT_EQ(0ul, rl_model_->read_size()); |
+} |
+ |
+TEST_F(ReadingListStoreTest, SaveOneModified) { |
+ ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
+ db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
+ LoadDatabase(); |
+ EXPECT_EQ(1ul, db_model_.size()); |
+ EntryMap::iterator it = db_model_.begin(); |
+ EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
+ EXPECT_EQ("unread title", it->second.entry().title()); |
+ EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
+ |
+ ReadingListEntry new_entry(GURL("http://unread.example.com/"), |
+ "unread new title"); |
+ store_->SaveEntry(new_entry, false); |
+ fake_db_->UpdateCallback(true); |
+ |
+ EXPECT_EQ(1ul, db_model_.size()); |
+ it = db_model_.begin(); |
+ EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
+ EXPECT_EQ("unread new title", it->second.entry().title()); |
+ EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::UNREAD); |
+ |
+ store_->SaveEntry(new_entry, true); |
+ fake_db_->UpdateCallback(true); |
+ it = db_model_.begin(); |
+ EXPECT_EQ("http://unread.example.com/", it->second.entry().url()); |
+ EXPECT_EQ("unread new title", it->second.entry().title()); |
+ EXPECT_EQ(it->second.entry().status(), sync_pb::ReadingListSpecifics::READ); |
+} |
+ |
+TEST_F(ReadingListStoreTest, RemoveOneEntry) { |
+ ReadingListEntry entry(GURL("http://unread.example.com/"), "unread title"); |
+ db_model_["http://unread.example.com/"] = (*entry.AsReadingListLocal(false)); |
+ LoadDatabase(); |
+ |
+ EXPECT_EQ(1ul, db_model_.size()); |
+ store_->RemoveEntry(entry); |
+ fake_db_->UpdateCallback(true); |
+ EXPECT_EQ(0ul, db_model_.size()); |
+} |