| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/metrics/variations/variations_service.h" | 5 #include "chrome/browser/metrics/variations/variations_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 36 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 namespace chrome_variations { | 39 namespace chrome_variations { |
| 40 | 40 |
| 41 namespace { | 41 namespace { |
| 42 | 42 |
| 43 // A test class used to validate expected functionality in VariationsService. | 43 // A test class used to validate expected functionality in VariationsService. |
| 44 class TestVariationsService : public VariationsService { | 44 class TestVariationsService : public VariationsService { |
| 45 public: | 45 public: |
| 46 TestVariationsService( | 46 TestVariationsService(web_resource::TestRequestAllowedNotifier* test_notifier, |
| 47 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, | 47 PrefService* local_state) |
| 48 PrefService* local_state) | |
| 49 : VariationsService(make_scoped_ptr(new ChromeVariationsServiceClient()), | 48 : VariationsService(make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 50 test_notifier.Pass(), | 49 test_notifier, |
| 51 local_state, | 50 local_state, |
| 52 NULL), | 51 NULL), |
| 53 intercepts_fetch_(true), | 52 intercepts_fetch_(true), |
| 54 fetch_attempted_(false), | 53 fetch_attempted_(false), |
| 55 seed_stored_(false) { | 54 seed_stored_(false) { |
| 56 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | 55 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
| 57 SetCreateTrialsFromSeedCalledForTesting(true); | 56 SetCreateTrialsFromSeedCalledForTesting(true); |
| 58 } | 57 } |
| 59 | 58 |
| 60 ~TestVariationsService() override {} | 59 ~TestVariationsService() override {} |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); | 257 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
| 259 }; | 258 }; |
| 260 | 259 |
| 261 TEST_F(VariationsServiceTest, GetVariationsServerURL) { | 260 TEST_F(VariationsServiceTest, GetVariationsServerURL) { |
| 262 TestVariationsPrefsStore prefs_store; | 261 TestVariationsPrefsStore prefs_store; |
| 263 PrefService* prefs = prefs_store.prefs(); | 262 PrefService* prefs = prefs_store.prefs(); |
| 264 const std::string default_variations_url = | 263 const std::string default_variations_url = |
| 265 VariationsService::GetDefaultVariationsServerURLForTesting(); | 264 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 266 | 265 |
| 267 std::string value; | 266 std::string value; |
| 268 TestVariationsService service( | 267 GURL url = VariationsService::GetVariationsServerURL(prefs, std::string()); |
| 269 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(prefs)), | |
| 270 prefs); | |
| 271 GURL url = service.GetVariationsServerURL(prefs, std::string()); | |
| 272 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 268 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 273 base::CompareCase::SENSITIVE)); | 269 base::CompareCase::SENSITIVE)); |
| 274 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 270 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 275 | 271 |
| 276 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); | 272 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); |
| 277 url = service.GetVariationsServerURL(prefs, std::string()); | 273 url = VariationsService::GetVariationsServerURL(prefs, std::string()); |
| 278 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 274 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 279 base::CompareCase::SENSITIVE)); | 275 base::CompareCase::SENSITIVE)); |
| 280 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 276 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 281 EXPECT_EQ("restricted", value); | 277 EXPECT_EQ("restricted", value); |
| 282 | 278 |
| 283 // The override value should take precedence over what's in prefs. | 279 // The override value should take precedence over what's in prefs. |
| 284 url = service.GetVariationsServerURL(prefs, "override"); | 280 url = VariationsService::GetVariationsServerURL(prefs, "override"); |
| 285 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 281 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 286 base::CompareCase::SENSITIVE)); | 282 base::CompareCase::SENSITIVE)); |
| 287 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 283 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 288 EXPECT_EQ("override", value); | 284 EXPECT_EQ("override", value); |
| 289 } | 285 } |
| 290 | 286 |
| 291 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { | 287 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { |
| 292 TestingPrefServiceSimple prefs; | 288 TestingPrefServiceSimple prefs; |
| 293 VariationsService::RegisterPrefs(prefs.registry()); | 289 VariationsService::RegisterPrefs(prefs.registry()); |
| 294 TestVariationsService service( | 290 const GURL url = |
| 295 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 291 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 296 &prefs); | |
| 297 const GURL url = service.GetVariationsServerURL(&prefs, std::string()); | |
| 298 | 292 |
| 299 std::string value; | 293 std::string value; |
| 300 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); | 294 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); |
| 301 EXPECT_FALSE(value.empty()); | 295 EXPECT_FALSE(value.empty()); |
| 302 } | 296 } |
| 303 | 297 |
| 304 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 298 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 305 TestingPrefServiceSimple prefs; | 299 TestingPrefServiceSimple prefs; |
| 306 VariationsService::RegisterPrefs(prefs.registry()); | 300 VariationsService::RegisterPrefs(prefs.registry()); |
| 307 | 301 |
| 308 // Pass ownership to TestVariationsService, but keep a weak pointer to | 302 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 309 // manipulate it for this test. | 303 // manipulate it for this test. |
| 310 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = | 304 web_resource::TestRequestAllowedNotifier* test_notifier = |
| 311 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)); | 305 new web_resource::TestRequestAllowedNotifier(&prefs); |
| 312 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); | 306 TestVariationsService test_service(test_notifier, &prefs); |
| 313 TestVariationsService test_service(test_notifier.Pass(), &prefs); | |
| 314 | 307 |
| 315 // Force the notifier to initially disallow requests. | 308 // Force the notifier to initially disallow requests. |
| 316 raw_notifier->SetRequestsAllowedOverride(false); | 309 test_notifier->SetRequestsAllowedOverride(false); |
| 317 test_service.StartRepeatedVariationsSeedFetch(); | 310 test_service.StartRepeatedVariationsSeedFetch(); |
| 318 EXPECT_FALSE(test_service.fetch_attempted()); | 311 EXPECT_FALSE(test_service.fetch_attempted()); |
| 319 | 312 |
| 320 raw_notifier->NotifyObserver(); | 313 test_notifier->NotifyObserver(); |
| 321 EXPECT_TRUE(test_service.fetch_attempted()); | 314 EXPECT_TRUE(test_service.fetch_attempted()); |
| 322 } | 315 } |
| 323 | 316 |
| 324 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { | 317 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { |
| 325 TestingPrefServiceSimple prefs; | 318 TestingPrefServiceSimple prefs; |
| 326 VariationsService::RegisterPrefs(prefs.registry()); | 319 VariationsService::RegisterPrefs(prefs.registry()); |
| 327 | 320 |
| 328 // Pass ownership to TestVariationsService, but keep a weak pointer to | 321 // Pass ownership to TestVariationsService, but keep a weak pointer to |
| 329 // manipulate it for this test. | 322 // manipulate it for this test. |
| 330 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = | 323 web_resource::TestRequestAllowedNotifier* test_notifier = |
| 331 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)); | 324 new web_resource::TestRequestAllowedNotifier(&prefs); |
| 332 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); | 325 TestVariationsService test_service(test_notifier, &prefs); |
| 333 TestVariationsService test_service(test_notifier.Pass(), &prefs); | |
| 334 | 326 |
| 335 raw_notifier->SetRequestsAllowedOverride(true); | 327 test_notifier->SetRequestsAllowedOverride(true); |
| 336 test_service.StartRepeatedVariationsSeedFetch(); | 328 test_service.StartRepeatedVariationsSeedFetch(); |
| 337 EXPECT_TRUE(test_service.fetch_attempted()); | 329 EXPECT_TRUE(test_service.fetch_attempted()); |
| 338 } | 330 } |
| 339 | 331 |
| 340 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 332 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 341 TestingPrefServiceSimple prefs; | 333 TestingPrefServiceSimple prefs; |
| 342 VariationsService::RegisterPrefs(prefs.registry()); | 334 VariationsService::RegisterPrefs(prefs.registry()); |
| 343 | 335 |
| 344 TestVariationsService service( | 336 TestVariationsService service( |
| 345 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 337 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 346 &prefs); | |
| 347 service.variations_server_url_ = | 338 service.variations_server_url_ = |
| 348 service.GetVariationsServerURL(&prefs, std::string()); | 339 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 349 service.set_intercepts_fetch(false); | 340 service.set_intercepts_fetch(false); |
| 350 | 341 |
| 351 net::TestURLFetcherFactory factory; | 342 net::TestURLFetcherFactory factory; |
| 352 service.DoActualFetch(); | 343 service.DoActualFetch(); |
| 353 | 344 |
| 354 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 345 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 355 SimulateServerResponse(net::HTTP_OK, fetcher); | 346 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 356 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 347 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| 357 | 348 |
| 358 EXPECT_FALSE(service.seed_stored()); | 349 EXPECT_FALSE(service.seed_stored()); |
| 359 service.OnURLFetchComplete(fetcher); | 350 service.OnURLFetchComplete(fetcher); |
| 360 EXPECT_TRUE(service.seed_stored()); | 351 EXPECT_TRUE(service.seed_stored()); |
| 361 } | 352 } |
| 362 | 353 |
| 363 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 354 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
| 364 const int non_ok_status_codes[] = { | 355 const int non_ok_status_codes[] = { |
| 365 net::HTTP_NO_CONTENT, | 356 net::HTTP_NO_CONTENT, |
| 366 net::HTTP_NOT_MODIFIED, | 357 net::HTTP_NOT_MODIFIED, |
| 367 net::HTTP_NOT_FOUND, | 358 net::HTTP_NOT_FOUND, |
| 368 net::HTTP_INTERNAL_SERVER_ERROR, | 359 net::HTTP_INTERNAL_SERVER_ERROR, |
| 369 net::HTTP_SERVICE_UNAVAILABLE, | 360 net::HTTP_SERVICE_UNAVAILABLE, |
| 370 }; | 361 }; |
| 371 | 362 |
| 372 TestingPrefServiceSimple prefs; | 363 TestingPrefServiceSimple prefs; |
| 373 VariationsService::RegisterPrefs(prefs.registry()); | 364 VariationsService::RegisterPrefs(prefs.registry()); |
| 374 | 365 |
| 375 VariationsService service( | 366 VariationsService service( |
| 376 make_scoped_ptr(new ChromeVariationsServiceClient()), | 367 make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 377 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 368 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 378 &prefs, NULL); | |
| 379 service.variations_server_url_ = | 369 service.variations_server_url_ = |
| 380 service.GetVariationsServerURL(&prefs, std::string()); | 370 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 381 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 371 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 382 net::TestURLFetcherFactory factory; | 372 net::TestURLFetcherFactory factory; |
| 383 service.DoActualFetch(); | 373 service.DoActualFetch(); |
| 384 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 374 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 385 | 375 |
| 386 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 376 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 387 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 377 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 388 service.OnURLFetchComplete(fetcher); | 378 service.OnURLFetchComplete(fetcher); |
| 389 | 379 |
| 390 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 380 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 391 } | 381 } |
| 392 } | 382 } |
| 393 | 383 |
| 394 TEST_F(VariationsServiceTest, CountryHeader) { | 384 TEST_F(VariationsServiceTest, CountryHeader) { |
| 395 TestingPrefServiceSimple prefs; | 385 TestingPrefServiceSimple prefs; |
| 396 VariationsService::RegisterPrefs(prefs.registry()); | 386 VariationsService::RegisterPrefs(prefs.registry()); |
| 397 | 387 |
| 398 TestVariationsService service( | 388 TestVariationsService service( |
| 399 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 389 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 400 &prefs); | |
| 401 service.variations_server_url_ = | 390 service.variations_server_url_ = |
| 402 service.GetVariationsServerURL(&prefs, std::string()); | 391 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 403 service.set_intercepts_fetch(false); | 392 service.set_intercepts_fetch(false); |
| 404 | 393 |
| 405 net::TestURLFetcherFactory factory; | 394 net::TestURLFetcherFactory factory; |
| 406 service.DoActualFetch(); | 395 service.DoActualFetch(); |
| 407 | 396 |
| 408 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 397 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 409 scoped_refptr<net::HttpResponseHeaders> headers = | 398 scoped_refptr<net::HttpResponseHeaders> headers = |
| 410 SimulateServerResponse(net::HTTP_OK, fetcher); | 399 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 411 headers->AddHeader("X-Country: test"); | 400 headers->AddHeader("X-Country: test"); |
| 412 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 401 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| 413 | 402 |
| 414 EXPECT_FALSE(service.seed_stored()); | 403 EXPECT_FALSE(service.seed_stored()); |
| 415 service.OnURLFetchComplete(fetcher); | 404 service.OnURLFetchComplete(fetcher); |
| 416 EXPECT_TRUE(service.seed_stored()); | 405 EXPECT_TRUE(service.seed_stored()); |
| 417 EXPECT_EQ("test", service.stored_country()); | 406 EXPECT_EQ("test", service.stored_country()); |
| 418 } | 407 } |
| 419 | 408 |
| 420 TEST_F(VariationsServiceTest, Observer) { | 409 TEST_F(VariationsServiceTest, Observer) { |
| 421 TestingPrefServiceSimple prefs; | 410 TestingPrefServiceSimple prefs; |
| 422 VariationsService::RegisterPrefs(prefs.registry()); | 411 VariationsService::RegisterPrefs(prefs.registry()); |
| 423 VariationsService service( | 412 VariationsService service( |
| 424 make_scoped_ptr(new ChromeVariationsServiceClient()), | 413 make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 425 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 414 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 426 &prefs, NULL); | |
| 427 | 415 |
| 428 struct { | 416 struct { |
| 429 int normal_count; | 417 int normal_count; |
| 430 int best_effort_count; | 418 int best_effort_count; |
| 431 int critical_count; | 419 int critical_count; |
| 432 int expected_best_effort_notifications; | 420 int expected_best_effort_notifications; |
| 433 int expected_crtical_notifications; | 421 int expected_crtical_notifications; |
| 434 } cases[] = { | 422 } cases[] = { |
| 435 {0, 0, 0, 0, 0}, | 423 {0, 0, 0, 0, 0}, |
| 436 {1, 0, 0, 0, 0}, | 424 {1, 0, 0, 0, 0}, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, | 503 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, |
| 516 {"badversion,ca", "20.0.0.0", nullptr, "", "", | 504 {"badversion,ca", "20.0.0.0", nullptr, "", "", |
| 517 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, | 505 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, |
| 518 }; | 506 }; |
| 519 | 507 |
| 520 for (const auto& test : test_cases) { | 508 for (const auto& test : test_cases) { |
| 521 TestingPrefServiceSimple prefs; | 509 TestingPrefServiceSimple prefs; |
| 522 VariationsService::RegisterPrefs(prefs.registry()); | 510 VariationsService::RegisterPrefs(prefs.registry()); |
| 523 VariationsService service( | 511 VariationsService service( |
| 524 make_scoped_ptr(new ChromeVariationsServiceClient()), | 512 make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 525 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 513 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 526 &prefs, NULL); | |
| 527 | 514 |
| 528 if (test.pref_value_before) { | 515 if (test.pref_value_before) { |
| 529 base::ListValue list_value; | 516 base::ListValue list_value; |
| 530 for (const std::string& component : | 517 for (const std::string& component : |
| 531 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | 518 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, |
| 532 base::SPLIT_WANT_ALL)) { | 519 base::SPLIT_WANT_ALL)) { |
| 533 list_value.AppendString(component); | 520 list_value.AppendString(component); |
| 534 } | 521 } |
| 535 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | 522 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); |
| 536 } | 523 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 560 << test.pref_value_before << ", " << test.version << ", " | 547 << test.pref_value_before << ", " << test.version << ", " |
| 561 << test.latest_country_code; | 548 << test.latest_country_code; |
| 562 | 549 |
| 563 histogram_tester.ExpectUniqueSample( | 550 histogram_tester.ExpectUniqueSample( |
| 564 "Variations.LoadPermanentConsistencyCountryResult", | 551 "Variations.LoadPermanentConsistencyCountryResult", |
| 565 test.expected_result, 1); | 552 test.expected_result, 1); |
| 566 } | 553 } |
| 567 } | 554 } |
| 568 | 555 |
| 569 } // namespace chrome_variations | 556 } // namespace chrome_variations |
| OLD | NEW |