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

Unified Diff: components/ntp_snippets/ntp_snippets_service_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: . Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_service_unittest.cc b/components/ntp_snippets/ntp_snippets_service_unittest.cc
index 0141584b4fcd151e524693a5ea8e584807859773..7bb3678b9b11c5fcd4d9ce237dc49958a2e9c9fd 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -8,6 +8,8 @@
#include <vector>
#include "base/command_line.h"
+#include "base/files/file_path.h"
+#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
@@ -21,6 +23,7 @@
#include "base/time/time.h"
#include "components/image_fetcher/image_fetcher.h"
#include "components/ntp_snippets/ntp_snippet.h"
+#include "components/ntp_snippets/ntp_snippets_database.h"
#include "components/ntp_snippets/ntp_snippets_fetcher.h"
#include "components/ntp_snippets/ntp_snippets_scheduler.h"
#include "components/ntp_snippets/switches.h"
@@ -236,6 +239,33 @@ class MockServiceObserver : public NTPSnippetsServiceObserver {
MOCK_METHOD0(NTPSnippetsServiceDisabled, void());
};
+class WaitForDBLoad : public NTPSnippetsServiceObserver {
+ public:
+ WaitForDBLoad(NTPSnippetsService* service) : service_(service) {
+ EXPECT_TRUE(!service_->loaded());
+ service_->AddObserver(this);
+ run_loop_.Run();
Bernhard Bauer 2016/05/25 15:01:15 If you already have an object for this, you could
Marc Treib 2016/05/27 14:03:12 Init() doesn't exist anymore :) I guess we could r
Bernhard Bauer 2016/05/27 16:06:43 It's a bit of a defense-in-depth thing: if you at
Marc Treib 2016/05/30 13:22:00 This pattern isn't possible anymore, since the NTP
+ }
+
+ ~WaitForDBLoad() override {
+ service_->RemoveObserver(this);
+ }
+
+ private:
+ void NTPSnippetsServiceLoaded() override {
+ EXPECT_TRUE(service_->loaded());
+ run_loop_.Quit();
+ }
+
+ void NTPSnippetsServiceShutdown() override {};
Bernhard Bauer 2016/05/25 15:01:15 No semicolon
Marc Treib 2016/05/27 14:03:13 Done.
+ void NTPSnippetsServiceDisabled() override {};
+
+ NTPSnippetsService* service_;
+ base::RunLoop run_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
+};
+
} // namespace
class NTPSnippetsServiceTest : public testing::Test {
@@ -256,6 +286,7 @@ class NTPSnippetsServiceTest : public testing::Test {
// service to fetch from all hosts.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDontRestrict);
+ EXPECT_TRUE(database_dir_.CreateUniqueTempDir());
}
~NTPSnippetsServiceTest() override {
@@ -277,15 +308,23 @@ class NTPSnippetsServiceTest : public testing::Test {
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(task_runner.get());
+ // Delete the current service, so that the database is destroyed before we
+ // create the new one, otherwise opening the new database will fail.
+ service_.reset();
+
service_.reset(new NTPSnippetsService(
- pref_service_.get(), mock_sync_service_.get(), nullptr, task_runner,
+ pref_service_.get(), mock_sync_service_.get(), nullptr,
std::string("fr"), &scheduler_,
base::WrapUnique(new NTPSnippetsFetcher(
fake_signin_manager_.get(), fake_token_service_.get(),
std::move(request_context_getter), base::Bind(&ParseJson),
/*is_stable_channel=*/true)),
- /*image_fetcher=*/nullptr));
+ /*image_fetcher=*/nullptr,
+ base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(),
+ task_runner))));
service_->Init(enabled);
+ if (enabled)
+ WaitForDBLoad(service_.get());
}
protected:
@@ -339,6 +378,8 @@ class NTPSnippetsServiceTest : public testing::Test {
// Last so that the dependencies are deleted after the service.
std::unique_ptr<NTPSnippetsService> service_;
+ base::ScopedTempDir database_dir_;
+
DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
};

Powered by Google App Engine
This is Rietveld 408576698