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

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: fix test memleaks 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/pref_names.h » ('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 service_->AddObserver(this);
246 if (!service_->loaded())
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();
295
296 // We need to run the message loop after deleting the database, because
297 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task
298 // runner. Without this, we'd get reports of memory leaks.
299 service_.reset();
300 base::RunLoop().RunUntilIdle();
264 } 301 }
265 302
266 void SetUp() override { 303 void SetUp() override {
267 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 304 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
268 CreateSnippetsService(/*enabled=*/true); 305 CreateSnippetsService(/*enabled=*/true);
269 } 306 }
270 307
271 void CreateSnippetsService(bool enabled) { 308 void CreateSnippetsService(bool enabled) {
272 if (service_) 309 if (service_)
273 service_->Shutdown(); 310 service_->Shutdown();
274 311
275 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 312 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
276 base::ThreadTaskRunnerHandle::Get()); 313 base::ThreadTaskRunnerHandle::Get());
277 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 314 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
278 new net::TestURLRequestContextGetter(task_runner.get()); 315 new net::TestURLRequestContextGetter(task_runner.get());
279 316
317 // Delete the current service, so that the database is destroyed before we
318 // create the new one, otherwise opening the new database will fail.
319 service_.reset();
320
280 service_.reset(new NTPSnippetsService( 321 service_.reset(new NTPSnippetsService(
281 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr, 322 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr,
282 task_runner, std::string("fr"), &scheduler_, 323 std::string("fr"), &scheduler_,
283 base::WrapUnique(new NTPSnippetsFetcher( 324 base::WrapUnique(new NTPSnippetsFetcher(
284 fake_signin_manager_.get(), fake_token_service_.get(), 325 fake_signin_manager_.get(), fake_token_service_.get(),
285 std::move(request_context_getter), base::Bind(&ParseJson), 326 std::move(request_context_getter), base::Bind(&ParseJson),
286 /*is_stable_channel=*/true)), 327 /*is_stable_channel=*/true)),
287 /*image_fetcher=*/nullptr)); 328 /*image_fetcher=*/nullptr,
329 base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(),
330 task_runner))));
331 if (enabled)
332 WaitForDBLoad(service_.get());
288 } 333 }
289 334
290 protected: 335 protected:
291 const GURL& test_url() { return test_url_; } 336 const GURL& test_url() { return test_url_; }
292 NTPSnippetsService* service() { return service_.get(); } 337 NTPSnippetsService* service() { return service_.get(); }
293 MockScheduler& mock_scheduler() { return scheduler_; } 338 MockScheduler& mock_scheduler() { return scheduler_; }
294 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); } 339 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); }
295 340
296 // Provide the json to be returned by the fake fetcher. 341 // Provide the json to be returned by the fake fetcher.
297 void SetUpFetchResponse(const std::string& json) { 342 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_; 376 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
332 std::unique_ptr<TestSigninClient> signin_client_; 377 std::unique_ptr<TestSigninClient> signin_client_;
333 std::unique_ptr<AccountTrackerService> account_tracker_; 378 std::unique_ptr<AccountTrackerService> account_tracker_;
334 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default. 379 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default.
335 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 380 std::unique_ptr<SigninManagerBase> fake_signin_manager_;
336 std::unique_ptr<OAuth2TokenService> fake_token_service_; 381 std::unique_ptr<OAuth2TokenService> fake_token_service_;
337 MockScheduler scheduler_; 382 MockScheduler scheduler_;
338 // Last so that the dependencies are deleted after the service. 383 // Last so that the dependencies are deleted after the service.
339 std::unique_ptr<NTPSnippetsService> service_; 384 std::unique_ptr<NTPSnippetsService> service_;
340 385
386 base::ScopedTempDir database_dir_;
387
341 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 388 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
342 }; 389 };
343 390
344 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest { 391 class NTPSnippetsServiceWithSyncTest : public NTPSnippetsServiceTest {
345 public: 392 public:
346 void SetUp() override { 393 void SetUp() override {
347 ResetSyncServiceMock(); 394 ResetSyncServiceMock();
348 NTPSnippetsServiceTest::SetUp(); 395 NTPSnippetsServiceTest::SetUp();
349 } 396 }
350 }; 397 };
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
881 // The service should be ready again and load snippets. 928 // The service should be ready again and load snippets.
882 SetUpFetchResponse(GetTestJson({GetSnippet()})); 929 SetUpFetchResponse(GetTestJson({GetSnippet()}));
883 service()->OnStateChanged(); 930 service()->OnStateChanged();
884 base::RunLoop().RunUntilIdle(); 931 base::RunLoop().RunUntilIdle();
885 EXPECT_FALSE(service()->snippets().empty()); 932 EXPECT_FALSE(service()->snippets().empty());
886 933
887 service()->RemoveObserver(&mock_observer); 934 service()->RemoveObserver(&mock_observer);
888 } 935 }
889 936
890 } // namespace ntp_snippets 937 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | components/ntp_snippets/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698