Make ChromeBrowsingDataRemoverDelegateTest not call Profile::GetRequestContext.

Profile::GetRequestContext won't work with the network service enabled.

Bug: 966633
Change-Id: Ic50f7104884972214796ea059014e94a0e3617f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1656929
Reviewed-by: John Abd-El-Malek <[email protected]>
Commit-Queue: Robbie McElrath <[email protected]>
Cr-Commit-Position: refs/heads/master@{#668883}
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS
index 84b5578..48cd7fc 100644
--- a/chrome/browser/DEPS
+++ b/chrome/browser/DEPS
@@ -230,4 +230,8 @@
   "input_method_manager_impl.cc": [
     "+ash/keyboard/ui/keyboard_controller.h",
   ],
+  "chrome_browsing_data_remover_delegate_unittest.cc": [
+    "+services/network/network_context.h",
+    "+services/network/network_service.h",
+  ],
 }
diff --git a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_unittest.cc b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_unittest.cc
index 1f957e34..51f62650 100644
--- a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_unittest.cc
+++ b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate_unittest.cc
@@ -86,6 +86,7 @@
 #include "content/public/browser/browser_task_traits.h"
 #include "content/public/browser/browsing_data_filter_builder.h"
 #include "content/public/browser/browsing_data_remover.h"
+#include "content/public/browser/network_service_instance.h"
 #include "content/public/test/browsing_data_remover_test_util.h"
 #include "content/public/test/mock_download_manager.h"
 #include "content/public/test/test_browser_thread_bundle.h"
@@ -94,8 +95,9 @@
 #include "net/http/http_transaction_factory.h"
 #include "net/net_buildflags.h"
 #include "net/url_request/url_request_context.h"
-#include "net/url_request/url_request_context_getter.h"
 #include "net/url_request/url_request_test_util.h"
+#include "services/network/network_context.h"
+#include "services/network/network_service.h"
 #include "services/network/public/mojom/cookie_manager.mojom.h"
 #include "third_party/skia/include/core/SkBitmap.h"
 #include "ui/gfx/favicon_size.h"
@@ -579,21 +581,6 @@
   DISALLOW_COPY_AND_ASSIGN(RemovePasswordsTester);
 };
 
-// Implementation of the TestingProfile that has the neccessary services for
-// BrowsingDataRemover.
-class BrowsingDataTestingProfile : public TestingProfile {
- public:
-  BrowsingDataTestingProfile() {}
-  ~BrowsingDataTestingProfile() override {}
-
-  content::SSLHostStateDelegate* GetSSLHostStateDelegate() override {
-    return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
-  }
-
- private:
-  DISALLOW_COPY_AND_ASSIGN(BrowsingDataTestingProfile);
-};
-
 class RemovePermissionPromptCountsTest {
  public:
   explicit RemovePermissionPromptCountsTest(TestingProfile* profile)
@@ -1019,28 +1006,25 @@
 
 class ClearReportingCacheTester {
  public:
-  ClearReportingCacheTester(TestingProfile* profile, bool create_service)
-      : profile_(profile) {
+  ClearReportingCacheTester(network::NetworkContext* network_context,
+                            bool create_service)
+      : url_request_context_(network_context->url_request_context()) {
     if (create_service)
       service_ = std::make_unique<MockReportingService>();
 
-    net::URLRequestContext* request_context =
-        profile_->GetRequestContext()->GetURLRequestContext();
-    old_service_ = request_context->reporting_service();
-    request_context->set_reporting_service(service_.get());
+    old_service_ = url_request_context_->reporting_service();
+    url_request_context_->set_reporting_service(service_.get());
   }
 
   ~ClearReportingCacheTester() {
-    net::URLRequestContext* request_context =
-        profile_->GetRequestContext()->GetURLRequestContext();
-    DCHECK_EQ(service_.get(), request_context->reporting_service());
-    request_context->set_reporting_service(old_service_);
+    DCHECK_EQ(service_.get(), url_request_context_->reporting_service());
+    url_request_context_->set_reporting_service(old_service_);
   }
 
   const MockReportingService& mock() { return *service_; }
 
  private:
-  TestingProfile* profile_;
+  net::URLRequestContext* url_request_context_;
   std::unique_ptr<MockReportingService> service_;
   net::ReportingService* old_service_;
 };
@@ -1092,28 +1076,25 @@
 
 class ClearNetworkErrorLoggingTester {
  public:
-  ClearNetworkErrorLoggingTester(TestingProfile* profile, bool create_service)
-      : profile_(profile) {
+  ClearNetworkErrorLoggingTester(network::NetworkContext* network_context,
+                                 bool create_service)
+      : url_request_context_(network_context->url_request_context()) {
     if (create_service)
       service_ = std::make_unique<MockNetworkErrorLoggingService>();
 
-    net::URLRequestContext* request_context =
-        profile_->GetRequestContext()->GetURLRequestContext();
-
-    request_context->set_network_error_logging_service(service_.get());
+    url_request_context_->set_network_error_logging_service(service_.get());
   }
 
   ~ClearNetworkErrorLoggingTester() {
-    net::URLRequestContext* request_context =
-        profile_->GetRequestContext()->GetURLRequestContext();
-    DCHECK_EQ(service_.get(), request_context->network_error_logging_service());
-    request_context->set_network_error_logging_service(nullptr);
+    DCHECK_EQ(service_.get(),
+              url_request_context_->network_error_logging_service());
+    url_request_context_->set_network_error_logging_service(nullptr);
   }
 
   const MockNetworkErrorLoggingService& mock() { return *service_; }
 
  private:
-  TestingProfile* profile_;
+  net::URLRequestContext* url_request_context_;
   std::unique_ptr<MockNetworkErrorLoggingService> service_;
 
   DISALLOW_COPY_AND_ASSIGN(ClearNetworkErrorLoggingTester);
@@ -1143,6 +1124,17 @@
       : profile_(new BrowsingDataRemoverTestingProfile()) {
     remover_ = content::BrowserContext::GetBrowsingDataRemover(profile_.get());
 
+    // Make sure the Network Service is started before making a NetworkContext.
+    content::GetNetworkService();
+    thread_bundle_.RunUntilIdle();
+
+    auto network_context = std::make_unique<network::NetworkContext>(
+        network::NetworkService::GetNetworkServiceForTesting(),
+        mojo::MakeRequest(&network_context_ptr_),
+        network::mojom::NetworkContextParams::New());
+    network_context_ = network_context.get();
+    profile_->SetNetworkContext(std::move(network_context));
+
     ProtocolHandlerRegistryFactory::GetInstance()->SetTestingFactory(
         profile_.get(), base::BindRepeating(&BuildProtocolHandlerRegistry));
 
@@ -1223,6 +1215,8 @@
     return remover_->GetLastUsedOriginTypeMask();
   }
 
+  network::NetworkContext* network_context() { return network_context_; }
+
   TestingProfile* GetProfile() {
     return profile_.get();
   }
@@ -1239,6 +1233,8 @@
   content::BrowsingDataRemover* remover_;
 
   content::TestBrowserThreadBundle thread_bundle_;
+  network::mojom::NetworkContextPtr network_context_ptr_;
+  network::NetworkContext* network_context_;
   std::unique_ptr<TestingProfile> profile_;
 
   DISALLOW_COPY_AND_ASSIGN(ChromeBrowsingDataRemoverDelegateTest);
@@ -2419,9 +2415,8 @@
 // Test that removing passwords clears HTTP auth data.
 TEST_F(ChromeBrowsingDataRemoverDelegateTest,
        ClearHttpAuthCache_RemovePasswords) {
-  net::HttpNetworkSession* http_session = GetProfile()
-                                              ->GetRequestContext()
-                                              ->GetURLRequestContext()
+  net::HttpNetworkSession* http_session = network_context()
+                                              ->url_request_context()
                                               ->http_transaction_factory()
                                               ->GetSession();
   DCHECK(http_session);
@@ -2761,7 +2756,7 @@
 
 #if BUILDFLAG(ENABLE_REPORTING)
 TEST_F(ChromeBrowsingDataRemoverDelegateTest, ReportingCache_NoService) {
-  ClearReportingCacheTester tester(GetProfile(), false);
+  ClearReportingCacheTester tester(network_context(), false);
 
   BlockUntilBrowsingDataRemoved(
       base::Time(), base::Time::Max(),
@@ -2772,7 +2767,7 @@
 }
 
 TEST_F(ChromeBrowsingDataRemoverDelegateTest, ReportingCache) {
-  ClearReportingCacheTester tester(GetProfile(), true);
+  ClearReportingCacheTester tester(network_context(), true);
 
   BlockUntilBrowsingDataRemoved(
       base::Time(), base::Time::Max(),
@@ -2790,7 +2785,7 @@
 // a filterable datatype.
 TEST_F(ChromeBrowsingDataRemoverDelegateTest,
        DISABLED_ReportingCache_WithFilter) {
-  ClearReportingCacheTester tester(GetProfile(), true);
+  ClearReportingCacheTester tester(network_context(), true);
 
   std::unique_ptr<BrowsingDataFilterBuilder> builder(
       BrowsingDataFilterBuilder::Create(BrowsingDataFilterBuilder::WHITELIST));
@@ -2809,7 +2804,7 @@
 }
 
 TEST_F(ChromeBrowsingDataRemoverDelegateTest, NetworkErrorLogging_NoDelegate) {
-  ClearNetworkErrorLoggingTester tester(GetProfile(), false);
+  ClearNetworkErrorLoggingTester tester(network_context(), false);
 
   BlockUntilBrowsingDataRemoved(
       base::Time(), base::Time::Max(),
@@ -2821,7 +2816,7 @@
 
 // This would use an origin filter, but history isn't yet filterable.
 TEST_F(ChromeBrowsingDataRemoverDelegateTest, NetworkErrorLogging_History) {
-  ClearNetworkErrorLoggingTester tester(GetProfile(), true);
+  ClearNetworkErrorLoggingTester tester(network_context(), true);
 
   BlockUntilBrowsingDataRemoved(
       base::Time(), base::Time::Max(),