Chromium Code Reviews| Index: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| diff --git a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| index de2e1ccd3bf94f7e2f2fecab6c5213ee85228574..c69c48f799838204a6c7c2a3230e5b169eb3185f 100644 |
| --- a/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| +++ b/components/ntp_snippets/ntp_snippets_fetcher_unittest.cc |
| @@ -179,6 +179,7 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| snippets_fetcher_->SetTickClockForTesting( |
| mock_task_runner_->GetMockTickClock()); |
| test_hosts_.insert("www.somehost.com"); |
| + test_excluded_.insert("1234567890"); |
| // Increase initial time such that ticks are non-zero. |
| mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| } |
| @@ -191,6 +192,7 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| const std::string& test_lang() const { return test_lang_; } |
| const GURL& test_url() { return test_url_; } |
| const std::set<std::string>& test_hosts() const { return test_hosts_; } |
| + const std::set<std::string>& test_excluded() const { return test_excluded_; } |
| base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| void InitFakeURLFetcherFactory() { |
| @@ -228,6 +230,7 @@ class NTPSnippetsFetcherTest : public testing::Test { |
| const std::string test_lang_; |
| const GURL test_url_; |
| std::set<std::string> test_hosts_; |
| + std::set<std::string> test_excluded_; |
| base::HistogramTester histogram_tester_; |
| DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| @@ -255,6 +258,7 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| params.only_return_personalized_results = true; |
| params.user_locale = "en"; |
| params.host_restricts = {"chromium.org"}; |
| + params.excluded_ids = {"1234567890"}; |
| params.count_to_fetch = 25; |
| params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| @@ -302,6 +306,9 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| " \"uiLanguage\": \"en\"," |
| " \"regularlyVisitedHostNames\": [" |
| " \"chromium.org\"" |
| + " ]," |
| + " \"excludedSuggestionIds\": [" |
| + " \"1234567890\"" |
| " ]" |
| "}")); |
| } |
| @@ -311,6 +318,7 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
| params.only_return_personalized_results = false; |
| params.host_restricts = {}; |
| params.count_to_fetch = 10; |
| + params.excluded_ids = {}; |
| params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| EXPECT_THAT(params.BuildRequest(), |
| @@ -347,7 +355,47 @@ TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
| params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| EXPECT_THAT(params.BuildRequest(), |
| EqualsJSON("{" |
| - " \"regularlyVisitedHostNames\": []" |
| + " \"regularlyVisitedHostNames\": []," |
| + " \"excludedSuggestionIds\": []" |
| + "}")); |
| +} |
| + |
| +TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
| + NTPSnippetsFetcher::RequestParams params; |
| + params.only_return_personalized_results = false; |
| + params.host_restricts = {}; |
| + params.count_to_fetch = 10; |
| + for (int i = 0; i < 200; ++i) { |
| + params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
| + } |
| + |
| + params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| + EXPECT_THAT(params.BuildRequest(), |
| + EqualsJSON("{" |
| + " \"regularlyVisitedHostNames\": []," |
| + " \"excludedSuggestionIds\": [" |
| + " \"000\", \"001\", \"002\", \"003\", \"004\"," |
| + " \"005\", \"006\", \"007\", \"008\", \"009\"," |
| + " \"010\", \"011\", \"012\", \"013\", \"014\"," |
| + " \"015\", \"016\", \"017\", \"018\", \"019\"," |
| + " \"020\", \"021\", \"022\", \"023\", \"024\"," |
| + " \"025\", \"026\", \"027\", \"028\", \"029\"," |
| + " \"030\", \"031\", \"032\", \"033\", \"034\"," |
| + " \"035\", \"036\", \"037\", \"038\", \"039\"," |
| + " \"040\", \"041\", \"042\", \"043\", \"044\"," |
| + " \"045\", \"046\", \"047\", \"048\", \"049\"," |
| + " \"050\", \"051\", \"052\", \"053\", \"054\"," |
| + " \"055\", \"056\", \"057\", \"058\", \"059\"," |
| + " \"060\", \"061\", \"062\", \"063\", \"064\"," |
| + " \"065\", \"066\", \"067\", \"068\", \"069\"," |
| + " \"070\", \"071\", \"072\", \"073\", \"074\"," |
| + " \"075\", \"076\", \"077\", \"078\", \"079\"," |
| + " \"080\", \"081\", \"082\", \"083\", \"084\"," |
| + " \"085\", \"086\", \"087\", \"088\", \"089\"," |
| + " \"090\", \"091\", \"092\", \"093\", \"094\"," |
| + " \"095\", \"096\", \"097\", \"098\", \"099\"" |
| + // Truncated to 100 entries, those lexically first. |
|
Marc Treib
2016/08/25 11:41:44
"lexically first" is really just how it happens to
sfiera
2016/08/25 11:58:07
Done.
|
| + " ]" |
| "}")); |
| } |
| @@ -378,6 +426,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -413,6 +462,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -465,6 +515,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) { |
| EXPECT_CALL(mock_callback(), Run(_)) |
| .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); |
| snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| + test_excluded(), |
| /*count=*/1, |
| /*force_request=*/true); |
| FastForwardUntilNoTasksRemain(); |
| @@ -505,6 +556,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| + test_excluded(), |
| /*count=*/1, |
| /*interactive_request=*/true); |
| FastForwardUntilNoTasksRemain(); |
| @@ -522,6 +574,7 @@ TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { |
| EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), |
| /*language_code=*/"en-US", |
| + test_excluded(), |
| /*count=*/1, |
| /*interactive_request=*/true); |
| FastForwardUntilNoTasksRemain(); |
| @@ -543,7 +596,8 @@ TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { |
| TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { |
| net::TestURLFetcherFactory test_url_fetcher_factory; |
| snippets_fetcher().FetchSnippetsFromHosts( |
| - {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, |
| + {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(), |
| + /*count=*/17, |
| /*interactive_request=*/true); |
| net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| ASSERT_THAT(fetcher, NotNull()); |
| @@ -575,6 +629,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -596,6 +651,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -616,6 +672,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -638,6 +695,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -657,6 +715,7 @@ TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -677,6 +736,7 @@ 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); |
| FastForwardUntilNoTasksRemain(); |
| @@ -688,11 +748,13 @@ TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| net::URLRequestStatus::SUCCESS); |
| EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| + test_excluded(), |
| /*count=*/1, |
| /*interactive_request=*/true); |
| // 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); |
| FastForwardUntilNoTasksRemain(); |