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

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 7 years 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 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 return success ? spdyproxy::SUCCEEDED_PROXY_ALREADY_ENABLED
59 : spdyproxy::FAILED_PROXY_DISABLED;
bengr 2014/01/02 17:30:40 I'd rewrite these with if/else instead of the tern
marq (ping after 24h) 2014/01/02 20:45:47 Done.
60 else
61 return success ? spdyproxy::SUCCEEDED_PROXY_ENABLED
62 : spdyproxy::FAILED_PROXY_ALREADY_DISABLED;
63 }
64
54 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() 65 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase()
55 : testing::Test() { 66 : testing::Test() {
56 } 67 }
57 68
58 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} 69 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {}
59 70
60 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { 71 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() {
61 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 72 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
62 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); 73 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
63 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 74 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 template void 122 template void
112 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(); 123 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>();
113 template void 124 template void
114 DataReductionProxySettingsTestBase::ResetSettings< 125 DataReductionProxySettingsTestBase::ResetSettings<
115 DataReductionProxySettingsAndroid>(); 126 DataReductionProxySettingsAndroid>();
116 127
117 template <class C> 128 template <class C>
118 void DataReductionProxySettingsTestBase::SetProbeResult( 129 void DataReductionProxySettingsTestBase::SetProbeResult(
119 const std::string& test_url, 130 const std::string& test_url,
120 const std::string& response, 131 const std::string& response,
132 ProbeURLFetchResult result,
121 bool success, 133 bool success,
122 int expected_calls) { 134 int expected_calls) {
123 MockDataReductionProxySettings<C>* settings = 135 MockDataReductionProxySettings<C>* settings =
124 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); 136 static_cast<MockDataReductionProxySettings<C>*>(settings_.get());
125 if (0 == expected_calls) { 137 if (0 == expected_calls) {
126 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); 138 EXPECT_CALL(*settings, GetURLFetcher()).Times(0);
139 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0);
127 } else { 140 } else {
141 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1);
128 EXPECT_CALL(*settings, GetURLFetcher()) 142 EXPECT_CALL(*settings, GetURLFetcher())
129 .Times(expected_calls) 143 .Times(expected_calls)
130 .WillRepeatedly(Return(new net::FakeURLFetcher( 144 .WillRepeatedly(Return(new net::FakeURLFetcher(
131 GURL(test_url), 145 GURL(test_url),
132 settings, 146 settings,
133 response, 147 response,
134 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, 148 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR,
135 success ? net::URLRequestStatus::SUCCESS : 149 success ? net::URLRequestStatus::SUCCESS :
136 net::URLRequestStatus::FAILED))); 150 net::URLRequestStatus::FAILED)));
137 } 151 }
138 } 152 }
139 153
140 // Explicitly generate required instantiations. 154 // Explicitly generate required instantiations.
141 template void 155 template void
142 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( 156 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>(
143 const std::string& test_url, 157 const std::string& test_url,
144 const std::string& response, 158 const std::string& response,
159 ProbeURLFetchResult result,
145 bool success, 160 bool success,
146 int expected_calls); 161 int expected_calls);
147 template void 162 template void
148 DataReductionProxySettingsTestBase::SetProbeResult< 163 DataReductionProxySettingsTestBase::SetProbeResult<
149 DataReductionProxySettingsAndroid>(const std::string& test_url, 164 DataReductionProxySettingsAndroid>(
150 const std::string& response, 165 const std::string& test_url,
bengr 2014/01/02 17:30:40 Can this be moved up a line?
marq (ping after 24h) 2014/01/02 20:45:47 Done.
151 bool success, 166 const std::string& response,
152 int expected_calls); 167 ProbeURLFetchResult result,
168 bool success,
169 int expected_calls);
153 170
154 void DataReductionProxySettingsTestBase::CheckProxyPref( 171 void DataReductionProxySettingsTestBase::CheckProxyPref(
155 const std::string& expected_servers, 172 const std::string& expected_servers,
156 const std::string& expected_mode) { 173 const std::string& expected_mode) {
157 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy); 174 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
158 std::string mode; 175 std::string mode;
159 std::string server; 176 std::string server;
160 dict->GetString("mode", &mode); 177 dict->GetString("mode", &mode);
161 ASSERT_EQ(expected_mode, mode); 178 ASSERT_EQ(expected_mode, mode);
162 dict->GetString("server", &server); 179 dict->GetString("server", &server);
(...skipping 16 matching lines...) Expand all
179 } 196 }
180 197
181 void DataReductionProxySettingsTestBase::CheckProbe( 198 void DataReductionProxySettingsTestBase::CheckProbe(
182 bool initially_enabled, 199 bool initially_enabled,
183 const std::string& probe_url, 200 const std::string& probe_url,
184 const std::string& response, 201 const std::string& response,
185 bool request_success, 202 bool request_success,
186 bool expected_enabled, 203 bool expected_enabled,
187 bool expected_restricted) { 204 bool expected_restricted) {
188 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled); 205 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
206 if (initially_enabled)
207 settings_->enabled_by_user_ = true;
189 settings_->restricted_by_carrier_ = false; 208 settings_->restricted_by_carrier_ = false;
190 SetProbeResult( 209 SetProbeResult(probe_url,
191 probe_url, response, request_success, initially_enabled ? 1 : 0); 210 response,
211 FetchResult(initially_enabled,
212 request_success && (response == "OK")),
bengr 2014/01/02 17:30:40 maybe rename as 'request_succeeded'
213 request_success,
214 initially_enabled ? 1 : 0);
192 settings_->MaybeActivateDataReductionProxy(false); 215 settings_->MaybeActivateDataReductionProxy(false);
193 base::MessageLoop::current()->RunUntilIdle(); 216 base::MessageLoop::current()->RunUntilIdle();
194 CheckProxyConfigs(expected_enabled, expected_restricted); 217 CheckProxyConfigs(expected_enabled, expected_restricted);
195 } 218 }
196 219
197 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( 220 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange(
198 const std::string& probe_url, 221 const std::string& probe_url,
199 const std::string& response, 222 const std::string& response,
200 bool request_success, 223 bool request_success,
201 bool expected_restricted) { 224 bool expected_restricted) {
202 SetProbeResult(probe_url, response, request_success, 1); 225 SetProbeResult(probe_url,
226 response,
227 FetchResult(!settings_->restricted_by_carrier_,
228 request_success && (response == "OK")),
229 request_success,
230 1);
203 settings_->OnIPAddressChanged(); 231 settings_->OnIPAddressChanged();
204 base::MessageLoop::current()->RunUntilIdle(); 232 base::MessageLoop::current()->RunUntilIdle();
205 CheckProxyConfigs(true, expected_restricted); 233 CheckProxyConfigs(true, expected_restricted);
206 } 234 }
207 235
208 void DataReductionProxySettingsTestBase::CheckOnPrefChange( 236 void DataReductionProxySettingsTestBase::CheckOnPrefChange(
209 bool enabled, 237 bool enabled,
210 bool expected_enabled) { 238 bool expected_enabled) {
211 // Always have a sucessful probe for pref change tests. 239 // Always have a sucessful probe for pref change tests.
212 SetProbeResult( 240 SetProbeResult(kProbeURLWithOKResponse,
213 kProbeURLWithOKResponse, "OK", true, expected_enabled ? 1 : 0); 241 "OK",
242 FetchResult(enabled, true),
243 true,
244 expected_enabled ? 1 : 0);
214 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled); 245 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled);
215 base::MessageLoop::current()->RunUntilIdle(); 246 base::MessageLoop::current()->RunUntilIdle();
216 // Never expect the proxy to be restricted for pref change tests. 247 // Never expect the proxy to be restricted for pref change tests.
217 CheckProxyConfigs(expected_enabled, false); 248 CheckProxyConfigs(expected_enabled, false);
218 } 249 }
219 250
220 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( 251 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy(
221 bool enabled_at_startup) { 252 bool enabled_at_startup) {
222 AddProxyToCommandLine(); 253 AddProxyToCommandLine();
223 base::MessageLoopForUI loop; 254 base::MessageLoopForUI loop;
224 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled_at_startup); 255 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled_at_startup);
225 SetProbeResult( 256 SetProbeResult(kProbeURLWithOKResponse,
226 kProbeURLWithOKResponse, "OK", true, enabled_at_startup ? 1 : 0); 257 "OK",
258 FetchResult(enabled_at_startup, true),
259 true,
260 enabled_at_startup ? 1 : 0);
227 settings_->InitDataReductionProxySettings(); 261 settings_->InitDataReductionProxySettings();
228 base::MessageLoop::current()->RunUntilIdle(); 262 base::MessageLoop::current()->RunUntilIdle();
229 if (enabled_at_startup) { 263 if (enabled_at_startup) {
230 CheckProxyConfigs(enabled_at_startup, false); 264 CheckProxyConfigs(enabled_at_startup, false);
231 } else { 265 } else {
232 // This presumes the proxy preference hadn't been set up by Chrome. 266 // This presumes the proxy preference hadn't been set up by Chrome.
233 CheckProxyPref(std::string(), std::string()); 267 CheckProxyPref(std::string(), std::string());
234 } 268 }
235 } 269 }
236 270
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true); 521 CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true);
488 // The proxy is disabled initially. Probes should not be emitted to change 522 // The proxy is disabled initially. Probes should not be emitted to change
489 // state. 523 // state.
490 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false); 524 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false);
491 } 525 }
492 526
493 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) { 527 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) {
494 AddProxyToCommandLine(); 528 AddProxyToCommandLine();
495 base::MessageLoopForUI loop; 529 base::MessageLoopForUI loop;
496 // The proxy is enabled initially. 530 // The proxy is enabled initially.
531 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
532 settings_->spdy_proxy_auth_enabled_.Init(
533 prefs::kSpdyProxyAuthEnabled,
534 settings_->GetOriginalProfilePrefs());
497 settings_->enabled_by_user_ = true; 535 settings_->enabled_by_user_ = true;
498 settings_->restricted_by_carrier_ = false; 536 settings_->restricted_by_carrier_ = false;
499 settings_->SetProxyConfigs(true, false, true); 537 settings_->SetProxyConfigs(true, false, true);
500 // IP address change triggers a probe that succeeds. Proxy remains 538 // IP address change triggers a probe that succeeds. Proxy remains
501 // unrestricted. 539 // unrestricted.
502 CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false); 540 CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false);
503 // IP address change triggers a probe that fails. Proxy is restricted. 541 // IP address change triggers a probe that fails. Proxy is restricted.
504 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true); 542 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
505 // IP address change triggers a probe that fails. Proxy remains restricted. 543 // IP address change triggers a probe that fails. Proxy remains restricted.
506 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true); 544 CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true);
507 // IP address change triggers a probe that succeed. Proxy is unrestricted. 545 // IP address change triggers a probe that succeed. Proxy is unrestricted.
508 CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false); 546 CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false);
509 } 547 }
510 548
511 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) { 549 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) {
512 AddProxyToCommandLine(); 550 AddProxyToCommandLine();
513 settings_->InitPrefMembers(); 551 settings_->InitPrefMembers();
514 base::MessageLoopForUI loop; 552 base::MessageLoopForUI loop;
515 // The proxy is enabled initially. 553 // The proxy is enabled initially.
516 settings_->enabled_by_user_ = true; 554 settings_->enabled_by_user_ = true;
517 settings_->SetProxyConfigs(true, false, true); 555 settings_->SetProxyConfigs(true, false, true);
518 // The pref is disabled, so correspondingly should be the proxy. 556 // The pref is disabled, so correspondingly should be the proxy.
519 CheckOnPrefChange(false, false); 557 CheckOnPrefChange(false, false);
520 // The pref is enabled, so correspondingly should be the proxy. 558 // The pref is enabled, so correspondingly should be the proxy.
521 CheckOnPrefChange(true, true); 559 CheckOnPrefChange(true, true);
522 } 560 }
523 561
524 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) { 562 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) {
563 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
564 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_ENABLED));
525 CheckInitDataReductionProxy(true); 565 CheckInitDataReductionProxy(true);
526 } 566 }
527 567
528 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) { 568 TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) {
529 // InitDataReductionProxySettings with the preference off will directly call 569 // InitDataReductionProxySettings with the preference off will directly call
530 // LogProxyState. 570 // LogProxyState.
531 MockSettings* settings = static_cast<MockSettings*>(settings_.get()); 571 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
532 EXPECT_CALL(*settings, LogProxyState(false, false, true)).Times(1); 572 EXPECT_CALL(*settings, LogProxyState(false, false, true)).Times(1);
573 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_DISABLED));
533 CheckInitDataReductionProxy(false); 574 CheckInitDataReductionProxy(false);
534 } 575 }
535 576
536 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { 577 TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) {
537 DataReductionProxySettings::ContentLengthList result = 578 DataReductionProxySettings::ContentLengthList result =
538 settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); 579 settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
539 580
540 ASSERT_FALSE(result.empty()); 581 ASSERT_FALSE(result.empty());
541 ASSERT_EQ(spdyproxy::kNumDaysInHistory, result.size()); 582 ASSERT_EQ(spdyproxy::kNumDaysInHistory, result.size());
542 583
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 647 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
607 std::string headers(tests[i].headers); 648 std::string headers(tests[i].headers);
608 HeadersToRaw(&headers); 649 HeadersToRaw(&headers);
609 scoped_refptr<net::HttpResponseHeaders> parsed( 650 scoped_refptr<net::HttpResponseHeaders> parsed(
610 new net::HttpResponseHeaders(headers)); 651 new net::HttpResponseHeaders(headers));
611 652
612 EXPECT_EQ(tests[i].expected_result, 653 EXPECT_EQ(tests[i].expected_result,
613 DataReductionProxySettings::WasFetchedViaProxy(parsed)); 654 DataReductionProxySettings::WasFetchedViaProxy(parsed));
614 } 655 }
615 } 656 }
657
658 TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) {
659 // We don't call |AddProxyToCommandLine()|, so the proxy feature
bengr 2014/01/02 17:30:40 Don't use 'We'. Maybe: // The proxy feature shou
660 // should be unavailable.
661 EXPECT_FALSE(DataReductionProxySettings::IsDataReductionProxyAllowed());
662 MockSettings* settings = static_cast<MockSettings*>(settings_.get());
663 EXPECT_CALL(*settings, RecordStartupState(spdyproxy::PROXY_NOT_AVAILABLE));
664 settings_->InitDataReductionProxySettings();
665 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698