Chromium Code Reviews| 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 2307a4baef113a10371f20fe3a72c39ca5b12f20..b9060a3d39092b8d7ab1b0ccd1d96df14a9fdc01 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()); |
|
Bernhard Bauer
2016/05/27 16:06:43
Really, shouldn't this be EXPECT_FALSE? :-D
Marc Treib
2016/05/30 13:22:00
Right :D
This line doesn't exist anymore though; t
|
| + service_->AddObserver(this); |
| + run_loop_.Run(); |
| + } |
| + |
| + ~WaitForDBLoad() override { |
| + service_->RemoveObserver(this); |
| + } |
| + |
| + private: |
| + void NTPSnippetsServiceLoaded() override { |
| + EXPECT_TRUE(service_->loaded()); |
| + run_loop_.Quit(); |
| + } |
| + |
| + void NTPSnippetsServiceShutdown() override {} |
| + 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,14 +308,22 @@ 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( |
| enabled, pref_service_.get(), mock_sync_service_.get(), nullptr, |
| - task_runner, std::string("fr"), &scheduler_, |
| + 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)))); |
| + if (enabled) |
| + WaitForDBLoad(service_.get()); |
| } |
| protected: |
| @@ -338,6 +377,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); |
| }; |