| Index: components/ntp_snippets/remote/remote_suggestions_fetcher_unittest.cc
|
| diff --git a/components/ntp_snippets/remote/remote_suggestions_fetcher_unittest.cc b/components/ntp_snippets/remote/remote_suggestions_fetcher_unittest.cc
|
| index f5716a1e768d5328cb295b129ae56e903b1db1d4..54a5cf03078d120f6d55351a0fd552aebd6b582b 100644
|
| --- a/components/ntp_snippets/remote/remote_suggestions_fetcher_unittest.cc
|
| +++ b/components/ntp_snippets/remote/remote_suggestions_fetcher_unittest.cc
|
| @@ -23,14 +23,14 @@
|
| #include "components/ntp_snippets/ntp_snippets_constants.h"
|
| #include "components/ntp_snippets/remote/remote_suggestion.h"
|
| #include "components/ntp_snippets/remote/request_params.h"
|
| +#include "components/ntp_snippets/remote/test_utils.h"
|
| #include "components/ntp_snippets/user_classifier.h"
|
| #include "components/prefs/testing_pref_service.h"
|
| -#include "components/signin/core/browser/account_tracker_service.h"
|
| #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
|
| #include "components/signin/core/browser/fake_signin_manager.h"
|
| -#include "components/signin/core/browser/test_signin_client.h"
|
| #include "components/variations/entropy_provider.h"
|
| #include "components/variations/variations_params_manager.h"
|
| +#include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
|
| #include "net/url_request/test_url_fetcher_factory.h"
|
| #include "net/url_request/url_request_test_util.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| @@ -57,9 +57,13 @@ using testing::WithArg;
|
| const char kAPIKey[] = "fakeAPIkey";
|
| const char kTestChromeReaderUrl[] =
|
| "https://chromereader-pa.googleapis.com/v1/fetch?key=fakeAPIkey";
|
| -const char kTestChromeContentSuggestionsUrl[] =
|
| +const char kTestChromeContentSuggestionsSignedOutUrl[] =
|
| "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/"
|
| "fetch?key=fakeAPIkey";
|
| +const char kTestChromeContentSuggestionsSignedInUrl[] =
|
| + "https://chromecontentsuggestions-pa.googleapis.com/v1/suggestions/fetch";
|
| +
|
| +const char kTestEmail[] = "foo@bar.com";
|
|
|
| // Artificial time delay for JSON parsing.
|
| const int64_t kTestJsonParsingLatencyMs = 20;
|
| @@ -260,15 +264,14 @@ void ParseJsonDelayed(const std::string& json,
|
| const SuccessCallback& success_callback,
|
| const ErrorCallback& error_callback) {
|
| base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
| - FROM_HERE, base::Bind(&ParseJson, json, std::move(success_callback),
|
| - std::move(error_callback)),
|
| + FROM_HERE,
|
| + base::Bind(&ParseJson, json, std::move(success_callback),
|
| + std::move(error_callback)),
|
| base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs));
|
| }
|
|
|
| } // namespace
|
|
|
| -// TODO(jkrcal): Add unit-tests with signin client being signed in (covering
|
| -// sign-in / refresh tokens / access token code). crbug.com/688310
|
| class RemoteSuggestionsFetcherTestBase : public testing::Test {
|
| public:
|
| explicit RemoteSuggestionsFetcherTestBase(const GURL& gurl)
|
| @@ -279,32 +282,49 @@ class RemoteSuggestionsFetcherTestBase : public testing::Test {
|
| {ntp_snippets::kArticleSuggestionsFeature.name}),
|
| mock_task_runner_(new base::TestMockTimeTaskRunner()),
|
| mock_task_runner_handle_(mock_task_runner_),
|
| - signin_client_(base::MakeUnique<TestSigninClient>(nullptr)),
|
| - account_tracker_(base::MakeUnique<AccountTrackerService>()),
|
| - fake_signin_manager_(
|
| - base::MakeUnique<FakeSigninManagerBase>(signin_client_.get(),
|
| - account_tracker_.get())),
|
| - fake_token_service_(base::MakeUnique<FakeProfileOAuth2TokenService>()),
|
| - pref_service_(base::MakeUnique<TestingPrefServiceSimple>()),
|
| test_url_(gurl) {
|
| - RequestThrottler::RegisterProfilePrefs(pref_service_->registry());
|
| - UserClassifier::RegisterProfilePrefs(pref_service_->registry());
|
| - user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get());
|
| + RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry());
|
| + UserClassifier::RegisterProfilePrefs(utils_.pref_service()->registry());
|
| + user_classifier_ = base::MakeUnique<UserClassifier>(utils_.pref_service());
|
| // Increase initial time such that ticks are non-zero.
|
| mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234));
|
| - ResetSnippetsFetcher();
|
| + ResetFetcher();
|
| + }
|
| +
|
| + void ResetFetcher() {
|
| + scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
|
| + new net::TestURLRequestContextGetter(mock_task_runner_.get());
|
| +
|
| + fake_token_service_delegate_ =
|
| + new FakeOAuth2TokenServiceDelegate(request_context_getter.get());
|
| + // Not a memleak because OAuth2TokenService takes ownership of the raw
|
| + // pointer (crbug.com/688387).
|
| + fake_token_service_ = base::MakeUnique<FakeProfileOAuth2TokenService>(
|
| + fake_token_service_delegate_);
|
| +
|
| + fetcher_ = base::MakeUnique<RemoteSuggestionsFetcher>(
|
| + utils_.fake_signin_manager(), fake_token_service_.get(),
|
| + std::move(request_context_getter), utils_.pref_service(), nullptr,
|
| + base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get());
|
| +
|
| + fetcher_->SetTickClockForTesting(mock_task_runner_->GetMockTickClock());
|
| }
|
|
|
| - void ResetSnippetsFetcher() {
|
| - snippets_fetcher_ = base::MakeUnique<RemoteSuggestionsFetcher>(
|
| - fake_signin_manager_.get(), fake_token_service_.get(),
|
| - scoped_refptr<net::TestURLRequestContextGetter>(
|
| - new net::TestURLRequestContextGetter(mock_task_runner_.get())),
|
| - pref_service_.get(), nullptr, base::Bind(&ParseJsonDelayed), kAPIKey,
|
| - user_classifier_.get());
|
| + void SignIn() { utils_.fake_signin_manager()->SignIn(kTestEmail); }
|
|
|
| - snippets_fetcher_->SetTickClockForTesting(
|
| - mock_task_runner_->GetMockTickClock());
|
| + void IssueRefreshToken() {
|
| + fake_token_service_delegate_->UpdateCredentials(kTestEmail, "token");
|
| + }
|
| +
|
| + void IssueOAuth2Token() {
|
| + fake_token_service_->IssueAllTokensForAccount(kTestEmail, "access_token",
|
| + base::Time::Max());
|
| + }
|
| +
|
| + void CancelOAuth2TokenRequests() {
|
| + fake_token_service_->IssueErrorForAllPendingRequestsForAccount(
|
| + kTestEmail, GoogleServiceAuthError(
|
| + GoogleServiceAuthError::State::REQUEST_CANCELED));
|
| }
|
|
|
| RemoteSuggestionsFetcher::SnippetsAvailableCallback
|
| @@ -313,7 +333,7 @@ class RemoteSuggestionsFetcherTestBase : public testing::Test {
|
| base::Unretained(callback));
|
| }
|
|
|
| - RemoteSuggestionsFetcher& snippets_fetcher() { return *snippets_fetcher_; }
|
| + RemoteSuggestionsFetcher& fetcher() { return *fetcher_; }
|
| MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; }
|
| void FastForwardUntilNoTasksRemain() {
|
| mock_task_runner_->FastForwardUntilNoTasksRemain();
|
| @@ -355,24 +375,20 @@ class RemoteSuggestionsFetcherTestBase : public testing::Test {
|
| response_code, status);
|
| }
|
|
|
| - TestingPrefServiceSimple* pref_service() const { return pref_service_.get(); }
|
| -
|
| protected:
|
| std::map<std::string, std::string> default_variation_params_;
|
|
|
| private:
|
| + test::RemoteSuggestionsTestUtils utils_;
|
| variations::testing::VariationParamsManager params_manager_;
|
| scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_;
|
| base::ThreadTaskRunnerHandle mock_task_runner_handle_;
|
| FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
|
| // Initialized lazily in SetFakeResponse().
|
| std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_;
|
| - std::unique_ptr<TestSigninClient> signin_client_;
|
| - std::unique_ptr<AccountTrackerService> account_tracker_;
|
| - std::unique_ptr<SigninManagerBase> fake_signin_manager_;
|
| - std::unique_ptr<OAuth2TokenService> fake_token_service_;
|
| - std::unique_ptr<RemoteSuggestionsFetcher> snippets_fetcher_;
|
| - std::unique_ptr<TestingPrefServiceSimple> pref_service_;
|
| + FakeOAuth2TokenServiceDelegate* fake_token_service_delegate_;
|
| + std::unique_ptr<FakeProfileOAuth2TokenService> fake_token_service_;
|
| + std::unique_ptr<RemoteSuggestionsFetcher> fetcher_;
|
| std::unique_ptr<UserClassifier> user_classifier_;
|
| MockSnippetsAvailableCallback mock_callback_;
|
| const GURL test_url_;
|
| @@ -381,27 +397,38 @@ class RemoteSuggestionsFetcherTestBase : public testing::Test {
|
| DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsFetcherTestBase);
|
| };
|
|
|
| -class ChromeReaderSnippetsFetcherTest
|
| +class RemoteSuggestionsChromeReaderFetcherTest
|
| : public RemoteSuggestionsFetcherTestBase {
|
| public:
|
| - ChromeReaderSnippetsFetcherTest()
|
| + RemoteSuggestionsChromeReaderFetcherTest()
|
| : RemoteSuggestionsFetcherTestBase(GURL(kTestChromeReaderUrl)) {
|
| default_variation_params_["content_suggestions_backend"] =
|
| kChromeReaderServer;
|
| SetVariationParam("content_suggestions_backend", kChromeReaderServer);
|
| - ResetSnippetsFetcher();
|
| + ResetFetcher();
|
| }
|
| };
|
|
|
| -class NTPSnippetsContentSuggestionsFetcherTest
|
| +class RemoteSuggestionsSignedOutFetcherTest
|
| : public RemoteSuggestionsFetcherTestBase {
|
| public:
|
| - NTPSnippetsContentSuggestionsFetcherTest()
|
| + RemoteSuggestionsSignedOutFetcherTest()
|
| : RemoteSuggestionsFetcherTestBase(
|
| - GURL(kTestChromeContentSuggestionsUrl)) {}
|
| + GURL(kTestChromeContentSuggestionsSignedOutUrl)) {}
|
| };
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldNotFetchOnCreation) {
|
| +// TODO(jkrcal): Add unit-tests for the "authentication in progress" case as it
|
| +// requires more changes (instead FakeSigninManagerBase use FakeSigninManager
|
| +// which does not exist on ChromeOS). crbug.com/688310
|
| +class RemoteSuggestionsSignedInFetcherTest
|
| + : public RemoteSuggestionsFetcherTestBase {
|
| + public:
|
| + RemoteSuggestionsSignedInFetcherTest()
|
| + : RemoteSuggestionsFetcherTestBase(
|
| + GURL(kTestChromeContentSuggestionsSignedInUrl)) {}
|
| +};
|
| +
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldNotFetchOnCreation) {
|
| // The lack of registered baked in responses would cause any fetch to fail.
|
| FastForwardUntilNoTasksRemain();
|
| EXPECT_THAT(histogram_tester().GetAllSamples(
|
| @@ -409,10 +436,10 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldNotFetchOnCreation) {
|
| IsEmpty());
|
| EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
|
| IsEmpty());
|
| - EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty());
|
| + EXPECT_THAT(fetcher().last_status(), IsEmpty());
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfully) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldFetchSuccessfully) {
|
| const std::string kJsonStr =
|
| "{\"recos\": [{"
|
| " \"contentInfo\": {"
|
| @@ -430,11 +457,93 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfully) {
|
| Run(IsSuccess(),
|
| AllOf(IsSingleArticle("http://localhost/foobar"),
|
| FirstCategoryHasInfo(IsCategoryInfoForArticles()))));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| + FastForwardUntilNoTasksRemain();
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(histogram_tester().GetAllSamples(
|
| + "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| + ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| + EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
|
| + ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
|
| + /*count=*/1)));
|
| +}
|
| +
|
| +TEST_F(RemoteSuggestionsSignedOutFetcherTest, ShouldFetchSuccessfully) {
|
| + 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\" "
|
| + " }]"
|
| + "}]}";
|
| + SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| + net::URLRequestStatus::SUCCESS);
|
| + EXPECT_CALL(mock_callback(),
|
| + Run(IsSuccess(),
|
| + AllOf(IsSingleArticle("http://localhost/foobar"),
|
| + FirstCategoryHasInfo(IsCategoryInfoForArticles()))));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| + FastForwardUntilNoTasksRemain();
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(histogram_tester().GetAllSamples(
|
| + "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| + ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| + EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
|
| + ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
|
| + /*count=*/1)));
|
| +}
|
| +
|
| +TEST_F(RemoteSuggestionsSignedInFetcherTest, ShouldFetchSuccessfully) {
|
| + SignIn();
|
| + IssueRefreshToken();
|
| +
|
| + 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\" "
|
| + " }]"
|
| + "}]}";
|
| + SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| + net::URLRequestStatus::SUCCESS);
|
| + EXPECT_CALL(mock_callback(),
|
| + Run(IsSuccess(),
|
| + AllOf(IsSingleArticle("http://localhost/foobar"),
|
| + FirstCategoryHasInfo(IsCategoryInfoForArticles()))));
|
| +
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| +
|
| + IssueOAuth2Token();
|
| + // Wait for the fake response.
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| +
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(histogram_tester().GetAllSamples(
|
| "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| @@ -443,7 +552,11 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfully) {
|
| /*count=*/1)));
|
| }
|
|
|
| -TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
|
| +TEST_F(RemoteSuggestionsSignedInFetcherTest,
|
| + ShouldRetryWhenOAuthCancelled) {
|
| + SignIn();
|
| + IssueRefreshToken();
|
| +
|
| const std::string kJsonStr =
|
| "{\"categories\" : [{"
|
| " \"id\": 1,"
|
| @@ -467,11 +580,17 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
|
| Run(IsSuccess(),
|
| AllOf(IsSingleArticle("http://localhost/foobar"),
|
| FirstCategoryHasInfo(IsCategoryInfoForArticles()))));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| +
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| +
|
| + CancelOAuth2TokenRequests();
|
| + IssueOAuth2Token();
|
| + // Wait for the fake response.
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| +
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(histogram_tester().GetAllSamples(
|
| "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| @@ -480,7 +599,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
|
| /*count=*/1)));
|
| }
|
|
|
| -TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
|
| +TEST_F(RemoteSuggestionsSignedOutFetcherTest, EmptyCategoryIsOK) {
|
| const std::string kJsonStr =
|
| "{\"categories\" : [{"
|
| " \"id\": 1,"
|
| @@ -489,11 +608,11 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
|
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(IsSuccess(), IsEmptyArticleList()));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(histogram_tester().GetAllSamples(
|
| "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| @@ -502,7 +621,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
|
| /*count=*/1)));
|
| }
|
|
|
| -TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
|
| +TEST_F(RemoteSuggestionsSignedOutFetcherTest, ServerCategories) {
|
| const std::string kJsonStr =
|
| "{\"categories\" : [{"
|
| " \"id\": 1,"
|
| @@ -541,8 +660,8 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
|
| RemoteSuggestionsFetcher::OptionalFetchedCategories fetched_categories;
|
| EXPECT_CALL(mock_callback(), Run(IsSuccess(), _))
|
| .WillOnce(MoveArgument1PointeeTo(&fetched_categories));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
|
|
| ASSERT_TRUE(fetched_categories);
|
| @@ -564,8 +683,8 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
|
| }
|
| }
|
|
|
| - EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(histogram_tester().GetAllSamples(
|
| "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| @@ -574,7 +693,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
|
| /*count=*/1)));
|
| }
|
|
|
| -TEST_F(NTPSnippetsContentSuggestionsFetcherTest,
|
| +TEST_F(RemoteSuggestionsSignedOutFetcherTest,
|
| SupportMissingAllowFetchingMoreResultsOption) {
|
| // This tests makes sure we handle the missing option although it's required
|
| // by the interface. It's just that the Service doesn't follow that
|
| @@ -602,8 +721,8 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest,
|
| RemoteSuggestionsFetcher::OptionalFetchedCategories fetched_categories;
|
| EXPECT_CALL(mock_callback(), Run(IsSuccess(), _))
|
| .WillOnce(MoveArgument1PointeeTo(&fetched_categories));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
|
|
| ASSERT_TRUE(fetched_categories);
|
| @@ -613,7 +732,7 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest,
|
| Eq(base::UTF8ToUTF16("Articles for Me")));
|
| }
|
|
|
| -TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) {
|
| +TEST_F(RemoteSuggestionsSignedOutFetcherTest, ExclusiveCategoryOnly) {
|
| const std::string kJsonStr =
|
| "{\"categories\" : [{"
|
| " \"id\": 1,"
|
| @@ -671,8 +790,8 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) {
|
| params.exclusive_category =
|
| base::Optional<Category>(Category::FromRemoteCategory(2));
|
|
|
| - snippets_fetcher().FetchSnippets(
|
| - params, ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(params,
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
|
|
| ASSERT_TRUE(fetched_categories);
|
| @@ -684,16 +803,17 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ExclusiveCategoryOnly) {
|
| Eq("http://localhost/foo2"));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest,
|
| + ShouldFetchSuccessfullyEmptyList) {
|
| const std::string kJsonStr = "{\"recos\": []}";
|
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(IsSuccess(), IsEmptyArticleList()));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(fetcher().last_status(), Eq("OK"));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
|
| @@ -702,20 +822,20 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, RetryOnInteractiveRequests) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, RetryOnInteractiveRequests) {
|
| DelegateCallingTestURLFetcherFactory fetcher_factory;
|
| RequestParams params = test_params();
|
| params.interactive_request = true;
|
|
|
| - snippets_fetcher().FetchSnippets(
|
| - params, ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(params,
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
|
|
| net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
|
| ASSERT_THAT(fetcher, NotNull());
|
| EXPECT_THAT(fetcher->GetMaxRetriesOn5xx(), Eq(2));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest,
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest,
|
| RetriesConfigurableOnNonInteractiveRequests) {
|
| struct ExpectationForVariationParam {
|
| std::string param_value;
|
| @@ -735,8 +855,8 @@ TEST_F(ChromeReaderSnippetsFetcherTest,
|
| DelegateCallingTestURLFetcherFactory fetcher_factory;
|
| SetVariationParam("background_5xx_retries_count", retry_config.param_value);
|
|
|
| - snippets_fetcher().FetchSnippets(
|
| - params, ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(params,
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
|
|
| net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
|
| ASSERT_THAT(fetcher, NotNull());
|
| @@ -745,18 +865,17 @@ TEST_F(ChromeReaderSnippetsFetcherTest,
|
| }
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportUrlStatusError) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldReportUrlStatusError) {
|
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
|
| net::URLRequestStatus::FAILED);
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(),
|
| - Eq("URLRequestStatus error -2"));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
|
| + EXPECT_THAT(fetcher().last_status(), Eq("URLRequestStatus error -2"));
|
| + EXPECT_THAT(fetcher().last_json(), IsEmpty());
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
|
| @@ -767,16 +886,16 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportUrlStatusError) {
|
| Not(IsEmpty()));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportHttpError) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldReportHttpError) {
|
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
|
| + EXPECT_THAT(fetcher().last_json(), IsEmpty());
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
|
| @@ -787,19 +906,19 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportHttpError) {
|
| Not(IsEmpty()));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonError) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldReportJsonError) {
|
| const std::string kInvalidJsonStr = "{ \"recos\": []";
|
| SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_status(),
|
| + EXPECT_THAT(fetcher().last_status(),
|
| StartsWith("Received invalid JSON (error "));
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kInvalidJsonStr));
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
|
| @@ -811,16 +930,17 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonError) {
|
| /*count=*/1)));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest,
|
| + ShouldReportJsonErrorForEmptyResponse) {
|
| SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_json(), std::string());
|
| + EXPECT_THAT(fetcher().last_json(), std::string());
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
|
| @@ -829,7 +949,7 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
|
| ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportInvalidListError) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest, ShouldReportInvalidListError) {
|
| const std::string kJsonStr =
|
| "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
|
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| @@ -837,10 +957,10 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportInvalidListError) {
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| - EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
|
| + EXPECT_THAT(fetcher().last_json(), Eq(kJsonStr));
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
| ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
|
| @@ -853,34 +973,35 @@ TEST_F(ChromeReaderSnippetsFetcherTest, ShouldReportInvalidListError) {
|
|
|
| // This test actually verifies that the test setup itself is sane, to prevent
|
| // hard-to-reproduce test failures.
|
| -TEST_F(ChromeReaderSnippetsFetcherTest,
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest,
|
| ShouldReportHttpErrorForMissingBakedResponse) {
|
| InitFakeURLFetcherFactory();
|
| EXPECT_CALL(mock_callback(), Run(HasCode(StatusCode::TEMPORARY_ERROR),
|
| /*snippets=*/Not(HasValue())))
|
| .Times(1);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| }
|
|
|
| -TEST_F(ChromeReaderSnippetsFetcherTest, ShouldProcessConcurrentFetches) {
|
| +TEST_F(RemoteSuggestionsChromeReaderFetcherTest,
|
| + ShouldProcessConcurrentFetches) {
|
| const std::string kJsonStr = "{ \"recos\": [] }";
|
| SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK,
|
| net::URLRequestStatus::SUCCESS);
|
| EXPECT_CALL(mock_callback(), Run(IsSuccess(), IsEmptyArticleList())).Times(5);
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| // More calls to FetchSnippets() do not interrupt the previous.
|
| // Callback is expected to be called once each time.
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| - snippets_fetcher().FetchSnippets(
|
| - test_params(), ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| + fetcher().FetchSnippets(test_params(),
|
| + ToSnippetsAvailableCallback(&mock_callback()));
|
| FastForwardUntilNoTasksRemain();
|
| EXPECT_THAT(
|
| histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
|
|
|