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/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 namespace chrome_variations { | 33 namespace chrome_variations { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // A test class used to validate expected functionality in VariationsService. | 37 // A test class used to validate expected functionality in VariationsService. |
38 class TestVariationsService : public VariationsService { | 38 class TestVariationsService : public VariationsService { |
39 public: | 39 public: |
40 TestVariationsService(TestRequestAllowedNotifier* test_notifier, | 40 TestVariationsService(TestRequestAllowedNotifier* test_notifier, |
41 PrefService* local_state) | 41 PrefService* local_state) |
42 : VariationsService(test_notifier, local_state), | 42 : VariationsService(test_notifier, local_state), |
43 fetch_attempted_(false) { | 43 intercepts_fetch_(true), |
| 44 fetch_attempted_(false), |
| 45 seed_stored_(false) { |
44 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | 46 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
45 SetCreateTrialsFromSeedCalledForTesting(true); | 47 SetCreateTrialsFromSeedCalledForTesting(true); |
46 } | 48 } |
47 | 49 |
48 virtual ~TestVariationsService() { | 50 virtual ~TestVariationsService() { |
49 } | 51 } |
50 | 52 |
| 53 void set_intercepts_fetch(bool value) { |
| 54 intercepts_fetch_ = value; |
| 55 } |
| 56 |
51 bool fetch_attempted() const { return fetch_attempted_; } | 57 bool fetch_attempted() const { return fetch_attempted_; } |
52 | 58 |
| 59 bool seed_stored() const { return seed_stored_; } |
| 60 |
| 61 virtual void DoActualFetch() OVERRIDE { |
| 62 if (intercepts_fetch_) { |
| 63 fetch_attempted_ = true; |
| 64 return; |
| 65 } |
| 66 |
| 67 VariationsService::DoActualFetch(); |
| 68 } |
| 69 |
53 protected: | 70 protected: |
54 virtual void DoActualFetch() OVERRIDE { | 71 virtual void StoreSeed(const std::string& seed_data, |
55 fetch_attempted_ = true; | 72 const std::string& seed_signature, |
| 73 const base::Time& date_fetched) OVERRIDE { |
| 74 seed_stored_ = true; |
56 } | 75 } |
57 | 76 |
58 private: | 77 private: |
| 78 bool intercepts_fetch_; |
59 bool fetch_attempted_; | 79 bool fetch_attempted_; |
| 80 bool seed_stored_; |
60 | 81 |
61 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | 82 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); |
62 }; | 83 }; |
63 | 84 |
64 // Populates |seed| with simple test data. The resulting seed will contain one | 85 // Populates |seed| with simple test data. The resulting seed will contain one |
65 // study called "test", which contains one experiment called "abc" with | 86 // study called "test", which contains one experiment called "abc" with |
66 // probability weight 100. |seed|'s study field will be cleared before adding | 87 // probability weight 100. |seed|'s study field will be cleared before adding |
67 // the new study. | 88 // the new study. |
68 VariationsSeed CreateTestSeed() { | 89 VariationsSeed CreateTestSeed() { |
69 VariationsSeed seed; | 90 VariationsSeed seed; |
70 Study* study = seed.add_study(); | 91 Study* study = seed.add_study(); |
71 study->set_name("test"); | 92 study->set_name("test"); |
72 study->set_default_experiment_name("abc"); | 93 study->set_default_experiment_name("abc"); |
73 Study_Experiment* experiment = study->add_experiment(); | 94 Study_Experiment* experiment = study->add_experiment(); |
74 experiment->set_name("abc"); | 95 experiment->set_name("abc"); |
75 experiment->set_probability_weight(100); | 96 experiment->set_probability_weight(100); |
76 seed.set_serial_number("123"); | 97 seed.set_serial_number("123"); |
77 return seed; | 98 return seed; |
78 } | 99 } |
79 | 100 |
80 // Serializes |seed| to protobuf binary format. | 101 // Serializes |seed| to protobuf binary format. |
81 std::string SerializeSeed(const VariationsSeed& seed) { | 102 std::string SerializeSeed(const VariationsSeed& seed) { |
82 std::string serialized_seed; | 103 std::string serialized_seed; |
83 seed.SerializeToString(&serialized_seed); | 104 seed.SerializeToString(&serialized_seed); |
84 return serialized_seed; | 105 return serialized_seed; |
85 } | 106 } |
86 | 107 |
87 // Serializes |seed| to base64-encoded protobuf binary format. | |
88 std::string SerializeSeedBase64(const VariationsSeed& seed, std::string* hash) { | |
89 std::string serialized_seed = SerializeSeed(seed); | |
90 if (hash != NULL) { | |
91 std::string sha1 = base::SHA1HashString(serialized_seed); | |
92 *hash = base::HexEncode(sha1.data(), sha1.size()); | |
93 } | |
94 std::string base64_serialized_seed; | |
95 base::Base64Encode(serialized_seed, &base64_serialized_seed); | |
96 return base64_serialized_seed; | |
97 } | |
98 | |
99 // Simulates a variations service response by setting a date header and the | 108 // Simulates a variations service response by setting a date header and the |
100 // specified HTTP |response_code| on |fetcher|. | 109 // specified HTTP |response_code| on |fetcher|. |
101 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { | 110 void SimulateServerResponse(int response_code, net::TestURLFetcher* fetcher) { |
102 ASSERT_TRUE(fetcher); | 111 ASSERT_TRUE(fetcher); |
103 scoped_refptr<net::HttpResponseHeaders> headers( | 112 scoped_refptr<net::HttpResponseHeaders> headers( |
104 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); | 113 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); |
105 fetcher->set_response_headers(headers); | 114 fetcher->set_response_headers(headers); |
106 fetcher->set_response_code(response_code); | 115 fetcher->set_response_code(response_code); |
107 } | 116 } |
108 | 117 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 EXPECT_TRUE(test_service.fetch_attempted()); | 259 EXPECT_TRUE(test_service.fetch_attempted()); |
251 } | 260 } |
252 | 261 |
253 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 262 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
254 base::MessageLoop message_loop; | 263 base::MessageLoop message_loop; |
255 content::TestBrowserThread io_thread(content::BrowserThread::IO, | 264 content::TestBrowserThread io_thread(content::BrowserThread::IO, |
256 &message_loop); | 265 &message_loop); |
257 TestingPrefServiceSimple prefs; | 266 TestingPrefServiceSimple prefs; |
258 VariationsService::RegisterPrefs(prefs.registry()); | 267 VariationsService::RegisterPrefs(prefs.registry()); |
259 | 268 |
260 VariationsService variations_service(new TestRequestAllowedNotifier, &prefs); | 269 TestVariationsService service(new TestRequestAllowedNotifier, &prefs); |
| 270 service.set_intercepts_fetch(false); |
261 | 271 |
262 net::TestURLFetcherFactory factory; | 272 net::TestURLFetcherFactory factory; |
263 variations_service.DoActualFetch(); | 273 service.DoActualFetch(); |
264 | 274 |
265 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 275 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
266 SimulateServerResponse(net::HTTP_OK, fetcher); | 276 SimulateServerResponse(net::HTTP_OK, fetcher); |
267 const VariationsSeed seed = CreateTestSeed(); | 277 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
268 fetcher->SetResponseString(SerializeSeed(seed)); | |
269 | 278 |
270 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 279 EXPECT_FALSE(service.seed_stored()); |
271 variations_service.OnURLFetchComplete(fetcher); | 280 service.OnURLFetchComplete(fetcher); |
272 EXPECT_FALSE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 281 EXPECT_TRUE(service.seed_stored()); |
273 const std::string expected_base64 = SerializeSeedBase64(seed, NULL); | |
274 EXPECT_EQ(expected_base64, prefs.GetString(prefs::kVariationsSeed)); | |
275 } | 282 } |
276 | 283 |
277 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 284 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
278 const int non_ok_status_codes[] = { | 285 const int non_ok_status_codes[] = { |
279 net::HTTP_NO_CONTENT, | 286 net::HTTP_NO_CONTENT, |
280 net::HTTP_NOT_MODIFIED, | 287 net::HTTP_NOT_MODIFIED, |
281 net::HTTP_NOT_FOUND, | 288 net::HTTP_NOT_FOUND, |
282 net::HTTP_INTERNAL_SERVER_ERROR, | 289 net::HTTP_INTERNAL_SERVER_ERROR, |
283 net::HTTP_SERVICE_UNAVAILABLE, | 290 net::HTTP_SERVICE_UNAVAILABLE, |
284 }; | 291 }; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | 324 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); |
318 | 325 |
319 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 326 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
320 SimulateServerResponse(net::HTTP_NOT_MODIFIED, fetcher); | 327 SimulateServerResponse(net::HTTP_NOT_MODIFIED, fetcher); |
321 variations_service.OnURLFetchComplete(fetcher); | 328 variations_service.OnURLFetchComplete(fetcher); |
322 EXPECT_FALSE( | 329 EXPECT_FALSE( |
323 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); | 330 prefs.FindPreference(prefs::kVariationsSeedDate)->IsDefaultValue()); |
324 } | 331 } |
325 | 332 |
326 } // namespace chrome_variations | 333 } // namespace chrome_variations |
OLD | NEW |