Chromium Code Reviews| Index: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc |
| diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc |
| index 8539c34b796c42b7683664738a573fdc1ecbfb66..0ecba5b1377626ae6ab10301e61b5472ac5528d4 100644 |
| --- a/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc |
| +++ b/components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc |
| @@ -174,9 +174,6 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| pref_service_.get(), &category_factory_, nullptr, |
| base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); |
| - snippets_fetcher_->SetCallback( |
| - base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
| - base::Unretained(&mock_callback_))); |
| snippets_fetcher_->SetTickClockForTesting( |
| mock_task_runner_->GetMockTickClock()); |
| test_excluded_.insert("1234567890"); |
| @@ -184,6 +181,20 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| } |
| + NTPSnippetsFetcher::SnippetsAvailableCallback MakeMockCallback() { |
| + return base::BindOnce(&MockSnippetsAvailableCallback::WrappedRun, |
| + base::Unretained(&mock_callback_)); |
| + } |
| + |
| + base::Optional<Category> NoExclusiveCategory() { |
| + return base::Optional<Category>(); |
| + } |
| + |
| + base::Optional<Category> OptionalArticlesCategory() { |
| + return base::Optional<Category>( |
| + CategoryFactory().FromKnownCategory(KnownCategories::ARTICLES)); |
| + } |
| + |
| NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } |
| MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } |
| void FastForwardUntilNoTasksRemain() { |
| @@ -522,10 +533,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| @@ -558,10 +569,10 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) { |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| @@ -582,10 +593,10 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| @@ -635,10 +646,10 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) { |
| NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
| EXPECT_CALL(mock_callback(), Run(_)) |
| .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| ASSERT_TRUE(fetched_categories); |
| @@ -671,15 +682,76 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) { |
| /*count=*/1))); |
| } |
| +TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) { |
| + const std::string kJsonStr = |
| + "{\"categories\" : [{" |
| + " \"id\": 1," |
| + " \"localizedTitle\": \"Articles for You\"," |
| + " \"suggestions\" : [{" |
| + " \"ids\" : [\"http://localhost/foobar\"]," |
| + " \"title\" : \"Foo Barred from Baz\"," |
| + " \"snippet\" : \"...\"," |
| + " \"fullPageUrl\" : \"http://localhost/foobar\"," |
| + " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
| + " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
| + " \"attribution\" : \"Foo News\"," |
| + " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| + " \"ampUrl\" : \"http://localhost/amp\"," |
| + " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| + " }]" |
| + "}, {" |
| + " \"id\": 2," |
| + " \"localizedTitle\": \"Articles for Me\"," |
| + " \"suggestions\" : [{" |
| + " \"ids\" : [\"http://localhost/foo2\"]," |
| + " \"title\" : \"Foo Barred from Baz\"," |
| + " \"snippet\" : \"...\"," |
| + " \"fullPageUrl\" : \"http://localhost/foo2\"," |
| + " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
| + " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
| + " \"attribution\" : \"Foo News\"," |
| + " \"imageUrl\" : \"http://localhost/foo2.jpg\"," |
| + " \"ampUrl\" : \"http://localhost/amp\"," |
| + " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| + " }]" |
| + "}]}"; |
| + SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| + net::URLRequestStatus::SUCCESS); |
| + NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
| + EXPECT_CALL(mock_callback(), Run(_)) |
| + .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), |
| + OptionalArticlesCategory()); |
| + FastForwardUntilNoTasksRemain(); |
| + |
| + ASSERT_TRUE(fetched_categories); |
| + ASSERT_THAT(fetched_categories->size(), Eq(1u)); |
| + for (const auto& category : *fetched_categories) { |
| + const auto& articles = category.snippets; |
| + switch (category.category.id()) { |
| + case static_cast<int>(KnownCategories::ARTICLES): |
|
Marc Treib
2016/10/20 16:51:40
Use CategoryFactory::IsKnownCategory(category.id()
Marc Treib
2016/10/28 14:49:49
Done.
|
| + ASSERT_THAT(articles.size(), Eq(1u)); |
| + EXPECT_THAT(articles[0]->best_source().url.spec(), |
| + Eq("http://localhost/foobar")); |
| + break; |
| + default: |
| + FAIL() << "unexpected category with ID " << category.category.id(); |
| + } |
| + } |
| +} |
| + |
| TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| const std::string kJsonStr = "{\"recos\": []}"; |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| @@ -696,7 +768,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { |
| snippets_fetcher().FetchSnippetsFromHosts( |
| {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(), |
| /*count=*/17, |
| - /*interactive_request=*/true); |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| ASSERT_THAT(fetcher, NotNull()); |
| std::unique_ptr<base::Value> value = |
| @@ -726,10 +798,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
| net::URLRequestStatus::FAILED); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), |
| Eq("URLRequestStatus error -2")); |
| @@ -748,10 +820,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| EXPECT_THAT( |
| @@ -769,10 +841,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_status(), |
| StartsWith("Received invalid JSON (error ")); |
| @@ -792,10 +864,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
| EXPECT_THAT( |
| @@ -812,10 +884,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| EXPECT_THAT( |
| @@ -833,10 +905,10 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
| InitFakeURLFetcherFactory(); |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| } |
| @@ -845,16 +917,16 @@ TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. |
| // Callback is expected to be called once. |
| - snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| - test_excluded(), |
| - /*count=*/1, |
| - /*interactive_request=*/true); |
| + snippets_fetcher().FetchSnippetsFromHosts( |
| + test_hosts(), test_lang(), test_excluded(), |
| + /*count=*/1, |
| + /*interactive_request=*/true, MakeMockCallback(), NoExclusiveCategory()); |
| FastForwardUntilNoTasksRemain(); |
| EXPECT_THAT( |
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |