Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: components/suggestions/suggestions_service_impl_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698