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

Side by Side Diff: chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.cc

Issue 111113006: Metrics fix for data reduction proxy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 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
« no previous file with comments | « chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/md5.h" 8 #include "base/md5.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
10 #include "base/prefs/pref_registry_simple.h" 12 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
12 #include "base/prefs/scoped_user_pref_update.h" 14 #include "base/prefs/scoped_user_pref_update.h"
13 #include "base/prefs/testing_pref_service.h" 15 #include "base/prefs/testing_pref_service.h"
14 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h" 18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" 19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
18 #include "chrome/browser/prefs/proxy_prefs.h" 20 #include "chrome/browser/prefs/proxy_prefs.h"
19 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
(...skipping 24 matching lines...) Expand all
44 const char kProbeURLWithNoResponse[] = "http://no.org/"; 46 const char kProbeURLWithNoResponse[] = "http://no.org/";
45 47
46 // Transform "normal"-looking headers (\n-separated) to the appropriate 48 // Transform "normal"-looking headers (\n-separated) to the appropriate
47 // input format for ParseRawHeaders (\0-separated). 49 // input format for ParseRawHeaders (\0-separated).
48 void HeadersToRaw(std::string* headers) { 50 void HeadersToRaw(std::string* headers) {
49 std::replace(headers->begin(), headers->end(), '\n', '\0'); 51 std::replace(headers->begin(), headers->end(), '\n', '\0');
50 if (!headers->empty()) 52 if (!headers->empty())
51 *headers += '\0'; 53 *headers += '\0';
52 } 54 }
53 55
56 ProbeURLFetchResult FetchResult(bool enabled, bool success) {
57 if (enabled) {
58 if (success)
59 return spdyproxy::SUCCEEDED_PROXY_ALREADY_ENABLED;
60 else
61 return spdyproxy::FAILED_PROXY_DISABLED;
62 } else {
63 if (success)
64 return spdyproxy::SUCCEEDED_PROXY_ENABLED;
65 else
66 return spdyproxy::FAILED_PROXY_ALREADY_DISABLED;
67 }
68 }
69
54 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() 70 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
55 : testing::Test() { 71 : testing::Test() {
56 } 72 }
57 73
58 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} 74 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
59 75
60 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { 76 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() {
61 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 77 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
62 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); 78 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
63 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 79 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 template void 128 template void
113 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(); 129 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>();
114 template void 130 template void
115 DataReductionProxySettingsTestBase::ResetSettings< 131 DataReductionProxySettingsTestBase::ResetSettings<
116 DataReductionProxySettingsAndroid>(); 132 DataReductionProxySettingsAndroid>();
117 133
118 template <class C> 134 template <class C>
119 void DataReductionProxySettingsTestBase::SetProbeResult( 135 void DataReductionProxySettingsTestBase::SetProbeResult(
120 const std::string& test_url, 136 const std::string& test_url,
121 const std::string& response, 137 const std::string& response,
138 ProbeURLFetchResult result,
122 bool success, 139 bool success,
123 int expected_calls) { 140 int expected_calls) {
124 MockDataReductionProxySettings<C>* settings = 141 MockDataReductionProxySettings<C>* settings =
125 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); 142 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
126 if (0 == expected_calls) { 143 if (0 == expected_calls) {
127 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); 144 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
145 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0);
128 } else { 146 } else {
147 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
129 EXPECT_CALL(*settings, GetURLFetcher()) 148 EXPECT_CALL(*settings, GetURLFetcher())
130 .Times(expected_calls) 149 .Times(expected_calls)
131 .WillRepeatedly(Return(new net::FakeURLFetcher( 150 .WillRepeatedly(Return(new net::FakeURLFetcher(
132 GURL(test_url), 151 GURL(test_url),
133 settings, 152 settings,
134 response, 153 response,
135 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, 154 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
136 success ? net::URLRequestStatus::SUCCESS : 155 success ? net::URLRequestStatus::SUCCESS :
137 net::URLRequestStatus::FAILED))); 156 net::URLRequestStatus::FAILED)));
138 } 157 }
139 } 158 }
140 159
141 // Explicitly generate required instantiations. 160 // Explicitly generate required instantiations.
142 template void 161 template void
143 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( 162 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
144 const std::string& test_url, 163 const std::string& test_url,
145 const std::string& response, 164 const std::string& response,
165 ProbeURLFetchResult result,
146 bool success, 166 bool success,
147 int expected_calls); 167 int expected_calls);
148 template void 168 template void
149 DataReductionProxySettingsTestBase::SetProbeResult< 169 DataReductionProxySettingsTestBase::SetProbeResult<
150 DataReductionProxySettingsAndroid>(const std::string& test_url, 170 DataReductionProxySettingsAndroid>(const std::string& test_url,
151 const std::string& response, 171 const std::string& response,
172 ProbeURLFetchResult result,
152 bool success, 173 bool success,
153 int expected_calls); 174 int expected_calls);
154 175
155 void DataReductionProxySettingsTestBase::CheckProxyPref( 176 void DataReductionProxySettingsTestBase::CheckProxyPref(
156 const std::string& expected_servers, 177 const std::string& expected_servers,
157 const std::string& expected_mode) { 178 const std::string& expected_mode) {
158 const base::DictionaryValue* dict = 179 const base::DictionaryValue* dict =
159 pref_service_.GetDictionary(prefs::kProxy); 180 pref_service_.GetDictionary(prefs::kProxy);
160 std::string mode; 181 std::string mode;
161 std::string server; 182 std::string server;
(...skipping 15 matching lines...) Expand all
177 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); 198 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
178 } else { 199 } else {
179 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 200 CheckProxyPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
180 } 201 }
181 } 202 }
182 203
183 void DataReductionProxySettingsTestBase::CheckProbe( 204 void DataReductionProxySettingsTestBase::CheckProbe(
184 bool initially_enabled, 205 bool initially_enabled,
185 const std::string& probe_url, 206 const std::string& probe_url,
186 const std::string& response, 207 const std::string& response,
187 bool request_success, 208 bool request_succeeded,
188 bool expected_enabled, 209 bool expected_enabled,
189 bool expected_restricted) { 210 bool expected_restricted) {
190 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled); 211 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
212 if (initially_enabled)
213 settings_->enabled_by_user_ = true;
191 settings_->restricted_by_carrier_ = false; 214 settings_->restricted_by_carrier_ = false;
192 SetProbeResult( 215 SetProbeResult(probe_url,
193 probe_url, response, request_success, initially_enabled ? 1 : 0); 216 response,
217 FetchResult(initially_enabled,
218 request_succeeded && (response == "OK")),
219 request_succeeded,
220 initially_enabled ? 1 : 0);
194 settings_->MaybeActivateDataReductionProxy(false); 221 settings_->MaybeActivateDataReductionProxy(false);
195 base::MessageLoop::current()->RunUntilIdle(); 222 base::MessageLoop::current()->RunUntilIdle();
196 CheckProxyConfigs(expected_enabled, expected_restricted); 223 CheckProxyConfigs(expected_enabled, expected_restricted);
197 } 224 }
198 225
199 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( 226 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange(
200 const std::string& probe_url, 227 const std::string& probe_url,
201 const std::string& response, 228 const std::string& response,
202 bool request_success, 229 bool request_succeeded,
203 bool expected_restricted) { 230 bool expected_restricted) {
204 SetProbeResult(probe_url, response, request_success, 1); 231 SetProbeResult(probe_url,
232 response,
233 FetchResult(!settings_->restricted_by_carrier_,
234 request_succeeded && (response == "OK")),
235 request_succeeded,
236 1);
205 settings_->OnIPAddressChanged(); 237 settings_->OnIPAddressChanged();
206 base::MessageLoop::current()->RunUntilIdle(); 238 base::MessageLoop::current()->RunUntilIdle();
207 CheckProxyConfigs(true, expected_restricted); 239 CheckProxyConfigs(true, expected_restricted);
208 } 240 }
209 241
210 void DataReductionProxySettingsTestBase::CheckOnPrefChange( 242 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
211 bool enabled, 243 bool enabled,
212 bool expected_enabled) { 244 bool expected_enabled) {
213 // Always have a sucessful probe for pref change tests. 245 // Always have a sucessful probe for pref change tests.
214 SetProbeResult( 246 SetProbeResult(kProbeURLWithOKResponse,
215 kProbeURLWithOKResponse, "OK", true, expected_enabled ? 1 : 0); 247 "OK",
248 FetchResult(enabled, true),
249 true,
250 expected_enabled ? 1 : 0);
216 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled); 251 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled);
217 base::MessageLoop::current()->RunUntilIdle(); 252 base::MessageLoop::current()->RunUntilIdle();
218 // Never expect the proxy to be restricted for pref change tests. 253 // Never expect the proxy to be restricted for pref change tests.
219 CheckProxyConfigs(expected_enabled, false); 254 CheckProxyConfigs(expected_enabled, false);
220 } 255 }
221 256
222 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( 257 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
223 bool enabled_at_startup) { 258 bool enabled_at_startup) {
224 AddProxyToCommandLine(); 259 AddProxyToCommandLine();
225 base::MessageLoopForUI loop; 260 base::MessageLoopForUI loop;
226 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled_at_startup); 261 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled_at_startup);
227 SetProbeResult( 262 SetProbeResult(kProbeURLWithOKResponse,
228 kProbeURLWithOKResponse, "OK", true, enabled_at_startup ? 1 : 0); 263 "OK",
264 FetchResult(enabled_at_startup, true),
265 true,
266 enabled_at_startup ? 1 : 0);
229 settings_->InitDataReductionProxySettings(); 267 settings_->InitDataReductionProxySettings();
230 base::MessageLoop::current()->RunUntilIdle(); 268 base::MessageLoop::current()->RunUntilIdle();
231 if (enabled_at_startup) { 269 if (enabled_at_startup) {
232 CheckProxyConfigs(enabled_at_startup, false); 270 CheckProxyConfigs(enabled_at_startup, false);
233 } else { 271 } else {
234 // This presumes the proxy preference hadn't been set up by Chrome. 272 // This presumes the proxy preference hadn't been set up by Chrome.
235 CheckProxyPref(std::string(), std::string()); 273 CheckProxyPref(std::string(), std::string());
236 } 274 }
237 } 275 }
238 276
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true); 528 CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true);
491 // The proxy is disabled initially. Probes should not be emitted to change 529 // The proxy is disabled initially. Probes should not be emitted to change
492 // state. 530 // state.
493 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false); 531 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false);
494 } 532 }
495 533
496 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) { 534 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) {
497 AddProxyToCommandLine(); 535 AddProxyToCommandLine();
498 base::MessageLoopForUI loop; 536 base::MessageLoopForUI loop;
499 // The proxy is enabled initially. 537 // The proxy is enabled initially.
538 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
539 settings_->spdy_proxy_auth_enabled_.Init(
540 prefs::kSpdyProxyAuthEnabled,
541 settings_->GetOriginalProfilePrefs());
500 settings_->enabled_by_user_ = true; 542 settings_->enabled_by_user_ = true;
501 settings_->restricted_by_carrier_ = false; 543 settings_->restricted_by_carrier_ = false;
502 settings_->SetProxyConfigs(true, false, true); 544 settings_->SetProxyConfigs(true, false, true);
503 // IP address change triggers a probe that succeeds. Proxy remains 545 // IP address change triggers a probe that succeeds. Proxy remains
504 // unrestricted. 546 // unrestricted.
505 CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false); 547 CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false);
506 // IP address change triggers a probe that fails. Proxy is restricted. 548 // IP address change triggers a probe that fails. Proxy is restricted.
507 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true); 549 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
508 // IP address change triggers a probe that fails. Proxy remains restricted. 550 // IP address change triggers a probe that fails. Proxy remains restricted.
509 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true); 551 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
510 // IP address change triggers a probe that succeed. Proxy is unrestricted. 552 // IP address change triggers a probe that succeed. Proxy is unrestricted.
511 CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false); 553 CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false);
512 } 554 }
513 555
514 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) { 556 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) {
515 AddProxyToCommandLine(); 557 AddProxyToCommandLine();
516 settings_->InitPrefMembers(); 558 settings_->InitPrefMembers();
517 base::MessageLoopForUI loop; 559 base::MessageLoopForUI loop;
518 // The proxy is enabled initially. 560 // The proxy is enabled initially.
519 settings_->enabled_by_user_ = true; 561 settings_->enabled_by_user_ = true;
520 settings_->SetProxyConfigs(true, false, true); 562 settings_->SetProxyConfigs(true, false, true);
521 // The pref is disabled, so correspondingly should be the proxy. 563 // The pref is disabled, so correspondingly should be the proxy.
522 CheckOnPrefChange(false, false); 564 CheckOnPrefChange(false, false);
523 // The pref is enabled, so correspondingly should be the proxy. 565 // The pref is enabled, so correspondingly should be the proxy.
524 CheckOnPrefChange(true, true); 566 CheckOnPrefChange(true, true);
525 } 567 }
526 568
527 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) { 569 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) {
570 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
571 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_ENABLED));
528 CheckInitDataReductionProxy(true); 572 CheckInitDataReductionProxy(true);
529 } 573 }
530 574
531 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) { 575 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) {
532 // InitDataReductionProxySettings with the preference off will directly call 576 // InitDataReductionProxySettings with the preference off will directly call
533 // LogProxyState. 577 // LogProxyState.
534 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); 578 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
535 EXPECT_CALL(*settings, LogProxyState(false, false, true)).Times(1); 579 EXPECT_CALL(*settings, LogProxyState(false, false, true)).Times(1);
580 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_DISABLED));
536 CheckInitDataReductionProxy(false); 581 CheckInitDataReductionProxy(false);
537 } 582 }
538 583
539 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { 584 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) {
540 DataReductionProxySettings::ContentLengthList result = 585 DataReductionProxySettings::ContentLengthList result =
541 settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); 586 settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
542 587
543 ASSERT_FALSE(result.empty()); 588 ASSERT_FALSE(result.empty());
544 ASSERT_EQ(spdyproxy::kNumDaysInHistory, result.size()); 589 ASSERT_EQ(spdyproxy::kNumDaysInHistory, result.size());
545 590
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 654 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
610 std::string headers(tests[i].headers); 655 std::string headers(tests[i].headers);
611 HeadersToRaw(&headers); 656 HeadersToRaw(&headers);
612 scoped_refptr<net::HttpResponseHeaders> parsed( 657 scoped_refptr<net::HttpResponseHeaders> parsed(
613 new net::HttpResponseHeaders(headers)); 658 new net::HttpResponseHeaders(headers));
614 659
615 EXPECT_EQ(tests[i].expected_result, 660 EXPECT_EQ(tests[i].expected_result,
616 DataReductionProxySettings::WasFetchedViaProxy(parsed)); 661 DataReductionProxySettings::WasFetchedViaProxy(parsed));
617 } 662 }
618 } 663 }
664
665 TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) {
666 // No call to |AddProxyToCommandLine()| was made, so the proxy feature
667 // should be unavailable.
668 EXPECT_FALSE(DataReductionProxySettings::IsDataReductionProxyAllowed());
669 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
670 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_NOT_AVAILABLE));
671 settings_->InitDataReductionProxySettings();
672 }
OLDNEW
« no previous file with comments | « chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698