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" |
11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
17 #include "base/version.h" | 17 #include "base/version.h" |
| 18 #include "chrome/browser/metrics/variations/chrome_variations_service_client.h" |
18 #include "chrome/test/base/testing_browser_process.h" | 19 #include "chrome/test/base/testing_browser_process.h" |
19 #include "chrome/test/base/testing_pref_service_syncable.h" | 20 #include "chrome/test/base/testing_pref_service_syncable.h" |
20 #include "components/variations/pref_names.h" | 21 #include "components/variations/pref_names.h" |
21 #include "components/variations/proto/study.pb.h" | 22 #include "components/variations/proto/study.pb.h" |
22 #include "components/variations/proto/variations_seed.pb.h" | 23 #include "components/variations/proto/variations_seed.pb.h" |
23 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" | 24 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" |
24 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
25 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
26 #include "net/base/url_util.h" | 27 #include "net/base/url_util.h" |
27 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
28 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
29 #include "net/url_request/test_url_fetcher_factory.h" | 30 #include "net/url_request/test_url_fetcher_factory.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
31 | 32 |
32 #if defined(OS_CHROMEOS) | 33 #if defined(OS_CHROMEOS) |
33 #include "chrome/browser/chromeos/settings/cros_settings.h" | 34 #include "chrome/browser/chromeos/settings/cros_settings.h" |
34 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 35 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
35 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 36 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
36 #endif | 37 #endif |
37 | 38 |
38 namespace chrome_variations { | 39 namespace chrome_variations { |
39 | 40 |
40 namespace { | 41 namespace { |
41 | 42 |
42 // A test class used to validate expected functionality in VariationsService. | 43 // A test class used to validate expected functionality in VariationsService. |
43 class TestVariationsService : public VariationsService { | 44 class TestVariationsService : public VariationsService { |
44 public: | 45 public: |
45 TestVariationsService(web_resource::TestRequestAllowedNotifier* test_notifier, | 46 TestVariationsService(web_resource::TestRequestAllowedNotifier* test_notifier, |
46 PrefService* local_state) | 47 PrefService* local_state) |
47 : VariationsService(test_notifier, local_state, NULL), | 48 : VariationsService(make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 49 test_notifier, |
| 50 local_state, |
| 51 NULL), |
48 intercepts_fetch_(true), | 52 intercepts_fetch_(true), |
49 fetch_attempted_(false), | 53 fetch_attempted_(false), |
50 seed_stored_(false) { | 54 seed_stored_(false) { |
51 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | 55 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
52 SetCreateTrialsFromSeedCalledForTesting(true); | 56 SetCreateTrialsFromSeedCalledForTesting(true); |
53 } | 57 } |
54 | 58 |
55 ~TestVariationsService() override {} | 59 ~TestVariationsService() override {} |
56 | 60 |
57 void set_intercepts_fetch(bool value) { | 61 void set_intercepts_fetch(bool value) { |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 net::HTTP_NOT_MODIFIED, | 357 net::HTTP_NOT_MODIFIED, |
354 net::HTTP_NOT_FOUND, | 358 net::HTTP_NOT_FOUND, |
355 net::HTTP_INTERNAL_SERVER_ERROR, | 359 net::HTTP_INTERNAL_SERVER_ERROR, |
356 net::HTTP_SERVICE_UNAVAILABLE, | 360 net::HTTP_SERVICE_UNAVAILABLE, |
357 }; | 361 }; |
358 | 362 |
359 TestingPrefServiceSimple prefs; | 363 TestingPrefServiceSimple prefs; |
360 VariationsService::RegisterPrefs(prefs.registry()); | 364 VariationsService::RegisterPrefs(prefs.registry()); |
361 | 365 |
362 VariationsService service( | 366 VariationsService service( |
| 367 make_scoped_ptr(new ChromeVariationsServiceClient()), |
363 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 368 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
364 service.variations_server_url_ = | 369 service.variations_server_url_ = |
365 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 370 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
366 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) { |
367 net::TestURLFetcherFactory factory; | 372 net::TestURLFetcherFactory factory; |
368 service.DoActualFetch(); | 373 service.DoActualFetch(); |
369 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 374 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
370 | 375 |
371 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 376 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
372 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 377 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
(...skipping 25 matching lines...) Expand all Loading... |
398 EXPECT_FALSE(service.seed_stored()); | 403 EXPECT_FALSE(service.seed_stored()); |
399 service.OnURLFetchComplete(fetcher); | 404 service.OnURLFetchComplete(fetcher); |
400 EXPECT_TRUE(service.seed_stored()); | 405 EXPECT_TRUE(service.seed_stored()); |
401 EXPECT_EQ("test", service.stored_country()); | 406 EXPECT_EQ("test", service.stored_country()); |
402 } | 407 } |
403 | 408 |
404 TEST_F(VariationsServiceTest, Observer) { | 409 TEST_F(VariationsServiceTest, Observer) { |
405 TestingPrefServiceSimple prefs; | 410 TestingPrefServiceSimple prefs; |
406 VariationsService::RegisterPrefs(prefs.registry()); | 411 VariationsService::RegisterPrefs(prefs.registry()); |
407 VariationsService service( | 412 VariationsService service( |
| 413 make_scoped_ptr(new ChromeVariationsServiceClient()), |
408 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 414 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
409 | 415 |
410 struct { | 416 struct { |
411 int normal_count; | 417 int normal_count; |
412 int best_effort_count; | 418 int best_effort_count; |
413 int critical_count; | 419 int critical_count; |
414 int expected_best_effort_notifications; | 420 int expected_best_effort_notifications; |
415 int expected_crtical_notifications; | 421 int expected_crtical_notifications; |
416 } cases[] = { | 422 } cases[] = { |
417 {0, 0, 0, 0, 0}, | 423 {0, 0, 0, 0, 0}, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 {"badversion,ca", "20.0.0.0", "us", "20.0.0.0,us", "us", | 502 {"badversion,ca", "20.0.0.0", "us", "20.0.0.0,us", "us", |
497 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, | 503 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, |
498 {"badversion,ca", "20.0.0.0", nullptr, "", "", | 504 {"badversion,ca", "20.0.0.0", nullptr, "", "", |
499 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, | 505 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, |
500 }; | 506 }; |
501 | 507 |
502 for (const auto& test : test_cases) { | 508 for (const auto& test : test_cases) { |
503 TestingPrefServiceSimple prefs; | 509 TestingPrefServiceSimple prefs; |
504 VariationsService::RegisterPrefs(prefs.registry()); | 510 VariationsService::RegisterPrefs(prefs.registry()); |
505 VariationsService service( | 511 VariationsService service( |
| 512 make_scoped_ptr(new ChromeVariationsServiceClient()), |
506 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 513 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
507 | 514 |
508 if (test.pref_value_before) { | 515 if (test.pref_value_before) { |
509 base::ListValue list_value; | 516 base::ListValue list_value; |
510 for (const std::string& component : | 517 for (const std::string& component : |
511 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | 518 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, |
512 base::SPLIT_WANT_ALL)) { | 519 base::SPLIT_WANT_ALL)) { |
513 list_value.AppendString(component); | 520 list_value.AppendString(component); |
514 } | 521 } |
515 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | 522 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); |
(...skipping 24 matching lines...) Expand all Loading... |
540 << test.pref_value_before << ", " << test.version << ", " | 547 << test.pref_value_before << ", " << test.version << ", " |
541 << test.latest_country_code; | 548 << test.latest_country_code; |
542 | 549 |
543 histogram_tester.ExpectUniqueSample( | 550 histogram_tester.ExpectUniqueSample( |
544 "Variations.LoadPermanentConsistencyCountryResult", | 551 "Variations.LoadPermanentConsistencyCountryResult", |
545 test.expected_result, 1); | 552 test.expected_result, 1); |
546 } | 553 } |
547 } | 554 } |
548 | 555 |
549 } // namespace chrome_variations | 556 } // namespace chrome_variations |
OLD | NEW |