Chromium Code Reviews| 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 "components/variations/service/variations_service.h" | 5 #include "components/variations/service/variations_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/sha1.h" | 16 #include "base/sha1.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/version.h" | 21 #include "base/version.h" |
| 22 #include "components/prefs/testing_pref_service.h" | 22 #include "components/prefs/testing_pref_service.h" |
| 23 #include "components/variations/pref_names.h" | 23 #include "components/variations/pref_names.h" |
| 24 #include "components/variations/proto/study.pb.h" | 24 #include "components/variations/proto/study.pb.h" |
| 25 #include "components/variations/proto/variations_seed.pb.h" | 25 #include "components/variations/proto/variations_seed.pb.h" |
| 26 #include "components/version_info/version_info.h" | |
| 26 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" | 27 #include "components/web_resource/resource_request_allowed_notifier_test_util.h" |
| 27 #include "net/base/url_util.h" | 28 #include "net/base/url_util.h" |
| 28 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
| 29 #include "net/http/http_status_code.h" | 30 #include "net/http/http_status_code.h" |
| 30 #include "net/url_request/test_url_fetcher_factory.h" | 31 #include "net/url_request/test_url_fetcher_factory.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 33 |
| 33 namespace variations { | 34 namespace variations { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 721 ListValueToString(*pref_value)) | 722 ListValueToString(*pref_value)) |
| 722 << test.pref_value_before << ", " << test.version << ", " | 723 << test.pref_value_before << ", " << test.version << ", " |
| 723 << test.latest_country_code; | 724 << test.latest_country_code; |
| 724 | 725 |
| 725 histogram_tester.ExpectUniqueSample( | 726 histogram_tester.ExpectUniqueSample( |
| 726 "Variations.LoadPermanentConsistencyCountryResult", | 727 "Variations.LoadPermanentConsistencyCountryResult", |
| 727 test.expected_result, 1); | 728 test.expected_result, 1); |
| 728 } | 729 } |
| 729 } | 730 } |
| 730 | 731 |
| 732 TEST_F(VariationsServiceTest, ForceSetStoredPermanentCountry) { | |
| 733 std::string kTestVersion = version_info::GetVersionNumber(); | |
| 734 std::string kPrefCa = version_info::GetVersionNumber() + ",ca"; | |
| 735 std::string kPrefUs = version_info::GetVersionNumber() + ",us"; | |
| 736 | |
| 737 struct { | |
| 738 // Comma separated list, empty string if the pref isn't set initially. | |
| 739 std::string pref_value_before; | |
| 740 std::string country_code_override; | |
| 741 // Comma separated list. | |
| 742 std::string expected_pref_value_after; | |
| 743 // Is the pref expected to be updated or not. | |
| 744 const bool has_updated; | |
| 745 } test_cases[] = { | |
| 746 {kPrefUs, "ca", kPrefCa, true}, | |
| 747 {kPrefUs, "us", kPrefUs, false}, | |
| 748 {kPrefUs, "", kPrefUs, false}, | |
| 749 {"", "ca", kPrefCa, true}, | |
| 750 {"", "", "", false}, | |
| 751 {"19.0.0.0,us", "ca", kPrefCa, true}, | |
| 752 {"19.0.0.0,us", "us", "19.0.0.0,us", false}, | |
| 753 }; | |
| 754 | |
| 755 for (const auto& test : test_cases) { | |
| 756 TestingPrefServiceSimple prefs; | |
| 757 VariationsService::RegisterPrefs(prefs.registry()); | |
| 758 TestVariationsService service( | |
| 759 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | |
| 760 &prefs); | |
| 761 | |
| 762 if (test.pref_value_before != "") { | |
|
Alexei Svitkine (slow)
2016/04/20 20:30:24
Nit: !.empty()
hamelphi
2016/04/20 21:39:47
Done.
| |
| 763 base::ListValue list_value; | |
| 764 for (const std::string& component : | |
| 765 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | |
| 766 base::SPLIT_WANT_ALL)) { | |
| 767 list_value.AppendString(component); | |
| 768 } | |
| 769 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | |
| 770 } | |
| 771 | |
| 772 variations::VariationsSeed seed(CreateTestSeed()); | |
| 773 | |
| 774 EXPECT_EQ(test.has_updated, service.ForceSetStoredPermanentCountry( | |
| 775 test.country_code_override)) | |
| 776 << test.pref_value_before << ", " << test.country_code_override; | |
| 777 | |
| 778 base::ListValue expected_list_value; | |
| 779 for (const std::string& component : | |
| 780 base::SplitString(test.expected_pref_value_after, ",", | |
| 781 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | |
| 782 expected_list_value.AppendString(component); | |
| 783 } | |
| 784 const base::ListValue* pref_value = | |
| 785 prefs.GetList(prefs::kVariationsPermanentConsistencyCountry); | |
| 786 EXPECT_EQ(ListValueToString(expected_list_value), | |
| 787 ListValueToString(*pref_value)) | |
| 788 << test.pref_value_before << ", " << test.country_code_override; | |
| 789 } | |
| 790 } | |
| 791 | |
| 731 } // namespace variations | 792 } // namespace variations |
| OLD | NEW |