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

Side by Side 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: review 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | components/ntp_snippets/proto/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
11 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
12 #include "base/macros.h" 14 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 17 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
19 #include "base/test/histogram_tester.h" 21 #include "base/test/histogram_tester.h"
20 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
21 #include "base/time/time.h" 23 #include "base/time/time.h"
22 #include "components/image_fetcher/image_fetcher.h" 24 #include "components/image_fetcher/image_fetcher.h"
23 #include "components/ntp_snippets/ntp_snippet.h" 25 #include "components/ntp_snippets/ntp_snippet.h"
26 #include "components/ntp_snippets/ntp_snippets_database.h"
24 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
25 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
26 #include "components/ntp_snippets/switches.h" 29 #include "components/ntp_snippets/switches.h"
27 #include "components/prefs/testing_pref_service.h" 30 #include "components/prefs/testing_pref_service.h"
28 #include "components/signin/core/browser/account_tracker_service.h" 31 #include "components/signin/core/browser/account_tracker_service.h"
29 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 32 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
30 #include "components/signin/core/browser/fake_signin_manager.h" 33 #include "components/signin/core/browser/fake_signin_manager.h"
31 #include "components/signin/core/browser/test_signin_client.h" 34 #include "components/signin/core/browser/test_signin_client.h"
32 #include "components/sync_driver/fake_sync_service.h" 35 #include "components/sync_driver/fake_sync_service.h"
33 #include "google_apis/google_api_keys.h" 36 #include "google_apis/google_api_keys.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet()); 232 MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet());
230 }; 233 };
231 234
232 class MockServiceObserver : public NTPSnippetsServiceObserver { 235 class MockServiceObserver : public NTPSnippetsServiceObserver {
233 public: 236 public:
234 MOCK_METHOD0(NTPSnippetsServiceLoaded, void()); 237 MOCK_METHOD0(NTPSnippetsServiceLoaded, void());
235 MOCK_METHOD0(NTPSnippetsServiceShutdown, void()); 238 MOCK_METHOD0(NTPSnippetsServiceShutdown, void());
236 MOCK_METHOD0(NTPSnippetsServiceDisabled, void()); 239 MOCK_METHOD0(NTPSnippetsServiceDisabled, void());
237 }; 240 };
238 241
242 class WaitForDBLoad : public NTPSnippetsServiceObserver {
243 public:
244 WaitForDBLoad(NTPSnippetsService* service) : service_(service) {
245 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
246 service_->AddObserver(this);
247 run_loop_.Run();
248 }
249
250 ~WaitForDBLoad() override {
251 service_->RemoveObserver(this);
252 }
253
254 private:
255 void NTPSnippetsServiceLoaded() override {
256 EXPECT_TRUE(service_->loaded());
257 run_loop_.Quit();
258 }
259
260 void NTPSnippetsServiceShutdown() override {}
261 void NTPSnippetsServiceDisabled() override {}
262
263 NTPSnippetsService* service_;
264 base::RunLoop run_loop_;
265
266 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
267 };
268
239 } // namespace 269 } // namespace
240 270
241 class NTPSnippetsServiceTest : public testing::Test { 271 class NTPSnippetsServiceTest : public testing::Test {
242 public: 272 public:
243 NTPSnippetsServiceTest() 273 NTPSnippetsServiceTest()
244 : fake_url_fetcher_factory_( 274 : fake_url_fetcher_factory_(
245 /*default_factory=*/&failing_url_fetcher_factory_), 275 /*default_factory=*/&failing_url_fetcher_factory_),
246 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, 276 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
247 google_apis::GetAPIKey().c_str())), 277 google_apis::GetAPIKey().c_str())),
248 pref_service_(new TestingPrefServiceSimple()), 278 pref_service_(new TestingPrefServiceSimple()),
249 signin_client_(new TestSigninClient(nullptr)), 279 signin_client_(new TestSigninClient(nullptr)),
250 account_tracker_(new AccountTrackerService()), 280 account_tracker_(new AccountTrackerService()),
251 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), 281 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
252 account_tracker_.get())), 282 account_tracker_.get())),
253 fake_token_service_(new FakeProfileOAuth2TokenService()) { 283 fake_token_service_(new FakeProfileOAuth2TokenService()) {
254 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); 284 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry());
255 // Since no SuggestionsService is injected in tests, we need to force the 285 // Since no SuggestionsService is injected in tests, we need to force the
256 // service to fetch from all hosts. 286 // service to fetch from all hosts.
257 base::CommandLine::ForCurrentProcess()->AppendSwitch( 287 base::CommandLine::ForCurrentProcess()->AppendSwitch(
258 switches::kDontRestrict); 288 switches::kDontRestrict);
289 EXPECT_TRUE(database_dir_.CreateUniqueTempDir());
259 } 290 }
260 291
261 ~NTPSnippetsServiceTest() override { 292 ~NTPSnippetsServiceTest() override {
262 if (service_) 293 if (service_)
263 service_->Shutdown(); 294 service_->Shutdown();
264 } 295 }
265 296
266 void SetUp() override { 297 void SetUp() override {
267 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 298 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
268 CreateSnippetsService(/*enabled=*/true); 299 CreateSnippetsService(/*enabled=*/true);
269 } 300 }
270 301
271 void CreateSnippetsService(bool enabled) { 302 void CreateSnippetsService(bool enabled) {
272 if (service_) 303 if (service_)
273 service_->Shutdown(); 304 service_->Shutdown();
274 305
275 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 306 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
276 base::ThreadTaskRunnerHandle::Get()); 307 base::ThreadTaskRunnerHandle::Get());
277 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 308 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
278 new net::TestURLRequestContextGetter(task_runner.get()); 309 new net::TestURLRequestContextGetter(task_runner.get());
279 310
311 // Delete the current service, so that the database is destroyed before we
312 // create the new one, otherwise opening the new database will fail.
313 service_.reset();
314
280 service_.reset(new NTPSnippetsService( 315 service_.reset(new NTPSnippetsService(
281 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr, 316 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr,
282 task_runner, std::string("fr"), &scheduler_, 317 std::string("fr"), &scheduler_,
283 base::WrapUnique(new NTPSnippetsFetcher( 318 base::WrapUnique(new NTPSnippetsFetcher(
284 fake_signin_manager_.get(), fake_token_service_.get(), 319 fake_signin_manager_.get(), fake_token_service_.get(),
285 std::move(request_context_getter), base::Bind(&ParseJson), 320 std::move(request_context_getter), base::Bind(&ParseJson),
286 /*is_stable_channel=*/true)), 321 /*is_stable_channel=*/true)),
287 /*image_fetcher=*/nullptr)); 322 /*image_fetcher=*/nullptr,
323 base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(),
324 task_runner))));
325 if (enabled)
326 WaitForDBLoad(service_.get());
288 } 327 }
289 328
290 protected: 329 protected:
291 const GURL& test_url() { return test_url_; } 330 const GURL& test_url() { return test_url_; }
292 NTPSnippetsService* service() { return service_.get(); } 331 NTPSnippetsService* service() { return service_.get(); }
293 MockScheduler& mock_scheduler() { return scheduler_; } 332 MockScheduler& mock_scheduler() { return scheduler_; }
294 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); } 333 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); }
295 334
296 // Provide the json to be returned by the fake fetcher. 335 // Provide the json to be returned by the fake fetcher.
297 void SetUpFetchResponse(const std::string& json) { 336 void SetUpFetchResponse(const std::string& json) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 370 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
332 std::unique_ptr<TestSigninClient> signin_client_; 371 std::unique_ptr<TestSigninClient> signin_client_;
333 std::unique_ptr<AccountTrackerService> account_tracker_; 372 std::unique_ptr<AccountTrackerService> account_tracker_;
334 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default. 373 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default.
335 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 374 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
336 std::unique_ptr<OAuth2TokenService> fake_token_service_; 375 std::unique_ptr<OAuth2TokenService> fake_token_service_;
337 MockScheduler scheduler_; 376 MockScheduler scheduler_;
338 // Last so that the dependencies are deleted after the service. 377 // Last so that the dependencies are deleted after the service.
339 std::unique_ptr<NTPSnippetsService> service_; 378 std::unique_ptr<NTPSnippetsService> service_;
340 379
380 base::ScopedTempDir database_dir_;
381
341 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 382 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
342 }; 383 };
343 384
344 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest { 385 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest {
345 public: 386 public:
346 void SetUp() override { 387 void SetUp() override {
347 ResetSyncServiceMock(); 388 ResetSyncServiceMock();
348 NTPSnippetsServiceTest::SetUp(); 389 NTPSnippetsServiceTest::SetUp();
349 } 390 }
350 }; 391 };
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 // The service should be ready again and load snippets. 922 // The service should be ready again and load snippets.
882 SetUpFetchResponse(GetTestJson({GetSnippet()})); 923 SetUpFetchResponse(GetTestJson({GetSnippet()}));
883 service()->OnStateChanged(); 924 service()->OnStateChanged();
884 base::RunLoop().RunUntilIdle(); 925 base::RunLoop().RunUntilIdle();
885 EXPECT_FALSE(service()->snippets().empty()); 926 EXPECT_FALSE(service()->snippets().empty());
886 927
887 service()->RemoveObserver(&mock_observer); 928 service()->RemoveObserver(&mock_observer);
888 } 929 }
889 930
890 } // namespace ntp_snippets 931 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | components/ntp_snippets/proto/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698