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/browser/data_reduction_proxy_settings_test_utils.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
13 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
16 using testing::AnyNumber;
17 using testing::Return;
21 const char kDataReductionProxy[] = "https://foo.com:443/";
22 const char kDataReductionProxyFallback[] = "http://bar.com:80";
23 const char kDataReductionProxyKey[] = "12345";
25 const char kProbeURLWithOKResponse[] = "http://ok.org/";
27 const char kProxy[] = "proxy";
31 namespace data_reduction_proxy {
33 // Transform "normal"-looking headers (\n-separated) to the appropriate
34 // input format for ParseRawHeaders (\0-separated).
35 void HeadersToRaw(std::string* headers) {
36 std::replace(headers->begin(), headers->end(), '\n', '\0');
37 if (!headers->empty())
41 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
44 return SUCCEEDED_PROXY_ALREADY_ENABLED;
45 return FAILED_PROXY_DISABLED;
48 return SUCCEEDED_PROXY_ENABLED;
49 return FAILED_PROXY_ALREADY_DISABLED;
52 void TestDataReductionProxyConfig::Enable(
54 bool fallback_restricted,
55 const std::string& primary_origin,
56 const std::string& fallback_origin) {
58 restricted_ = restricted;
59 fallback_restricted_ = fallback_restricted;
62 void TestDataReductionProxyConfig::Disable() {
65 fallback_restricted_ = false;
68 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
72 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
74 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() {
75 DataReductionProxySettings::SetAllowed(true);
76 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
77 switches::kDataReductionProxy, kDataReductionProxy);
78 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
79 switches::kDataReductionProxyFallback, kDataReductionProxyFallback);
80 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
81 switches::kDataReductionProxyKey, kDataReductionProxyKey);
84 // testing::Test implementation:
85 void DataReductionProxySettingsTestBase::SetUp() {
86 DataReductionProxySettings::SetAllowed(true);
87 PrefRegistrySimple* registry = pref_service_.registry();
88 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
89 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
90 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate,
92 registry->RegisterDictionaryPref(kProxy);
93 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false);
94 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore,
98 ListPrefUpdate original_update(&pref_service_,
99 prefs::kDailyHttpOriginalContentLength);
100 ListPrefUpdate received_update(&pref_service_,
101 prefs::kDailyHttpReceivedContentLength);
102 for (int64 i = 0; i < kNumDaysInHistory; i++) {
103 original_update->Insert(0,
104 new base::StringValue(base::Int64ToString(2 * i)));
105 received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));
107 last_update_time_ = base::Time::Now().LocalMidnight();
108 pref_service_.SetInt64(
109 prefs::kDailyHttpContentLengthLastUpdateDate,
110 last_update_time_.ToInternalValue());
114 void DataReductionProxySettingsTestBase::ResetSettings() {
115 MockDataReductionProxySettings<C>* settings =
116 new MockDataReductionProxySettings<C>();
117 EXPECT_CALL(*settings, GetOriginalProfilePrefs())
119 .WillRepeatedly(Return(&pref_service_));
120 EXPECT_CALL(*settings, GetLocalStatePrefs())
122 .WillRepeatedly(Return(&pref_service_));
123 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
124 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0);
125 settings_.reset(settings);
126 settings_->config_.reset(new TestDataReductionProxyConfig());
129 // Explicitly generate required instantiations.
131 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>();
134 void DataReductionProxySettingsTestBase::SetProbeResult(
135 const std::string& test_url,
136 const std::string& response,
137 ProbeURLFetchResult result,
139 int expected_calls) {
140 MockDataReductionProxySettings<C>* settings =
141 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
142 if (0 == expected_calls) {
143 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
144 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0);
146 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
147 EXPECT_CALL(*settings, GetURLFetcher())
148 .Times(expected_calls)
149 .WillRepeatedly(Return(new net::FakeURLFetcher(
153 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
154 success ? net::URLRequestStatus::SUCCESS :
155 net::URLRequestStatus::FAILED)));
159 // Explicitly generate required instantiations.
161 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
162 const std::string& test_url,
163 const std::string& response,
164 ProbeURLFetchResult result,
168 void DataReductionProxySettingsTestBase::CheckProxyConfigs(
169 bool expected_enabled,
170 bool expected_restricted,
171 bool expected_fallback_restricted) {
172 TestDataReductionProxyConfig* config =
173 static_cast<TestDataReductionProxyConfig*>(settings_->config_.get());
174 ASSERT_EQ(expected_restricted, config->restricted_);
175 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_);
176 ASSERT_EQ(expected_enabled, config->enabled_);
179 void DataReductionProxySettingsTestBase::CheckProbe(
180 bool initially_enabled,
181 const std::string& probe_url,
182 const std::string& response,
183 bool request_succeeded,
184 bool expected_enabled,
185 bool expected_restricted,
186 bool expected_fallback_restricted) {
187 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled,
189 if (initially_enabled)
190 settings_->enabled_by_user_ = true;
191 settings_->restricted_by_carrier_ = false;
192 SetProbeResult(probe_url,
194 FetchResult(initially_enabled,
195 request_succeeded && (response == "OK")),
197 initially_enabled ? 1 : 0);
198 settings_->MaybeActivateDataReductionProxy(false);
199 base::MessageLoop::current()->RunUntilIdle();
200 CheckProxyConfigs(expected_enabled,
202 expected_fallback_restricted);
205 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange(
206 const std::string& probe_url,
207 const std::string& response,
208 bool request_succeeded,
209 bool expected_restricted,
210 bool expected_fallback_restricted) {
211 SetProbeResult(probe_url,
213 FetchResult(!settings_->restricted_by_carrier_,
214 request_succeeded && (response == "OK")),
217 settings_->OnIPAddressChanged();
218 base::MessageLoop::current()->RunUntilIdle();
219 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted);
222 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
224 bool expected_enabled,
226 // Always have a sucessful probe for pref change tests.
227 SetProbeResult(kProbeURLWithOKResponse,
229 FetchResult(enabled, true),
231 expected_enabled ? 1 : 0);
233 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled,
234 base::Value::CreateBooleanValue(enabled));
236 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled);
238 base::MessageLoop::current()->RunUntilIdle();
239 // Never expect the proxy to be restricted for pref change tests.
240 CheckProxyConfigs(expected_enabled, false, false);
243 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
244 bool enabled_at_startup) {
245 AddProxyToCommandLine();
246 base::MessageLoopForUI loop;
247 SetProbeResult(kProbeURLWithOKResponse,
249 FetchResult(enabled_at_startup, true),
251 enabled_at_startup ? 1 : 0);
252 scoped_ptr<DataReductionProxyConfigurator> configurator(
253 new TestDataReductionProxyConfig());
254 settings_->SetProxyConfigurator(configurator.Pass());
255 scoped_refptr<net::TestURLRequestContextGetter> request_context =
256 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
257 settings_->InitDataReductionProxySettings(&pref_service_,
259 request_context.get());
261 base::MessageLoop::current()->RunUntilIdle();
262 CheckProxyConfigs(enabled_at_startup, false, false);
265 } // namespace data_reduction_proxy