1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "base/time/time.h"
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.h"
18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
22 using testing::AnyNumber;
23 using testing::Return;
27 const char kProbeURLWithOKResponse[] = "http://ok.org/";
29 const char kProxy[] = "proxy";
33 namespace data_reduction_proxy {
35 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
38 return SUCCEEDED_PROXY_ALREADY_ENABLED;
39 return FAILED_PROXY_DISABLED;
42 return SUCCEEDED_PROXY_ENABLED;
43 return FAILED_PROXY_ALREADY_DISABLED;
46 TestDataReductionProxyConfig::TestDataReductionProxyConfig()
49 fallback_restricted_(false) {}
51 void TestDataReductionProxyConfig::Enable(
53 bool fallback_restricted,
54 const std::string& primary_origin,
55 const std::string& fallback_origin,
56 const std::string& ssl_origin) {
58 restricted_ = restricted;
59 fallback_restricted_ = fallback_restricted;
60 origin_ = primary_origin;
61 fallback_origin_ = fallback_origin;
62 ssl_origin_ = ssl_origin;
65 void TestDataReductionProxyConfig::Disable() {
68 fallback_restricted_ = false;
70 fallback_origin_ = "";
74 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
77 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
79 // testing::Test implementation:
80 void DataReductionProxySettingsTestBase::SetUp() {
81 PrefRegistrySimple* registry = pref_service_.registry();
82 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
83 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
84 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate,
86 registry->RegisterDictionaryPref(kProxy);
87 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false);
88 registry->RegisterBooleanPref(prefs::kDataReductionProxyAltEnabled, false);
89 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore,
92 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
94 scoped_refptr<base::TestSimpleTaskRunner>(
95 new base::TestSimpleTaskRunner()),
98 //AddProxyToCommandLine();
99 ResetSettings(true, true, false, true, false);
101 ListPrefUpdate original_update(&pref_service_,
102 prefs::kDailyHttpOriginalContentLength);
103 ListPrefUpdate received_update(&pref_service_,
104 prefs::kDailyHttpReceivedContentLength);
105 for (int64 i = 0; i < kNumDaysInHistory; i++) {
106 original_update->Insert(0,
107 new base::StringValue(base::Int64ToString(2 * i)));
108 received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));
110 last_update_time_ = base::Time::Now().LocalMidnight();
111 statistics_prefs_->SetInt64(
112 prefs::kDailyHttpContentLengthLastUpdateDate,
113 last_update_time_.ToInternalValue());
114 expected_params_.reset(new TestDataReductionProxyParams(
115 DataReductionProxyParams::kAllowed |
116 DataReductionProxyParams::kFallbackAllowed |
117 DataReductionProxyParams::kPromoAllowed,
118 TestDataReductionProxyParams::HAS_EVERYTHING &
119 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
120 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN));
124 void DataReductionProxySettingsTestBase::ResetSettings(bool allowed,
125 bool fallback_allowed,
131 flags |= DataReductionProxyParams::kAllowed;
132 if (fallback_allowed)
133 flags |= DataReductionProxyParams::kFallbackAllowed;
135 flags |= DataReductionProxyParams::kAlternativeAllowed;
137 flags |= DataReductionProxyParams::kPromoAllowed;
139 flags |= DataReductionProxyParams::kHoldback;
140 MockDataReductionProxySettings<C>* settings =
141 new MockDataReductionProxySettings<C>(flags);
142 EXPECT_CALL(*settings, GetOriginalProfilePrefs())
144 .WillRepeatedly(Return(&pref_service_));
145 EXPECT_CALL(*settings, GetLocalStatePrefs())
147 .WillRepeatedly(Return(&pref_service_));
148 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0);
149 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0);
150 settings_.reset(settings);
151 configurator_.reset(new TestDataReductionProxyConfig());
152 settings_->configurator_ = configurator_.get();
153 settings_->SetDataReductionProxyStatisticsPrefs(statistics_prefs_.get());
156 // Explicitly generate required instantiations.
158 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(
160 bool fallback_allowed,
166 void DataReductionProxySettingsTestBase::SetProbeResult(
167 const std::string& test_url,
168 const std::string& response,
169 ProbeURLFetchResult result,
171 int expected_calls) {
172 MockDataReductionProxySettings<C>* settings =
173 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
174 if (0 == expected_calls) {
175 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0);
176 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0);
178 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
179 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck())
180 .Times(expected_calls)
181 .WillRepeatedly(Return(new net::FakeURLFetcher(
185 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
186 success ? net::URLRequestStatus::SUCCESS :
187 net::URLRequestStatus::FAILED)));
191 // Explicitly generate required instantiations.
193 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
194 const std::string& test_url,
195 const std::string& response,
196 ProbeURLFetchResult result,
200 void DataReductionProxySettingsTestBase::CheckProxyConfigs(
201 bool expected_enabled,
202 bool expected_restricted,
203 bool expected_fallback_restricted) {
204 TestDataReductionProxyConfig* config =
205 static_cast<TestDataReductionProxyConfig*>(settings_->configurator_);
206 ASSERT_EQ(expected_restricted, config->restricted_);
207 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_);
208 ASSERT_EQ(expected_enabled, config->enabled_);
211 void DataReductionProxySettingsTestBase::CheckProbe(
212 bool initially_enabled,
213 const std::string& probe_url,
214 const std::string& response,
215 bool request_succeeded,
216 bool expected_enabled,
217 bool expected_restricted,
218 bool expected_fallback_restricted) {
219 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled,
221 if (initially_enabled)
222 settings_->enabled_by_user_ = true;
223 settings_->restricted_by_carrier_ = false;
224 SetProbeResult(probe_url,
226 FetchResult(initially_enabled,
227 request_succeeded && (response == "OK")),
229 initially_enabled ? 1 : 0);
230 settings_->MaybeActivateDataReductionProxy(false);
231 base::MessageLoop::current()->RunUntilIdle();
232 CheckProxyConfigs(expected_enabled,
234 expected_fallback_restricted);
237 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange(
238 const std::string& probe_url,
239 const std::string& response,
240 bool request_succeeded,
241 bool expected_restricted,
242 bool expected_fallback_restricted) {
243 SetProbeResult(probe_url,
245 FetchResult(!settings_->restricted_by_carrier_,
246 request_succeeded && (response == "OK")),
249 settings_->OnIPAddressChanged();
250 base::MessageLoop::current()->RunUntilIdle();
251 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted);
254 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
256 bool expected_enabled,
258 // Always have a sucessful probe for pref change tests.
259 SetProbeResult(kProbeURLWithOKResponse,
261 FetchResult(enabled, true),
263 expected_enabled ? 1 : 0);
265 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled,
266 new base::FundamentalValue(enabled));
268 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled);
270 base::MessageLoop::current()->RunUntilIdle();
271 // Never expect the proxy to be restricted for pref change tests.
272 CheckProxyConfigs(expected_enabled, false, false);
275 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
276 bool enabled_at_startup) {
277 base::MessageLoopForUI loop;
278 scoped_ptr<DataReductionProxyConfigurator> configurator(
279 new TestDataReductionProxyConfig());
280 settings_->SetProxyConfigurator(configurator.get());
281 scoped_refptr<net::TestURLRequestContextGetter> request_context =
282 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
284 settings_->InitDataReductionProxySettings(
286 request_context.get());
287 settings_->SetOnDataReductionEnabledCallback(
288 base::Bind(&DataReductionProxySettingsTestBase::
289 RegisterSyntheticFieldTrialCallback,
290 base::Unretained(this)));
292 base::MessageLoop::current()->RunUntilIdle();
293 EXPECT_EQ(enabled_at_startup, proxy_enabled_);
296 } // namespace data_reduction_proxy