Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_settings_test_utils.cc
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.
4
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h"
6
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"
14
15 using testing::_;
16 using testing::AnyNumber;
17 using testing::Return;
18
19 namespace {
20
21 const char kDataReductionProxy[] = "https://foo.com:443/";
22 const char kDataReductionProxyFallback[] = "http://bar.com:80";
23 const char kDataReductionProxyKey[] = "12345";
24
25 const char kProbeURLWithOKResponse[] = "http://ok.org/";
26
27 const char kProxy[] = "proxy";
28
29 }  // namespace
30
31 namespace data_reduction_proxy {
32
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())
38     *headers += '\0';
39 }
40
41 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
42   if (enabled) {
43     if (success)
44       return SUCCEEDED_PROXY_ALREADY_ENABLED;
45     return FAILED_PROXY_DISABLED;
46   }
47   if (success)
48     return SUCCEEDED_PROXY_ENABLED;
49   return FAILED_PROXY_ALREADY_DISABLED;
50 }
51
52 void TestDataReductionProxyConfig::Enable(
53     bool restricted,
54     bool fallback_restricted,
55     const std::string& primary_origin,
56     const std::string& fallback_origin) {
57   enabled_ = true;
58   restricted_ = restricted;
59   fallback_restricted_ = fallback_restricted;
60 }
61
62 void TestDataReductionProxyConfig::Disable() {
63   enabled_ = false;
64   restricted_ = false;
65   fallback_restricted_ = false;
66 }
67
68 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
69     : testing::Test() {
70 }
71
72 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
73
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);
82 }
83
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,
91                               0L);
92   registry->RegisterDictionaryPref(kProxy);
93   registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false);
94   registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore,
95                                 false);
96   ResetSettings();
97
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)));
106   }
107   last_update_time_ = base::Time::Now().LocalMidnight();
108   pref_service_.SetInt64(
109       prefs::kDailyHttpContentLengthLastUpdateDate,
110       last_update_time_.ToInternalValue());
111 }
112
113 template <class C>
114 void DataReductionProxySettingsTestBase::ResetSettings() {
115   MockDataReductionProxySettings<C>* settings =
116       new MockDataReductionProxySettings<C>();
117   EXPECT_CALL(*settings, GetOriginalProfilePrefs())
118       .Times(AnyNumber())
119       .WillRepeatedly(Return(&pref_service_));
120   EXPECT_CALL(*settings, GetLocalStatePrefs())
121       .Times(AnyNumber())
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());
127 }
128
129 // Explicitly generate required instantiations.
130 template void
131 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>();
132
133 template <class C>
134 void DataReductionProxySettingsTestBase::SetProbeResult(
135     const std::string& test_url,
136     const std::string& response,
137     ProbeURLFetchResult result,
138     bool success,
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);
145   } else {
146     EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
147     EXPECT_CALL(*settings, GetURLFetcher())
148         .Times(expected_calls)
149         .WillRepeatedly(Return(new net::FakeURLFetcher(
150             GURL(test_url),
151             settings,
152             response,
153             success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
154             success ? net::URLRequestStatus::SUCCESS :
155                       net::URLRequestStatus::FAILED)));
156   }
157 }
158
159 // Explicitly generate required instantiations.
160 template void
161 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
162     const std::string& test_url,
163     const std::string& response,
164     ProbeURLFetchResult result,
165     bool success,
166     int expected_calls);
167
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_);
177 }
178
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,
188                            initially_enabled);
189   if (initially_enabled)
190     settings_->enabled_by_user_ = true;
191   settings_->restricted_by_carrier_ = false;
192   SetProbeResult(probe_url,
193                  response,
194                  FetchResult(initially_enabled,
195                              request_succeeded && (response == "OK")),
196                  request_succeeded,
197                  initially_enabled ? 1 : 0);
198   settings_->MaybeActivateDataReductionProxy(false);
199   base::MessageLoop::current()->RunUntilIdle();
200   CheckProxyConfigs(expected_enabled,
201                     expected_restricted,
202                     expected_fallback_restricted);
203 }
204
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,
212                  response,
213                  FetchResult(!settings_->restricted_by_carrier_,
214                              request_succeeded && (response == "OK")),
215                  request_succeeded,
216                  1);
217   settings_->OnIPAddressChanged();
218   base::MessageLoop::current()->RunUntilIdle();
219   CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted);
220 }
221
222 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
223     bool enabled,
224     bool expected_enabled,
225     bool managed) {
226   // Always have a sucessful probe for pref change tests.
227   SetProbeResult(kProbeURLWithOKResponse,
228                  "OK",
229                  FetchResult(enabled, true),
230                  true,
231                  expected_enabled ? 1 : 0);
232   if (managed) {
233     pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled,
234                                  base::Value::CreateBooleanValue(enabled));
235   } else {
236     pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled);
237   }
238   base::MessageLoop::current()->RunUntilIdle();
239   // Never expect the proxy to be restricted for pref change tests.
240   CheckProxyConfigs(expected_enabled, false, false);
241 }
242
243 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
244     bool enabled_at_startup) {
245   AddProxyToCommandLine();
246   base::MessageLoopForUI loop;
247   SetProbeResult(kProbeURLWithOKResponse,
248                  "OK",
249                  FetchResult(enabled_at_startup, true),
250                  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_,
258                                             &pref_service_,
259                                             request_context.get());
260
261   base::MessageLoop::current()->RunUntilIdle();
262   CheckProxyConfigs(enabled_at_startup, false, false);
263 }
264
265 }  // namespace data_reduction_proxy