| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/variations_http_header_provider.h" | 5 #include "components/variations/variations_http_header_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "components/variations/entropy_provider.h" | 13 #include "components/variations/entropy_provider.h" |
| 14 #include "components/variations/proto/client_variations.pb.h" | 14 #include "components/variations/proto/client_variations.pb.h" |
| 15 #include "components/variations/variations_associated_data.h" | 15 #include "components/variations/variations_associated_data.h" |
| 16 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 // TODO(mathp): Remove once everything is under namespace 'variations'. | |
| 21 using chrome_variations::GOOGLE_WEB_PROPERTIES; | |
| 22 using chrome_variations::GOOGLE_WEB_PROPERTIES_TRIGGER; | |
| 23 using chrome_variations::IDCollectionKey; | |
| 24 using chrome_variations::VariationID; | |
| 25 | |
| 26 namespace variations { | 20 namespace variations { |
| 27 | 21 |
| 28 namespace { | 22 namespace { |
| 29 | 23 |
| 30 // Decodes the variations header and extracts the variation ids. | 24 // Decodes the variations header and extracts the variation ids. |
| 31 bool ExtractVariationIds(const std::string& variations, | 25 bool ExtractVariationIds(const std::string& variations, |
| 32 std::set<VariationID>* variation_ids, | 26 std::set<VariationID>* variation_ids, |
| 33 std::set<VariationID>* trigger_ids) { | 27 std::set<VariationID>* trigger_ids) { |
| 34 std::string serialized_proto; | 28 std::string serialized_proto; |
| 35 if (!base::Base64Decode(variations, &serialized_proto)) | 29 if (!base::Base64Decode(variations, &serialized_proto)) |
| 36 return false; | 30 return false; |
| 37 ClientVariations proto; | 31 ClientVariations proto; |
| 38 if (!proto.ParseFromString(serialized_proto)) return false; | 32 if (!proto.ParseFromString(serialized_proto)) return false; |
| 39 for (int i = 0; i < proto.variation_id_size(); ++i) | 33 for (int i = 0; i < proto.variation_id_size(); ++i) |
| 40 variation_ids->insert(proto.variation_id(i)); | 34 variation_ids->insert(proto.variation_id(i)); |
| 41 for (int i = 0; i < proto.trigger_variation_id_size(); ++i) | 35 for (int i = 0; i < proto.trigger_variation_id_size(); ++i) |
| 42 trigger_ids->insert(proto.trigger_variation_id(i)); | 36 trigger_ids->insert(proto.trigger_variation_id(i)); |
| 43 return true; | 37 return true; |
| 44 } | 38 } |
| 45 | 39 |
| 46 scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId( | 40 scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId( |
| 47 const std::string& trial_name, const std::string& default_group_name, | 41 const std::string& trial_name, |
| 48 IDCollectionKey key, VariationID id) { | 42 const std::string& default_group_name, |
| 43 IDCollectionKey key, |
| 44 VariationID id) { |
| 49 scoped_refptr<base::FieldTrial> trial( | 45 scoped_refptr<base::FieldTrial> trial( |
| 50 base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name)); | 46 base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name)); |
| 51 | 47 |
| 52 chrome_variations::AssociateGoogleVariationID(key, trial->trial_name(), | 48 AssociateGoogleVariationID(key, trial->trial_name(), trial->group_name(), id); |
| 53 trial->group_name(), id); | |
| 54 | 49 |
| 55 return trial; | 50 return trial; |
| 56 } | 51 } |
| 57 | 52 |
| 58 } // namespace | 53 } // namespace |
| 59 | 54 |
| 60 class VariationsHttpHeaderProviderTest : public ::testing::Test { | 55 class VariationsHttpHeaderProviderTest : public ::testing::Test { |
| 61 public: | 56 public: |
| 62 VariationsHttpHeaderProviderTest() {} | 57 VariationsHttpHeaderProviderTest() {} |
| 63 | 58 |
| 64 virtual ~VariationsHttpHeaderProviderTest() {} | 59 virtual ~VariationsHttpHeaderProviderTest() {} |
| 65 | 60 |
| 66 virtual void TearDown() OVERRIDE { | 61 virtual void TearDown() OVERRIDE { |
| 67 chrome_variations::testing::ClearAllVariationIDs(); | 62 testing::ClearAllVariationIDs(); |
| 68 } | 63 } |
| 69 }; | 64 }; |
| 70 | 65 |
| 71 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) { | 66 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) { |
| 72 struct { | 67 struct { |
| 73 const char* url; | 68 const char* url; |
| 74 bool should_append_headers; | 69 bool should_append_headers; |
| 75 } cases[] = { | 70 } cases[] = { |
| 76 {"http://google.com", true}, | 71 {"http://google.com", true}, |
| 77 {"http://www.google.com", true}, | 72 {"http://www.google.com", true}, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 {"http://a.b.doubleclick.net", true}, | 144 {"http://a.b.doubleclick.net", true}, |
| 150 {"http://a.b.ggpht.com", true}, | 145 {"http://a.b.ggpht.com", true}, |
| 151 {"http://a.b.googleadservices.com", true}, | 146 {"http://a.b.googleadservices.com", true}, |
| 152 {"http://a.b.googleapis.com", true}, | 147 {"http://a.b.googleapis.com", true}, |
| 153 {"http://a.b.googlesyndication.com", true}, | 148 {"http://a.b.googlesyndication.com", true}, |
| 154 {"http://a.b.googleusercontent.com", true}, | 149 {"http://a.b.googleusercontent.com", true}, |
| 155 {"http://a.b.googlevideo.com", true}, | 150 {"http://a.b.googlevideo.com", true}, |
| 156 {"http://ssl.gstatic.com", true}, | 151 {"http://ssl.gstatic.com", true}, |
| 157 {"http://a.b.gstatic.com", true}, | 152 {"http://a.b.gstatic.com", true}, |
| 158 {"http://a.b.ytimg.com", true}, | 153 {"http://a.b.ytimg.com", true}, |
| 159 | |
| 160 }; | 154 }; |
| 161 | 155 |
| 162 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 156 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 163 const GURL url(cases[i].url); | 157 const GURL url(cases[i].url); |
| 164 EXPECT_EQ(cases[i].should_append_headers, | 158 EXPECT_EQ(cases[i].should_append_headers, |
| 165 VariationsHttpHeaderProvider::ShouldAppendHeaders(url)) | 159 VariationsHttpHeaderProvider::ShouldAppendHeaders(url)) |
| 166 << url; | 160 << url; |
| 167 } | 161 } |
| 168 } | 162 } |
| 169 | 163 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 headers.GetHeader("X-Client-Data", &variations); | 231 headers.GetHeader("X-Client-Data", &variations); |
| 238 | 232 |
| 239 std::set<VariationID> variation_ids; | 233 std::set<VariationID> variation_ids; |
| 240 std::set<VariationID> trigger_ids; | 234 std::set<VariationID> trigger_ids; |
| 241 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); | 235 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); |
| 242 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end()); | 236 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end()); |
| 243 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end()); | 237 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end()); |
| 244 } | 238 } |
| 245 | 239 |
| 246 } // namespace variations | 240 } // namespace variations |
| OLD | NEW |