| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 suggestions_empty_data_count_(0), | 115 suggestions_empty_data_count_(0), |
| 116 factory_(NULL, base::Bind(&CreateURLFetcher)), | 116 factory_(NULL, base::Bind(&CreateURLFetcher)), |
| 117 mock_suggestions_store_(NULL) { | 117 mock_suggestions_store_(NULL) { |
| 118 profile_ = profile_builder_.Build(); | 118 profile_ = profile_builder_.Build(); |
| 119 } | 119 } |
| 120 virtual ~SuggestionsServiceTest() {} | 120 virtual ~SuggestionsServiceTest() {} |
| 121 | 121 |
| 122 // Enables the "ChromeSuggestions.Group1" field trial. | 122 // Enables the "ChromeSuggestions.Group1" field trial. |
| 123 void EnableFieldTrial(const std::string& url, | 123 void EnableFieldTrial(const std::string& url, |
| 124 const std::string& suggestions_suffix, | 124 const std::string& suggestions_suffix, |
| 125 const std::string& blacklist_suffix) { | 125 const std::string& blacklist_suffix, |
| 126 bool control_group) { |
| 126 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 127 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
| 127 field_trial_list_.reset(NULL); | 128 field_trial_list_.reset(NULL); |
| 128 field_trial_list_.reset( | 129 field_trial_list_.reset( |
| 129 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 130 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
| 130 | 131 |
| 131 chrome_variations::testing::ClearAllVariationParams(); | 132 chrome_variations::testing::ClearAllVariationParams(); |
| 132 std::map<std::string, std::string> params; | 133 std::map<std::string, std::string> params; |
| 133 params[kSuggestionsFieldTrialStateParam] = | 134 params[kSuggestionsFieldTrialStateParam] = |
| 134 kSuggestionsFieldTrialStateEnabled; | 135 kSuggestionsFieldTrialStateEnabled; |
| 136 if (control_group) { |
| 137 params[kSuggestionsFieldTrialControlParam] = |
| 138 kSuggestionsFieldTrialStateEnabled; |
| 139 } |
| 135 params[kSuggestionsFieldTrialURLParam] = url; | 140 params[kSuggestionsFieldTrialURLParam] = url; |
| 136 params[kSuggestionsFieldTrialSuggestionsSuffixParam] = suggestions_suffix; | 141 params[kSuggestionsFieldTrialSuggestionsSuffixParam] = suggestions_suffix; |
| 137 params[kSuggestionsFieldTrialBlacklistSuffixParam] = blacklist_suffix; | 142 params[kSuggestionsFieldTrialBlacklistSuffixParam] = blacklist_suffix; |
| 138 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, | 143 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, |
| 139 "Group1", params); | 144 "Group1", params); |
| 140 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 145 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
| 141 kSuggestionsFieldTrialName, "Group1"); | 146 kSuggestionsFieldTrialName, "Group1"); |
| 142 field_trial_->group(); | 147 field_trial_->group(); |
| 143 } | 148 } |
| 144 | 149 |
| 145 SuggestionsService* CreateSuggestionsService() { | 150 SuggestionsService* CreateSuggestionsService() { |
| 146 SuggestionsServiceFactory* suggestions_service_factory = | 151 SuggestionsServiceFactory* suggestions_service_factory = |
| 147 SuggestionsServiceFactory::GetInstance(); | 152 SuggestionsServiceFactory::GetInstance(); |
| 148 return suggestions_service_factory->GetForProfile(profile_.get()); | 153 return suggestions_service_factory->GetForProfile(profile_.get()); |
| 149 } | 154 } |
| 150 | 155 |
| 151 // Should not be called more than once per test since it stashes the | 156 // Should not be called more than once per test since it stashes the |
| 152 // SuggestionsStore in |mock_suggestions_store_|. | 157 // SuggestionsStore in |mock_suggestions_store_|. |
| 153 SuggestionsService* CreateSuggestionsServiceWithMockStore() { | 158 SuggestionsService* CreateSuggestionsServiceWithMockStore() { |
| 154 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); | 159 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); |
| 155 return new SuggestionsService( | 160 return new SuggestionsService( |
| 156 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_)); | 161 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_)); |
| 157 } | 162 } |
| 158 | 163 |
| 159 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { | 164 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { |
| 160 // Field trial enabled with a specific suggestions URL. | 165 // Field trial enabled with a specific suggestions URL. |
| 161 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 166 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 162 kFakeBlacklistSuffix); | 167 kFakeBlacklistSuffix, false); |
| 163 scoped_ptr<SuggestionsService> suggestions_service( | 168 scoped_ptr<SuggestionsService> suggestions_service( |
| 164 CreateSuggestionsServiceWithMockStore()); | 169 CreateSuggestionsServiceWithMockStore()); |
| 165 EXPECT_TRUE(suggestions_service != NULL); | 170 EXPECT_TRUE(suggestions_service != NULL); |
| 166 scoped_ptr<SuggestionsProfile> suggestions_profile( | 171 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 167 CreateSuggestionsProfile()); | 172 CreateSuggestionsProfile()); |
| 168 | 173 |
| 169 // Set up net::FakeURLFetcherFactory. | 174 // Set up net::FakeURLFetcherFactory. |
| 170 std::string expected_url = | 175 std::string expected_url = |
| 171 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 176 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; |
| 172 factory_.SetFakeResponse(GURL(expected_url), | 177 factory_.SetFakeResponse(GURL(expected_url), |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 scoped_ptr<TestingProfile> profile_; | 221 scoped_ptr<TestingProfile> profile_; |
| 217 | 222 |
| 218 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 223 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 219 }; | 224 }; |
| 220 | 225 |
| 221 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { | 226 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { |
| 222 // Field trial not enabled. | 227 // Field trial not enabled. |
| 223 EXPECT_TRUE(CreateSuggestionsService() == NULL); | 228 EXPECT_TRUE(CreateSuggestionsService() == NULL); |
| 224 | 229 |
| 225 // Field trial enabled. | 230 // Field trial enabled. |
| 226 EnableFieldTrial("", "", ""); | 231 EnableFieldTrial("", "", "", false); |
| 227 EXPECT_TRUE(CreateSuggestionsService() != NULL); | 232 EXPECT_TRUE(CreateSuggestionsService() != NULL); |
| 228 } | 233 } |
| 229 | 234 |
| 235 TEST_F(SuggestionsServiceTest, IsControlGroup) { |
| 236 // Field trial enabled. |
| 237 EnableFieldTrial("", "", "", false); |
| 238 EXPECT_FALSE(SuggestionsService::IsControlGroup()); |
| 239 |
| 240 EnableFieldTrial("", "", "", true); |
| 241 EXPECT_TRUE(SuggestionsService::IsControlGroup()); |
| 242 } |
| 243 |
| 230 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { | 244 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { |
| 231 FetchSuggestionsDataNoTimeoutHelper(false); | 245 FetchSuggestionsDataNoTimeoutHelper(false); |
| 232 } | 246 } |
| 233 | 247 |
| 234 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { | 248 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { |
| 235 FetchSuggestionsDataNoTimeoutHelper(true); | 249 FetchSuggestionsDataNoTimeoutHelper(true); |
| 236 } | 250 } |
| 237 | 251 |
| 238 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { | 252 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { |
| 239 // Field trial enabled with a specific suggestions URL. | 253 // Field trial enabled with a specific suggestions URL. |
| 240 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 254 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 241 kFakeBlacklistSuffix); | 255 kFakeBlacklistSuffix, false); |
| 242 scoped_ptr<SuggestionsService> suggestions_service( | 256 scoped_ptr<SuggestionsService> suggestions_service( |
| 243 CreateSuggestionsServiceWithMockStore()); | 257 CreateSuggestionsServiceWithMockStore()); |
| 244 EXPECT_TRUE(suggestions_service != NULL); | 258 EXPECT_TRUE(suggestions_service != NULL); |
| 245 | 259 |
| 246 // Fake a request error. | 260 // Fake a request error. |
| 247 std::string expected_url = | 261 std::string expected_url = |
| 248 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 262 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; |
| 249 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, | 263 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, |
| 250 net::URLRequestStatus::FAILED); | 264 net::URLRequestStatus::FAILED); |
| 251 | 265 |
| 252 // Set up expectations on the SuggestionsStore. | 266 // Set up expectations on the SuggestionsStore. |
| 253 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) | 267 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) |
| 254 .WillOnce(Return(true)); | 268 .WillOnce(Return(true)); |
| 255 | 269 |
| 256 // Send the request. Empty data will be returned to the callback. | 270 // Send the request. Empty data will be returned to the callback. |
| 257 suggestions_service->FetchSuggestionsData( | 271 suggestions_service->FetchSuggestionsData( |
| 258 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 272 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 259 base::Unretained(this))); | 273 base::Unretained(this))); |
| 260 | 274 |
| 261 // (Testing only) wait until suggestion fetch is complete. | 275 // (Testing only) wait until suggestion fetch is complete. |
| 262 base::MessageLoop::current()->RunUntilIdle(); | 276 base::MessageLoop::current()->RunUntilIdle(); |
| 263 | 277 |
| 264 // Ensure that ExpectEmptySuggestionsProfile ran once. | 278 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 265 EXPECT_EQ(1, suggestions_empty_data_count_); | 279 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 266 } | 280 } |
| 267 | 281 |
| 268 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 282 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
| 269 // Field trial enabled with a specific suggestions URL. | 283 // Field trial enabled with a specific suggestions URL. |
| 270 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 284 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 271 kFakeBlacklistSuffix); | 285 kFakeBlacklistSuffix, false); |
| 272 scoped_ptr<SuggestionsService> suggestions_service( | 286 scoped_ptr<SuggestionsService> suggestions_service( |
| 273 CreateSuggestionsServiceWithMockStore()); | 287 CreateSuggestionsServiceWithMockStore()); |
| 274 EXPECT_TRUE(suggestions_service != NULL); | 288 EXPECT_TRUE(suggestions_service != NULL); |
| 275 | 289 |
| 276 // Response code != 200. | 290 // Response code != 200. |
| 277 std::string expected_url = | 291 std::string expected_url = |
| 278 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 292 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; |
| 279 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", | 293 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", |
| 280 net::HTTP_BAD_REQUEST, | 294 net::HTTP_BAD_REQUEST, |
| 281 net::URLRequestStatus::SUCCESS); | 295 net::URLRequestStatus::SUCCESS); |
| 282 | 296 |
| 283 // Set up expectations on the SuggestionsStore. | 297 // Set up expectations on the SuggestionsStore. |
| 284 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); | 298 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); |
| 285 | 299 |
| 286 // Send the request. Empty data will be returned to the callback. | 300 // Send the request. Empty data will be returned to the callback. |
| 287 suggestions_service->FetchSuggestionsData( | 301 suggestions_service->FetchSuggestionsData( |
| 288 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 302 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 289 base::Unretained(this))); | 303 base::Unretained(this))); |
| 290 | 304 |
| 291 // (Testing only) wait until suggestion fetch is complete. | 305 // (Testing only) wait until suggestion fetch is complete. |
| 292 base::MessageLoop::current()->RunUntilIdle(); | 306 base::MessageLoop::current()->RunUntilIdle(); |
| 293 | 307 |
| 294 // Ensure that ExpectEmptySuggestionsProfile ran once. | 308 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 295 EXPECT_EQ(1, suggestions_empty_data_count_); | 309 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 296 } | 310 } |
| 297 | 311 |
| 298 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 312 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 299 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 313 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 300 kFakeBlacklistSuffix); | 314 kFakeBlacklistSuffix, false); |
| 301 scoped_ptr<SuggestionsService> suggestions_service( | 315 scoped_ptr<SuggestionsService> suggestions_service( |
| 302 CreateSuggestionsServiceWithMockStore()); | 316 CreateSuggestionsServiceWithMockStore()); |
| 303 EXPECT_TRUE(suggestions_service != NULL); | 317 EXPECT_TRUE(suggestions_service != NULL); |
| 304 | 318 |
| 305 std::string expected_url(kFakeSuggestionsURL); | 319 std::string expected_url(kFakeSuggestionsURL); |
| 306 expected_url.append(kFakeBlacklistSuffix) | 320 expected_url.append(kFakeBlacklistSuffix) |
| 307 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); | 321 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); |
| 308 scoped_ptr<SuggestionsProfile> suggestions_profile( | 322 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 309 CreateSuggestionsProfile()); | 323 CreateSuggestionsProfile()); |
| 310 factory_.SetFakeResponse(GURL(expected_url), | 324 factory_.SetFakeResponse(GURL(expected_url), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 323 base::Unretained(this))); | 337 base::Unretained(this))); |
| 324 | 338 |
| 325 // (Testing only) wait until blacklist request is complete. | 339 // (Testing only) wait until blacklist request is complete. |
| 326 base::MessageLoop::current()->RunUntilIdle(); | 340 base::MessageLoop::current()->RunUntilIdle(); |
| 327 | 341 |
| 328 // Ensure that CheckSuggestionsData() ran once. | 342 // Ensure that CheckSuggestionsData() ran once. |
| 329 EXPECT_EQ(1, suggestions_data_check_count_); | 343 EXPECT_EQ(1, suggestions_data_check_count_); |
| 330 } | 344 } |
| 331 | 345 |
| 332 } // namespace suggestions | 346 } // namespace suggestions |
| OLD | NEW |