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/suggestions/suggestions_service.h" | 5 #include "components/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 token_service_.set_auto_post_fetch_response_on_message_loop(true); | 206 token_service_.set_auto_post_fetch_response_on_message_loop(true); |
207 } | 207 } |
208 | 208 |
209 ~SuggestionsServiceTest() override {} | 209 ~SuggestionsServiceTest() override {} |
210 | 210 |
211 void SetUp() override { | 211 void SetUp() override { |
212 request_context_ = | 212 request_context_ = |
213 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); | 213 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); |
214 } | 214 } |
215 | 215 |
216 SuggestionsService* CreateSuggestionsServiceWithMocks() { | 216 std::unique_ptr<SuggestionsServiceImpl> CreateSuggestionsServiceWithMocks() { |
217 mock_sync_service_.reset(new MockSyncService); | 217 mock_sync_service_.reset(new MockSyncService); |
218 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); | 218 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); |
219 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); | 219 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); |
220 ON_CALL(*mock_sync_service_, ConfigurationDone()) | 220 ON_CALL(*mock_sync_service_, ConfigurationDone()) |
221 .WillByDefault(Return(true)); | 221 .WillByDefault(Return(true)); |
222 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) | 222 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) |
223 .WillByDefault( | 223 .WillByDefault( |
224 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); | 224 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); |
225 | 225 |
226 // These objects are owned by the returned SuggestionsService, but we keep | 226 // These objects are owned by the returned SuggestionsService, but we keep |
227 // the pointer around for testing. | 227 // the pointer around for testing. |
228 test_suggestions_store_ = new TestSuggestionsStore(); | 228 test_suggestions_store_ = new TestSuggestionsStore(); |
229 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); | 229 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); |
230 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); | 230 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); |
231 return new SuggestionsService( | 231 return base::MakeUnique<SuggestionsServiceImpl>( |
232 nullptr /* signin_manager */, &token_service_, mock_sync_service_.get(), | 232 nullptr /* signin_manager */, &token_service_, mock_sync_service_.get(), |
233 request_context_.get(), base::WrapUnique(test_suggestions_store_), | 233 request_context_.get(), base::WrapUnique(test_suggestions_store_), |
234 base::WrapUnique(mock_thumbnail_manager_), | 234 base::WrapUnique(mock_thumbnail_manager_), |
235 base::WrapUnique(mock_blacklist_store_)); | 235 base::WrapUnique(mock_blacklist_store_)); |
236 } | 236 } |
237 | 237 |
238 void Blacklist(SuggestionsService* suggestions_service, GURL url) { | 238 void Blacklist(SuggestionsService* suggestions_service, GURL url) { |
239 blacklisting_failed_ = !suggestions_service->BlacklistURL(url); | 239 blacklisting_failed_ = !suggestions_service->BlacklistURL(url); |
240 } | 240 } |
241 | 241 |
242 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { | 242 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { |
243 undo_blacklisting_failed_ = !suggestions_service->UndoBlacklistURL(url); | 243 undo_blacklisting_failed_ = !suggestions_service->UndoBlacklistURL(url); |
244 } | 244 } |
245 | 245 |
246 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either | 246 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either |
247 // the case where the URL is no longer in the local blacklist or the case | 247 // the case where the URL is no longer in the local blacklist or the case |
248 // in which it's not yet candidate for upload. | 248 // in which it's not yet candidate for upload. |
249 void UndoBlacklistURLFailsHelper(bool is_uploaded) { | 249 void UndoBlacklistURLFailsHelper(bool is_uploaded) { |
250 std::unique_ptr<SuggestionsService> suggestions_service( | 250 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
251 CreateSuggestionsServiceWithMocks()); | 251 CreateSuggestionsServiceWithMocks()); |
252 EXPECT_TRUE(suggestions_service != nullptr); | 252 EXPECT_TRUE(suggestions_service != nullptr); |
253 // Ensure scheduling the request doesn't happen before undo. | 253 // Ensure scheduling the request doesn't happen before undo. |
254 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 254 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
255 suggestions_service->set_blacklist_delay(delay); | 255 suggestions_service->set_blacklist_delay(delay); |
256 | 256 |
257 auto subscription = suggestions_service->AddCallback(base::Bind( | 257 auto subscription = suggestions_service->AddCallback(base::Bind( |
258 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 258 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
259 | 259 |
260 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 260 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
(...skipping 22 matching lines...) Expand all Loading... |
283 } | 283 } |
284 | 284 |
285 Blacklist(suggestions_service.get(), blacklisted_url); | 285 Blacklist(suggestions_service.get(), blacklisted_url); |
286 UndoBlacklist(suggestions_service.get(), blacklisted_url); | 286 UndoBlacklist(suggestions_service.get(), blacklisted_url); |
287 | 287 |
288 EXPECT_EQ(1, suggestions_data_callback_count_); | 288 EXPECT_EQ(1, suggestions_data_callback_count_); |
289 EXPECT_FALSE(blacklisting_failed_); | 289 EXPECT_FALSE(blacklisting_failed_); |
290 EXPECT_TRUE(undo_blacklisting_failed_); | 290 EXPECT_TRUE(undo_blacklisting_failed_); |
291 } | 291 } |
292 | 292 |
293 bool HasPendingSuggestionsRequest(SuggestionsService* suggestions_service) { | 293 bool HasPendingSuggestionsRequest( |
| 294 SuggestionsServiceImpl* suggestions_service) { |
294 return !!suggestions_service->pending_request_.get(); | 295 return !!suggestions_service->pending_request_.get(); |
295 } | 296 } |
296 | 297 |
297 protected: | 298 protected: |
298 base::MessageLoopForIO io_message_loop_; | 299 base::MessageLoopForIO io_message_loop_; |
299 net::FakeURLFetcherFactory factory_; | 300 net::FakeURLFetcherFactory factory_; |
300 FakeProfileOAuth2TokenService token_service_; | 301 FakeProfileOAuth2TokenService token_service_; |
301 std::unique_ptr<MockSyncService> mock_sync_service_; | 302 std::unique_ptr<MockSyncService> mock_sync_service_; |
302 // Only used if the SuggestionsService is built with mocks. Not owned. | 303 // Only used if the SuggestionsService is built with mocks. Not owned. |
303 MockImageManager* mock_thumbnail_manager_; | 304 MockImageManager* mock_thumbnail_manager_; |
304 MockBlacklistStore* mock_blacklist_store_; | 305 MockBlacklistStore* mock_blacklist_store_; |
305 TestSuggestionsStore* test_suggestions_store_; | 306 TestSuggestionsStore* test_suggestions_store_; |
306 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 307 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
307 | 308 |
308 private: | 309 private: |
309 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 310 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
310 }; | 311 }; |
311 | 312 |
312 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { | 313 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { |
313 std::unique_ptr<SuggestionsService> suggestions_service( | 314 std::unique_ptr<SuggestionsService> suggestions_service( |
314 CreateSuggestionsServiceWithMocks()); | 315 CreateSuggestionsServiceWithMocks()); |
315 ASSERT_TRUE(suggestions_service != nullptr); | 316 ASSERT_TRUE(suggestions_service != nullptr); |
316 auto subscription = suggestions_service->AddCallback(base::Bind( | 317 auto subscription = suggestions_service->AddCallback(base::Bind( |
317 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 318 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
318 | 319 |
319 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 320 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
320 | 321 |
321 // Set up net::FakeURLFetcherFactory. | 322 // Set up net::FakeURLFetcherFactory. |
322 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | 323 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
323 suggestions_profile.SerializeAsString(), | 324 suggestions_profile.SerializeAsString(), |
324 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 325 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
325 | 326 |
326 // Expectations. | 327 // Expectations. |
327 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)); | 328 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)); |
328 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 329 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
329 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 330 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
330 .WillOnce(Return(false)); | 331 .WillOnce(Return(false)); |
331 | 332 |
332 // Send the request. The data should be returned to the callback. | 333 // Send the request. The data should be returned to the callback. |
(...skipping 29 matching lines...) Expand all Loading... |
362 EXPECT_EQ(0, suggestions_data_callback_count_); | 363 EXPECT_EQ(0, suggestions_data_callback_count_); |
363 | 364 |
364 // |test_suggestions_store_| should still contain the default values. | 365 // |test_suggestions_store_| should still contain the default values. |
365 SuggestionsProfile suggestions; | 366 SuggestionsProfile suggestions; |
366 test_suggestions_store_->LoadSuggestions(&suggestions); | 367 test_suggestions_store_->LoadSuggestions(&suggestions); |
367 EXPECT_EQ(CreateSuggestionsProfile().SerializeAsString(), | 368 EXPECT_EQ(CreateSuggestionsProfile().SerializeAsString(), |
368 suggestions.SerializeAsString()); | 369 suggestions.SerializeAsString()); |
369 } | 370 } |
370 | 371 |
371 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { | 372 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { |
372 std::unique_ptr<SuggestionsService> suggestions_service( | 373 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
373 CreateSuggestionsServiceWithMocks()); | 374 CreateSuggestionsServiceWithMocks()); |
374 ASSERT_TRUE(suggestions_service != nullptr); | 375 ASSERT_TRUE(suggestions_service != nullptr); |
375 EXPECT_CALL(*mock_sync_service_, CanSyncStart()) | 376 EXPECT_CALL(*mock_sync_service_, CanSyncStart()) |
376 .WillRepeatedly(Return(false)); | 377 .WillRepeatedly(Return(false)); |
377 | 378 |
378 auto subscription = suggestions_service->AddCallback(base::Bind( | 379 auto subscription = suggestions_service->AddCallback(base::Bind( |
379 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 380 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
380 | 381 |
381 // Tell SuggestionsService that the sync state changed. The cache should be | 382 // Tell SuggestionsService that the sync state changed. The cache should be |
382 // cleared and empty data returned to the callback. | 383 // cleared and empty data returned to the callback. |
(...skipping 10 matching lines...) Expand all Loading... |
393 // Let any network request run. | 394 // Let any network request run. |
394 base::RunLoop().RunUntilIdle(); | 395 base::RunLoop().RunUntilIdle(); |
395 | 396 |
396 // Ensure that CheckCallback didn't run again. | 397 // Ensure that CheckCallback didn't run again. |
397 EXPECT_EQ(1, suggestions_data_callback_count_); | 398 EXPECT_EQ(1, suggestions_data_callback_count_); |
398 } | 399 } |
399 | 400 |
400 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { | 401 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { |
401 token_service_.RevokeCredentials(kAccountId); | 402 token_service_.RevokeCredentials(kAccountId); |
402 | 403 |
403 std::unique_ptr<SuggestionsService> suggestions_service( | 404 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
404 CreateSuggestionsServiceWithMocks()); | 405 CreateSuggestionsServiceWithMocks()); |
405 ASSERT_TRUE(suggestions_service != nullptr); | 406 ASSERT_TRUE(suggestions_service != nullptr); |
406 | 407 |
407 auto subscription = suggestions_service->AddCallback(base::Bind( | 408 auto subscription = suggestions_service->AddCallback(base::Bind( |
408 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 409 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
409 | 410 |
410 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 411 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
411 .WillOnce(Return(false)); | 412 .WillOnce(Return(false)); |
412 | 413 |
413 suggestions_service->FetchSuggestionsData(); | 414 suggestions_service->FetchSuggestionsData(); |
414 | 415 |
415 // No network request should be sent. | 416 // No network request should be sent. |
416 base::RunLoop().RunUntilIdle(); | 417 base::RunLoop().RunUntilIdle(); |
417 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); | 418 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); |
418 EXPECT_EQ(0, suggestions_data_callback_count_); | 419 EXPECT_EQ(0, suggestions_data_callback_count_); |
419 } | 420 } |
420 | 421 |
421 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { | 422 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { |
422 std::unique_ptr<SuggestionsService> suggestions_service( | 423 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
423 CreateSuggestionsServiceWithMocks()); | 424 CreateSuggestionsServiceWithMocks()); |
424 ASSERT_TRUE(suggestions_service != nullptr); | 425 ASSERT_TRUE(suggestions_service != nullptr); |
425 | 426 |
426 // Fake a request error. | 427 // Fake a request error. |
427 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | 428 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
428 "irrelevant", net::HTTP_OK, | 429 "irrelevant", net::HTTP_OK, |
429 net::URLRequestStatus::FAILED); | 430 net::URLRequestStatus::FAILED); |
430 | 431 |
431 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 432 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
432 .WillOnce(Return(false)); | 433 .WillOnce(Return(false)); |
433 | 434 |
434 // Send the request. Empty data will be returned to the callback. | 435 // Send the request. Empty data will be returned to the callback. |
435 suggestions_service->IssueRequestIfNoneOngoing( | 436 suggestions_service->IssueRequestIfNoneOngoing( |
436 SuggestionsService::BuildSuggestionsURL()); | 437 SuggestionsServiceImpl::BuildSuggestionsURL()); |
437 | 438 |
438 // (Testing only) wait until suggestion fetch is complete. | 439 // (Testing only) wait until suggestion fetch is complete. |
439 base::RunLoop().RunUntilIdle(); | 440 base::RunLoop().RunUntilIdle(); |
440 } | 441 } |
441 | 442 |
442 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { | 443 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { |
443 std::unique_ptr<SuggestionsService> suggestions_service( | 444 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
444 CreateSuggestionsServiceWithMocks()); | 445 CreateSuggestionsServiceWithMocks()); |
445 ASSERT_TRUE(suggestions_service != nullptr); | 446 ASSERT_TRUE(suggestions_service != nullptr); |
446 | 447 |
447 // Fake a non-200 response code. | 448 // Fake a non-200 response code. |
448 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | 449 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
449 "irrelevant", net::HTTP_BAD_REQUEST, | 450 "irrelevant", net::HTTP_BAD_REQUEST, |
450 net::URLRequestStatus::SUCCESS); | 451 net::URLRequestStatus::SUCCESS); |
451 | 452 |
452 // Expect that an upload to the blacklist is scheduled. | 453 // Expect that an upload to the blacklist is scheduled. |
453 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 454 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
454 .WillOnce(Return(false)); | 455 .WillOnce(Return(false)); |
455 | 456 |
456 // Send the request. Empty data will be returned to the callback. | 457 // Send the request. Empty data will be returned to the callback. |
457 suggestions_service->IssueRequestIfNoneOngoing( | 458 suggestions_service->IssueRequestIfNoneOngoing( |
458 SuggestionsService::BuildSuggestionsURL()); | 459 SuggestionsServiceImpl::BuildSuggestionsURL()); |
459 | 460 |
460 // (Testing only) wait until suggestion fetch is complete. | 461 // (Testing only) wait until suggestion fetch is complete. |
461 base::RunLoop().RunUntilIdle(); | 462 base::RunLoop().RunUntilIdle(); |
462 | 463 |
463 // Expect no suggestions in the cache. | 464 // Expect no suggestions in the cache. |
464 SuggestionsProfile empty_suggestions; | 465 SuggestionsProfile empty_suggestions; |
465 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions)); | 466 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions)); |
466 } | 467 } |
467 | 468 |
468 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 469 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
469 std::unique_ptr<SuggestionsService> suggestions_service( | 470 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
470 CreateSuggestionsServiceWithMocks()); | 471 CreateSuggestionsServiceWithMocks()); |
471 EXPECT_TRUE(suggestions_service != nullptr); | 472 EXPECT_TRUE(suggestions_service != nullptr); |
472 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 473 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
473 suggestions_service->set_blacklist_delay(no_delay); | 474 suggestions_service->set_blacklist_delay(no_delay); |
474 | 475 |
475 auto subscription = suggestions_service->AddCallback(base::Bind( | 476 auto subscription = suggestions_service->AddCallback(base::Bind( |
476 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 477 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
477 | 478 |
478 GURL blacklisted_url(kBlacklistedUrl); | 479 GURL blacklisted_url(kBlacklistedUrl); |
479 GURL request_url( | 480 GURL request_url( |
480 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); | 481 SuggestionsServiceImpl::BuildSuggestionsBlacklistURL(blacklisted_url)); |
481 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 482 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
482 factory_.SetFakeResponse(request_url, | 483 factory_.SetFakeResponse(request_url, |
483 suggestions_profile.SerializeAsString(), | 484 suggestions_profile.SerializeAsString(), |
484 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 485 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
485 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2); | 486 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2); |
486 | 487 |
487 // Expected calls to the blacklist store. | 488 // Expected calls to the blacklist store. |
488 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 489 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
489 .WillOnce(Return(true)); | 490 .WillOnce(Return(true)); |
490 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); | 491 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 .WillOnce(Return(false)); | 524 .WillOnce(Return(false)); |
524 | 525 |
525 Blacklist(suggestions_service.get(), blacklisted_url); | 526 Blacklist(suggestions_service.get(), blacklisted_url); |
526 | 527 |
527 EXPECT_TRUE(blacklisting_failed_); | 528 EXPECT_TRUE(blacklisting_failed_); |
528 EXPECT_EQ(0, suggestions_data_callback_count_); | 529 EXPECT_EQ(0, suggestions_data_callback_count_); |
529 } | 530 } |
530 | 531 |
531 // Initial blacklist request fails, triggering a second which succeeds. | 532 // Initial blacklist request fails, triggering a second which succeeds. |
532 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { | 533 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { |
533 std::unique_ptr<SuggestionsService> suggestions_service( | 534 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
534 CreateSuggestionsServiceWithMocks()); | 535 CreateSuggestionsServiceWithMocks()); |
535 ASSERT_TRUE(suggestions_service != nullptr); | 536 ASSERT_TRUE(suggestions_service != nullptr); |
536 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 537 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
537 suggestions_service->set_blacklist_delay(no_delay); | 538 suggestions_service->set_blacklist_delay(no_delay); |
538 | 539 |
539 auto subscription = suggestions_service->AddCallback(base::Bind( | 540 auto subscription = suggestions_service->AddCallback(base::Bind( |
540 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 541 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
541 | 542 |
542 GURL blacklisted_url(kBlacklistedUrl); | 543 GURL blacklisted_url(kBlacklistedUrl); |
543 GURL request_url( | 544 GURL request_url( |
544 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); | 545 SuggestionsServiceImpl::BuildSuggestionsBlacklistURL(blacklisted_url)); |
545 GURL blacklisted_url_alt(kBlacklistedUrlAlt); | 546 GURL blacklisted_url_alt(kBlacklistedUrlAlt); |
546 GURL request_url_alt( | 547 GURL request_url_alt(SuggestionsServiceImpl::BuildSuggestionsBlacklistURL( |
547 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url_alt)); | 548 blacklisted_url_alt)); |
548 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 549 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
549 | 550 |
550 // Note: we want to set the response for the blacklist URL to first | 551 // Note: we want to set the response for the blacklist URL to first |
551 // succeed, then fail. This doesn't seem possible. For simplicity of testing, | 552 // succeed, then fail. This doesn't seem possible. For simplicity of testing, |
552 // we'll pretend the URL changed in the BlacklistStore between the first and | 553 // we'll pretend the URL changed in the BlacklistStore between the first and |
553 // the second request, and adjust expectations accordingly. | 554 // the second request, and adjust expectations accordingly. |
554 factory_.SetFakeResponse(request_url, "irrelevant", net::HTTP_OK, | 555 factory_.SetFakeResponse(request_url, "irrelevant", net::HTTP_OK, |
555 net::URLRequestStatus::FAILED); | 556 net::URLRequestStatus::FAILED); |
556 factory_.SetFakeResponse(request_url_alt, | 557 factory_.SetFakeResponse(request_url_alt, |
557 suggestions_profile.SerializeAsString(), | 558 suggestions_profile.SerializeAsString(), |
(...skipping 21 matching lines...) Expand all Loading... |
579 | 580 |
580 // Wait for the first scheduling, the first request, the second scheduling, | 581 // Wait for the first scheduling, the first request, the second scheduling, |
581 // second request and the third scheduling. Again, note that calling | 582 // second request and the third scheduling. Again, note that calling |
582 // RunUntilIdle on the MessageLoop only works when the task is not posted for | 583 // RunUntilIdle on the MessageLoop only works when the task is not posted for |
583 // the future. | 584 // the future. |
584 base::RunLoop().RunUntilIdle(); | 585 base::RunLoop().RunUntilIdle(); |
585 CheckSuggestionsData(); | 586 CheckSuggestionsData(); |
586 } | 587 } |
587 | 588 |
588 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { | 589 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { |
589 std::unique_ptr<SuggestionsService> suggestions_service( | 590 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
590 CreateSuggestionsServiceWithMocks()); | 591 CreateSuggestionsServiceWithMocks()); |
591 ASSERT_TRUE(suggestions_service != nullptr); | 592 ASSERT_TRUE(suggestions_service != nullptr); |
592 // Ensure scheduling the request doesn't happen before undo. | 593 // Ensure scheduling the request doesn't happen before undo. |
593 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 594 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
594 suggestions_service->set_blacklist_delay(delay); | 595 suggestions_service->set_blacklist_delay(delay); |
595 | 596 |
596 auto subscription = suggestions_service->AddCallback(base::Bind( | 597 auto subscription = suggestions_service->AddCallback(base::Bind( |
597 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 598 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
598 | 599 |
599 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 600 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
(...skipping 18 matching lines...) Expand all Loading... |
618 | 619 |
619 Blacklist(suggestions_service.get(), blacklisted_url); | 620 Blacklist(suggestions_service.get(), blacklisted_url); |
620 UndoBlacklist(suggestions_service.get(), blacklisted_url); | 621 UndoBlacklist(suggestions_service.get(), blacklisted_url); |
621 | 622 |
622 EXPECT_EQ(2, suggestions_data_callback_count_); | 623 EXPECT_EQ(2, suggestions_data_callback_count_); |
623 EXPECT_FALSE(blacklisting_failed_); | 624 EXPECT_FALSE(blacklisting_failed_); |
624 EXPECT_FALSE(undo_blacklisting_failed_); | 625 EXPECT_FALSE(undo_blacklisting_failed_); |
625 } | 626 } |
626 | 627 |
627 TEST_F(SuggestionsServiceTest, ClearBlacklist) { | 628 TEST_F(SuggestionsServiceTest, ClearBlacklist) { |
628 std::unique_ptr<SuggestionsService> suggestions_service( | 629 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
629 CreateSuggestionsServiceWithMocks()); | 630 CreateSuggestionsServiceWithMocks()); |
630 ASSERT_TRUE(suggestions_service != nullptr); | 631 ASSERT_TRUE(suggestions_service != nullptr); |
631 // Ensure scheduling the request doesn't happen before undo. | 632 // Ensure scheduling the request doesn't happen before undo. |
632 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 633 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
633 suggestions_service->set_blacklist_delay(delay); | 634 suggestions_service->set_blacklist_delay(delay); |
634 | 635 |
635 auto subscription = suggestions_service->AddCallback(base::Bind( | 636 auto subscription = suggestions_service->AddCallback(base::Bind( |
636 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 637 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
637 | 638 |
638 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 639 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
639 GURL blacklisted_url(kBlacklistedUrl); | 640 GURL blacklisted_url(kBlacklistedUrl); |
640 | 641 |
641 factory_.SetFakeResponse( | 642 factory_.SetFakeResponse( |
642 SuggestionsService::BuildSuggestionsBlacklistClearURL(), | 643 SuggestionsServiceImpl::BuildSuggestionsBlacklistClearURL(), |
643 suggestions_profile.SerializeAsString(), net::HTTP_OK, | 644 suggestions_profile.SerializeAsString(), net::HTTP_OK, |
644 net::URLRequestStatus::SUCCESS); | 645 net::URLRequestStatus::SUCCESS); |
645 | 646 |
646 // Blacklist expectations. | 647 // Blacklist expectations. |
647 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 648 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
648 .WillOnce(Return(true)); | 649 .WillOnce(Return(true)); |
649 EXPECT_CALL(*mock_thumbnail_manager_, | 650 EXPECT_CALL(*mock_thumbnail_manager_, |
650 Initialize(EqualsProto(suggestions_profile))) | 651 Initialize(EqualsProto(suggestions_profile))) |
651 .Times(AnyNumber()); | 652 .Times(AnyNumber()); |
652 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber()); | 653 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber()); |
(...skipping 18 matching lines...) Expand all Loading... |
671 | 672 |
672 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { | 673 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { |
673 std::unique_ptr<GURL> request_url; | 674 std::unique_ptr<GURL> request_url; |
674 std::unique_ptr<net::FakeURLFetcher> fetcher; | 675 std::unique_ptr<net::FakeURLFetcher> fetcher; |
675 GURL retrieved_url; | 676 GURL retrieved_url; |
676 | 677 |
677 // Not a blacklist request. | 678 // Not a blacklist request. |
678 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); | 679 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); |
679 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, | 680 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, |
680 net::URLRequestStatus::SUCCESS); | 681 net::URLRequestStatus::SUCCESS); |
681 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | 682 EXPECT_FALSE( |
| 683 SuggestionsServiceImpl::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
682 | 684 |
683 // An actual blacklist request. | 685 // An actual blacklist request. |
684 std::string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; | 686 std::string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; |
685 std::string encoded_blacklisted_url = | 687 std::string encoded_blacklisted_url = |
686 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; | 688 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; |
687 std::string blacklist_request_prefix( | 689 std::string blacklist_request_prefix( |
688 SuggestionsService::BuildSuggestionsBlacklistURLPrefix()); | 690 SuggestionsServiceImpl::BuildSuggestionsBlacklistURLPrefix()); |
689 request_url.reset( | 691 request_url.reset( |
690 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); | 692 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); |
691 fetcher.reset(); | 693 fetcher.reset(); |
692 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, | 694 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, |
693 net::URLRequestStatus::SUCCESS); | 695 net::URLRequestStatus::SUCCESS); |
694 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | 696 EXPECT_TRUE( |
| 697 SuggestionsServiceImpl::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
695 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); | 698 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); |
696 } | 699 } |
697 | 700 |
698 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { | 701 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { |
699 std::unique_ptr<SuggestionsService> suggestions_service( | 702 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
700 CreateSuggestionsServiceWithMocks()); | 703 CreateSuggestionsServiceWithMocks()); |
701 base::TimeDelta initial_delay = suggestions_service->blacklist_delay(); | 704 base::TimeDelta initial_delay = suggestions_service->blacklist_delay(); |
702 | 705 |
703 // Delay unchanged on success. | 706 // Delay unchanged on success. |
704 suggestions_service->UpdateBlacklistDelay(true); | 707 suggestions_service->UpdateBlacklistDelay(true); |
705 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); | 708 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
706 | 709 |
707 // Delay increases on failure. | 710 // Delay increases on failure. |
708 suggestions_service->UpdateBlacklistDelay(false); | 711 suggestions_service->UpdateBlacklistDelay(false); |
709 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); | 712 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); |
710 | 713 |
711 // Delay resets on success. | 714 // Delay resets on success. |
712 suggestions_service->UpdateBlacklistDelay(true); | 715 suggestions_service->UpdateBlacklistDelay(true); |
713 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); | 716 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
714 } | 717 } |
715 | 718 |
716 TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) { | 719 TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) { |
717 std::unique_ptr<SuggestionsService> suggestions_service( | 720 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
718 CreateSuggestionsServiceWithMocks()); | 721 CreateSuggestionsServiceWithMocks()); |
719 SuggestionsProfile suggestions = | 722 SuggestionsProfile suggestions = |
720 CreateSuggestionsProfileWithExpiryTimestamps(); | 723 CreateSuggestionsProfileWithExpiryTimestamps(); |
721 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, | 724 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, |
722 kTestDefaultExpiry); | 725 kTestDefaultExpiry); |
723 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); | 726 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); |
724 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); | 727 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); |
725 } | 728 } |
726 | 729 |
727 TEST_F(SuggestionsServiceTest, GetPageThumbnail) { | 730 TEST_F(SuggestionsServiceTest, GetPageThumbnail) { |
728 std::unique_ptr<SuggestionsService> suggestions_service( | 731 std::unique_ptr<SuggestionsService> suggestions_service( |
729 CreateSuggestionsServiceWithMocks()); | 732 CreateSuggestionsServiceWithMocks()); |
730 ASSERT_TRUE(suggestions_service != nullptr); | 733 ASSERT_TRUE(suggestions_service != nullptr); |
731 | 734 |
732 GURL test_url(kTestUrl); | 735 GURL test_url(kTestUrl); |
733 GURL thumbnail_url("https://www.thumbnails.com/thumb.jpg"); | 736 GURL thumbnail_url("https://www.thumbnails.com/thumb.jpg"); |
734 base::Callback<void(const GURL&, const gfx::Image&)> dummy_callback; | 737 base::Callback<void(const GURL&, const gfx::Image&)> dummy_callback; |
735 | 738 |
736 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 739 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
737 suggestions_service->GetPageThumbnail(test_url, dummy_callback); | 740 suggestions_service->GetPageThumbnail(test_url, dummy_callback); |
738 | 741 |
739 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); | 742 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); |
740 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 743 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
741 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, | 744 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, |
742 dummy_callback); | 745 dummy_callback); |
743 | 746 |
744 } | 747 } |
745 | 748 |
746 } // namespace suggestions | 749 } // namespace suggestions |
OLD | NEW |