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

Side by Side Diff: chrome/browser/chrome_content_browser_client_unittest.cc

Issue 2768883002: Change MockBrowsingDataRemover to MockBrowsingDataRemoverDelegate (Closed)
Patch Set: Fixed the comment. Created 3 years, 9 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/chrome_content_browser_client.h" 5 #include "chrome/browser/chrome_content_browser_client.h"
6 6
7 #include <list> 7 #include <list>
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/field_trial.h" 16 #include "base/metrics/field_trial.h"
17 #include "base/run_loop.h"
17 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "chrome/browser/browsing_data/browsing_data_helper.h" 21 #include "chrome/browser/browsing_data/browsing_data_helper.h"
21 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 22 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
22 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" 23 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
23 #include "chrome/browser/browsing_data/mock_browsing_data_remover.h" 24 #include "chrome/browser/browsing_data/mock_browsing_data_remover_delegate.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h" 25 #include "chrome/browser/search_engines/template_url_service_factory.h"
25 #include "chrome/test/base/testing_profile.h" 26 #include "chrome/test/base/testing_profile.h"
26 #include "components/content_settings/core/browser/host_content_settings_map.h" 27 #include "components/content_settings/core/browser/host_content_settings_map.h"
27 #include "components/search_engines/template_url_service.h" 28 #include "components/search_engines/template_url_service.h"
28 #include "components/variations/entropy_provider.h" 29 #include "components/variations/entropy_provider.h"
29 #include "components/variations/variations_associated_data.h" 30 #include "components/variations/variations_associated_data.h"
30 #include "components/version_info/version_info.h" 31 #include "components/version_info/version_info.h"
31 #include "content/public/browser/browsing_data_filter_builder.h" 32 #include "content/public/browser/browsing_data_filter_builder.h"
32 #include "content/public/browser/navigation_controller.h" 33 #include "content/public/browser/navigation_controller.h"
33 #include "content/public/browser/navigation_entry.h" 34 #include "content/public/browser/navigation_entry.h"
35 #include "content/public/browser/storage_partition.h"
34 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/content_switches.h" 37 #include "content/public/common/content_switches.h"
36 #include "content/public/test/test_browser_thread_bundle.h" 38 #include "content/public/test/test_browser_thread_bundle.h"
37 #include "media/media_features.h" 39 #include "media/media_features.h"
38 #include "testing/gmock/include/gmock/gmock.h" 40 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
40 #include "url/gurl.h" 42 #include "url/gurl.h"
41 43
42 #if !defined(OS_ANDROID) 44 #if !defined(OS_ANDROID)
43 #include "chrome/browser/ui/browser.h" 45 #include "chrome/browser/ui/browser.h"
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 343
342 } // namespace content 344 } // namespace content
343 #endif // !defined(OS_ANDROID) 345 #endif // !defined(OS_ANDROID)
344 346
345 namespace { 347 namespace {
346 348
347 // Tests for ChromeContentBrowserClient::ClearSiteData(). 349 // Tests for ChromeContentBrowserClient::ClearSiteData().
348 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test { 350 class ChromeContentBrowserClientClearSiteDataTest : public testing::Test {
349 public: 351 public:
350 void SetUp() override { 352 void SetUp() override {
351 BrowsingDataRemoverFactory::GetInstance()->SetTestingFactoryAndUse( 353 BrowsingDataRemoverFactory::GetForBrowserContext(profile())
352 &profile_, &ChromeContentBrowserClientClearSiteDataTest::GetRemover); 354 ->SetEmbedderDelegate(
355 base::MakeUnique<MockBrowsingDataRemoverDelegate>());
356 run_loop_.reset(new base::RunLoop());
353 } 357 }
354 358
355 content::BrowserContext* profile() { return &profile_; } 359 content::BrowserContext* profile() { return &profile_; }
356 360
357 MockBrowsingDataRemover* remover() { 361 MockBrowsingDataRemoverDelegate* delegate() {
358 return static_cast<MockBrowsingDataRemover*>( 362 return static_cast<MockBrowsingDataRemoverDelegate*>(
359 BrowsingDataRemoverFactory::GetForBrowserContext(&profile_)); 363 BrowsingDataRemoverFactory::GetForBrowserContext(profile())
364 ->GetEmbedderDelegate());
360 } 365 }
361 366
362 void SetClearingFinished(bool finished) { finished_ = finished; } 367 void OnClearingFinished() { run_loop_->Quit(); }
363 368
364 bool IsClearingFinished() { return finished_; } 369 void WaitForClearingFinished() {
370 run_loop_->Run();
371 run_loop_.reset(new base::RunLoop());
372 }
365 373
366 private: 374 private:
367 static std::unique_ptr<KeyedService> GetRemover( 375 std::unique_ptr<base::RunLoop> run_loop_;
368 content::BrowserContext* context) {
369 return base::WrapUnique(new MockBrowsingDataRemover(context));
370 }
371
372 content::TestBrowserThreadBundle thread_bundle_; 376 content::TestBrowserThreadBundle thread_bundle_;
373 TestingProfile profile_; 377 TestingProfile profile_;
374 bool finished_;
375 }; 378 };
376 379
377 // Tests that the parameters to ClearBrowsingData() are translated to 380 // Tests that the parameters to ClearBrowsingData() are translated to
378 // the correct BrowsingDataRemover::RemoveInternal() operation. The fourth 381 // the correct BrowsingDataRemover::RemoveInternal() operation. The fourth
379 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains 382 // parameter, |filter_builder|, is tested in detail in the RegistrableDomains
380 // test below. 383 // test below.
381 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) { 384 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Parameters) {
382 ChromeContentBrowserClient client; 385 ChromeContentBrowserClient client;
383 386
384 struct TestCase { 387 struct TestCase {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 // calls. In the latter case, the removal mask will be split into two 430 // calls. In the latter case, the removal mask will be split into two
428 // parts - one for the origin deletion and one for the registrable domain. 431 // parts - one for the origin deletion and one for the registrable domain.
429 const int domain_scoped_types = 432 const int domain_scoped_types =
430 BrowsingDataRemover::DATA_TYPE_COOKIES | 433 BrowsingDataRemover::DATA_TYPE_COOKIES |
431 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS | 434 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
432 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA; 435 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
433 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types; 436 int registrable_domain_deletion_mask = test_case.mask & domain_scoped_types;
434 int origin_deletion_mask = test_case.mask & ~domain_scoped_types; 437 int origin_deletion_mask = test_case.mask & ~domain_scoped_types;
435 438
436 if (registrable_domain_deletion_mask) { 439 if (registrable_domain_deletion_mask) {
437 remover()->ExpectCallDontCareAboutFilterBuilder( 440 delegate()->ExpectCallDontCareAboutFilterBuilder(
438 base::Time(), base::Time::Max(), 441 base::Time(), base::Time::Max(), registrable_domain_deletion_mask,
439 registrable_domain_deletion_mask, all_origin_types); 442 all_origin_types);
440 } 443 }
441 444
442 if (origin_deletion_mask) { 445 if (origin_deletion_mask) {
443 remover()->ExpectCallDontCareAboutFilterBuilder( 446 delegate()->ExpectCallDontCareAboutFilterBuilder(
444 base::Time(), base::Time::Max(), 447 base::Time(), base::Time::Max(), origin_deletion_mask,
445 origin_deletion_mask, all_origin_types); 448 all_origin_types);
446 } 449 }
447 450
448 SetClearingFinished(false);
449 client.ClearSiteData( 451 client.ClearSiteData(
450 profile(), url::Origin(GURL("https://www.example.com")), 452 profile(), url::Origin(GURL("https://www.example.com")),
451 test_case.cookies, test_case.storage, test_case.cache, 453 test_case.cookies, test_case.storage, test_case.cache,
452 base::Bind( 454 base::Bind(
453 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 455 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
454 base::Unretained(this), true)); 456 base::Unretained(this)));
455 EXPECT_TRUE(IsClearingFinished()); 457 WaitForClearingFinished();
456 458
457 remover()->VerifyAndClearExpectations(); 459 delegate()->VerifyAndClearExpectations();
458 } 460 }
459 } 461 }
460 462
461 // Tests that ClearBrowsingData() called for an origin deletes cookies in the 463 // Tests that ClearBrowsingData() called for an origin deletes cookies in the
462 // scope of the registrable domain corresponding to that origin, while cache 464 // scope of the registrable domain corresponding to that origin, while cache
463 // is deleted for that exact origin. 465 // is deleted for that exact origin.
464 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) { 466 TEST_F(ChromeContentBrowserClientClearSiteDataTest, RegistrableDomains) {
465 ChromeContentBrowserClient client; 467 ChromeContentBrowserClient client;
466 468
467 struct TestCase { 469 struct TestCase {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 }; 504 };
503 505
504 for (const TestCase& test_case : test_cases) { 506 for (const TestCase& test_case : test_cases) {
505 SCOPED_TRACE(test_case.origin); 507 SCOPED_TRACE(test_case.origin);
506 508
507 std::unique_ptr<BrowsingDataFilterBuilder> 509 std::unique_ptr<BrowsingDataFilterBuilder>
508 registrable_domain_filter_builder(BrowsingDataFilterBuilder::Create( 510 registrable_domain_filter_builder(BrowsingDataFilterBuilder::Create(
509 BrowsingDataFilterBuilder::WHITELIST)); 511 BrowsingDataFilterBuilder::WHITELIST));
510 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain); 512 registrable_domain_filter_builder->AddRegisterableDomain(test_case.domain);
511 513
512 remover()->ExpectCall( 514 delegate()->ExpectCall(
513 base::Time(), base::Time::Max(), 515 base::Time(), base::Time::Max(),
514 BrowsingDataRemover::DATA_TYPE_COOKIES | 516 BrowsingDataRemover::DATA_TYPE_COOKIES |
515 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS | 517 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS |
516 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA, 518 ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA,
517 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES, 519 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
518 std::move(registrable_domain_filter_builder)); 520 *registrable_domain_filter_builder);
519 521
520 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder( 522 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder(
521 BrowsingDataFilterBuilder::Create( 523 BrowsingDataFilterBuilder::Create(
522 BrowsingDataFilterBuilder::WHITELIST)); 524 BrowsingDataFilterBuilder::WHITELIST));
523 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin))); 525 origin_filter_builder->AddOrigin(url::Origin(GURL(test_case.origin)));
524 526
525 remover()->ExpectCall(base::Time(), base::Time::Max(), 527 delegate()->ExpectCall(base::Time(), base::Time::Max(),
526 BrowsingDataRemover::DATA_TYPE_CACHE, 528 BrowsingDataRemover::DATA_TYPE_CACHE,
527 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES, 529 ChromeBrowsingDataRemoverDelegate::ALL_ORIGIN_TYPES,
528 std::move(origin_filter_builder)); 530 *origin_filter_builder);
529 531
530 SetClearingFinished(false);
531 client.ClearSiteData( 532 client.ClearSiteData(
532 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */, 533 profile(), url::Origin(GURL(test_case.origin)), true /* cookies */,
533 false /* storage */, true /* cache */, 534 false /* storage */, true /* cache */,
534 base::Bind( 535 base::Bind(
535 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 536 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
536 base::Unretained(this), true)); 537 base::Unretained(this)));
537 EXPECT_TRUE(IsClearingFinished()); 538 WaitForClearingFinished();
538 539
539 remover()->VerifyAndClearExpectations(); 540 delegate()->VerifyAndClearExpectations();
540 } 541 }
541 } 542 }
542 543
543 // Tests that we always wait for all scheduled BrowsingDataRemover tasks and 544 // Tests that we always wait for all scheduled BrowsingDataRemover tasks and
544 // that BrowsingDataRemoverObserver never leaks. 545 // that BrowsingDataRemoverObserver never leaks.
545 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) { 546 TEST_F(ChromeContentBrowserClientClearSiteDataTest, Tasks) {
546 ChromeContentBrowserClient client; 547 ChromeContentBrowserClient client;
547 url::Origin origin(GURL("https://www.example.com")); 548 url::Origin origin(GURL("https://www.example.com"));
548 549
549 // No removal tasks. 550 // No removal tasks.
550 SetClearingFinished(false);
551 client.ClearSiteData( 551 client.ClearSiteData(
552 profile(), origin, false /* cookies */, false /* storage */, 552 profile(), origin, false /* cookies */, false /* storage */,
553 false /* cache */, 553 false /* cache */,
554 base::Bind( 554 base::Bind(
555 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 555 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
556 base::Unretained(this), true)); 556 base::Unretained(this)));
557 EXPECT_TRUE(IsClearingFinished()); 557 WaitForClearingFinished();
558 558
559 // One removal task: deleting cookies with a domain filter. 559 // One removal task: deleting cookies with a domain filter.
560 SetClearingFinished(false);
561 client.ClearSiteData( 560 client.ClearSiteData(
562 profile(), origin, true /* cookies */, false /* storage */, 561 profile(), origin, true /* cookies */, false /* storage */,
563 false /* cache */, 562 false /* cache */,
564 base::Bind( 563 base::Bind(
565 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 564 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
566 base::Unretained(this), true)); 565 base::Unretained(this)));
567 EXPECT_TRUE(IsClearingFinished()); 566 WaitForClearingFinished();
568 567
569 // One removal task: deleting cache with a domain filter. 568 // One removal task: deleting cache with a domain filter.
570 SetClearingFinished(false);
571 client.ClearSiteData( 569 client.ClearSiteData(
572 profile(), origin, false /* cookies */, false /* storage */, 570 profile(), origin, false /* cookies */, false /* storage */,
573 true /* cache */, 571 true /* cache */,
574 base::Bind( 572 base::Bind(
575 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 573 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
576 base::Unretained(this), true)); 574 base::Unretained(this)));
577 EXPECT_TRUE(IsClearingFinished()); 575 WaitForClearingFinished();
578 576
579 // Two removal tasks, with domain and origin filters respectively. 577 // Two removal tasks, with domain and origin filters respectively.
580 SetClearingFinished(false);
581 client.ClearSiteData( 578 client.ClearSiteData(
582 profile(), origin, true /* cookies */, false /* storage */, 579 profile(), origin, true /* cookies */, false /* storage */,
583 true /* cache */, 580 true /* cache */,
584 base::Bind( 581 base::Bind(
585 &ChromeContentBrowserClientClearSiteDataTest::SetClearingFinished, 582 &ChromeContentBrowserClientClearSiteDataTest::OnClearingFinished,
586 base::Unretained(this), true)); 583 base::Unretained(this)));
587 EXPECT_TRUE(IsClearingFinished()); 584 WaitForClearingFinished();
588 } 585 }
589 586
590 } // namespace 587 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698