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 "chrome/browser/search/suggestions/suggestions_service.h" | 5 #include "chrome/browser/search/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
23 #include "net/http/http_status_code.h" | 23 #include "net/http/http_status_code.h" |
24 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
25 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
26 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; | 31 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; |
| 32 const char kFakeSuggestionsSuffix[] = "?foo=bar"; |
| 33 const char kFakeBlacklistSuffix[] = "/blacklist?foo=bar&baz="; |
32 | 34 |
33 const char kTestTitle[] = "a title"; | 35 const char kTestTitle[] = "a title"; |
34 const char kTestUrl[] = "http://go.com"; | 36 const char kTestUrl[] = "http://go.com"; |
| 37 const char kBlacklistUrl[] = "http://blacklist.com"; |
35 | 38 |
36 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 39 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
37 const GURL& url, net::URLFetcherDelegate* delegate, | 40 const GURL& url, net::URLFetcherDelegate* delegate, |
38 const std::string& response_data, net::HttpStatusCode response_code, | 41 const std::string& response_data, net::HttpStatusCode response_code, |
39 net::URLRequestStatus::Status status) { | 42 net::URLRequestStatus::Status status) { |
40 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 43 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
41 url, delegate, response_data, response_code, status)); | 44 url, delegate, response_data, response_code, status)); |
42 | 45 |
43 if (response_code == net::HTTP_OK) { | 46 if (response_code == net::HTTP_OK) { |
44 scoped_refptr<net::HttpResponseHeaders> download_headers( | 47 scoped_refptr<net::HttpResponseHeaders> download_headers( |
45 new net::HttpResponseHeaders("")); | 48 new net::HttpResponseHeaders("")); |
46 download_headers->AddHeader("Content-Type: text/html"); | 49 download_headers->AddHeader("Content-Type: text/html"); |
47 fetcher->set_response_headers(download_headers); | 50 fetcher->set_response_headers(download_headers); |
48 } | 51 } |
49 return fetcher.Pass(); | 52 return fetcher.Pass(); |
50 } | 53 } |
51 | 54 |
52 } // namespace | 55 } // namespace |
53 | 56 |
54 namespace suggestions { | 57 namespace suggestions { |
55 | 58 |
| 59 namespace { |
| 60 |
| 61 scoped_ptr<SuggestionsProfile> CreateSuggestionsProfile() { |
| 62 scoped_ptr<SuggestionsProfile> profile(new SuggestionsProfile()); |
| 63 ChromeSuggestion* suggestion = profile->add_suggestions(); |
| 64 suggestion->set_title(kTestTitle); |
| 65 suggestion->set_url(kTestUrl); |
| 66 return profile.Pass(); |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
56 class SuggestionsServiceTest : public testing::Test { | 71 class SuggestionsServiceTest : public testing::Test { |
57 public: | 72 public: |
58 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { | 73 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { |
59 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | 74 EXPECT_EQ(1, suggestions_profile.suggestions_size()); |
60 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | 75 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); |
61 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | 76 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); |
62 ++suggestions_data_check_count_; | 77 ++suggestions_data_check_count_; |
63 } | 78 } |
64 | 79 |
65 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { | 80 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { |
66 EXPECT_EQ(0, profile.suggestions_size()); | 81 EXPECT_EQ(0, profile.suggestions_size()); |
67 ++suggestions_empty_data_count_; | 82 ++suggestions_empty_data_count_; |
68 } | 83 } |
69 | 84 |
70 int suggestions_data_check_count_; | 85 int suggestions_data_check_count_; |
71 int suggestions_empty_data_count_; | 86 int suggestions_empty_data_count_; |
72 | 87 |
73 protected: | 88 protected: |
74 SuggestionsServiceTest() | 89 SuggestionsServiceTest() |
75 : suggestions_data_check_count_(0), | 90 : suggestions_data_check_count_(0), |
76 suggestions_empty_data_count_(0), | 91 suggestions_empty_data_count_(0), |
77 factory_(NULL, base::Bind(&CreateURLFetcher)) { | 92 factory_(NULL, base::Bind(&CreateURLFetcher)) { |
78 profile_ = profile_builder_.Build(); | 93 profile_ = profile_builder_.Build(); |
79 } | 94 } |
80 virtual ~SuggestionsServiceTest() {} | 95 virtual ~SuggestionsServiceTest() {} |
81 | 96 |
82 // Enables the "ChromeSuggestions.Group1" field trial. | 97 // Enables the "ChromeSuggestions.Group1" field trial. |
83 void EnableFieldTrial(const std::string& url) { | 98 void EnableFieldTrial(const std::string& url, |
| 99 const std::string& suggestions_suffix, |
| 100 const std::string& blacklist_suffix) { |
84 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 101 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
85 field_trial_list_.reset(NULL); | 102 field_trial_list_.reset(NULL); |
86 field_trial_list_.reset( | 103 field_trial_list_.reset( |
87 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 104 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
88 | 105 |
89 chrome_variations::testing::ClearAllVariationParams(); | 106 chrome_variations::testing::ClearAllVariationParams(); |
90 std::map<std::string, std::string> params; | 107 std::map<std::string, std::string> params; |
91 params[kSuggestionsFieldTrialStateParam] = | 108 params[kSuggestionsFieldTrialStateParam] = |
92 kSuggestionsFieldTrialStateEnabled; | 109 kSuggestionsFieldTrialStateEnabled; |
93 params[kSuggestionsFieldTrialURLParam] = url; | 110 params[kSuggestionsFieldTrialURLParam] = url; |
| 111 params[kSuggestionsFieldTrialSuggestionsSuffixParam] = suggestions_suffix; |
| 112 params[kSuggestionsFieldTrialBlacklistSuffixParam] = blacklist_suffix; |
94 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, | 113 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, |
95 "Group1", params); | 114 "Group1", params); |
96 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 115 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
97 kSuggestionsFieldTrialName, "Group1"); | 116 kSuggestionsFieldTrialName, "Group1"); |
98 field_trial_->group(); | 117 field_trial_->group(); |
99 } | 118 } |
100 | 119 |
101 SuggestionsService* CreateSuggestionsService() { | 120 SuggestionsService* CreateSuggestionsService() { |
102 SuggestionsServiceFactory* suggestions_service_factory = | 121 SuggestionsServiceFactory* suggestions_service_factory = |
103 SuggestionsServiceFactory::GetInstance(); | 122 SuggestionsServiceFactory::GetInstance(); |
(...skipping 11 matching lines...) Expand all Loading... |
115 scoped_ptr<TestingProfile> profile_; | 134 scoped_ptr<TestingProfile> profile_; |
116 | 135 |
117 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 136 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
118 }; | 137 }; |
119 | 138 |
120 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { | 139 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { |
121 // Field trial not enabled. | 140 // Field trial not enabled. |
122 EXPECT_TRUE(CreateSuggestionsService() == NULL); | 141 EXPECT_TRUE(CreateSuggestionsService() == NULL); |
123 | 142 |
124 // Field trial enabled. | 143 // Field trial enabled. |
125 EnableFieldTrial(""); | 144 EnableFieldTrial("", "", ""); |
126 EXPECT_TRUE(CreateSuggestionsService() != NULL); | 145 EXPECT_TRUE(CreateSuggestionsService() != NULL); |
127 } | 146 } |
128 | 147 |
129 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { | 148 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { |
130 // Field trial enabled with a specific suggestions URL. | 149 // Field trial enabled with a specific suggestions URL. |
131 EnableFieldTrial(kFakeSuggestionsURL); | 150 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 151 kFakeBlacklistSuffix); |
132 SuggestionsService* suggestions_service = CreateSuggestionsService(); | 152 SuggestionsService* suggestions_service = CreateSuggestionsService(); |
133 EXPECT_TRUE(suggestions_service != NULL); | 153 EXPECT_TRUE(suggestions_service != NULL); |
134 | 154 |
135 SuggestionsProfile suggestions_profile; | 155 std::string expected_url = std::string(kFakeSuggestionsURL) + |
136 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 156 kFakeSuggestionsSuffix; |
137 suggestion->set_title(kTestTitle); | 157 scoped_ptr<SuggestionsProfile> suggestions_profile( |
138 suggestion->set_url(kTestUrl); | 158 CreateSuggestionsProfile()); |
139 factory_.SetFakeResponse(GURL(kFakeSuggestionsURL), | 159 factory_.SetFakeResponse(GURL(expected_url), |
140 suggestions_profile.SerializeAsString(), | 160 suggestions_profile->SerializeAsString(), |
141 net::HTTP_OK, | 161 net::HTTP_OK, |
142 net::URLRequestStatus::SUCCESS); | 162 net::URLRequestStatus::SUCCESS); |
143 | 163 |
144 // Send the request. The data will be returned to the callback. | 164 // Send the request. The data will be returned to the callback. |
145 suggestions_service->FetchSuggestionsData( | 165 suggestions_service->FetchSuggestionsData( |
146 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 166 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
147 base::Unretained(this))); | 167 base::Unretained(this))); |
148 | 168 |
149 // Send the request a second time. | 169 // Send the request a second time. |
150 suggestions_service->FetchSuggestionsData( | 170 suggestions_service->FetchSuggestionsData( |
151 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 171 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
152 base::Unretained(this))); | 172 base::Unretained(this))); |
153 | 173 |
154 // (Testing only) wait until suggestion fetch is complete. | 174 // (Testing only) wait until suggestion fetch is complete. |
155 base::MessageLoop::current()->RunUntilIdle(); | 175 base::MessageLoop::current()->RunUntilIdle(); |
156 | 176 |
157 // Ensure that CheckSuggestionsData() ran twice. | 177 // Ensure that CheckSuggestionsData() ran twice. |
158 EXPECT_EQ(2, suggestions_data_check_count_); | 178 EXPECT_EQ(2, suggestions_data_check_count_); |
159 } | 179 } |
160 | 180 |
161 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { | 181 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { |
162 // Field trial enabled with a specific suggestions URL. | 182 // Field trial enabled with a specific suggestions URL. |
163 EnableFieldTrial(kFakeSuggestionsURL); | 183 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 184 kFakeBlacklistSuffix); |
164 SuggestionsService* suggestions_service = CreateSuggestionsService(); | 185 SuggestionsService* suggestions_service = CreateSuggestionsService(); |
165 EXPECT_TRUE(suggestions_service != NULL); | 186 EXPECT_TRUE(suggestions_service != NULL); |
166 | 187 |
167 // Fake a request error. | 188 // Fake a request error. |
168 factory_.SetFakeResponse(GURL(kFakeSuggestionsURL), | 189 std::string expected_url = std::string(kFakeSuggestionsURL) + |
| 190 kFakeSuggestionsSuffix; |
| 191 factory_.SetFakeResponse(GURL(expected_url), |
169 "irrelevant", | 192 "irrelevant", |
170 net::HTTP_OK, | 193 net::HTTP_OK, |
171 net::URLRequestStatus::FAILED); | 194 net::URLRequestStatus::FAILED); |
172 | 195 |
173 // Send the request. Empty data will be returned to the callback. | 196 // Send the request. Empty data will be returned to the callback. |
174 suggestions_service->FetchSuggestionsData( | 197 suggestions_service->FetchSuggestionsData( |
175 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 198 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
176 base::Unretained(this))); | 199 base::Unretained(this))); |
177 | 200 |
178 // (Testing only) wait until suggestion fetch is complete. | 201 // (Testing only) wait until suggestion fetch is complete. |
179 base::MessageLoop::current()->RunUntilIdle(); | 202 base::MessageLoop::current()->RunUntilIdle(); |
180 | 203 |
181 // Ensure that ExpectEmptySuggestionsProfile ran once. | 204 // Ensure that ExpectEmptySuggestionsProfile ran once. |
182 EXPECT_EQ(1, suggestions_empty_data_count_); | 205 EXPECT_EQ(1, suggestions_empty_data_count_); |
183 } | 206 } |
184 | 207 |
185 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 208 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
186 // Field trial enabled with a specific suggestions URL. | 209 // Field trial enabled with a specific suggestions URL. |
187 EnableFieldTrial(kFakeSuggestionsURL); | 210 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 211 kFakeBlacklistSuffix); |
188 SuggestionsService* suggestions_service = CreateSuggestionsService(); | 212 SuggestionsService* suggestions_service = CreateSuggestionsService(); |
189 EXPECT_TRUE(suggestions_service != NULL); | 213 EXPECT_TRUE(suggestions_service != NULL); |
190 | 214 |
191 // Response code != 200. | 215 // Response code != 200. |
192 factory_.SetFakeResponse(GURL(kFakeSuggestionsURL), | 216 std::string expected_url = std::string(kFakeSuggestionsURL) + |
| 217 kFakeSuggestionsSuffix; |
| 218 factory_.SetFakeResponse(GURL(expected_url), |
193 "irrelevant", | 219 "irrelevant", |
194 net::HTTP_BAD_REQUEST, | 220 net::HTTP_BAD_REQUEST, |
195 net::URLRequestStatus::SUCCESS); | 221 net::URLRequestStatus::SUCCESS); |
196 | 222 |
197 // Send the request. Empty data will be returned to the callback. | 223 // Send the request. Empty data will be returned to the callback. |
198 suggestions_service->FetchSuggestionsData( | 224 suggestions_service->FetchSuggestionsData( |
199 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 225 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
200 base::Unretained(this))); | 226 base::Unretained(this))); |
201 | 227 |
202 // (Testing only) wait until suggestion fetch is complete. | 228 // (Testing only) wait until suggestion fetch is complete. |
203 base::MessageLoop::current()->RunUntilIdle(); | 229 base::MessageLoop::current()->RunUntilIdle(); |
204 | 230 |
205 // Ensure that ExpectEmptySuggestionsProfile ran once. | 231 // Ensure that ExpectEmptySuggestionsProfile ran once. |
206 EXPECT_EQ(1, suggestions_empty_data_count_); | 232 EXPECT_EQ(1, suggestions_empty_data_count_); |
207 } | 233 } |
208 | 234 |
| 235 TEST_F(SuggestionsServiceTest, AddBlacklistedURL) { |
| 236 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 237 kFakeBlacklistSuffix); |
| 238 SuggestionsService* suggestions_service = CreateSuggestionsService(); |
| 239 EXPECT_TRUE(suggestions_service != NULL); |
| 240 |
| 241 std::string expected_url(kFakeSuggestionsURL); |
| 242 expected_url.append(kFakeBlacklistSuffix) |
| 243 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); |
| 244 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 245 CreateSuggestionsProfile()); |
| 246 factory_.SetFakeResponse(GURL(expected_url), |
| 247 suggestions_profile->SerializeAsString(), |
| 248 net::HTTP_OK, |
| 249 net::URLRequestStatus::SUCCESS); |
| 250 |
| 251 // Send the request. The data will be returned to the callback. |
| 252 suggestions_service->AddBlacklistedURL( |
| 253 GURL(kBlacklistUrl), |
| 254 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 255 base::Unretained(this))); |
| 256 |
| 257 // (Testing only) wait until blacklist request is complete. |
| 258 base::MessageLoop::current()->RunUntilIdle(); |
| 259 |
| 260 // Ensure that CheckSuggestionsData() ran once. |
| 261 EXPECT_EQ(1, suggestions_data_check_count_); |
| 262 } |
| 263 |
209 } // namespace suggestions | 264 } // namespace suggestions |
OLD | NEW |