| OLD | NEW |
| 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 const char kWarmupURLWithNoContentResponse[] = "http://warm.org/"; |
| 26 | 27 |
| 27 const char kProxy[] = "proxy"; | 28 const char kProxy[] = "proxy"; |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 namespace data_reduction_proxy { | 32 namespace data_reduction_proxy { |
| 32 | 33 |
| 33 // Transform "normal"-looking headers (\n-separated) to the appropriate | 34 // Transform "normal"-looking headers (\n-separated) to the appropriate |
| 34 // input format for ParseRawHeaders (\0-separated). | 35 // input format for ParseRawHeaders (\0-separated). |
| 35 void HeadersToRaw(std::string* headers) { | 36 void HeadersToRaw(std::string* headers) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 if (promo_allowed) | 145 if (promo_allowed) |
| 145 flags |= DataReductionProxyParams::kPromoAllowed; | 146 flags |= DataReductionProxyParams::kPromoAllowed; |
| 146 MockDataReductionProxySettings<C>* settings = | 147 MockDataReductionProxySettings<C>* settings = |
| 147 new MockDataReductionProxySettings<C>(flags); | 148 new MockDataReductionProxySettings<C>(flags); |
| 148 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) | 149 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) |
| 149 .Times(AnyNumber()) | 150 .Times(AnyNumber()) |
| 150 .WillRepeatedly(Return(&pref_service_)); | 151 .WillRepeatedly(Return(&pref_service_)); |
| 151 EXPECT_CALL(*settings, GetLocalStatePrefs()) | 152 EXPECT_CALL(*settings, GetLocalStatePrefs()) |
| 152 .Times(AnyNumber()) | 153 .Times(AnyNumber()) |
| 153 .WillRepeatedly(Return(&pref_service_)); | 154 .WillRepeatedly(Return(&pref_service_)); |
| 154 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); | 155 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); |
| 156 EXPECT_CALL(*settings, GetURLFetcherForWarmup()).Times(0); |
| 155 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); | 157 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); |
| 156 settings_.reset(settings); | 158 settings_.reset(settings); |
| 157 settings_->configurator_.reset(new TestDataReductionProxyConfig()); | 159 settings_->configurator_.reset(new TestDataReductionProxyConfig()); |
| 158 } | 160 } |
| 159 | 161 |
| 160 // Explicitly generate required instantiations. | 162 // Explicitly generate required instantiations. |
| 161 template void | 163 template void |
| 162 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>( | 164 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>( |
| 163 bool allowed, bool fallback_allowed, bool alt_allowed, bool promo_allowed); | 165 bool allowed, bool fallback_allowed, bool alt_allowed, bool promo_allowed); |
| 164 | 166 |
| 165 template <class C> | 167 template <class C> |
| 166 void DataReductionProxySettingsTestBase::SetProbeResult( | 168 void DataReductionProxySettingsTestBase::SetProbeResult( |
| 167 const std::string& test_url, | 169 const std::string& test_url, |
| 170 const std::string& warmup_test_url, |
| 168 const std::string& response, | 171 const std::string& response, |
| 169 ProbeURLFetchResult result, | 172 ProbeURLFetchResult result, |
| 170 bool success, | 173 bool success, |
| 171 int expected_calls) { | 174 int expected_calls) { |
| 172 MockDataReductionProxySettings<C>* settings = | 175 MockDataReductionProxySettings<C>* settings = |
| 173 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); | 176 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); |
| 174 if (0 == expected_calls) { | 177 if (0 == expected_calls) { |
| 175 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); | 178 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); |
| 179 EXPECT_CALL(*settings, GetURLFetcherForWarmup()).Times(0); |
| 176 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0); | 180 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0); |
| 177 } else { | 181 } else { |
| 178 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1); | 182 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1); |
| 179 EXPECT_CALL(*settings, GetURLFetcher()) | 183 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()) |
| 180 .Times(expected_calls) | 184 .Times(expected_calls) |
| 181 .WillRepeatedly(Return(new net::FakeURLFetcher( | 185 .WillRepeatedly(Return(new net::FakeURLFetcher( |
| 182 GURL(test_url), | 186 GURL(test_url), |
| 183 settings, | 187 settings, |
| 184 response, | 188 response, |
| 185 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, | 189 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, |
| 186 success ? net::URLRequestStatus::SUCCESS : | 190 success ? net::URLRequestStatus::SUCCESS : |
| 187 net::URLRequestStatus::FAILED))); | 191 net::URLRequestStatus::FAILED))); |
| 192 EXPECT_CALL(*settings, GetURLFetcherForWarmup()) |
| 193 .Times(expected_calls) |
| 194 .WillRepeatedly(Return(new net::FakeURLFetcher( |
| 195 GURL(warmup_test_url), |
| 196 settings, |
| 197 "", |
| 198 success ? net::HTTP_NO_CONTENT : net::HTTP_INTERNAL_SERVER_ERROR, |
| 199 success ? net::URLRequestStatus::SUCCESS : |
| 200 net::URLRequestStatus::FAILED))); |
| 188 } | 201 } |
| 189 } | 202 } |
| 190 | 203 |
| 191 // Explicitly generate required instantiations. | 204 // Explicitly generate required instantiations. |
| 192 template void | 205 template void |
| 193 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( | 206 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( |
| 194 const std::string& test_url, | 207 const std::string& test_url, |
| 208 const std::string& warmup_test_url, |
| 195 const std::string& response, | 209 const std::string& response, |
| 196 ProbeURLFetchResult result, | 210 ProbeURLFetchResult result, |
| 197 bool success, | 211 bool success, |
| 198 int expected_calls); | 212 int expected_calls); |
| 199 | 213 |
| 200 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 214 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
| 201 bool expected_enabled, | 215 bool expected_enabled, |
| 202 bool expected_restricted, | 216 bool expected_restricted, |
| 203 bool expected_fallback_restricted) { | 217 bool expected_fallback_restricted) { |
| 204 TestDataReductionProxyConfig* config = | 218 TestDataReductionProxyConfig* config = |
| 205 static_cast<TestDataReductionProxyConfig*>( | 219 static_cast<TestDataReductionProxyConfig*>( |
| 206 settings_->configurator_.get()); | 220 settings_->configurator_.get()); |
| 207 ASSERT_EQ(expected_restricted, config->restricted_); | 221 ASSERT_EQ(expected_restricted, config->restricted_); |
| 208 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); | 222 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); |
| 209 ASSERT_EQ(expected_enabled, config->enabled_); | 223 ASSERT_EQ(expected_enabled, config->enabled_); |
| 210 } | 224 } |
| 211 | 225 |
| 212 void DataReductionProxySettingsTestBase::CheckProbe( | 226 void DataReductionProxySettingsTestBase::CheckProbe( |
| 213 bool initially_enabled, | 227 bool initially_enabled, |
| 214 const std::string& probe_url, | 228 const std::string& probe_url, |
| 229 const std::string& warmup_url, |
| 215 const std::string& response, | 230 const std::string& response, |
| 216 bool request_succeeded, | 231 bool request_succeeded, |
| 217 bool expected_enabled, | 232 bool expected_enabled, |
| 218 bool expected_restricted, | 233 bool expected_restricted, |
| 219 bool expected_fallback_restricted) { | 234 bool expected_fallback_restricted) { |
| 220 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, | 235 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, |
| 221 initially_enabled); | 236 initially_enabled); |
| 222 if (initially_enabled) | 237 if (initially_enabled) |
| 223 settings_->enabled_by_user_ = true; | 238 settings_->enabled_by_user_ = true; |
| 224 settings_->restricted_by_carrier_ = false; | 239 settings_->restricted_by_carrier_ = false; |
| 225 SetProbeResult(probe_url, | 240 SetProbeResult(probe_url, |
| 241 warmup_url, |
| 226 response, | 242 response, |
| 227 FetchResult(initially_enabled, | 243 FetchResult(initially_enabled, |
| 228 request_succeeded && (response == "OK")), | 244 request_succeeded && (response == "OK")), |
| 229 request_succeeded, | 245 request_succeeded, |
| 230 initially_enabled ? 1 : 0); | 246 initially_enabled ? 1 : 0); |
| 231 settings_->MaybeActivateDataReductionProxy(false); | 247 settings_->MaybeActivateDataReductionProxy(false); |
| 232 base::MessageLoop::current()->RunUntilIdle(); | 248 base::MessageLoop::current()->RunUntilIdle(); |
| 233 CheckProxyConfigs(expected_enabled, | 249 CheckProxyConfigs(expected_enabled, |
| 234 expected_restricted, | 250 expected_restricted, |
| 235 expected_fallback_restricted); | 251 expected_fallback_restricted); |
| 236 } | 252 } |
| 237 | 253 |
| 238 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( | 254 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( |
| 239 const std::string& probe_url, | 255 const std::string& probe_url, |
| 256 const std::string& warmup_url, |
| 240 const std::string& response, | 257 const std::string& response, |
| 241 bool request_succeeded, | 258 bool request_succeeded, |
| 242 bool expected_restricted, | 259 bool expected_restricted, |
| 243 bool expected_fallback_restricted) { | 260 bool expected_fallback_restricted) { |
| 244 SetProbeResult(probe_url, | 261 SetProbeResult(probe_url, |
| 262 warmup_url, |
| 245 response, | 263 response, |
| 246 FetchResult(!settings_->restricted_by_carrier_, | 264 FetchResult(!settings_->restricted_by_carrier_, |
| 247 request_succeeded && (response == "OK")), | 265 request_succeeded && (response == "OK")), |
| 248 request_succeeded, | 266 request_succeeded, |
| 249 1); | 267 1); |
| 250 settings_->OnIPAddressChanged(); | 268 settings_->OnIPAddressChanged(); |
| 251 base::MessageLoop::current()->RunUntilIdle(); | 269 base::MessageLoop::current()->RunUntilIdle(); |
| 252 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted); | 270 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted); |
| 253 } | 271 } |
| 254 | 272 |
| 255 void DataReductionProxySettingsTestBase::CheckOnPrefChange( | 273 void DataReductionProxySettingsTestBase::CheckOnPrefChange( |
| 256 bool enabled, | 274 bool enabled, |
| 257 bool expected_enabled, | 275 bool expected_enabled, |
| 258 bool managed) { | 276 bool managed) { |
| 259 // Always have a sucessful probe for pref change tests. | 277 // Always have a sucessful probe for pref change tests. |
| 260 SetProbeResult(kProbeURLWithOKResponse, | 278 SetProbeResult(kProbeURLWithOKResponse, |
| 279 kWarmupURLWithNoContentResponse, |
| 261 "OK", | 280 "OK", |
| 262 FetchResult(enabled, true), | 281 FetchResult(enabled, true), |
| 263 true, | 282 true, |
| 264 expected_enabled ? 1 : 0); | 283 expected_enabled ? 1 : 0); |
| 265 if (managed) { | 284 if (managed) { |
| 266 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled, | 285 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled, |
| 267 base::Value::CreateBooleanValue(enabled)); | 286 base::Value::CreateBooleanValue(enabled)); |
| 268 } else { | 287 } else { |
| 269 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); | 288 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); |
| 270 } | 289 } |
| 271 base::MessageLoop::current()->RunUntilIdle(); | 290 base::MessageLoop::current()->RunUntilIdle(); |
| 272 // Never expect the proxy to be restricted for pref change tests. | 291 // Never expect the proxy to be restricted for pref change tests. |
| 273 CheckProxyConfigs(expected_enabled, false, false); | 292 CheckProxyConfigs(expected_enabled, false, false); |
| 274 } | 293 } |
| 275 | 294 |
| 276 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( | 295 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( |
| 277 bool enabled_at_startup) { | 296 bool enabled_at_startup) { |
| 278 base::MessageLoopForUI loop; | 297 base::MessageLoopForUI loop; |
| 279 SetProbeResult(kProbeURLWithOKResponse, | 298 SetProbeResult(kProbeURLWithOKResponse, |
| 299 kWarmupURLWithNoContentResponse, |
| 280 "OK", | 300 "OK", |
| 281 FetchResult(enabled_at_startup, true), | 301 FetchResult(enabled_at_startup, true), |
| 282 true, | 302 true, |
| 283 enabled_at_startup ? 1 : 0); | 303 enabled_at_startup ? 1 : 0); |
| 284 scoped_ptr<DataReductionProxyConfigurator> configurator( | 304 scoped_ptr<DataReductionProxyConfigurator> configurator( |
| 285 new TestDataReductionProxyConfig()); | 305 new TestDataReductionProxyConfig()); |
| 286 settings_->SetProxyConfigurator(configurator.Pass()); | 306 settings_->SetProxyConfigurator(configurator.Pass()); |
| 287 scoped_refptr<net::TestURLRequestContextGetter> request_context = | 307 scoped_refptr<net::TestURLRequestContextGetter> request_context = |
| 288 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 308 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); |
| 289 settings_->InitDataReductionProxySettings(&pref_service_, | 309 settings_->InitDataReductionProxySettings(&pref_service_, |
| 290 &pref_service_, | 310 &pref_service_, |
| 291 request_context.get()); | 311 request_context.get()); |
| 292 | 312 |
| 293 base::MessageLoop::current()->RunUntilIdle(); | 313 base::MessageLoop::current()->RunUntilIdle(); |
| 294 CheckProxyConfigs(enabled_at_startup, false, false); | 314 CheckProxyConfigs(enabled_at_startup, false, false); |
| 295 } | 315 } |
| 296 | 316 |
| 297 } // namespace data_reduction_proxy | 317 } // namespace data_reduction_proxy |
| OLD | NEW |