OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 int id, const GURL& url, net::URLFetcher::RequestType request_type, | 253 int id, const GURL& url, net::URLFetcher::RequestType request_type, |
254 net::URLFetcherDelegate* d) override { | 254 net::URLFetcherDelegate* d) override { |
255 return base::MakeUnique<net::FakeURLFetcher>( | 255 return base::MakeUnique<net::FakeURLFetcher>( |
256 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 256 url, d, /*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
257 net::URLRequestStatus::FAILED); | 257 net::URLRequestStatus::FAILED); |
258 } | 258 } |
259 }; | 259 }; |
260 | 260 |
261 class MockScheduler : public NTPSnippetsScheduler { | 261 class MockScheduler : public NTPSnippetsScheduler { |
262 public: | 262 public: |
263 MOCK_METHOD3(Schedule, | 263 MOCK_METHOD2(Schedule, |
264 bool(base::TimeDelta period_wifi, | 264 bool(base::TimeDelta period_wifi, |
265 base::TimeDelta period_fallback, | 265 base::TimeDelta period_fallback)); |
266 base::Time reschedule_time)); | |
267 MOCK_METHOD0(Unschedule, bool()); | 266 MOCK_METHOD0(Unschedule, bool()); |
268 }; | 267 }; |
269 | 268 |
270 class MockImageFetcher : public ImageFetcher { | 269 class MockImageFetcher : public ImageFetcher { |
271 public: | 270 public: |
272 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 271 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
273 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | 272 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); |
274 MOCK_METHOD3( | 273 MOCK_METHOD3( |
275 StartOrQueueNetworkRequest, | 274 StartOrQueueNetworkRequest, |
276 void(const std::string&, | 275 void(const std::string&, |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 | 474 |
476 base::ScopedTempDir database_dir_; | 475 base::ScopedTempDir database_dir_; |
477 | 476 |
478 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 477 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
479 }; | 478 }; |
480 | 479 |
481 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { | 480 TEST_F(NTPSnippetsServiceTest, ScheduleOnStart) { |
482 // We should get two |Schedule| calls: The first when initialization | 481 // We should get two |Schedule| calls: The first when initialization |
483 // completes, the second one after the automatic (since the service doesn't | 482 // completes, the second one after the automatic (since the service doesn't |
484 // have any data yet) fetch finishes. | 483 // have any data yet) fetch finishes. |
485 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 484 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
486 auto service = MakeSnippetsService(); | 485 auto service = MakeSnippetsService(); |
487 | 486 |
488 // When we have no snippets are all, loading the service initiates a fetch. | 487 // When we have no snippets are all, loading the service initiates a fetch. |
489 base::RunLoop().RunUntilIdle(); | 488 base::RunLoop().RunUntilIdle(); |
490 EXPECT_EQ("OK", service->snippets_fetcher()->last_status()); | 489 EXPECT_EQ("OK", service->snippets_fetcher()->last_status()); |
491 } | 490 } |
492 | 491 |
493 TEST_F(NTPSnippetsServiceTest, RescheduleAfterSuccessfulFetch) { | 492 TEST_F(NTPSnippetsServiceTest, RescheduleAfterSuccessfulFetch) { |
494 // We should get two |Schedule| calls: The first when initialization | 493 // We should get two |Schedule| calls: The first when initialization |
495 // completes, the second one after the automatic (since the service doesn't | 494 // completes, the second one after the automatic (since the service doesn't |
496 // have any data yet) fetch finishes. | 495 // have any data yet) fetch finishes. |
497 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 496 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
498 auto service = MakeSnippetsService(); | 497 auto service = MakeSnippetsService(); |
499 base::RunLoop().RunUntilIdle(); | 498 base::RunLoop().RunUntilIdle(); |
500 | 499 |
501 // A successful fetch should trigger another |Schedule|. | 500 // A successful fetch should trigger another |Schedule|. |
502 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)); | 501 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); |
503 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); | 502 LoadFromJSONString(service.get(), GetTestJson({GetSnippet()})); |
504 } | 503 } |
505 | 504 |
506 TEST_F(NTPSnippetsServiceTest, DontRescheduleAfterFailedFetch) { | 505 TEST_F(NTPSnippetsServiceTest, DontRescheduleAfterFailedFetch) { |
507 // We should get two |Schedule| calls: The first when initialization | 506 // We should get two |Schedule| calls: The first when initialization |
508 // completes, the second one after the automatic (since the service doesn't | 507 // completes, the second one after the automatic (since the service doesn't |
509 // have any data yet) fetch finishes. | 508 // have any data yet) fetch finishes. |
510 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 509 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
511 auto service = MakeSnippetsService(); | 510 auto service = MakeSnippetsService(); |
512 base::RunLoop().RunUntilIdle(); | 511 base::RunLoop().RunUntilIdle(); |
513 | 512 |
514 // A failed fetch should NOT trigger another |Schedule|. | 513 // A failed fetch should NOT trigger another |Schedule|. |
515 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(0); | 514 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(0); |
516 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); | 515 LoadFromJSONString(service.get(), GetTestJson({GetInvalidSnippet()})); |
517 } | 516 } |
518 | 517 |
519 TEST_F(NTPSnippetsServiceTest, DontRescheduleBeforeInit) { | 518 TEST_F(NTPSnippetsServiceTest, DontRescheduleBeforeInit) { |
520 // We should get two |Schedule| calls: The first when initialization | 519 // We should get two |Schedule| calls: The first when initialization |
521 // completes, the second one after the automatic (since the service doesn't | 520 // completes, the second one after the automatic (since the service doesn't |
522 // have any data yet) fetch finishes. | 521 // have any data yet) fetch finishes. |
523 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 522 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
524 // The |RescheduleFetching| call shouldn't do anything (in particular not | 523 // The |RescheduleFetching| call shouldn't do anything (in particular not |
525 // result in an |Unschedule|), since the service isn't initialized yet. | 524 // result in an |Unschedule|), since the service isn't initialized yet. |
526 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); | 525 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(0); |
527 auto service = MakeSnippetsServiceWithoutInitialization(); | 526 auto service = MakeSnippetsServiceWithoutInitialization(); |
528 service->RescheduleFetching(); | 527 service->RescheduleFetching(); |
529 WaitForSnippetsServiceInitialization(); | 528 WaitForSnippetsServiceInitialization(); |
530 | 529 |
531 // Now that initialization has finished, |RescheduleFetching| should work. | 530 // Now that initialization has finished, |RescheduleFetching| should work. |
532 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)); | 531 EXPECT_CALL(mock_scheduler(), Schedule(_, _)); |
533 service->RescheduleFetching(); | 532 service->RescheduleFetching(); |
534 } | 533 } |
535 | 534 |
536 TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) { | 535 TEST_F(NTPSnippetsServiceTest, RescheduleOnStateChange) { |
537 { | 536 { |
538 InSequence s; | 537 InSequence s; |
539 // Initial startup. | 538 // Initial startup. |
540 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 539 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
541 // Service gets disabled. | 540 // Service gets disabled. |
542 EXPECT_CALL(mock_scheduler(), Unschedule()); | 541 EXPECT_CALL(mock_scheduler(), Unschedule()); |
543 // Service gets enabled again. | 542 // Service gets enabled again. |
544 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _)).Times(2); | 543 EXPECT_CALL(mock_scheduler(), Schedule(_, _)).Times(2); |
545 } | 544 } |
546 auto service = MakeSnippetsService(); | 545 auto service = MakeSnippetsService(); |
547 ASSERT_TRUE(service->ready()); | 546 ASSERT_TRUE(service->ready()); |
548 base::RunLoop().RunUntilIdle(); | 547 base::RunLoop().RunUntilIdle(); |
549 | 548 |
550 service->OnDisabledReasonChanged(DisabledReason::EXPLICITLY_DISABLED); | 549 service->OnDisabledReasonChanged(DisabledReason::EXPLICITLY_DISABLED); |
551 ASSERT_FALSE(service->ready()); | 550 ASSERT_FALSE(service->ready()); |
552 base::RunLoop().RunUntilIdle(); | 551 base::RunLoop().RunUntilIdle(); |
553 | 552 |
554 service->OnDisabledReasonChanged(DisabledReason::NONE); | 553 service->OnDisabledReasonChanged(DisabledReason::NONE); |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 end = base::Time::FromTimeT(456); | 1037 end = base::Time::FromTimeT(456); |
1039 base::Callback<bool(const GURL& url)> filter; | 1038 base::Callback<bool(const GURL& url)> filter; |
1040 service->ClearHistory(begin, end, filter); | 1039 service->ClearHistory(begin, end, filter); |
1041 | 1040 |
1042 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); | 1041 EXPECT_THAT(service->GetSnippetsForTesting(articles_category()), IsEmpty()); |
1043 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), | 1042 EXPECT_THAT(service->GetDismissedSnippetsForTesting(articles_category()), |
1044 IsEmpty()); | 1043 IsEmpty()); |
1045 } | 1044 } |
1046 | 1045 |
1047 } // namespace ntp_snippets | 1046 } // namespace ntp_snippets |
OLD | NEW |