Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.cc

Issue 286013002: Added alternative configuration for the data reduction proxy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_ test_utils.h" 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_ test_utils.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/scoped_user_pref_update.h" 10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names .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 " 13 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h "
14 14
15 using testing::_; 15 using testing::_;
16 using testing::AnyNumber; 16 using testing::AnyNumber;
17 using testing::Return; 17 using testing::Return;
18 18
19 namespace { 19 namespace {
20 20
21 const char kDataReductionProxy[] = "https://foo.com:443/"; 21 const char kDataReductionProxy[] = "https://foo.com:443/";
22 const char kDataReductionProxyFallback[] = "http://bar.com:80"; 22 const char kDataReductionProxyFallback[] = "http://bar.com:80/";
23 const char kDataReductionProxyKey[] = "12345"; 23 const char kDataReductionProxyKey[] = "12345";
24 24
25 const char kProbeURLWithOKResponse[] = "http://ok.org/"; 25 const char kProbeURLWithOKResponse[] = "http://ok.org/";
26 26
27 const char kProxy[] = "proxy"; 27 const char kProxy[] = "proxy";
28 28
29 } // namespace 29 } // namespace
30 30
31 namespace data_reduction_proxy { 31 namespace data_reduction_proxy {
32 32
33 // Transform "normal"-looking headers (\n-separated) to the appropriate 33 // Transform "normal"-looking headers (\n-separated) to the appropriate
34 // input format for ParseRawHeaders (\0-separated). 34 // input format for ParseRawHeaders (\0-separated).
35 void HeadersToRaw(std::string* headers) { 35 void HeadersToRaw(std::string* headers) {
36 std::replace(headers->begin(), headers->end(), '\n', '\0'); 36 std::replace(headers->begin(), headers->end(), '\n', '\0');
37 if (!headers->empty()) 37 if (!headers->empty())
38 *headers += '\0'; 38 *headers += '\0';
39 } 39 }
40 40
41 ProbeURLFetchResult FetchResult(bool enabled, bool success) { 41 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
42 if (enabled) { 42 if (enabled) {
43 if (success) 43 if (success)
44 return SUCCEEDED_PROXY_ALREADY_ENABLED; 44 return SUCCEEDED_PROXY_ALREADY_ENABLED;
45 return FAILED_PROXY_DISABLED; 45 return FAILED_PROXY_DISABLED;
46 } 46 }
47 if (success) 47 if (success)
48 return SUCCEEDED_PROXY_ENABLED; 48 return SUCCEEDED_PROXY_ENABLED;
49 return FAILED_PROXY_ALREADY_DISABLED; 49 return FAILED_PROXY_ALREADY_DISABLED;
50 } 50 }
51 51
52 TestDataReductionProxyConfig::TestDataReductionProxyConfig()
53 : enabled_(false),
54 restricted_(false),
55 fallback_restricted_(false) {}
56
52 void TestDataReductionProxyConfig::Enable( 57 void TestDataReductionProxyConfig::Enable(
53 bool restricted, 58 bool restricted,
54 bool fallback_restricted, 59 bool fallback_restricted,
55 const std::string& primary_origin, 60 const std::string& primary_origin,
56 const std::string& fallback_origin) { 61 const std::string& fallback_origin,
62 const std::string& ssl_origin) {
57 enabled_ = true; 63 enabled_ = true;
58 restricted_ = restricted; 64 restricted_ = restricted;
59 fallback_restricted_ = fallback_restricted; 65 fallback_restricted_ = fallback_restricted;
66 origin_ = primary_origin;
67 fallback_origin_ = fallback_origin;
68 ssl_origin_ = ssl_origin;
60 } 69 }
61 70
62 void TestDataReductionProxyConfig::Disable() { 71 void TestDataReductionProxyConfig::Disable() {
63 enabled_ = false; 72 enabled_ = false;
64 restricted_ = false; 73 restricted_ = false;
65 fallback_restricted_ = false; 74 fallback_restricted_ = false;
75 origin_ = "";
76 fallback_origin_ = "";
77 ssl_origin_ = "";
78 }
79
80 // static
81 void DataReductionProxySettingsTestBase::AddTestProxyToCommandLine() {
82 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
83 switches::kDataReductionProxy, kDataReductionProxy);
84 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
85 switches::kDataReductionProxyFallback, kDataReductionProxyFallback);
86 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
87 switches::kDataReductionProxyKey, kDataReductionProxyKey);
88 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
89 switches::kDataReductionProxyProbeURL, kProbeURLWithOKResponse);
66 } 90 }
67 91
68 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() 92 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
69 : testing::Test() { 93 : testing::Test() {
70 } 94 }
71 95
72 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} 96 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
73 97
74 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { 98 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() {
75 DataReductionProxySettings::SetAllowed(true); 99 AddTestProxyToCommandLine();
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 } 100 }
83 101
84 // testing::Test implementation: 102 // testing::Test implementation:
85 void DataReductionProxySettingsTestBase::SetUp() { 103 void DataReductionProxySettingsTestBase::SetUp() {
86 DataReductionProxySettings::SetAllowed(true);
87 PrefRegistrySimple* registry = pref_service_.registry(); 104 PrefRegistrySimple* registry = pref_service_.registry();
88 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); 105 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
89 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); 106 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
90 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, 107 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate,
91 0L); 108 0L);
92 registry->RegisterDictionaryPref(kProxy); 109 registry->RegisterDictionaryPref(kProxy);
93 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false); 110 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false);
111 registry->RegisterBooleanPref(prefs::kDataReductionProxyAltEnabled, false);
94 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, 112 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore,
95 false); 113 false);
96 ResetSettings(); 114 AddProxyToCommandLine();
115 ResetSettings(true, true, false, true);
97 116
98 ListPrefUpdate original_update(&pref_service_, 117 ListPrefUpdate original_update(&pref_service_,
99 prefs::kDailyHttpOriginalContentLength); 118 prefs::kDailyHttpOriginalContentLength);
100 ListPrefUpdate received_update(&pref_service_, 119 ListPrefUpdate received_update(&pref_service_,
101 prefs::kDailyHttpReceivedContentLength); 120 prefs::kDailyHttpReceivedContentLength);
102 for (int64 i = 0; i < kNumDaysInHistory; i++) { 121 for (int64 i = 0; i < kNumDaysInHistory; i++) {
103 original_update->Insert(0, 122 original_update->Insert(0,
104 new base::StringValue(base::Int64ToString(2 * i))); 123 new base::StringValue(base::Int64ToString(2 * i)));
105 received_update->Insert(0, new base::StringValue(base::Int64ToString(i))); 124 received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));
106 } 125 }
107 last_update_time_ = base::Time::Now().LocalMidnight(); 126 last_update_time_ = base::Time::Now().LocalMidnight();
108 pref_service_.SetInt64( 127 pref_service_.SetInt64(
109 prefs::kDailyHttpContentLengthLastUpdateDate, 128 prefs::kDailyHttpContentLengthLastUpdateDate,
110 last_update_time_.ToInternalValue()); 129 last_update_time_.ToInternalValue());
111 } 130 }
112 131
113 template <class C> 132 template <class C>
114 void DataReductionProxySettingsTestBase::ResetSettings() { 133 void DataReductionProxySettingsTestBase::ResetSettings(bool allowed,
134 bool fallback_allowed,
135 bool alt_allowed,
136 bool promo_allowed) {
115 MockDataReductionProxySettings<C>* settings = 137 MockDataReductionProxySettings<C>* settings =
116 new MockDataReductionProxySettings<C>(); 138 new MockDataReductionProxySettings<C>(
139 allowed, fallback_allowed, alt_allowed, promo_allowed);
117 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) 140 EXPECT_CALL(*settings, GetOriginalProfilePrefs())
118 .Times(AnyNumber()) 141 .Times(AnyNumber())
119 .WillRepeatedly(Return(&pref_service_)); 142 .WillRepeatedly(Return(&pref_service_));
120 EXPECT_CALL(*settings, GetLocalStatePrefs()) 143 EXPECT_CALL(*settings, GetLocalStatePrefs())
121 .Times(AnyNumber()) 144 .Times(AnyNumber())
122 .WillRepeatedly(Return(&pref_service_)); 145 .WillRepeatedly(Return(&pref_service_));
123 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); 146 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
124 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); 147 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0);
125 settings_.reset(settings); 148 settings_.reset(settings);
126 settings_->config_.reset(new TestDataReductionProxyConfig()); 149 settings_->configurator_.reset(new TestDataReductionProxyConfig());
127 } 150 }
128 151
129 // Explicitly generate required instantiations. 152 // Explicitly generate required instantiations.
130 template void 153 template void
131 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(); 154 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(
155 bool allowed, bool fallback_allowed, bool alt_allowed, bool promo_allowed);
132 156
133 template <class C> 157 template <class C>
134 void DataReductionProxySettingsTestBase::SetProbeResult( 158 void DataReductionProxySettingsTestBase::SetProbeResult(
135 const std::string& test_url, 159 const std::string& test_url,
136 const std::string& response, 160 const std::string& response,
137 ProbeURLFetchResult result, 161 ProbeURLFetchResult result,
138 bool success, 162 bool success,
139 int expected_calls) { 163 int expected_calls) {
140 MockDataReductionProxySettings<C>* settings = 164 MockDataReductionProxySettings<C>* settings =
141 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); 165 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
(...skipping 21 matching lines...) Expand all
163 const std::string& response, 187 const std::string& response,
164 ProbeURLFetchResult result, 188 ProbeURLFetchResult result,
165 bool success, 189 bool success,
166 int expected_calls); 190 int expected_calls);
167 191
168 void DataReductionProxySettingsTestBase::CheckProxyConfigs( 192 void DataReductionProxySettingsTestBase::CheckProxyConfigs(
169 bool expected_enabled, 193 bool expected_enabled,
170 bool expected_restricted, 194 bool expected_restricted,
171 bool expected_fallback_restricted) { 195 bool expected_fallback_restricted) {
172 TestDataReductionProxyConfig* config = 196 TestDataReductionProxyConfig* config =
173 static_cast<TestDataReductionProxyConfig*>(settings_->config_.get()); 197 static_cast<TestDataReductionProxyConfig*>(
198 settings_->configurator_.get());
174 ASSERT_EQ(expected_restricted, config->restricted_); 199 ASSERT_EQ(expected_restricted, config->restricted_);
175 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); 200 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_);
176 ASSERT_EQ(expected_enabled, config->enabled_); 201 ASSERT_EQ(expected_enabled, config->enabled_);
177 } 202 }
178 203
179 void DataReductionProxySettingsTestBase::CheckProbe( 204 void DataReductionProxySettingsTestBase::CheckProbe(
180 bool initially_enabled, 205 bool initially_enabled,
181 const std::string& probe_url, 206 const std::string& probe_url,
182 const std::string& response, 207 const std::string& response,
183 bool request_succeeded, 208 bool request_succeeded,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } else { 260 } else {
236 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); 261 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled);
237 } 262 }
238 base::MessageLoop::current()->RunUntilIdle(); 263 base::MessageLoop::current()->RunUntilIdle();
239 // Never expect the proxy to be restricted for pref change tests. 264 // Never expect the proxy to be restricted for pref change tests.
240 CheckProxyConfigs(expected_enabled, false, false); 265 CheckProxyConfigs(expected_enabled, false, false);
241 } 266 }
242 267
243 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( 268 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
244 bool enabled_at_startup) { 269 bool enabled_at_startup) {
245 AddProxyToCommandLine();
246 base::MessageLoopForUI loop; 270 base::MessageLoopForUI loop;
247 SetProbeResult(kProbeURLWithOKResponse, 271 SetProbeResult(kProbeURLWithOKResponse,
248 "OK", 272 "OK",
249 FetchResult(enabled_at_startup, true), 273 FetchResult(enabled_at_startup, true),
250 true, 274 true,
251 enabled_at_startup ? 1 : 0); 275 enabled_at_startup ? 1 : 0);
252 scoped_ptr<DataReductionProxyConfigurator> configurator( 276 scoped_ptr<DataReductionProxyConfigurator> configurator(
253 new TestDataReductionProxyConfig()); 277 new TestDataReductionProxyConfig());
254 settings_->SetProxyConfigurator(configurator.Pass()); 278 settings_->SetProxyConfigurator(configurator.Pass());
255 scoped_refptr<net::TestURLRequestContextGetter> request_context = 279 scoped_refptr<net::TestURLRequestContextGetter> request_context =
256 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); 280 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
257 settings_->InitDataReductionProxySettings(&pref_service_, 281 settings_->InitDataReductionProxySettings(&pref_service_,
258 &pref_service_, 282 &pref_service_,
259 request_context.get()); 283 request_context.get());
260 284
261 base::MessageLoop::current()->RunUntilIdle(); 285 base::MessageLoop::current()->RunUntilIdle();
262 CheckProxyConfigs(enabled_at_startup, false, false); 286 CheckProxyConfigs(enabled_at_startup, false, false);
263 } 287 }
264 288
265 } // namespace data_reduction_proxy 289 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698