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_impl.h" | 5 #include "components/suggestions/suggestions_service_impl.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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 SuggestionsProfile suggestions; | 265 SuggestionsProfile suggestions; |
266 test_suggestions_store_->LoadSuggestions(&suggestions); | 266 test_suggestions_store_->LoadSuggestions(&suggestions); |
267 ASSERT_EQ(1, suggestions.suggestions_size()); | 267 ASSERT_EQ(1, suggestions.suggestions_size()); |
268 EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title()); | 268 EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title()); |
269 EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url()); | 269 EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url()); |
270 EXPECT_EQ(kTestFaviconUrl, suggestions.suggestions(0).favicon_url()); | 270 EXPECT_EQ(kTestFaviconUrl, suggestions.suggestions(0).favicon_url()); |
271 } | 271 } |
272 | 272 |
273 TEST_F(SuggestionsServiceTest, IgnoresNoopSyncChange) { | 273 TEST_F(SuggestionsServiceTest, IgnoresNoopSyncChange) { |
274 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 274 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 275 EXPECT_CALL(callback, Run(_)).Times(0); |
275 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 276 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
276 | 277 |
277 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), | 278 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
278 CreateSuggestionsProfile().SerializeAsString(), | 279 CreateSuggestionsProfile().SerializeAsString(), |
279 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 280 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
280 | 281 |
281 // An no-op change should not result in a suggestions refresh. | 282 // An no-op change should not result in a suggestions refresh. |
282 EXPECT_CALL(callback, Run(_)).Times(0); | |
283 suggestions_service_->OnStateChanged(&mock_sync_service_); | 283 suggestions_service_->OnStateChanged(&mock_sync_service_); |
284 | 284 |
285 // Let any network request run (there shouldn't be one). | 285 // Let any network request run (there shouldn't be one). |
286 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
287 } | 287 } |
288 | 288 |
289 TEST_F(SuggestionsServiceTest, IgnoresUninterestingSyncChange) { | 289 TEST_F(SuggestionsServiceTest, IgnoresUninterestingSyncChange) { |
290 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 290 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 291 EXPECT_CALL(callback, Run(_)).Times(0); |
291 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 292 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
292 | 293 |
293 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), | 294 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
294 CreateSuggestionsProfile().SerializeAsString(), | 295 CreateSuggestionsProfile().SerializeAsString(), |
295 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 296 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
296 | 297 |
297 // An uninteresting change should not result in a network request (the | 298 // An uninteresting change should not result in a network request (the |
298 // SyncState is INITIALIZED_ENABLED_HISTORY before and after). | 299 // SyncState is INITIALIZED_ENABLED_HISTORY before and after). |
299 EXPECT_CALL(mock_sync_service_, GetActiveDataTypes()) | 300 EXPECT_CALL(mock_sync_service_, GetActiveDataTypes()) |
300 .Times(AnyNumber()) | 301 .Times(AnyNumber()) |
301 .WillRepeatedly(Return(syncer::ModelTypeSet( | 302 .WillRepeatedly(Return(syncer::ModelTypeSet( |
302 syncer::HISTORY_DELETE_DIRECTIVES, syncer::BOOKMARKS))); | 303 syncer::HISTORY_DELETE_DIRECTIVES, syncer::BOOKMARKS))); |
303 EXPECT_CALL(callback, Run(_)).Times(0); | |
304 suggestions_service_->OnStateChanged(&mock_sync_service_); | 304 suggestions_service_->OnStateChanged(&mock_sync_service_); |
305 | 305 |
306 // Let any network request run (there shouldn't be one). | 306 // Let any network request run (there shouldn't be one). |
307 base::RunLoop().RunUntilIdle(); | 307 base::RunLoop().RunUntilIdle(); |
308 } | 308 } |
| 309 |
| 310 // During startup, the state changes from NOT_INITIALIZED_ENABLED to |
| 311 // INITIALIZED_ENABLED_HISTORY (for a signed-in user with history sync enabled). |
| 312 // This should *not* result in an automatic fetch. |
| 313 TEST_F(SuggestionsServiceTest, DoesNotFetchOnStartup) { |
| 314 // The sync service starts out inactive. |
| 315 EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(false)); |
| 316 suggestions_service_->OnStateChanged(&mock_sync_service_); |
| 317 |
| 318 ASSERT_EQ(SuggestionsServiceImpl::NOT_INITIALIZED_ENABLED, |
| 319 suggestions_service_->ComputeSyncState()); |
| 320 |
| 321 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 322 EXPECT_CALL(callback, Run(_)).Times(0); |
| 323 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
| 324 |
| 325 factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(), |
| 326 CreateSuggestionsProfile().SerializeAsString(), |
| 327 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 328 |
| 329 // Sync getting enabled should not result in a fetch. |
| 330 EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(true)); |
| 331 suggestions_service_->OnStateChanged(&mock_sync_service_); |
| 332 |
| 333 ASSERT_EQ(SuggestionsServiceImpl::INITIALIZED_ENABLED_HISTORY, |
| 334 suggestions_service_->ComputeSyncState()); |
| 335 |
| 336 // Let any network request run (there shouldn't be one). |
| 337 base::RunLoop().RunUntilIdle(); |
| 338 } |
309 | 339 |
310 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { | 340 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { |
311 EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(false)); | 341 EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(false)); |
312 suggestions_service_->OnStateChanged(&mock_sync_service_); | 342 suggestions_service_->OnStateChanged(&mock_sync_service_); |
313 | 343 |
314 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 344 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 345 EXPECT_CALL(callback, Run(_)).Times(0); |
315 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 346 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
316 | 347 |
317 // Try to fetch suggestions. Since sync is not active, no network request | 348 // Try to fetch suggestions. Since sync is not active, no network request |
318 // should be sent. | 349 // should be sent. |
319 EXPECT_CALL(callback, Run(_)).Times(0); | |
320 suggestions_service_->FetchSuggestionsData(); | 350 suggestions_service_->FetchSuggestionsData(); |
321 | 351 |
322 // Let any network request run (there shouldn't be one). | 352 // Let any network request run (there shouldn't be one). |
323 base::RunLoop().RunUntilIdle(); | 353 base::RunLoop().RunUntilIdle(); |
324 | 354 |
325 // |test_suggestions_store_| should still contain the default values. | 355 // |test_suggestions_store_| should still contain the default values. |
326 SuggestionsProfile suggestions; | 356 SuggestionsProfile suggestions; |
327 test_suggestions_store_->LoadSuggestions(&suggestions); | 357 test_suggestions_store_->LoadSuggestions(&suggestions); |
328 EXPECT_THAT(suggestions, EqualsProto(CreateSuggestionsProfile())); | 358 EXPECT_THAT(suggestions, EqualsProto(CreateSuggestionsProfile())); |
329 } | 359 } |
(...skipping 14 matching lines...) Expand all Loading... |
344 suggestions_service_->FetchSuggestionsData(); | 374 suggestions_service_->FetchSuggestionsData(); |
345 | 375 |
346 // Let any network request run. | 376 // Let any network request run. |
347 base::RunLoop().RunUntilIdle(); | 377 base::RunLoop().RunUntilIdle(); |
348 } | 378 } |
349 | 379 |
350 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { | 380 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { |
351 token_service_.set_auto_post_fetch_response_on_message_loop(false); | 381 token_service_.set_auto_post_fetch_response_on_message_loop(false); |
352 | 382 |
353 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 383 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 384 EXPECT_CALL(callback, Run(_)).Times(0); |
354 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 385 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
355 | 386 |
356 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 387 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
357 .WillOnce(Return(false)); | 388 .WillOnce(Return(false)); |
358 | 389 |
359 EXPECT_CALL(callback, Run(_)).Times(0); | |
360 | |
361 suggestions_service_->FetchSuggestionsData(); | 390 suggestions_service_->FetchSuggestionsData(); |
362 | 391 |
363 token_service_.IssueErrorForAllPendingRequests(GoogleServiceAuthError( | 392 token_service_.IssueErrorForAllPendingRequests(GoogleServiceAuthError( |
364 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS)); | 393 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS)); |
365 | 394 |
366 // No network request should be sent. | 395 // No network request should be sent. |
367 base::RunLoop().RunUntilIdle(); | 396 base::RunLoop().RunUntilIdle(); |
368 EXPECT_FALSE(HasPendingSuggestionsRequest()); | 397 EXPECT_FALSE(HasPendingSuggestionsRequest()); |
369 } | 398 } |
370 | 399 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 SuggestionsProfile suggestions; | 476 SuggestionsProfile suggestions; |
448 test_suggestions_store_->LoadSuggestions(&suggestions); | 477 test_suggestions_store_->LoadSuggestions(&suggestions); |
449 ASSERT_EQ(1, suggestions.suggestions_size()); | 478 ASSERT_EQ(1, suggestions.suggestions_size()); |
450 EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title()); | 479 EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title()); |
451 EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url()); | 480 EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url()); |
452 EXPECT_EQ(kTestFaviconUrl, suggestions.suggestions(0).favicon_url()); | 481 EXPECT_EQ(kTestFaviconUrl, suggestions.suggestions(0).favicon_url()); |
453 } | 482 } |
454 | 483 |
455 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { | 484 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { |
456 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 485 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
| 486 EXPECT_CALL(callback, Run(_)).Times(0); |
457 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 487 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
458 | 488 |
459 const GURL blacklisted_url(kBlacklistedUrl); | 489 const GURL blacklisted_url(kBlacklistedUrl); |
460 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 490 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
461 .WillOnce(Return(false)); | 491 .WillOnce(Return(false)); |
462 EXPECT_CALL(callback, Run(_)).Times(0); | |
463 EXPECT_FALSE(suggestions_service_->BlacklistURL(blacklisted_url)); | 492 EXPECT_FALSE(suggestions_service_->BlacklistURL(blacklisted_url)); |
464 } | 493 } |
465 | 494 |
466 // Initial blacklist request fails, triggering a second which succeeds. | 495 // Initial blacklist request fails, triggering a second which succeeds. |
467 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { | 496 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { |
468 const base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 497 const base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
469 suggestions_service_->set_blacklist_delay(no_delay); | 498 suggestions_service_->set_blacklist_delay(no_delay); |
470 | 499 |
471 base::MockCallback<SuggestionsService::ResponseCallback> callback; | 500 base::MockCallback<SuggestionsService::ResponseCallback> callback; |
472 auto subscription = suggestions_service_->AddCallback(callback.Get()); | 501 auto subscription = suggestions_service_->AddCallback(callback.Get()); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 733 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
705 suggestions_service_->GetPageThumbnail(test_url, dummy_callback); | 734 suggestions_service_->GetPageThumbnail(test_url, dummy_callback); |
706 | 735 |
707 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); | 736 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); |
708 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 737 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
709 suggestions_service_->GetPageThumbnailWithURL(test_url, thumbnail_url, | 738 suggestions_service_->GetPageThumbnailWithURL(test_url, thumbnail_url, |
710 dummy_callback); | 739 dummy_callback); |
711 } | 740 } |
712 | 741 |
713 } // namespace suggestions | 742 } // namespace suggestions |
OLD | NEW |