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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 SetCreateTrialsFromSeedCalledForTesting(true); | 52 SetCreateTrialsFromSeedCalledForTesting(true); |
53 } | 53 } |
54 | 54 |
55 ~TestVariationsService() override {} | 55 ~TestVariationsService() override {} |
56 | 56 |
57 void set_intercepts_fetch(bool value) { | 57 void set_intercepts_fetch(bool value) { |
58 intercepts_fetch_ = value; | 58 intercepts_fetch_ = value; |
59 } | 59 } |
60 | 60 |
61 bool fetch_attempted() const { return fetch_attempted_; } | 61 bool fetch_attempted() const { return fetch_attempted_; } |
62 | |
63 bool seed_stored() const { return seed_stored_; } | 62 bool seed_stored() const { return seed_stored_; } |
| 63 const std::string& stored_country() const { return stored_country_; } |
64 | 64 |
65 void DoActualFetch() override { | 65 void DoActualFetch() override { |
66 if (intercepts_fetch_) { | 66 if (intercepts_fetch_) { |
67 fetch_attempted_ = true; | 67 fetch_attempted_ = true; |
68 return; | 68 return; |
69 } | 69 } |
70 | 70 |
71 VariationsService::DoActualFetch(); | 71 VariationsService::DoActualFetch(); |
72 } | 72 } |
73 | 73 |
74 protected: | 74 protected: |
75 void StoreSeed(const std::string& seed_data, | 75 bool StoreSeed(const std::string& seed_data, |
76 const std::string& seed_signature, | 76 const std::string& seed_signature, |
77 const base::Time& date_fetched) override { | 77 const std::string& country_code, |
| 78 const base::Time& date_fetched, |
| 79 bool is_delta_compressed) override { |
78 seed_stored_ = true; | 80 seed_stored_ = true; |
| 81 stored_country_ = country_code; |
| 82 return true; |
79 } | 83 } |
80 | 84 |
81 private: | 85 private: |
82 bool intercepts_fetch_; | 86 bool intercepts_fetch_; |
83 bool fetch_attempted_; | 87 bool fetch_attempted_; |
84 bool seed_stored_; | 88 bool seed_stored_; |
| 89 std::string stored_country_; |
85 | 90 |
86 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | 91 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); |
87 }; | 92 }; |
88 | 93 |
89 class TestVariationsServiceObserver : public VariationsService::Observer { | 94 class TestVariationsServiceObserver : public VariationsService::Observer { |
90 public: | 95 public: |
91 TestVariationsServiceObserver() | 96 TestVariationsServiceObserver() |
92 : best_effort_changes_notified_(0), | 97 : best_effort_changes_notified_(0), |
93 crticial_changes_notified_(0) { | 98 crticial_changes_notified_(0) { |
94 } | 99 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 146 |
142 // Serializes |seed| to protobuf binary format. | 147 // Serializes |seed| to protobuf binary format. |
143 std::string SerializeSeed(const variations::VariationsSeed& seed) { | 148 std::string SerializeSeed(const variations::VariationsSeed& seed) { |
144 std::string serialized_seed; | 149 std::string serialized_seed; |
145 seed.SerializeToString(&serialized_seed); | 150 seed.SerializeToString(&serialized_seed); |
146 return serialized_seed; | 151 return serialized_seed; |
147 } | 152 } |
148 | 153 |
149 // Simulates a variations service response by setting a date header and the | 154 // Simulates a variations service response by setting a date header and the |
150 // specified HTTP |response_code| on |fetcher|. | 155 // specified HTTP |response_code| on |fetcher|. |
151 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { | 156 scoped_refptr<net::HttpResponseHeaders> SimulateServerResponse( |
152 ASSERT_TRUE(fetcher); | 157 int response_code, |
| 158 net::TestURLFetcher* fetcher) { |
| 159 EXPECT_TRUE(fetcher); |
153 scoped_refptr<net::HttpResponseHeaders> headers( | 160 scoped_refptr<net::HttpResponseHeaders> headers( |
154 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); | 161 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); |
155 fetcher->set_response_headers(headers); | 162 fetcher->set_response_headers(headers); |
156 fetcher->set_response_code(response_code); | 163 fetcher->set_response_code(response_code); |
| 164 return headers; |
157 } | 165 } |
158 | 166 |
159 // Helper class that abstracts away platform-specific details relating to the | 167 // Helper class that abstracts away platform-specific details relating to the |
160 // pref store used for the "restrict" param policy. | 168 // pref store used for the "restrict" param policy. |
161 class TestVariationsPrefsStore { | 169 class TestVariationsPrefsStore { |
162 public: | 170 public: |
163 TestVariationsPrefsStore() { | 171 TestVariationsPrefsStore() { |
164 #if defined(OS_ANDROID) | 172 #if defined(OS_ANDROID) |
165 // Android uses profile prefs as the PrefService to generate the URL. | 173 // Android uses profile prefs as the PrefService to generate the URL. |
166 VariationsService::RegisterProfilePrefs(prefs_.registry()); | 174 VariationsService::RegisterProfilePrefs(prefs_.registry()); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 369 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
362 | 370 |
363 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 371 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
364 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 372 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
365 service.OnURLFetchComplete(fetcher); | 373 service.OnURLFetchComplete(fetcher); |
366 | 374 |
367 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 375 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
368 } | 376 } |
369 } | 377 } |
370 | 378 |
371 TEST_F(VariationsServiceTest, SeedDateUpdatedOn304Status) { | 379 TEST_F(VariationsServiceTest, CountryHeader) { |
372 TestingPrefServiceSimple prefs; | 380 TestingPrefServiceSimple prefs; |
373 VariationsService::RegisterPrefs(prefs.registry()); | 381 VariationsService::RegisterPrefs(prefs.registry()); |
374 | 382 |
375 net::TestURLFetcherFactory factory; | 383 TestVariationsService service( |
376 VariationsService service( | 384 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
377 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | |
378 service.variations_server_url_ = | 385 service.variations_server_url_ = |
379 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 386 VariationsService::GetVariationsServerURL(&prefs, std::string()); |
| 387 service.set_intercepts_fetch(false); |
| 388 |
| 389 net::TestURLFetcherFactory factory; |
380 service.DoActualFetch(); | 390 service.DoActualFetch(); |
381 EXPECT_TRUE( | |
382 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | |
383 | 391 |
384 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 392 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
385 SimulateServerResponse(net::HTTP_NOT_MODIFIED, fetcher); | 393 scoped_refptr<net::HttpResponseHeaders> headers = |
| 394 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 395 headers->AddHeader("X-Country: test"); |
| 396 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| 397 |
| 398 EXPECT_FALSE(service.seed_stored()); |
386 service.OnURLFetchComplete(fetcher); | 399 service.OnURLFetchComplete(fetcher); |
387 EXPECT_FALSE( | 400 EXPECT_TRUE(service.seed_stored()); |
388 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | 401 EXPECT_EQ("test", service.stored_country()); |
389 } | 402 } |
390 | 403 |
391 TEST_F(VariationsServiceTest, Observer) { | 404 TEST_F(VariationsServiceTest, Observer) { |
392 TestingPrefServiceSimple prefs; | 405 TestingPrefServiceSimple prefs; |
393 VariationsService::RegisterPrefs(prefs.registry()); | 406 VariationsService::RegisterPrefs(prefs.registry()); |
394 VariationsService service( | 407 VariationsService service( |
395 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 408 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
396 | 409 |
397 struct { | 410 struct { |
398 int normal_count; | 411 int normal_count; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 << test.pref_value_before << ", " << test.version << ", " | 540 << test.pref_value_before << ", " << test.version << ", " |
528 << test.latest_country_code; | 541 << test.latest_country_code; |
529 | 542 |
530 histogram_tester.ExpectUniqueSample( | 543 histogram_tester.ExpectUniqueSample( |
531 "Variations.LoadPermanentConsistencyCountryResult", | 544 "Variations.LoadPermanentConsistencyCountryResult", |
532 test.expected_result, 1); | 545 test.expected_result, 1); |
533 } | 546 } |
534 } | 547 } |
535 | 548 |
536 } // namespace chrome_variations | 549 } // namespace chrome_variations |
OLD | NEW |