| 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 "components/ntp_snippets/ntp_snippets_database.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using testing::ElementsAre; |
| 21 using testing::IsEmpty; |
| 22 using testing::Mock; |
| 23 using testing::_; |
| 24 |
| 25 namespace ntp_snippets { |
| 26 |
| 27 bool operator==(const SnippetSource& lhs, const SnippetSource& rhs) { |
| 28 return lhs.url == rhs.url && lhs.publisher_name == rhs.publisher_name && |
| 29 lhs.amp_url == rhs.amp_url; |
| 30 } |
| 31 |
| 32 bool operator==(const NTPSnippet& lhs, const NTPSnippet& rhs) { |
| 33 return lhs.id() == rhs.id() && lhs.title() == rhs.title() && |
| 34 lhs.snippet() == rhs.snippet() && |
| 35 lhs.salient_image_url() == rhs.salient_image_url() && |
| 36 lhs.publish_date() == rhs.publish_date() && |
| 37 lhs.expiry_date() == rhs.expiry_date() && |
| 38 lhs.source_index() == rhs.source_index() && |
| 39 lhs.sources() == rhs.sources() && lhs.score() == rhs.score() && |
| 40 lhs.is_discarded() == rhs.is_discarded(); |
| 41 } |
| 42 |
| 43 namespace { |
| 44 |
| 45 std::unique_ptr<NTPSnippet> CreateTestSnippet() { |
| 46 std::unique_ptr<NTPSnippet> snippet(new NTPSnippet("http://localhost")); |
| 47 snippet->add_source( |
| 48 SnippetSource(GURL("http://localhost"), "Publisher", GURL("http://amp"))); |
| 49 return snippet; |
| 50 } |
| 51 |
| 52 MATCHER_P(SnippetEq, snippet, "") { |
| 53 return *arg == *snippet; |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 class NTPSnippetsDatabaseTest : public testing::Test { |
| 59 public: |
| 60 NTPSnippetsDatabaseTest() { |
| 61 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 62 } |
| 63 |
| 64 ~NTPSnippetsDatabaseTest() override { |
| 65 // We need to run the message loop after deleting the database, because |
| 66 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task |
| 67 // runner. Without this, we'd get reports of memory leaks. |
| 68 db_.reset(); |
| 69 base::RunLoop().RunUntilIdle(); |
| 70 } |
| 71 |
| 72 void CreateDatabase() { |
| 73 // Explicitly destroy any existing database first, so it releases the lock |
| 74 // on the file. |
| 75 db_.reset(); |
| 76 |
| 77 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), |
| 78 base::ThreadTaskRunnerHandle::Get())); |
| 79 } |
| 80 |
| 81 NTPSnippetsDatabase* db() { return db_.get(); } |
| 82 |
| 83 bool db_inited() { return db_->database_initialized_; } |
| 84 |
| 85 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { |
| 86 OnSnippetsLoadedImpl(snippets); |
| 87 } |
| 88 |
| 89 MOCK_METHOD1(OnSnippetsLoadedImpl, |
| 90 void(const NTPSnippet::PtrVector& snippets)); |
| 91 |
| 92 private: |
| 93 base::MessageLoop message_loop_; |
| 94 base::ScopedTempDir database_dir_; |
| 95 std::unique_ptr<NTPSnippetsDatabase> db_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); |
| 98 }; |
| 99 |
| 100 TEST_F(NTPSnippetsDatabaseTest, Init) { |
| 101 ASSERT_FALSE(db()); |
| 102 |
| 103 CreateDatabase(); |
| 104 EXPECT_FALSE(db_inited()); |
| 105 |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 EXPECT_TRUE(db_inited()); |
| 108 } |
| 109 |
| 110 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { |
| 111 CreateDatabase(); |
| 112 EXPECT_FALSE(db_inited()); |
| 113 |
| 114 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 115 base::Unretained(this))); |
| 116 |
| 117 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 118 base::RunLoop().RunUntilIdle(); |
| 119 EXPECT_TRUE(db_inited()); |
| 120 } |
| 121 |
| 122 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { |
| 123 CreateDatabase(); |
| 124 EXPECT_FALSE(db_inited()); |
| 125 |
| 126 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); |
| 127 base::RunLoop().RunUntilIdle(); |
| 128 EXPECT_TRUE(db_inited()); |
| 129 |
| 130 Mock::VerifyAndClearExpectations(this); |
| 131 |
| 132 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 133 base::Unretained(this))); |
| 134 |
| 135 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 136 base::RunLoop().RunUntilIdle(); |
| 137 } |
| 138 |
| 139 TEST_F(NTPSnippetsDatabaseTest, Save) { |
| 140 CreateDatabase(); |
| 141 base::RunLoop().RunUntilIdle(); |
| 142 ASSERT_TRUE(db_inited()); |
| 143 |
| 144 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 145 |
| 146 db()->Save(*snippet); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 |
| 149 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 150 base::Unretained(this))); |
| 151 |
| 152 EXPECT_CALL(*this, |
| 153 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 154 base::RunLoop().RunUntilIdle(); |
| 155 |
| 156 Mock::VerifyAndClearExpectations(this); |
| 157 |
| 158 // The snippet should still exist after recreating the database. |
| 159 CreateDatabase(); |
| 160 |
| 161 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 162 base::Unretained(this))); |
| 163 |
| 164 EXPECT_CALL(*this, |
| 165 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 166 base::RunLoop().RunUntilIdle(); |
| 167 } |
| 168 |
| 169 TEST_F(NTPSnippetsDatabaseTest, Update) { |
| 170 CreateDatabase(); |
| 171 base::RunLoop().RunUntilIdle(); |
| 172 ASSERT_TRUE(db_inited()); |
| 173 |
| 174 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 175 |
| 176 db()->Save(*snippet); |
| 177 base::RunLoop().RunUntilIdle(); |
| 178 |
| 179 const std::string text("some text"); |
| 180 snippet->set_snippet(text); |
| 181 |
| 182 db()->Save(*snippet); |
| 183 base::RunLoop().RunUntilIdle(); |
| 184 |
| 185 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 186 base::Unretained(this))); |
| 187 |
| 188 EXPECT_CALL(*this, |
| 189 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 190 base::RunLoop().RunUntilIdle(); |
| 191 } |
| 192 |
| 193 TEST_F(NTPSnippetsDatabaseTest, Delete) { |
| 194 CreateDatabase(); |
| 195 base::RunLoop().RunUntilIdle(); |
| 196 ASSERT_TRUE(db_inited()); |
| 197 |
| 198 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 199 |
| 200 db()->Save(*snippet); |
| 201 base::RunLoop().RunUntilIdle(); |
| 202 |
| 203 db()->Delete(snippet->id()); |
| 204 base::RunLoop().RunUntilIdle(); |
| 205 |
| 206 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 207 base::Unretained(this))); |
| 208 |
| 209 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); |
| 210 base::RunLoop().RunUntilIdle(); |
| 211 } |
| 212 |
| 213 } // namespace ntp_snippets |
| OLD | NEW |