| 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 "components/suggestions/suggestions_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/search/suggestions/blacklist_store.h" | 17 #include "components/suggestions/blacklist_store.h" |
| 17 #include "chrome/browser/search/suggestions/image_manager.h" | 18 #include "components/suggestions/image_manager.h" |
| 18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 19 #include "components/suggestions/proto/suggestions.pb.h" |
| 19 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 20 #include "components/suggestions/suggestions_store.h" |
| 20 #include "chrome/browser/search/suggestions/suggestions_store.h" | |
| 21 #include "components/variations/entropy_provider.h" | 21 #include "components/variations/entropy_provider.h" |
| 22 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | |
| 24 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 25 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 26 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
| 27 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 30 |
| 32 using testing::DoAll; | 31 using testing::DoAll; |
| 33 using ::testing::Eq; | 32 using ::testing::Eq; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 SuggestionsServiceTest() | 140 SuggestionsServiceTest() |
| 142 : suggestions_data_check_count_(0), | 141 : suggestions_data_check_count_(0), |
| 143 suggestions_empty_data_count_(0), | 142 suggestions_empty_data_count_(0), |
| 144 factory_(NULL, base::Bind(&CreateURLFetcher)), | 143 factory_(NULL, base::Bind(&CreateURLFetcher)), |
| 145 mock_suggestions_store_(NULL), | 144 mock_suggestions_store_(NULL), |
| 146 mock_thumbnail_manager_(NULL) {} | 145 mock_thumbnail_manager_(NULL) {} |
| 147 | 146 |
| 148 virtual ~SuggestionsServiceTest() {} | 147 virtual ~SuggestionsServiceTest() {} |
| 149 | 148 |
| 150 virtual void SetUp() OVERRIDE { | 149 virtual void SetUp() OVERRIDE { |
| 151 request_context_ = | 150 request_context_ = new net::TestURLRequestContextGetter( |
| 152 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 151 io_message_loop_.message_loop_proxy()); |
| 153 } | 152 } |
| 154 | 153 |
| 155 // Enables the "ChromeSuggestions.Group1" field trial. | 154 // Enables the "ChromeSuggestions.Group1" field trial. |
| 156 void EnableFieldTrial(const std::string& url, | 155 void EnableFieldTrial(const std::string& url, |
| 157 const std::string& common_params, | 156 const std::string& common_params, |
| 158 const std::string& blacklist_path, | 157 const std::string& blacklist_path, |
| 159 const std::string& blacklist_url_param, | 158 const std::string& blacklist_url_param, |
| 160 bool control_group) { | 159 bool control_group) { |
| 161 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 160 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
| 162 field_trial_list_.reset(NULL); | 161 field_trial_list_.reset(NULL); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 .Times(expected_count); | 225 .Times(expected_count); |
| 227 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | 226 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 228 .Times(expected_count) | 227 .Times(expected_count) |
| 229 .WillRepeatedly(Return(false)); | 228 .WillRepeatedly(Return(false)); |
| 230 | 229 |
| 231 // Send the request. The data will be returned to the callback. | 230 // Send the request. The data will be returned to the callback. |
| 232 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 231 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 233 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 232 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 234 | 233 |
| 235 if (!interleaved_requests) | 234 if (!interleaved_requests) |
| 236 base::MessageLoop::current()->RunUntilIdle(); // Let request complete. | 235 io_message_loop_.RunUntilIdle(); // Let request complete. |
| 237 | 236 |
| 238 // Send the request a second time. | 237 // Send the request a second time. |
| 239 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 238 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 240 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 239 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 241 | 240 |
| 242 // (Testing only) wait until suggestion fetch is complete. | 241 // (Testing only) wait until suggestion fetch is complete. |
| 243 base::MessageLoop::current()->RunUntilIdle(); | 242 io_message_loop_.RunUntilIdle(); |
| 244 | 243 |
| 245 // Ensure that CheckSuggestionsData() ran twice. | 244 // Ensure that CheckSuggestionsData() ran twice. |
| 246 EXPECT_EQ(2, suggestions_data_check_count_); | 245 EXPECT_EQ(2, suggestions_data_check_count_); |
| 247 } | 246 } |
| 248 | 247 |
| 249 protected: | 248 protected: |
| 249 base::MessageLoopForIO io_message_loop_; |
| 250 net::FakeURLFetcherFactory factory_; | 250 net::FakeURLFetcherFactory factory_; |
| 251 // Only used if the SuggestionsService is built with mocks. Not owned. | 251 // Only used if the SuggestionsService is built with mocks. Not owned. |
| 252 MockSuggestionsStore* mock_suggestions_store_; | 252 MockSuggestionsStore* mock_suggestions_store_; |
| 253 MockImageManager* mock_thumbnail_manager_; | 253 MockImageManager* mock_thumbnail_manager_; |
| 254 MockBlacklistStore* mock_blacklist_store_; | 254 MockBlacklistStore* mock_blacklist_store_; |
| 255 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 255 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 256 | 256 |
| 257 private: | 257 private: |
| 258 content::TestBrowserThreadBundle thread_bundle_; | |
| 259 scoped_ptr<base::FieldTrialList> field_trial_list_; | 258 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 260 scoped_refptr<base::FieldTrial> field_trial_; | 259 scoped_refptr<base::FieldTrial> field_trial_; |
| 261 | 260 |
| 262 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 261 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 263 }; | 262 }; |
| 264 | 263 |
| 265 TEST_F(SuggestionsServiceTest, IsControlGroup) { | 264 TEST_F(SuggestionsServiceTest, IsControlGroup) { |
| 266 // Field trial enabled. | 265 // Field trial enabled. |
| 267 EnableFieldTrial("", "", "", "", false); | 266 EnableFieldTrial("", "", "", "", false); |
| 268 EXPECT_FALSE(SuggestionsService::IsControlGroup()); | 267 EXPECT_FALSE(SuggestionsService::IsControlGroup()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 301 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 303 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | 302 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 304 .WillOnce(Return(false)); | 303 .WillOnce(Return(false)); |
| 305 | 304 |
| 306 // Send the request. Empty data will be returned to the callback. | 305 // Send the request. Empty data will be returned to the callback. |
| 307 suggestions_service->FetchSuggestionsData( | 306 suggestions_service->FetchSuggestionsData( |
| 308 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 307 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 309 base::Unretained(this))); | 308 base::Unretained(this))); |
| 310 | 309 |
| 311 // (Testing only) wait until suggestion fetch is complete. | 310 // (Testing only) wait until suggestion fetch is complete. |
| 312 base::MessageLoop::current()->RunUntilIdle(); | 311 io_message_loop_.RunUntilIdle(); |
| 313 | 312 |
| 314 // Ensure that ExpectEmptySuggestionsProfile ran once. | 313 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 315 EXPECT_EQ(1, suggestions_empty_data_count_); | 314 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 316 } | 315 } |
| 317 | 316 |
| 318 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 317 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
| 319 // Field trial enabled with a specific suggestions URL. | 318 // Field trial enabled with a specific suggestions URL. |
| 320 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | 319 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 321 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); | 320 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 322 scoped_ptr<SuggestionsService> suggestions_service( | 321 scoped_ptr<SuggestionsService> suggestions_service( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 337 // blacklist. | 336 // blacklist. |
| 338 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | 337 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 339 .WillOnce(Return(false)); | 338 .WillOnce(Return(false)); |
| 340 | 339 |
| 341 // Send the request. Empty data will be returned to the callback. | 340 // Send the request. Empty data will be returned to the callback. |
| 342 suggestions_service->FetchSuggestionsData( | 341 suggestions_service->FetchSuggestionsData( |
| 343 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 342 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 344 base::Unretained(this))); | 343 base::Unretained(this))); |
| 345 | 344 |
| 346 // (Testing only) wait until suggestion fetch is complete. | 345 // (Testing only) wait until suggestion fetch is complete. |
| 347 base::MessageLoop::current()->RunUntilIdle(); | 346 io_message_loop_.RunUntilIdle(); |
| 348 | 347 |
| 349 // Ensure that ExpectEmptySuggestionsProfile ran once. | 348 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 350 EXPECT_EQ(1, suggestions_empty_data_count_); | 349 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 351 } | 350 } |
| 352 | 351 |
| 353 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 352 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 354 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | 353 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 355 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); | 354 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 356 scoped_ptr<SuggestionsService> suggestions_service( | 355 scoped_ptr<SuggestionsService> suggestions_service( |
| 357 CreateSuggestionsServiceWithMocks()); | 356 CreateSuggestionsServiceWithMocks()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 378 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 377 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 379 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | 378 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 380 .WillOnce(Return(false)); | 379 .WillOnce(Return(false)); |
| 381 | 380 |
| 382 // Send the request. The data will be returned to the callback. | 381 // Send the request. The data will be returned to the callback. |
| 383 suggestions_service->BlacklistURL( | 382 suggestions_service->BlacklistURL( |
| 384 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 383 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 385 base::Unretained(this))); | 384 base::Unretained(this))); |
| 386 | 385 |
| 387 // (Testing only) wait until blacklist request is complete. | 386 // (Testing only) wait until blacklist request is complete. |
| 388 base::MessageLoop::current()->RunUntilIdle(); | 387 io_message_loop_.RunUntilIdle(); |
| 389 | 388 |
| 390 // Ensure that CheckSuggestionsData() ran once. | 389 // Ensure that CheckSuggestionsData() ran once. |
| 391 EXPECT_EQ(1, suggestions_data_check_count_); | 390 EXPECT_EQ(1, suggestions_data_check_count_); |
| 392 } | 391 } |
| 393 | 392 |
| 394 // Initial blacklist request fails, triggering a scheduled upload which | 393 // Initial blacklist request fails, triggering a scheduled upload which |
| 395 // succeeds. | 394 // succeeds. |
| 396 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { | 395 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { |
| 397 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | 396 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 398 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); | 397 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 433 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 435 base::Unretained(this))); | 434 base::Unretained(this))); |
| 436 | 435 |
| 437 // The first FakeURLFetcher was created; we can now set up behavior for the | 436 // The first FakeURLFetcher was created; we can now set up behavior for the |
| 438 // second call to blacklist. | 437 // second call to blacklist. |
| 439 factory_.SetFakeResponse(GURL(request_url), | 438 factory_.SetFakeResponse(GURL(request_url), |
| 440 suggestions_profile->SerializeAsString(), | 439 suggestions_profile->SerializeAsString(), |
| 441 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 440 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 442 | 441 |
| 443 // (Testing only) wait until both requests are complete. | 442 // (Testing only) wait until both requests are complete. |
| 443 io_message_loop_.RunUntilIdle(); |
| 444 // ... Other task gets posted to the message loop. |
| 444 base::MessageLoop::current()->RunUntilIdle(); | 445 base::MessageLoop::current()->RunUntilIdle(); |
| 446 // ... And completes. |
| 447 io_message_loop_.RunUntilIdle(); |
| 445 | 448 |
| 446 // Ensure that CheckSuggestionsData() ran once. | 449 // Ensure that CheckSuggestionsData() ran once. |
| 447 EXPECT_EQ(1, suggestions_data_check_count_); | 450 EXPECT_EQ(1, suggestions_data_check_count_); |
| 448 } | 451 } |
| 449 | 452 |
| 450 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { | 453 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { |
| 451 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | 454 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 452 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); | 455 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 453 | 456 |
| 454 scoped_ptr<GURL> request_url; | 457 scoped_ptr<GURL> request_url; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 // Delay increases on failure. | 491 // Delay increases on failure. |
| 489 suggestions_service->UpdateBlacklistDelay(false); | 492 suggestions_service->UpdateBlacklistDelay(false); |
| 490 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); | 493 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); |
| 491 | 494 |
| 492 // Delay resets on success. | 495 // Delay resets on success. |
| 493 suggestions_service->UpdateBlacklistDelay(true); | 496 suggestions_service->UpdateBlacklistDelay(true); |
| 494 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); | 497 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
| 495 } | 498 } |
| 496 | 499 |
| 497 } // namespace suggestions | 500 } // namespace suggestions |
| OLD | NEW |