| 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 <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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 } | 50 } |
| 51 version_info::Channel GetChannel() override { | 51 version_info::Channel GetChannel() override { |
| 52 return version_info::Channel::UNKNOWN; | 52 return version_info::Channel::UNKNOWN; |
| 53 } | 53 } |
| 54 bool OverridesRestrictParameter(std::string* parameter) override { | 54 bool OverridesRestrictParameter(std::string* parameter) override { |
| 55 if (restrict_parameter_.empty()) | 55 if (restrict_parameter_.empty()) |
| 56 return false; | 56 return false; |
| 57 *parameter = restrict_parameter_; | 57 *parameter = restrict_parameter_; |
| 58 return true; | 58 return true; |
| 59 } | 59 } |
| 60 void OverrideUIString(uint32_t hash, const base::string16& string) override {} | |
| 61 void OnInitialStartup() override {} | 60 void OnInitialStartup() override {} |
| 62 | 61 |
| 63 void set_restrict_parameter(std::string value) { | 62 void set_restrict_parameter(std::string value) { |
| 64 restrict_parameter_ = value; | 63 restrict_parameter_ = value; |
| 65 } | 64 } |
| 66 | 65 |
| 67 private: | 66 private: |
| 68 std::string restrict_parameter_; | 67 std::string restrict_parameter_; |
| 69 | 68 |
| 70 DISALLOW_COPY_AND_ASSIGN(TestVariationsServiceClient); | 69 DISALLOW_COPY_AND_ASSIGN(TestVariationsServiceClient); |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 // A test class used to validate expected functionality in VariationsService. | 72 // A test class used to validate expected functionality in VariationsService. |
| 74 class TestVariationsService : public VariationsService { | 73 class TestVariationsService : public VariationsService { |
| 75 public: | 74 public: |
| 76 TestVariationsService( | 75 TestVariationsService( |
| 77 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, | 76 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, |
| 78 PrefService* local_state) | 77 PrefService* local_state) |
| 79 : VariationsService(make_scoped_ptr(new TestVariationsServiceClient()), | 78 : VariationsService(make_scoped_ptr(new TestVariationsServiceClient()), |
| 80 test_notifier.Pass(), | 79 test_notifier.Pass(), |
| 81 local_state, | 80 local_state, |
| 82 NULL), | 81 NULL, |
| 82 UIStringOverrider()), |
| 83 intercepts_fetch_(true), | 83 intercepts_fetch_(true), |
| 84 fetch_attempted_(false), | 84 fetch_attempted_(false), |
| 85 seed_stored_(false) { | 85 seed_stored_(false) { |
| 86 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | 86 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
| 87 SetCreateTrialsFromSeedCalledForTesting(true); | 87 SetCreateTrialsFromSeedCalledForTesting(true); |
| 88 } | 88 } |
| 89 | 89 |
| 90 ~TestVariationsService() override {} | 90 ~TestVariationsService() override {} |
| 91 | 91 |
| 92 void set_intercepts_fetch(bool value) { | 92 void set_intercepts_fetch(bool value) { |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 const std::string default_variations_url = | 226 const std::string default_variations_url = |
| 227 VariationsService::GetDefaultVariationsServerURLForTesting(); | 227 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 228 | 228 |
| 229 std::string value; | 229 std::string value; |
| 230 scoped_ptr<TestVariationsServiceClient> client = | 230 scoped_ptr<TestVariationsServiceClient> client = |
| 231 make_scoped_ptr(new TestVariationsServiceClient()); | 231 make_scoped_ptr(new TestVariationsServiceClient()); |
| 232 TestVariationsServiceClient* raw_client = client.get(); | 232 TestVariationsServiceClient* raw_client = client.get(); |
| 233 VariationsService service( | 233 VariationsService service( |
| 234 client.Pass(), | 234 client.Pass(), |
| 235 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 235 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), |
| 236 &prefs, NULL); | 236 &prefs, NULL, UIStringOverrider()); |
| 237 GURL url = service.GetVariationsServerURL(&prefs, std::string()); | 237 GURL url = service.GetVariationsServerURL(&prefs, std::string()); |
| 238 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 238 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 239 base::CompareCase::SENSITIVE)); | 239 base::CompareCase::SENSITIVE)); |
| 240 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 240 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 241 | 241 |
| 242 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); | 242 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); |
| 243 url = service.GetVariationsServerURL(&prefs, std::string()); | 243 url = service.GetVariationsServerURL(&prefs, std::string()); |
| 244 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 244 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 245 base::CompareCase::SENSITIVE)); | 245 base::CompareCase::SENSITIVE)); |
| 246 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 246 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 net::HTTP_INTERNAL_SERVER_ERROR, | 343 net::HTTP_INTERNAL_SERVER_ERROR, |
| 344 net::HTTP_SERVICE_UNAVAILABLE, | 344 net::HTTP_SERVICE_UNAVAILABLE, |
| 345 }; | 345 }; |
| 346 | 346 |
| 347 TestingPrefServiceSimple prefs; | 347 TestingPrefServiceSimple prefs; |
| 348 VariationsService::RegisterPrefs(prefs.registry()); | 348 VariationsService::RegisterPrefs(prefs.registry()); |
| 349 | 349 |
| 350 VariationsService service( | 350 VariationsService service( |
| 351 make_scoped_ptr(new TestVariationsServiceClient()), | 351 make_scoped_ptr(new TestVariationsServiceClient()), |
| 352 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 352 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), |
| 353 &prefs, NULL); | 353 &prefs, NULL, UIStringOverrider()); |
| 354 service.variations_server_url_ = | 354 service.variations_server_url_ = |
| 355 service.GetVariationsServerURL(&prefs, std::string()); | 355 service.GetVariationsServerURL(&prefs, std::string()); |
| 356 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 356 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 357 net::TestURLFetcherFactory factory; | 357 net::TestURLFetcherFactory factory; |
| 358 service.DoActualFetch(); | 358 service.DoActualFetch(); |
| 359 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 359 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 360 | 360 |
| 361 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 361 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 362 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 362 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 363 service.OnURLFetchComplete(fetcher); | 363 service.OnURLFetchComplete(fetcher); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 391 EXPECT_TRUE(service.seed_stored()); | 391 EXPECT_TRUE(service.seed_stored()); |
| 392 EXPECT_EQ("test", service.stored_country()); | 392 EXPECT_EQ("test", service.stored_country()); |
| 393 } | 393 } |
| 394 | 394 |
| 395 TEST_F(VariationsServiceTest, Observer) { | 395 TEST_F(VariationsServiceTest, Observer) { |
| 396 TestingPrefServiceSimple prefs; | 396 TestingPrefServiceSimple prefs; |
| 397 VariationsService::RegisterPrefs(prefs.registry()); | 397 VariationsService::RegisterPrefs(prefs.registry()); |
| 398 VariationsService service( | 398 VariationsService service( |
| 399 make_scoped_ptr(new TestVariationsServiceClient()), | 399 make_scoped_ptr(new TestVariationsServiceClient()), |
| 400 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 400 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), |
| 401 &prefs, NULL); | 401 &prefs, NULL, UIStringOverrider()); |
| 402 | 402 |
| 403 struct { | 403 struct { |
| 404 int normal_count; | 404 int normal_count; |
| 405 int best_effort_count; | 405 int best_effort_count; |
| 406 int critical_count; | 406 int critical_count; |
| 407 int expected_best_effort_notifications; | 407 int expected_best_effort_notifications; |
| 408 int expected_crtical_notifications; | 408 int expected_crtical_notifications; |
| 409 } cases[] = { | 409 } cases[] = { |
| 410 {0, 0, 0, 0, 0}, | 410 {0, 0, 0, 0, 0}, |
| 411 {1, 0, 0, 0, 0}, | 411 {1, 0, 0, 0, 0}, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 {"badversion,ca", "20.0.0.0", nullptr, "", "", | 491 {"badversion,ca", "20.0.0.0", nullptr, "", "", |
| 492 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, | 492 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, |
| 493 }; | 493 }; |
| 494 | 494 |
| 495 for (const auto& test : test_cases) { | 495 for (const auto& test : test_cases) { |
| 496 TestingPrefServiceSimple prefs; | 496 TestingPrefServiceSimple prefs; |
| 497 VariationsService::RegisterPrefs(prefs.registry()); | 497 VariationsService::RegisterPrefs(prefs.registry()); |
| 498 VariationsService service( | 498 VariationsService service( |
| 499 make_scoped_ptr(new TestVariationsServiceClient()), | 499 make_scoped_ptr(new TestVariationsServiceClient()), |
| 500 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 500 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), |
| 501 &prefs, NULL); | 501 &prefs, NULL, UIStringOverrider()); |
| 502 | 502 |
| 503 if (test.pref_value_before) { | 503 if (test.pref_value_before) { |
| 504 base::ListValue list_value; | 504 base::ListValue list_value; |
| 505 for (const std::string& component : | 505 for (const std::string& component : |
| 506 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | 506 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, |
| 507 base::SPLIT_WANT_ALL)) { | 507 base::SPLIT_WANT_ALL)) { |
| 508 list_value.AppendString(component); | 508 list_value.AppendString(component); |
| 509 } | 509 } |
| 510 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | 510 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); |
| 511 } | 511 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 535 << test.pref_value_before << ", " << test.version << ", " | 535 << test.pref_value_before << ", " << test.version << ", " |
| 536 << test.latest_country_code; | 536 << test.latest_country_code; |
| 537 | 537 |
| 538 histogram_tester.ExpectUniqueSample( | 538 histogram_tester.ExpectUniqueSample( |
| 539 "Variations.LoadPermanentConsistencyCountryResult", | 539 "Variations.LoadPermanentConsistencyCountryResult", |
| 540 test.expected_result, 1); | 540 test.expected_result, 1); |
| 541 } | 541 } |
| 542 } | 542 } |
| 543 | 543 |
| 544 } // namespace variations | 544 } // namespace variations |
| OLD | NEW |