Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1181)

Side by Side Diff: components/ntp_snippets/ntp_snippets_database_unittest.cc

Issue 1987333003: [NTP Snippets] Persist snippets in a LevelDB instead of prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: prefs cleanup; fix internals Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
66 void CreateDatabase() {
67 // Explicitly destroy any existing database first, so it releases the lock
68 // on the file.
69 db_.reset();
70
71 db_.reset(new NTPSnippetsDatabase(database_dir_.path(),
72 base::ThreadTaskRunnerHandle::Get()));
73 }
74
75 NTPSnippetsDatabase* db() { return db_.get(); }
76
77 bool db_inited() { return db_->database_initialized_; }
78
79 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) {
80 OnSnippetsLoadedImpl(snippets);
81 }
82
83 MOCK_METHOD1(OnSnippetsLoadedImpl,
84 void(const NTPSnippet::PtrVector& snippets));
85
86 private:
87 base::MessageLoop message_loop_;
88 base::ScopedTempDir database_dir_;
89 std::unique_ptr<NTPSnippetsDatabase> db_;
90
91 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest);
92 };
93
94 TEST_F(NTPSnippetsDatabaseTest, Init) {
95 ASSERT_FALSE(db());
96
97 CreateDatabase();
98 EXPECT_FALSE(db_inited());
99
100 base::RunLoop().RunUntilIdle();
101 EXPECT_TRUE(db_inited());
102 }
103
104 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) {
105 CreateDatabase();
106 EXPECT_FALSE(db_inited());
107
108 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
109 base::Unretained(this)));
110
111 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_));
112 base::RunLoop().RunUntilIdle();
113 EXPECT_TRUE(db_inited());
114 }
115
116 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) {
117 CreateDatabase();
118 EXPECT_FALSE(db_inited());
119
120 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0);
121 base::RunLoop().RunUntilIdle();
122 EXPECT_TRUE(db_inited());
123
124 Mock::VerifyAndClearExpectations(this);
125
126 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
127 base::Unretained(this)));
128
129 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_));
130 base::RunLoop().RunUntilIdle();
131 }
132
133 TEST_F(NTPSnippetsDatabaseTest, Save) {
134 CreateDatabase();
135 base::RunLoop().RunUntilIdle();
136 ASSERT_TRUE(db_inited());
137
138 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet();
139
140 db()->Save(*snippet);
141 base::RunLoop().RunUntilIdle();
142
143 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
144 base::Unretained(this)));
145
146 EXPECT_CALL(*this,
147 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get()))));
148 base::RunLoop().RunUntilIdle();
149
150 Mock::VerifyAndClearExpectations(this);
151
152 // The snippet should still exist after recreating the database.
153 CreateDatabase();
154
155 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
156 base::Unretained(this)));
157
158 EXPECT_CALL(*this,
159 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get()))));
160 base::RunLoop().RunUntilIdle();
161 }
162
163 TEST_F(NTPSnippetsDatabaseTest, Update) {
164 CreateDatabase();
165 base::RunLoop().RunUntilIdle();
166 ASSERT_TRUE(db_inited());
167
168 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet();
169
170 db()->Save(*snippet);
171 base::RunLoop().RunUntilIdle();
172
173 const std::string text("some text");
174 snippet->set_snippet(text);
175
176 db()->Save(*snippet);
177 base::RunLoop().RunUntilIdle();
178
179 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
180 base::Unretained(this)));
181
182 EXPECT_CALL(*this,
183 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get()))));
184 base::RunLoop().RunUntilIdle();
185 }
186
187 TEST_F(NTPSnippetsDatabaseTest, Delete) {
188 CreateDatabase();
189 base::RunLoop().RunUntilIdle();
190 ASSERT_TRUE(db_inited());
191
192 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet();
193
194 db()->Save(*snippet);
195 base::RunLoop().RunUntilIdle();
196
197 db()->Delete(snippet->id());
198 base::RunLoop().RunUntilIdle();
199
200 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded,
201 base::Unretained(this)));
202
203 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty()));
204 base::RunLoop().RunUntilIdle();
205 }
206
207 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698