From e9448c81348c4d10000654c29ac2c83a6cd76086 Mon Sep 17 00:00:00 2001 From: jeremyim Date: Wed, 25 Feb 2015 15:28:21 -0800 Subject: [PATCH] Create fluent Builder for DataReductionProxyTestContext. This simplifies the way in which a DRP TestContext is created, in that optional parameters are specified with explicit methods on the Builder. BUG=461914 Review URL: https://codereview.chromium.org/959913002 Cr-Commit-Position: refs/heads/master@{#318142} --- ...ata_reduction_proxy_chrome_settings_unittest.cc | 56 +++--- ...ction_proxy_debug_resource_throttle_unittest.cc | 9 +- ...ata_reduction_proxy_bypass_protocol_unittest.cc | 18 +- .../data_reduction_proxy_config_unittest.cc | 23 +-- .../data_reduction_proxy_interceptor_unittest.cc | 16 +- .../data_reduction_proxy_metrics_unittest.cc | 7 +- ...ta_reduction_proxy_network_delegate_unittest.cc | 31 ++-- ...ata_reduction_proxy_request_options_unittest.cc | 19 +- .../data_reduction_proxy_settings_test_utils.cc | 23 +-- .../data_reduction_proxy_settings_unittest.cc | 78 ++++---- .../browser/data_reduction_proxy_test_utils.cc | 200 +++++++++++++++------ .../core/browser/data_reduction_proxy_test_utils.h | 127 ++++++++----- .../data_reduction_proxy_usage_stats_unittest.cc | 31 ++-- 13 files changed, 394 insertions(+), 244 deletions(-) diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_unittest.cc b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_unittest.cc index 326095d1ca36..9d0d21343b5b 100644 --- a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_unittest.cc +++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_unittest.cc @@ -23,30 +23,33 @@ class DataReductionProxyChromeSettingsTest : public testing::Test { void SetUp() override { drp_chrome_settings_ = make_scoped_ptr(new DataReductionProxyChromeSettings()); - test_context_.reset(new data_reduction_proxy::DataReductionProxyTestContext( - data_reduction_proxy::DataReductionProxyParams::kAllowed | - data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed | - data_reduction_proxy::DataReductionProxyParams::kPromoAllowed, - data_reduction_proxy::TestDataReductionProxyParams::HAS_EVERYTHING & - ~data_reduction_proxy::TestDataReductionProxyParams:: - HAS_DEV_ORIGIN & - ~data_reduction_proxy::TestDataReductionProxyParams:: - HAS_DEV_FALLBACK_ORIGIN, - data_reduction_proxy::DataReductionProxyTestContext::USE_MOCK_CONFIG | - data_reduction_proxy::DataReductionProxyTestContext:: - SKIP_SETTINGS_INITIALIZATION)); + test_context_ = + data_reduction_proxy::DataReductionProxyTestContext::Builder() + .WithParamsFlags( + data_reduction_proxy::DataReductionProxyParams::kAllowed | + data_reduction_proxy::DataReductionProxyParams:: + kFallbackAllowed | + data_reduction_proxy::DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + data_reduction_proxy::TestDataReductionProxyParams:: + HAS_EVERYTHING & + ~data_reduction_proxy::TestDataReductionProxyParams:: + HAS_DEV_ORIGIN & + ~data_reduction_proxy::TestDataReductionProxyParams:: + HAS_DEV_FALLBACK_ORIGIN) + .WithMockConfig() + .SkipSettingsInitialization() + .Build(); config_ = test_context_->mock_config(); drp_chrome_settings_->ResetConfigForTest(config_); dict_ = make_scoped_ptr(new base::DictionaryValue()); - mock_pref_service_ = make_scoped_ptr(new TestingPrefServiceSimple()); - PrefRegistrySimple* registry = mock_pref_service_->registry(); + PrefRegistrySimple* registry = test_context_->pref_service()->registry(); registry->RegisterDictionaryPref(prefs::kProxy); } scoped_ptr drp_chrome_settings_; scoped_ptr dict_; - scoped_ptr mock_pref_service_; scoped_ptr test_context_; data_reduction_proxy::MockDataReductionProxyConfig* config_; }; @@ -54,47 +57,48 @@ class DataReductionProxyChromeSettingsTest : public testing::Test { TEST_F(DataReductionProxyChromeSettingsTest, MigrateEmptyProxy) { EXPECT_CALL(*config_, ContainsDataReductionProxy(_)).Times(0); drp_chrome_settings_->MigrateDataReductionProxyOffProxyPrefs( - mock_pref_service_.get()); + test_context_->pref_service()); - EXPECT_EQ(NULL, mock_pref_service_->GetUserPref(prefs::kProxy)); + EXPECT_EQ(NULL, test_context_->pref_service()->GetUserPref(prefs::kProxy)); } TEST_F(DataReductionProxyChromeSettingsTest, MigrateSystemProxy) { dict_->SetString("mode", "system"); - mock_pref_service_->Set(prefs::kProxy, *dict_.get()); + test_context_->pref_service()->Set(prefs::kProxy, *dict_.get()); EXPECT_CALL(*config_, ContainsDataReductionProxy(_)).Times(0); drp_chrome_settings_->MigrateDataReductionProxyOffProxyPrefs( - mock_pref_service_.get()); + test_context_->pref_service()); - EXPECT_EQ(NULL, mock_pref_service_->GetUserPref(prefs::kProxy)); + EXPECT_EQ(NULL, test_context_->pref_service()->GetUserPref(prefs::kProxy)); } TEST_F(DataReductionProxyChromeSettingsTest, MigrateDataReductionProxy) { dict_->SetString("mode", "fixed_servers"); dict_->SetString("server", "http=https://proxy.googlezip.net"); - mock_pref_service_->Set(prefs::kProxy, *dict_.get()); + test_context_->pref_service()->Set(prefs::kProxy, *dict_.get()); EXPECT_CALL(*config_, ContainsDataReductionProxy(_)).Times(1) .WillOnce(Return(true)); drp_chrome_settings_->MigrateDataReductionProxyOffProxyPrefs( - mock_pref_service_.get()); + test_context_->pref_service()); - EXPECT_EQ(NULL, mock_pref_service_->GetUserPref(prefs::kProxy)); + EXPECT_EQ(NULL, test_context_->pref_service()->GetUserPref(prefs::kProxy)); } TEST_F(DataReductionProxyChromeSettingsTest, MigrateIgnoreOtherProxy) { dict_->SetString("mode", "fixed_servers"); dict_->SetString("server", "http=https://youtube.com"); - mock_pref_service_->Set(prefs::kProxy, *dict_.get()); + test_context_->pref_service()->Set(prefs::kProxy, *dict_.get()); EXPECT_CALL(*config_, ContainsDataReductionProxy(_)).Times(1) .WillOnce(Return(false)); drp_chrome_settings_->MigrateDataReductionProxyOffProxyPrefs( - mock_pref_service_.get()); + test_context_->pref_service()); base::DictionaryValue* value = - (base::DictionaryValue*)mock_pref_service_->GetUserPref(prefs::kProxy); + (base::DictionaryValue*)test_context_->pref_service()->GetUserPref( + prefs::kProxy); std::string mode; EXPECT_TRUE(value->GetString("mode", &mode)); EXPECT_EQ("fixed_servers", mode); diff --git a/components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle_unittest.cc b/components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle_unittest.cc index c107a30a5eae..dbf5bb5ea055 100644 --- a/components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle_unittest.cc +++ b/components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle_unittest.cc @@ -65,10 +65,11 @@ class DataReductionProxyDebugResourceThrottleTest : public testing::Test { context_.set_job_factory(&test_job_factory_); - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed) + .WithParamsDefinitions(TestDataReductionProxyParams::HAS_EVERYTHING) + .Build(); request_ = context_.CreateRequest(GURL("http://www.google.com/"), net::IDLE, &delegate_, NULL); ui_service_.reset(new ContentDataReductionProxyDebugUIService( diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc index 149029e4b63c..75e40d87f228 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc @@ -90,14 +90,16 @@ class DataReductionProxyProtocolTest : public testing::Test { } void SetUp() override { - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .Build(); network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); net::NetworkChangeNotifier::SetTestNotificationsOnly(true); test_context_->RunUntilIdle(); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc index 07a4e3345efe..10827bdb5912 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc @@ -38,16 +38,19 @@ class DataReductionProxyConfigTest : public testing::Test { ~DataReductionProxyConfigTest() override {} void SetUp() override { - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::USE_MOCK_CONFIG | - DataReductionProxyTestContext::USE_TEST_CONFIGURATOR | - DataReductionProxyTestContext::USE_MOCK_SERVICE)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .WithMockConfig() + .WithTestConfigurator() + .WithMockDataReductionProxyService() + .Build(); ResetSettings(true, true, false, true, false); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc index e386b511cfb0..1e0c25fb6f2b 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc @@ -182,13 +182,15 @@ class DataReductionProxyInterceptorWithServerTest : public testing::Test { ASSERT_TRUE(proxy_.InitializeAndWaitUntilReady()); ASSERT_TRUE(direct_.InitializeAndWaitUntilReady()); - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS, - &context_)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .WithURLRequestContext(&context_) + .Build(); std::string spec; base::TrimString(proxy_.GetURL("/").spec(), "/", &spec); test_context_->config()->test_params()->set_origin( diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc index f12182d99e41..24140c9e29a3 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc @@ -673,9 +673,10 @@ TEST_F(ChromeNetworkDailyDataSavingMetricsTest, BackwardTwoDays) { TEST_F(ChromeNetworkDailyDataSavingMetricsTest, GetDataReductionProxyRequestType) { scoped_ptr test_context = - make_scoped_ptr(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_ORIGIN, 0)); + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed) + .WithParamsDefinitions(TestDataReductionProxyParams::HAS_ORIGIN) + .Build(); TestDataReductionProxyParams* params = test_context->config()->test_params(); net::ProxyConfig data_reduction_proxy_config; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc index 8f43dae0bce6..3e91abfa8a61 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc @@ -77,24 +77,24 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test { test_job_interceptor_)); context_.set_job_factory(&test_job_factory_); - test_context_.reset( - new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS, - &context_)); - - request_options_.reset( - new DataReductionProxyRequestOptions( - kClient, config(), test_context_->task_runner())); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)\ + .WithClient(kClient) + .WithURLRequestContext(&context_) + .Build(); + data_reduction_proxy_network_delegate_.reset( new DataReductionProxyNetworkDelegate( scoped_ptr(new TestNetworkDelegate()), - config(), request_options_.get(), test_context_->configurator())); + config(), test_context_->io_data()->request_options(), + test_context_->configurator())); } const net::ProxyConfig& GetProxyConfig() const { @@ -155,7 +155,6 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test { return test_context_->data_reduction_proxy_service()->statistics_prefs(); } - scoped_ptr request_options_; scoped_ptr data_reduction_proxy_network_delegate_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc index a5a05ee66141..70ea1f46df1e 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options_unittest.cc @@ -158,15 +158,16 @@ void SetHeaderExpectations(const std::string& session, class DataReductionProxyRequestOptionsTest : public testing::Test { public: DataReductionProxyRequestOptionsTest() { - test_context_.reset( - new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .Build(); } void CreateRequestOptions(const std::string& version) { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc index 27d344d72f71..27c27617557c 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc @@ -41,16 +41,19 @@ DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} // testing::Test implementation: void DataReductionProxySettingsTestBase::SetUp() { - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::USE_MOCK_CONFIG | - DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION | - DataReductionProxyTestContext::USE_MOCK_SERVICE)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .WithMockConfig() + .WithMockDataReductionProxyService() + .SkipSettingsInitialization() + .Build(); TestingPrefServiceSimple* pref_service = test_context_->pref_service(); pref_service->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate, 0L); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc index e2da5b20ea7b..c9fe21dd0a83 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc @@ -180,26 +180,29 @@ TEST(DataReductionProxySettingsStandaloneTest, TestEndToEndProbe) { for (const TestCase& test_case : kTestCases) { net::TestURLRequestContext context(true); - DataReductionProxyTestContext drp_test_context( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::USE_TEST_CONFIGURATOR | - DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION, - &context); - - context.set_net_log(drp_test_context.net_log()); + scoped_ptr drp_test_context = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .WithURLRequestContext(&context) + .WithTestConfigurator() + .SkipSettingsInitialization() + .Build(); + + context.set_net_log(drp_test_context->net_log()); net::MockClientSocketFactory mock_socket_factory; context.set_client_socket_factory(&mock_socket_factory); context.Init(); // Start with the Data Reduction Proxy disabled. - drp_test_context.pref_service()->SetBoolean( + drp_test_context->pref_service()->SetBoolean( prefs::kDataReductionProxyEnabled, false); - drp_test_context.InitSettings(); + drp_test_context->InitSettings(); net::MockRead mock_reads[] = { net::MockRead(test_case.response_headers), @@ -211,43 +214,46 @@ TEST(DataReductionProxySettingsStandaloneTest, TestEndToEndProbe) { mock_socket_factory.AddSocketDataProvider(&socket_data_provider); // Toggle the pref to trigger the probe. - drp_test_context.pref_service()->SetBoolean( + drp_test_context->pref_service()->SetBoolean( prefs::kDataReductionProxyEnabled, true); - drp_test_context.RunUntilIdle(); + drp_test_context->RunUntilIdle(); EXPECT_EQ(test_case.expected_restricted, - drp_test_context.test_configurator()->restricted()); + drp_test_context->test_configurator()->restricted()); } } TEST(DataReductionProxySettingsStandaloneTest, TestOnProxyEnabledPrefChange) { - DataReductionProxyTestContext drp_test_context( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::USE_MOCK_CONFIG | - DataReductionProxyTestContext::USE_TEST_CONFIGURATOR | - DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION | - DataReductionProxyTestContext::USE_MOCK_SERVICE); + scoped_ptr drp_test_context = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .WithMockConfig() + .WithTestConfigurator() + .WithMockDataReductionProxyService() + .SkipSettingsInitialization() + .Build(); // The proxy is enabled initially. - drp_test_context.config()->SetStateForTest(true, false, false, true); - drp_test_context.InitSettings(); + drp_test_context->config()->SetStateForTest(true, false, false, true); + drp_test_context->InitSettings(); // The pref is disabled, so correspondingly should be the proxy. - EXPECT_CALL(*drp_test_context.mock_config(), + EXPECT_CALL(*drp_test_context->mock_config(), SetProxyPrefs(false, false, false)); - drp_test_context.pref_service()->SetBoolean(prefs::kDataReductionProxyEnabled, - false); + drp_test_context->pref_service()->SetBoolean( + prefs::kDataReductionProxyEnabled, false); // The pref is enabled, so correspondingly should be the proxy. - EXPECT_CALL(*drp_test_context.mock_config(), + EXPECT_CALL(*drp_test_context->mock_config(), SetProxyPrefs(true, false, false)); - drp_test_context.pref_service()->SetBoolean(prefs::kDataReductionProxyEnabled, - true); + drp_test_context->pref_service()->SetBoolean( + prefs::kDataReductionProxyEnabled, true); } TEST_F(DataReductionProxySettingsTest, TestMaybeActivateDataReductionProxy) { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc index 8ec2958e3b26..d688d76ae55d 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc @@ -51,77 +51,160 @@ TestDataReductionProxyIOData::~TestDataReductionProxyIOData() { shutdown_on_ui_ = true; } -DataReductionProxyTestContext::DataReductionProxyTestContext( - int params_flags, - unsigned int params_definitions, - unsigned int test_context_flags, - net::URLRequestContext* request_context) - : test_context_flags_(test_context_flags), - task_runner_(base::MessageLoopProxy::current()), - request_context_getter_(scoped_refptr( - new net::TrivialURLRequestContextGetter(request_context, - task_runner_))) { - Init(params_flags, params_definitions); +DataReductionProxyTestContext::Builder::Builder() + : params_flags_(0), + params_definitions_(0), + client_(Client::UNKNOWN), + request_context_(nullptr), + use_mock_config_(false), + use_test_configurator_(false), + use_mock_service_(false), + skip_settings_initialization_(false) { } -DataReductionProxyTestContext::DataReductionProxyTestContext( - int params_flags, - unsigned int params_definitions, - unsigned int test_context_flags) - : test_context_flags_(test_context_flags), - task_runner_(base::MessageLoopProxy::current()), - request_context_getter_(scoped_refptr( - new net::TestURLRequestContextGetter(task_runner_))) { - Init(params_flags, params_definitions); +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithParamsFlags(int params_flags) { + params_flags_ = params_flags; + return *this; } -DataReductionProxyTestContext::~DataReductionProxyTestContext() { +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithParamsDefinitions( + unsigned int params_definitions) { + params_definitions_ = params_definitions; + return *this; } -void DataReductionProxyTestContext::Init(int params_flags, - unsigned int params_definitions) { - scoped_ptr event_store = - make_scoped_ptr(new DataReductionProxyEventStore(task_runner_)); +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithURLRequestContext( + net::URLRequestContext* request_context) { + request_context_ = request_context; + return *this; +} + +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithClient(Client client) { + client_ = client; + return *this; +} + +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithMockConfig() { + use_mock_config_ = true; + return *this; +} + +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithTestConfigurator() { + use_test_configurator_ = true; + return *this; +} + +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::WithMockDataReductionProxyService() { + use_mock_service_ = true; + return *this; +} + +DataReductionProxyTestContext::Builder& +DataReductionProxyTestContext::Builder::SkipSettingsInitialization() { + skip_settings_initialization_ = true; + return *this; +} + +scoped_ptr +DataReductionProxyTestContext::Builder::Build() { + scoped_ptr loop(new base::MessageLoopForIO()); + + unsigned int test_context_flags = 0; + scoped_refptr task_runner = + base::MessageLoopProxy::current(); + scoped_refptr request_context_getter; + scoped_ptr pref_service( + new TestingPrefServiceSimple()); + scoped_ptr net_log(new net::CapturingNetLog()); + if (request_context_) { + request_context_getter = new net::TrivialURLRequestContextGetter( + request_context_, task_runner); + } else { + request_context_getter = new net::TestURLRequestContextGetter(task_runner); + } + + scoped_ptr event_store( + new DataReductionProxyEventStore(task_runner)); scoped_ptr configurator; - if (test_context_flags_ & - DataReductionProxyTestContext::USE_TEST_CONFIGURATOR) { - configurator = make_scoped_ptr(new TestDataReductionProxyConfigurator( - task_runner_, &net_log_, event_store.get())); + if (use_test_configurator_) { + test_context_flags |= USE_TEST_CONFIGURATOR; + configurator.reset(new TestDataReductionProxyConfigurator( + task_runner, net_log.get(), event_store.get())); } else { - configurator = make_scoped_ptr(new DataReductionProxyConfigurator( - task_runner_, &net_log_, event_store.get())); + configurator.reset(new DataReductionProxyConfigurator( + task_runner, net_log.get(), event_store.get())); } scoped_ptr config; - if (test_context_flags_ & DataReductionProxyTestContext::USE_MOCK_CONFIG) { + if (use_mock_config_) { + test_context_flags |= USE_MOCK_CONFIG; config.reset(new MockDataReductionProxyConfig( - params_flags, params_definitions, task_runner_, &net_log_, + params_flags_, params_definitions_, task_runner, net_log.get(), configurator.get(), event_store.get())); } else { config.reset(new TestDataReductionProxyConfig( - params_flags, params_definitions, task_runner_, &net_log_, + params_flags_, params_definitions_, task_runner, net_log.get(), configurator.get(), event_store.get())); } - scoped_ptr request_options = - make_scoped_ptr(new DataReductionProxyRequestOptions( - Client::UNKNOWN, config.get(), task_runner_)); - settings_.reset(new DataReductionProxySettings()); - - RegisterSimpleProfilePrefs(simple_pref_service_.registry()); - - io_data_.reset(new TestDataReductionProxyIOData( - task_runner_.get(), config.Pass(), event_store.Pass(), - request_options.Pass(), configurator.Pass())); - io_data_->InitOnUIThread(&simple_pref_service_); - - if (!(test_context_flags_ & - DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION)) { - settings_->InitDataReductionProxySettings( - &simple_pref_service_, io_data_.get(), - CreateDataReductionProxyServiceInternal()); - io_data_->SetDataReductionProxyService( - settings_->data_reduction_proxy_service()->GetWeakPtr()); - } + + scoped_ptr request_options( + new DataReductionProxyRequestOptions(client_, config.get(), task_runner)); + + scoped_ptr settings( + new DataReductionProxySettings()); + if (skip_settings_initialization_) + test_context_flags |= SKIP_SETTINGS_INITIALIZATION; + + if (use_mock_service_) + test_context_flags |= USE_MOCK_SERVICE; + + RegisterSimpleProfilePrefs(pref_service->registry()); + + scoped_ptr io_data( + new TestDataReductionProxyIOData( + task_runner, config.Pass(), event_store.Pass(), + request_options.Pass(), configurator.Pass())); + io_data->InitOnUIThread(pref_service.get()); + + scoped_ptr test_context( + new DataReductionProxyTestContext( + loop.Pass(), task_runner, pref_service.Pass(), net_log.Pass(), + request_context_getter, io_data.Pass(), settings.Pass(), + test_context_flags)); + + if (!skip_settings_initialization_) + test_context->InitSettingsWithoutCheck(); + + return test_context.Pass(); +} + +DataReductionProxyTestContext::DataReductionProxyTestContext( + scoped_ptr loop, + scoped_refptr task_runner, + scoped_ptr simple_pref_service, + scoped_ptr net_log, + scoped_refptr request_context_getter, + scoped_ptr io_data, + scoped_ptr settings, + unsigned int test_context_flags) + : test_context_flags_(test_context_flags), + loop_(loop.Pass()), + task_runner_(task_runner), + simple_pref_service_(simple_pref_service.Pass()), + net_log_(net_log.Pass()), + request_context_getter_(request_context_getter), + io_data_(io_data.Pass()), + settings_(settings.Pass()) { +} + +DataReductionProxyTestContext::~DataReductionProxyTestContext() { } void DataReductionProxyTestContext::RunUntilIdle() { @@ -131,8 +214,13 @@ void DataReductionProxyTestContext::RunUntilIdle() { void DataReductionProxyTestContext::InitSettings() { DCHECK(test_context_flags_ & DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION); + InitSettingsWithoutCheck(); +} + +void DataReductionProxyTestContext::InitSettingsWithoutCheck() { settings_->InitDataReductionProxySettings( - &simple_pref_service_, io_data_.get(), CreateDataReductionProxyService()); + simple_pref_service_.get(), io_data_.get(), + CreateDataReductionProxyServiceInternal()); io_data_->SetDataReductionProxyService( settings_->data_reduction_proxy_service()->GetWeakPtr()); } @@ -148,7 +236,7 @@ scoped_ptr DataReductionProxyTestContext::CreateDataReductionProxyServiceInternal() { scoped_ptr statistics_prefs = make_scoped_ptr(new DataReductionProxyStatisticsPrefs( - &simple_pref_service_, task_runner_, base::TimeDelta())); + simple_pref_service_.get(), task_runner_, base::TimeDelta())); if (test_context_flags_ & DataReductionProxyTestContext::USE_MOCK_SERVICE) { return make_scoped_ptr(new MockDataReductionProxyService( diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h index a414b7050d75..41adb21ea666 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h @@ -74,40 +74,54 @@ class TestDataReductionProxyIOData : public DataReductionProxyIOData { // underlying objects. class DataReductionProxyTestContext { public: - static const unsigned int DEFAULT_TEST_CONTEXT_OPTIONS = 0; + // Allows for a fluent builder interface to configure what kind of objects + // (test vs mock vs real) are used by the |DataReductionProxyTestContext|. + class Builder { + public: + Builder(); + + // |DataReductionProxyParams| flags to use. + Builder& WithParamsFlags(int params_flags); + + // |TestDataReductionProxyParams| flags to use. + Builder& WithParamsDefinitions(unsigned int params_definitions); + + // The |Client| enum to use for |DataReductionProxyRequestOptions|. + Builder& WithClient(Client client); + + // Specifies a |net::URLRequestContext| to use. The |request_context| is + // owned by the caller. + Builder& WithURLRequestContext(net::URLRequestContext* request_context); + + // Specifies the use of |MockDataReductionProxyConfig| instead of + // |TestDataReductionProxyConfig|. + Builder& WithMockConfig(); + + // Specifies the use of |TestDataReductionProxyConfigurator| instead of + // |DataReductionProxyConfigurator|. + Builder& WithTestConfigurator(); + + // Specifies the use of |MockDataReductionProxyService| instead of + // |DataReductionProxyService|. + Builder& WithMockDataReductionProxyService(); - enum TestContextOptions { - // Permits mocking of the underlying |DataReductionProxyConfig|. - USE_MOCK_CONFIG = 0x1, - // Uses a |TestDataReductionProxyConfigurator| to record proxy configuration - // changes. - USE_TEST_CONFIGURATOR = 0x2, // Construct, but do not initialize the |DataReductionProxySettings| object. - // Primarily used for testing of the |DataReductionProxySettings| object - // itself. - SKIP_SETTINGS_INITIALIZATION = 0x4, - // Permits mocking of the underlying |DataReductionProxyService|. - USE_MOCK_SERVICE = 0x8, - }; + Builder& SkipSettingsInitialization(); - // Creates a new DataReductionProxyTestContext. |params_flags| controls what - // is enabled in the underlying |DataReductionProxyParams|. - // |params_definitions| is used to control the HasNames enum for the - // underlying |TestDataReductionProxyParams|. |test_context_flags| is the - // |TestContextOptions| enum to control what underlying objects are created. - // |request_context| is the |URLRequestContext| that should be used for all - // requests made by underlying objects, such as the probe request. - // |request_context| must outlive |this|. - explicit DataReductionProxyTestContext( - int params_flags, - unsigned int params_definitions, - unsigned int test_context_flags, - net::URLRequestContext* request_context); - - // Convenience constructor that uses a dummy |URLRequestContextGetter|. - explicit DataReductionProxyTestContext(int params_flags, - unsigned int params_definitions, - unsigned int test_context_flags); + // Creates a |DataReductionProxyTestContext|. Owned by the caller. + scoped_ptr Build(); + + private: + int params_flags_; + unsigned int params_definitions_; + Client client_; + net::URLRequestContext* request_context_; + + bool use_mock_config_; + bool use_test_configurator_; + bool use_mock_service_; + bool skip_settings_initialization_; + }; virtual ~DataReductionProxyTestContext(); @@ -115,26 +129,27 @@ class DataReductionProxyTestContext { void RunUntilIdle(); // Initializes the |DataReductionProxySettings| object. Can only be called if - // |SKIP_SETTINGS_INITIALIZATION| was specified. + // built with SkipSettingsInitialization. void InitSettings(); // Creates a |DataReductionProxyService| object, or a - // |MockDataReductionProxyService| if |USE_MOCK_SERVICE| was specified. Can - // only be called if |SKIP_SETTINGS_INITIALIZATION| was specified. + // |MockDataReductionProxyService| if built with + // WithMockDataReductionProxyService. Can only be called if built with + // SkipSettingsInitialization. scoped_ptr CreateDataReductionProxyService(); // Returns the underlying |TestDataReductionProxyConfigurator|. This can only - // be called if |USE_TEST_CONFIGURATOR| was specified. + // be called if built with WithTestConfigurator. TestDataReductionProxyConfigurator* test_configurator() const; // Returns the underlying |MockDataReductionProxyConfig|. This can only be - // called if |USE_MOCK_CONFIG| was specified. + // called if built with WithMockConfig. MockDataReductionProxyConfig* mock_config() const; DataReductionProxyService* data_reduction_proxy_service() const; // Returns the underlying |MockDataReductionProxyService|. This can only - // be called if |USE_MOCK_SERVICE| was specified. + // be called if built with WithMockDataReductionProxyService. MockDataReductionProxyService* mock_data_reduction_proxy_service() const; scoped_refptr task_runner() const { @@ -142,11 +157,11 @@ class DataReductionProxyTestContext { } TestingPrefServiceSimple* pref_service() { - return &simple_pref_service_; + return simple_pref_service_.get(); } net::NetLog* net_log() { - return &net_log_; + return net_log_.get(); } net::URLRequestContextGetter* request_context_getter() const { @@ -174,20 +189,42 @@ class DataReductionProxyTestContext { } private: - // Creates and initializes the members of this class. Called in the - // constructor. - void Init(int params_flags, unsigned int params_definitions); + enum TestContextOptions { + // Permits mocking of the underlying |DataReductionProxyConfig|. + USE_MOCK_CONFIG = 0x1, + // Uses a |TestDataReductionProxyConfigurator| to record proxy configuration + // changes. + USE_TEST_CONFIGURATOR = 0x2, + // Construct, but do not initialize the |DataReductionProxySettings| object. + // Primarily used for testing of the |DataReductionProxySettings| object + // itself. + SKIP_SETTINGS_INITIALIZATION = 0x4, + // Permits mocking of the underlying |DataReductionProxyService|. + USE_MOCK_SERVICE = 0x8, + }; + + DataReductionProxyTestContext( + scoped_ptr loop, + scoped_refptr task_runner, + scoped_ptr simple_pref_service, + scoped_ptr net_log, + scoped_refptr request_context_getter, + scoped_ptr io_data, + scoped_ptr settings, + unsigned int test_context_flags); + + void InitSettingsWithoutCheck(); scoped_ptr CreateDataReductionProxyServiceInternal(); unsigned int test_context_flags_; - base::MessageLoopForIO loop_; + scoped_ptr loop_; scoped_refptr task_runner_; - TestingPrefServiceSimple simple_pref_service_; - net::CapturingNetLog net_log_; + scoped_ptr simple_pref_service_; + scoped_ptr net_log_; scoped_refptr request_context_getter_; scoped_ptr io_data_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc index aadf49edc88f..338bdec6fa6f 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc @@ -90,15 +90,16 @@ class DataReductionProxyUsageStatsTest : public testing::Test { context_.set_job_factory(&test_job_factory_); - test_context_.reset( - new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN, - DataReductionProxyTestContext::DEFAULT_TEST_CONTEXT_OPTIONS)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .Build(); mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, NULL); scoped_ptr mock_params = @@ -590,11 +591,13 @@ class DataReductionProxyUsageStatsEndToEndTest : public testing::Test { // test bypassed bytes due to proxy fallbacks. This way, a test just needs // to cause one proxy fallback in order for the data reduction proxy to be // fully bypassed. - test_context_.reset(new DataReductionProxyTestContext( - DataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_ORIGIN, - DataReductionProxyTestContext::SKIP_SETTINGS_INITIALIZATION, - &context_)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed) + .WithParamsDefinitions(TestDataReductionProxyParams::HAS_ORIGIN) + .WithURLRequestContext(&context_) + .SkipSettingsInitialization() + .Build(); test_context_->pref_service()->SetBoolean(prefs::kDataReductionProxyEnabled, true); test_context_->InitSettings(); -- 2.11.4.GIT