Chromium Code Reviews| 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; | |
|
Marc Treib
2016/05/23 14:00:58
Arguably, this and the two operator==s above shoul
| |
| 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 | |
| 66 void CreateDatabase() { | |
| 67 // Explicitly destroy any existing database first, so it releases the lock | |
| 68 // on the file. | |
| 69 if (db_) | |
|
Bernhard Bauer
2016/05/25 15:01:15
This isn't necessary -- a .reset() on a null point
Marc Treib
2016/05/27 14:03:12
Done.
| |
| 70 db_.reset(); | |
| 71 | |
| 72 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), | |
| 73 base::ThreadTaskRunnerHandle::Get())); | |
| 74 } | |
| 75 | |
| 76 NTPSnippetsDatabase* db() { return db_.get(); } | |
| 77 | |
| 78 bool db_inited() { return db_->database_inited_; } | |
| 79 | |
| 80 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { | |
| 81 OnSnippetsLoadedImpl(snippets); | |
| 82 } | |
| 83 | |
| 84 MOCK_METHOD1(OnSnippetsLoadedImpl, | |
| 85 void(const NTPSnippet::PtrVector& snippets)); | |
| 86 | |
| 87 private: | |
| 88 base::MessageLoop message_loop_; | |
| 89 base::ScopedTempDir database_dir_; | |
| 90 std::unique_ptr<NTPSnippetsDatabase> db_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); | |
| 93 }; | |
| 94 | |
| 95 TEST_F(NTPSnippetsDatabaseTest, Init) { | |
| 96 ASSERT_FALSE(db()); | |
| 97 | |
| 98 CreateDatabase(); | |
| 99 EXPECT_FALSE(db_inited()); | |
| 100 | |
| 101 base::RunLoop().RunUntilIdle(); | |
| 102 EXPECT_TRUE(db_inited()); | |
| 103 } | |
| 104 | |
| 105 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { | |
| 106 CreateDatabase(); | |
| 107 EXPECT_FALSE(db_inited()); | |
| 108 | |
| 109 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 110 base::Unretained(this))); | |
| 111 | |
| 112 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | |
| 113 base::RunLoop().RunUntilIdle(); | |
| 114 EXPECT_TRUE(db_inited()); | |
| 115 } | |
| 116 | |
| 117 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { | |
| 118 CreateDatabase(); | |
| 119 EXPECT_FALSE(db_inited()); | |
| 120 | |
| 121 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); | |
| 122 base::RunLoop().RunUntilIdle(); | |
| 123 EXPECT_TRUE(db_inited()); | |
| 124 | |
| 125 Mock::VerifyAndClearExpectations(this); | |
| 126 | |
| 127 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 128 base::Unretained(this))); | |
| 129 | |
| 130 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | |
| 131 base::RunLoop().RunUntilIdle(); | |
| 132 } | |
| 133 | |
| 134 TEST_F(NTPSnippetsDatabaseTest, Save) { | |
| 135 CreateDatabase(); | |
| 136 base::RunLoop().RunUntilIdle(); | |
| 137 ASSERT_TRUE(db_inited()); | |
| 138 | |
| 139 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | |
| 140 | |
| 141 db()->Save(*snippet); | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 | |
| 144 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 145 base::Unretained(this))); | |
| 146 | |
| 147 EXPECT_CALL(*this, | |
| 148 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | |
| 149 base::RunLoop().RunUntilIdle(); | |
| 150 | |
| 151 Mock::VerifyAndClearExpectations(this); | |
| 152 | |
| 153 // The snippet should still exist after recreating the database. | |
| 154 CreateDatabase(); | |
| 155 | |
| 156 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 157 base::Unretained(this))); | |
| 158 | |
| 159 EXPECT_CALL(*this, | |
| 160 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | |
| 161 base::RunLoop().RunUntilIdle(); | |
| 162 } | |
| 163 | |
| 164 TEST_F(NTPSnippetsDatabaseTest, Update) { | |
| 165 CreateDatabase(); | |
| 166 base::RunLoop().RunUntilIdle(); | |
| 167 ASSERT_TRUE(db_inited()); | |
| 168 | |
| 169 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | |
| 170 | |
| 171 db()->Save(*snippet); | |
| 172 base::RunLoop().RunUntilIdle(); | |
| 173 | |
| 174 const std::string text("some text"); | |
| 175 snippet->set_snippet(text); | |
| 176 | |
| 177 db()->Save(*snippet); | |
| 178 base::RunLoop().RunUntilIdle(); | |
| 179 | |
| 180 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 181 base::Unretained(this))); | |
| 182 | |
| 183 EXPECT_CALL(*this, | |
| 184 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | |
| 185 base::RunLoop().RunUntilIdle(); | |
| 186 } | |
| 187 | |
| 188 TEST_F(NTPSnippetsDatabaseTest, Delete) { | |
| 189 CreateDatabase(); | |
| 190 base::RunLoop().RunUntilIdle(); | |
| 191 ASSERT_TRUE(db_inited()); | |
| 192 | |
| 193 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | |
| 194 | |
| 195 db()->Save(*snippet); | |
| 196 base::RunLoop().RunUntilIdle(); | |
| 197 | |
| 198 db()->Delete(snippet->id()); | |
| 199 base::RunLoop().RunUntilIdle(); | |
| 200 | |
| 201 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | |
| 202 base::Unretained(this))); | |
| 203 | |
| 204 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); | |
| 205 base::RunLoop().RunUntilIdle(); | |
| 206 } | |
| 207 | |
| 208 } // namespace ntp_snippets | |
| OLD | NEW |