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