| Index: components/suggestions/suggestions_service_impl_unittest.cc
|
| diff --git a/components/suggestions/suggestions_service_impl_unittest.cc b/components/suggestions/suggestions_service_impl_unittest.cc
|
| index 84c28fd3d131703d27a14d486f28a67e06ef81bc..c12f4b5eac0ffdbecdc638904990e4e400c0a80d 100644
|
| --- a/components/suggestions/suggestions_service_impl_unittest.cc
|
| +++ b/components/suggestions/suggestions_service_impl_unittest.cc
|
| @@ -35,10 +35,12 @@
|
| #include "ui/gfx/image/image.h"
|
|
|
| using sync_preferences::TestingPrefServiceSyncable;
|
| +using syncer::SyncServiceObserver;
|
| using testing::_;
|
| using testing::AnyNumber;
|
| using testing::DoAll;
|
| using testing::Eq;
|
| +using testing::Mock;
|
| using testing::Return;
|
| using testing::SetArgPointee;
|
| using testing::StrictMock;
|
| @@ -46,33 +48,16 @@ using testing::StrictMock;
|
| namespace {
|
|
|
| const char kAccountId[] = "account";
|
| +const char kSuggestionsUrlPath[] = "/chromesuggestions";
|
| +const char kBlacklistUrlPath[] = "/chromesuggestions/blacklist";
|
| +const char kBlacklistClearUrlPath[] = "/chromesuggestions/blacklist/clear";
|
| const char kTestTitle[] = "a title";
|
| const char kTestUrl[] = "http://go.com";
|
| const char kTestFaviconUrl[] =
|
| "https://s2.googleusercontent.com/s2/favicons?domain_url="
|
| "http://go.com&alt=s&sz=32";
|
| const char kBlacklistedUrl[] = "http://blacklist.com";
|
| -const char kBlacklistedUrlAlt[] = "http://blacklist-atl.com";
|
| -const int64_t kTestDefaultExpiry = 1402200000000000;
|
| -const int64_t kTestSetExpiry = 1404792000000000;
|
| -
|
| -std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher(
|
| - const GURL& url,
|
| - net::URLFetcherDelegate* delegate,
|
| - const std::string& response_data,
|
| - net::HttpStatusCode response_code,
|
| - net::URLRequestStatus::Status status) {
|
| - std::unique_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher(
|
| - url, delegate, response_data, response_code, status));
|
| -
|
| - if (response_code == net::HTTP_OK) {
|
| - scoped_refptr<net::HttpResponseHeaders> download_headers(
|
| - new net::HttpResponseHeaders(""));
|
| - download_headers->AddHeader("Content-Type: text/html");
|
| - fetcher->set_response_headers(download_headers);
|
| - }
|
| - return fetcher;
|
| -}
|
| +const int64_t kTestSetExpiry = 12121212; // This timestamp lies in the past.
|
|
|
| // GMock matcher for protobuf equality.
|
| MATCHER_P(EqualsProto, message, "") {
|
| @@ -95,23 +80,6 @@ SuggestionsProfile CreateSuggestionsProfile() {
|
| ChromeSuggestion* suggestion = profile.add_suggestions();
|
| suggestion->set_title(kTestTitle);
|
| suggestion->set_url(kTestUrl);
|
| - suggestion->set_expiry_ts(kTestSetExpiry);
|
| - return profile;
|
| -}
|
| -
|
| -// Creates one suggestion with expiry timestamp and one without.
|
| -SuggestionsProfile CreateSuggestionsProfileWithExpiryTimestamps() {
|
| - SuggestionsProfile profile;
|
| - profile.set_timestamp(123);
|
| - ChromeSuggestion* suggestion = profile.add_suggestions();
|
| - suggestion->set_title(kTestTitle);
|
| - suggestion->set_url(kTestUrl);
|
| - suggestion->set_expiry_ts(kTestSetExpiry);
|
| -
|
| - suggestion = profile.add_suggestions();
|
| - suggestion->set_title(kTestTitle);
|
| - suggestion->set_url(kTestUrl);
|
| -
|
| return profile;
|
| }
|
|
|
| @@ -171,7 +139,8 @@ class SuggestionsServiceTest : public testing::Test {
|
| SuggestionsServiceTest()
|
| : signin_client_(&pref_service_),
|
| signin_manager_(&signin_client_, &account_tracker_),
|
| - factory_(nullptr, base::Bind(&CreateURLFetcher)),
|
| + request_context_(new net::TestURLRequestContextGetter(
|
| + io_message_loop_.task_runner())),
|
| mock_thumbnail_manager_(nullptr),
|
| mock_blacklist_store_(nullptr),
|
| test_suggestions_store_(nullptr) {
|
| @@ -186,23 +155,19 @@ class SuggestionsServiceTest : public testing::Test {
|
| ~SuggestionsServiceTest() override {}
|
|
|
| void SetUp() override {
|
| - request_context_ =
|
| - new net::TestURLRequestContextGetter(io_message_loop_.task_runner());
|
| -
|
| - EXPECT_CALL(mock_sync_service_, CanSyncStart())
|
| + EXPECT_CALL(*sync_service(), CanSyncStart())
|
| .Times(AnyNumber())
|
| .WillRepeatedly(Return(true));
|
| - EXPECT_CALL(mock_sync_service_, IsSyncActive())
|
| + EXPECT_CALL(*sync_service(), IsSyncActive())
|
| .Times(AnyNumber())
|
| .WillRepeatedly(Return(true));
|
| - EXPECT_CALL(mock_sync_service_, ConfigurationDone())
|
| + EXPECT_CALL(*sync_service(), ConfigurationDone())
|
| .Times(AnyNumber())
|
| .WillRepeatedly(Return(true));
|
| - EXPECT_CALL(mock_sync_service_, GetActiveDataTypes())
|
| + EXPECT_CALL(*sync_service(), GetActiveDataTypes())
|
| .Times(AnyNumber())
|
| .WillRepeatedly(
|
| Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES)));
|
| -
|
| // These objects are owned by the SuggestionsService, but we keep the
|
| // pointers around for testing.
|
| test_suggestions_store_ = new TestSuggestionsStore();
|
| @@ -215,17 +180,52 @@ class SuggestionsServiceTest : public testing::Test {
|
| base::WrapUnique(mock_blacklist_store_));
|
| }
|
|
|
| - bool HasPendingSuggestionsRequest() const {
|
| - return !!suggestions_service_->pending_request_.get();
|
| + GURL GetCurrentlyQueriedUrl() {
|
| + net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
|
| + if (!fetcher) {
|
| + return GURL();
|
| + }
|
| + return fetcher->GetOriginalURL();
|
| }
|
|
|
| - protected:
|
| + void RespondToFetch(const std::string& response_body,
|
| + net::HttpStatusCode response_code,
|
| + net::URLRequestStatus status) {
|
| + net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
|
| + ASSERT_TRUE(fetcher) << "Tried to respond to fetch that is not ongoing!";
|
| + fetcher->SetResponseString(response_body);
|
| + fetcher->set_response_code(response_code);
|
| + fetcher->set_status(status);
|
| + fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| + }
|
| +
|
| + void RespondToFetchWithProfile(const SuggestionsProfile& suggestions) {
|
| + RespondToFetch(
|
| + suggestions.SerializeAsString(), net::HTTP_OK,
|
| + net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
|
| + }
|
| +
|
| + FakeProfileOAuth2TokenService* token_service() { return &token_service_; }
|
| +
|
| + MockSyncService* sync_service() { return &mock_sync_service_; }
|
| +
|
| + MockImageManager* thumbnail_manager() { return mock_thumbnail_manager_; }
|
| +
|
| + MockBlacklistStore* blacklist_store() { return mock_blacklist_store_; }
|
| +
|
| + TestSuggestionsStore* suggestions_store() { return test_suggestions_store_; }
|
| +
|
| + SuggestionsServiceImpl* suggestions_service() {
|
| + return suggestions_service_.get();
|
| + }
|
| +
|
| + private:
|
| base::MessageLoopForIO io_message_loop_;
|
| TestingPrefServiceSyncable pref_service_;
|
| AccountTrackerService account_tracker_;
|
| TestSigninClient signin_client_;
|
| FakeSigninManagerBase signin_manager_;
|
| - net::FakeURLFetcherFactory factory_;
|
| + net::TestURLFetcherFactory factory_;
|
| FakeProfileOAuth2TokenService token_service_;
|
| MockSyncService mock_sync_service_;
|
| scoped_refptr<net::TestURLRequestContextGetter> request_context_;
|
| @@ -236,34 +236,31 @@ class SuggestionsServiceTest : public testing::Test {
|
|
|
| std::unique_ptr<SuggestionsServiceImpl> suggestions_service_;
|
|
|
| - private:
|
| DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest);
|
| };
|
|
|
| TEST_F(SuggestionsServiceTest, FetchSuggestionsData) {
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| - // Set up net::FakeURLFetcherFactory.
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| -
|
| - EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_));
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_));
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(Return(false));
|
|
|
| // Send the request. The data should be returned to the callback.
|
| - suggestions_service_->FetchSuggestionsData();
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| EXPECT_CALL(callback, Run(_));
|
|
|
| - // Let the network request run.
|
| + // Wait for the eventual network request.
|
| base::RunLoop().RunUntilIdle();
|
| + ASSERT_TRUE(GetCurrentlyQueriedUrl().is_valid());
|
| + EXPECT_EQ(GetCurrentlyQueriedUrl().path(), kSuggestionsUrlPath);
|
| + RespondToFetchWithProfile(CreateSuggestionsProfile());
|
|
|
| SuggestionsProfile suggestions;
|
| - test_suggestions_store_->LoadSuggestions(&suggestions);
|
| + suggestions_store()->LoadSuggestions(&suggestions);
|
| ASSERT_EQ(1, suggestions.suggestions_size());
|
| EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title());
|
| EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url());
|
| @@ -273,38 +270,34 @@ TEST_F(SuggestionsServiceTest, FetchSuggestionsData) {
|
| TEST_F(SuggestionsServiceTest, IgnoresNoopSyncChange) {
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // An no-op change should not result in a suggestions refresh.
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| - // Let any network request run (there shouldn't be one).
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, IgnoresUninterestingSyncChange) {
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // An uninteresting change should not result in a network request (the
|
| // SyncState is INITIALIZED_ENABLED_HISTORY before and after).
|
| - EXPECT_CALL(mock_sync_service_, GetActiveDataTypes())
|
| + EXPECT_CALL(*sync_service(), GetActiveDataTypes())
|
| .Times(AnyNumber())
|
| .WillRepeatedly(Return(syncer::ModelTypeSet(
|
| syncer::HISTORY_DELETE_DIRECTIVES, syncer::BOOKMARKS)));
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| - // Let any network request run (there shouldn't be one).
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
| }
|
|
|
| // During startup, the state changes from NOT_INITIALIZED_ENABLED to
|
| @@ -312,160 +305,144 @@ TEST_F(SuggestionsServiceTest, IgnoresUninterestingSyncChange) {
|
| // This should *not* result in an automatic fetch.
|
| TEST_F(SuggestionsServiceTest, DoesNotFetchOnStartup) {
|
| // The sync service starts out inactive.
|
| - EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(false));
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| -
|
| - ASSERT_EQ(SuggestionsServiceImpl::NOT_INITIALIZED_ENABLED,
|
| - suggestions_service_->ComputeSyncState());
|
| -
|
| - base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + EXPECT_CALL(*sync_service(), IsSyncActive()).WillRepeatedly(Return(false));
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
|
|
| // Sync getting enabled should not result in a fetch.
|
| - EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(true));
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| + EXPECT_CALL(*sync_service(), IsSyncActive()).WillRepeatedly(Return(true));
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| - ASSERT_EQ(SuggestionsServiceImpl::INITIALIZED_ENABLED_HISTORY,
|
| - suggestions_service_->ComputeSyncState());
|
| -
|
| - // Let any network request run (there shouldn't be one).
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) {
|
| - EXPECT_CALL(mock_sync_service_, IsSyncActive()).WillRepeatedly(Return(false));
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| + EXPECT_CALL(*sync_service(), IsSyncActive()).WillRepeatedly(Return(false));
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Try to fetch suggestions. Since sync is not active, no network request
|
| // should be sent.
|
| - suggestions_service_->FetchSuggestionsData();
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| - // Let any network request run (there shouldn't be one).
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
|
|
| - // |test_suggestions_store_| should still contain the default values.
|
| + // |suggestions_store()| should still contain the default values.
|
| SuggestionsProfile suggestions;
|
| - test_suggestions_store_->LoadSuggestions(&suggestions);
|
| + suggestions_store()->LoadSuggestions(&suggestions);
|
| EXPECT_THAT(suggestions, EqualsProto(CreateSuggestionsProfile()));
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) {
|
| - EXPECT_CALL(mock_sync_service_, CanSyncStart()).WillRepeatedly(Return(false));
|
| + EXPECT_CALL(*sync_service(), CanSyncStart()).WillRepeatedly(Return(false));
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Tell SuggestionsService that the sync state changed. The cache should be
|
| // cleared and empty data returned to the callback.
|
| EXPECT_CALL(callback, Run(EqualsProto(SuggestionsProfile())));
|
| - suggestions_service_->OnStateChanged(&mock_sync_service_);
|
| + static_cast<SyncServiceObserver*>(suggestions_service())
|
| + ->OnStateChanged(sync_service());
|
|
|
| // Try to fetch suggestions. Since sync is not active, no network request
|
| // should be sent.
|
| - suggestions_service_->FetchSuggestionsData();
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| - // Let any network request run.
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) {
|
| - token_service_.set_auto_post_fetch_response_on_message_loop(false);
|
| + token_service()->set_auto_post_fetch_response_on_message_loop(false);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(Return(false));
|
|
|
| - suggestions_service_->FetchSuggestionsData();
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| - token_service_.IssueErrorForAllPendingRequests(GoogleServiceAuthError(
|
| + token_service()->IssueErrorForAllPendingRequests(GoogleServiceAuthError(
|
| GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS));
|
|
|
| - // No network request should be sent.
|
| + // Wait for eventual (but unexpected) network requests.
|
| base::RunLoop().RunUntilIdle();
|
| - EXPECT_FALSE(HasPendingSuggestionsRequest());
|
| + EXPECT_FALSE(suggestions_service()->has_pending_request_for_testing());
|
| }
|
|
|
| -TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) {
|
| - // Fake a request error.
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - "irrelevant", net::HTTP_OK,
|
| - net::URLRequestStatus::FAILED);
|
| -
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| +TEST_F(SuggestionsServiceTest, FetchingSuggestionsIgnoresRequestFailure) {
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(Return(false));
|
|
|
| - // Send the request. Empty data will be returned to the callback.
|
| - suggestions_service_->IssueRequestIfNoneOngoing(
|
| - SuggestionsServiceImpl::BuildSuggestionsURL());
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| - // (Testing only) wait until suggestion fetch is complete.
|
| + // Wait for the eventual network request.
|
| base::RunLoop().RunUntilIdle();
|
| + RespondToFetch("irrelevant", net::HTTP_OK,
|
| + net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
| + net::ERR_INVALID_RESPONSE));
|
| }
|
|
|
| -TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) {
|
| - // Fake a non-200 response code.
|
| - factory_.SetFakeResponse(SuggestionsServiceImpl::BuildSuggestionsURL(),
|
| - "irrelevant", net::HTTP_BAD_REQUEST,
|
| - net::URLRequestStatus::SUCCESS);
|
| +TEST_F(SuggestionsServiceTest, FetchingSuggestionsClearsStoreIfResponseNotOK) {
|
| + suggestions_store()->StoreSuggestions(CreateSuggestionsProfile());
|
|
|
| // Expect that an upload to the blacklist is scheduled.
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(Return(false));
|
|
|
| // Send the request. Empty data will be returned to the callback.
|
| - suggestions_service_->IssueRequestIfNoneOngoing(
|
| - SuggestionsServiceImpl::BuildSuggestionsURL());
|
| + suggestions_service()->FetchSuggestionsData();
|
|
|
| - // (Testing only) wait until suggestion fetch is complete.
|
| + // Wait for the eventual network request.
|
| base::RunLoop().RunUntilIdle();
|
| + RespondToFetch(
|
| + "irrelevant", net::HTTP_BAD_REQUEST,
|
| + net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
|
|
|
| - // Expect no suggestions in the cache.
|
| SuggestionsProfile empty_suggestions;
|
| - EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions));
|
| + EXPECT_FALSE(suggestions_store()->LoadSuggestions(&empty_suggestions));
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, BlacklistURL) {
|
| + // Calling RunUntilIdle on the RunLoop only works when the task is not posted
|
| + // for the future.
|
| const base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0);
|
| - suggestions_service_->set_blacklist_delay(no_delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(no_delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| - const GURL request_url(
|
| - SuggestionsServiceImpl::BuildSuggestionsBlacklistURL(blacklisted_url));
|
| - factory_.SetFakeResponse(request_url,
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| - EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2);
|
| -
|
| - // Expected calls to the blacklist store.
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
| +
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_)).Times(2);
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2);
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_)).Times(2);
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
|
| .WillOnce(Return(false));
|
| - EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_))
|
| - .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true)));
|
| - EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), GetCandidateForUpload(_))
|
| + .WillOnce(DoAll(SetArgPointee<0>(GURL(kBlacklistedUrl)), Return(true)));
|
| + EXPECT_CALL(*blacklist_store(), RemoveUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
|
|
| EXPECT_CALL(callback, Run(_)).Times(2);
|
|
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
|
|
| // Wait on the upload task, the blacklist request and the next blacklist
|
| // scheduling task. This only works when the scheduling task is not for future
|
| @@ -473,8 +450,11 @@ TEST_F(SuggestionsServiceTest, BlacklistURL) {
|
| // BlacklistStore's candidacy delay are zero).
|
| base::RunLoop().RunUntilIdle();
|
|
|
| + EXPECT_EQ(GetCurrentlyQueriedUrl().path(), kBlacklistUrlPath);
|
| + RespondToFetchWithProfile(CreateSuggestionsProfile());
|
| +
|
| SuggestionsProfile suggestions;
|
| - test_suggestions_store_->LoadSuggestions(&suggestions);
|
| + suggestions_store()->LoadSuggestions(&suggestions);
|
| ASSERT_EQ(1, suggestions.suggestions_size());
|
| EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title());
|
| EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url());
|
| @@ -484,68 +464,69 @@ TEST_F(SuggestionsServiceTest, BlacklistURL) {
|
| TEST_F(SuggestionsServiceTest, BlacklistURLFails) {
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| EXPECT_CALL(callback, Run(_)).Times(0);
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(false));
|
| - EXPECT_FALSE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| +
|
| + EXPECT_FALSE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
| }
|
|
|
| -// Initial blacklist request fails, triggering a second which succeeds.
|
| -TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) {
|
| +TEST_F(SuggestionsServiceTest, RetryBlacklistURLRequestAfterFailure) {
|
| + // Calling RunUntilIdle on the RunLoop only works when the task is not
|
| + // posted for the future.
|
| const base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0);
|
| - suggestions_service_->set_blacklist_delay(no_delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(no_delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| - const GURL request_url(
|
| - SuggestionsServiceImpl::BuildSuggestionsBlacklistURL(blacklisted_url));
|
| - const GURL blacklisted_url_alt(kBlacklistedUrlAlt);
|
| - const GURL request_url_alt(
|
| - SuggestionsServiceImpl::BuildSuggestionsBlacklistURL(
|
| - blacklisted_url_alt));
|
| -
|
| - // Note: we want to set the response for the blacklist URL to first
|
| - // succeed, then fail. This doesn't seem possible. For simplicity of testing,
|
| - // we'll pretend the URL changed in the BlacklistStore between the first and
|
| - // the second request, and adjust expectations accordingly.
|
| - factory_.SetFakeResponse(request_url, "irrelevant", net::HTTP_OK,
|
| - net::URLRequestStatus::FAILED);
|
| - factory_.SetFakeResponse(request_url_alt,
|
| - CreateSuggestionsProfile().SerializeAsString(),
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS);
|
| -
|
| - // Expectations.
|
| - EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2);
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
| +
|
| + // Set expectations for first, failing request.
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_));
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2);
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| - .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)))
|
| - .WillOnce(Return(false));
|
| - EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_))
|
| - .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true)))
|
| - .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url_alt), Return(true)));
|
| - EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url_alt)))
|
| - .WillOnce(Return(true));
|
| + .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true)));
|
| + EXPECT_CALL(*blacklist_store(), GetCandidateForUpload(_))
|
| + .WillOnce(DoAll(SetArgPointee<0>(GURL(kBlacklistedUrl)), Return(true)));
|
|
|
| EXPECT_CALL(callback, Run(_)).Times(2);
|
|
|
| // Blacklist call, first request attempt.
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
|
|
| - // Wait for the first scheduling, the first request, the second scheduling,
|
| - // second request and the third scheduling. Again, note that calling
|
| - // RunUntilIdle on the MessageLoop only works when the task is not posted for
|
| - // the future.
|
| + // Wait for the first scheduling receiving a failing response.
|
| base::RunLoop().RunUntilIdle();
|
| + ASSERT_TRUE(GetCurrentlyQueriedUrl().is_valid());
|
| + EXPECT_EQ(GetCurrentlyQueriedUrl().path(), kBlacklistUrlPath);
|
| + RespondToFetch("irrelevant", net::HTTP_OK,
|
| + net::URLRequestStatus(net::URLRequestStatus::FAILED,
|
| + net::ERR_INVALID_RESPONSE));
|
| +
|
| + // Assert that the failure was processed as expected.
|
| + Mock::VerifyAndClearExpectations(thumbnail_manager());
|
| + Mock::VerifyAndClearExpectations(blacklist_store());
|
| +
|
| + // Now expect the retried request to succeed.
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_));
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| + .WillOnce(Return(false));
|
| + EXPECT_CALL(*blacklist_store(), GetCandidateForUpload(_))
|
| + .WillOnce(DoAll(SetArgPointee<0>(GURL(kBlacklistedUrl)), Return(true)));
|
| + EXPECT_CALL(*blacklist_store(), RemoveUrl(Eq(GURL(kBlacklistedUrl))))
|
| + .WillOnce(Return(true));
|
| +
|
| + // Wait for the second scheduling followed by a successful response.
|
| + base::RunLoop().RunUntilIdle();
|
| + ASSERT_TRUE(GetCurrentlyQueriedUrl().is_valid());
|
| + EXPECT_EQ(GetCurrentlyQueriedUrl().path(), kBlacklistUrlPath);
|
| + RespondToFetchWithProfile(CreateSuggestionsProfile());
|
|
|
| SuggestionsProfile suggestions;
|
| - test_suggestions_store_->LoadSuggestions(&suggestions);
|
| + suggestions_store()->LoadSuggestions(&suggestions);
|
| ASSERT_EQ(1, suggestions.suggestions_size());
|
| EXPECT_EQ(kTestTitle, suggestions.suggestions(0).title());
|
| EXPECT_EQ(kTestUrl, suggestions.suggestions(0).url());
|
| @@ -555,174 +536,173 @@ TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) {
|
| TEST_F(SuggestionsServiceTest, UndoBlacklistURL) {
|
| // Ensure scheduling the request doesn't happen before undo.
|
| const base::TimeDelta delay = base::TimeDelta::FromHours(1);
|
| - suggestions_service_->set_blacklist_delay(delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Blacklist expectations.
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_thumbnail_manager_,
|
| + EXPECT_CALL(*thumbnail_manager(),
|
| Initialize(EqualsProto(CreateSuggestionsProfile())))
|
| .Times(AnyNumber());
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber());
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_)).Times(AnyNumber());
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
|
| // Undo expectations.
|
| - EXPECT_CALL(*mock_blacklist_store_,
|
| - GetTimeUntilURLReadyForUpload(Eq(blacklisted_url), _))
|
| + EXPECT_CALL(*blacklist_store(),
|
| + GetTimeUntilURLReadyForUpload(Eq(GURL(kBlacklistedUrl)), _))
|
| .WillOnce(DoAll(SetArgPointee<1>(delay), Return(true)));
|
| - EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), RemoveUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
|
|
| EXPECT_CALL(callback, Run(_)).Times(2);
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| - EXPECT_TRUE(suggestions_service_->UndoBlacklistURL(blacklisted_url));
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
| + EXPECT_TRUE(suggestions_service()->UndoBlacklistURL(GURL(kBlacklistedUrl)));
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, ClearBlacklist) {
|
| - // Ensure scheduling the request doesn't happen before undo.
|
| const base::TimeDelta delay = base::TimeDelta::FromHours(1);
|
| - suggestions_service_->set_blacklist_delay(delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const SuggestionsProfile suggestions_profile = CreateSuggestionsProfile();
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| -
|
| - factory_.SetFakeResponse(
|
| - SuggestionsServiceImpl::BuildSuggestionsBlacklistClearURL(),
|
| - suggestions_profile.SerializeAsString(), net::HTTP_OK,
|
| - net::URLRequestStatus::SUCCESS);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Blacklist expectations.
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_thumbnail_manager_,
|
| - Initialize(EqualsProto(suggestions_profile)))
|
| + EXPECT_CALL(*thumbnail_manager(),
|
| + Initialize(EqualsProto(CreateSuggestionsProfile())))
|
| .Times(AnyNumber());
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber());
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_)).Times(AnyNumber());
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
|
| - EXPECT_CALL(*mock_blacklist_store_, ClearBlacklist());
|
| + EXPECT_CALL(*blacklist_store(), ClearBlacklist());
|
|
|
| EXPECT_CALL(callback, Run(_)).Times(2);
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| - suggestions_service_->ClearBlacklist();
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
| + suggestions_service()->ClearBlacklist();
|
| +
|
| + // Wait for the eventual network request.
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(GetCurrentlyQueriedUrl().path(), kBlacklistClearUrlPath);
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) {
|
| // Ensure scheduling the request doesn't happen before undo.
|
| const base::TimeDelta delay = base::TimeDelta::FromHours(1);
|
| - suggestions_service_->set_blacklist_delay(delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Blacklist expectations.
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_thumbnail_manager_,
|
| + EXPECT_CALL(*thumbnail_manager(),
|
| Initialize(EqualsProto(CreateSuggestionsProfile())));
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
|
| -
|
| + // Undo expectations.
|
| // URL is not in local blacklist.
|
| - EXPECT_CALL(*mock_blacklist_store_,
|
| - GetTimeUntilURLReadyForUpload(Eq(blacklisted_url), _))
|
| + EXPECT_CALL(*blacklist_store(),
|
| + GetTimeUntilURLReadyForUpload(Eq(GURL(kBlacklistedUrl)), _))
|
| .WillOnce(Return(false));
|
|
|
| EXPECT_CALL(callback, Run(_));
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| - EXPECT_FALSE(suggestions_service_->UndoBlacklistURL(blacklisted_url));
|
| +
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
| + EXPECT_FALSE(suggestions_service()->UndoBlacklistURL(GURL(kBlacklistedUrl)));
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) {
|
| // Ensure scheduling the request doesn't happen before undo.
|
| const base::TimeDelta delay = base::TimeDelta::FromHours(1);
|
| - suggestions_service_->set_blacklist_delay(delay);
|
| + suggestions_service()->set_blacklist_delay_for_testing(delay);
|
|
|
| base::MockCallback<SuggestionsService::ResponseCallback> callback;
|
| - auto subscription = suggestions_service_->AddCallback(callback.Get());
|
| -
|
| - const GURL blacklisted_url(kBlacklistedUrl);
|
| + auto subscription = suggestions_service()->AddCallback(callback.Get());
|
|
|
| // Blacklist expectations.
|
| - EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url)))
|
| + EXPECT_CALL(*blacklist_store(), BlacklistUrl(Eq(GURL(kBlacklistedUrl))))
|
| .WillOnce(Return(true));
|
| - EXPECT_CALL(*mock_thumbnail_manager_,
|
| + EXPECT_CALL(*thumbnail_manager(),
|
| Initialize(EqualsProto(CreateSuggestionsProfile())));
|
| - EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_));
|
| - EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_))
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true)));
|
|
|
| // URL is not yet candidate for upload.
|
| - base::TimeDelta negative_delay = base::TimeDelta::FromHours(-1);
|
| - EXPECT_CALL(*mock_blacklist_store_,
|
| - GetTimeUntilURLReadyForUpload(Eq(blacklisted_url), _))
|
| + const base::TimeDelta negative_delay = base::TimeDelta::FromHours(-1);
|
| + EXPECT_CALL(*blacklist_store(),
|
| + GetTimeUntilURLReadyForUpload(Eq(GURL(kBlacklistedUrl)), _))
|
| .WillOnce(DoAll(SetArgPointee<1>(negative_delay), Return(true)));
|
|
|
| EXPECT_CALL(callback, Run(_));
|
| - EXPECT_TRUE(suggestions_service_->BlacklistURL(blacklisted_url));
|
| - EXPECT_FALSE(suggestions_service_->UndoBlacklistURL(blacklisted_url));
|
| -}
|
| -
|
| -TEST_F(SuggestionsServiceTest, GetBlacklistedUrlNotBlacklistRequest) {
|
| - // Not a blacklist request.
|
| - std::unique_ptr<net::FakeURLFetcher> fetcher(
|
| - CreateURLFetcher(GURL("http://not-blacklisting.com/a?b=c"), nullptr, "",
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS));
|
| - GURL retrieved_url;
|
| - EXPECT_FALSE(
|
| - SuggestionsServiceImpl::GetBlacklistedUrl(*fetcher, &retrieved_url));
|
| -}
|
|
|
| -TEST_F(SuggestionsServiceTest, GetBlacklistedUrlBlacklistRequest) {
|
| - // An actual blacklist request.
|
| - const GURL blacklisted_url("http://blacklisted.com/a?b=c&d=e");
|
| - const std::string encoded_blacklisted_url =
|
| - "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De";
|
| - const std::string blacklist_request_prefix(
|
| - SuggestionsServiceImpl::BuildSuggestionsBlacklistURLPrefix());
|
| - std::unique_ptr<net::FakeURLFetcher> fetcher(CreateURLFetcher(
|
| - GURL(blacklist_request_prefix + encoded_blacklisted_url), nullptr, "",
|
| - net::HTTP_OK, net::URLRequestStatus::SUCCESS));
|
| - GURL retrieved_url;
|
| - EXPECT_TRUE(
|
| - SuggestionsServiceImpl::GetBlacklistedUrl(*fetcher, &retrieved_url));
|
| - EXPECT_EQ(blacklisted_url, retrieved_url);
|
| + EXPECT_TRUE(suggestions_service()->BlacklistURL(GURL(kBlacklistedUrl)));
|
| + EXPECT_FALSE(suggestions_service()->UndoBlacklistURL(GURL(kBlacklistedUrl)));
|
| }
|
|
|
| -TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) {
|
| - const base::TimeDelta initial_delay = suggestions_service_->blacklist_delay();
|
| +TEST_F(SuggestionsServiceTest, TemporarilyIncreasesBlacklistDelayOnFailure) {
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_)).Times(AnyNumber());
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_)).Times(AnyNumber());
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| + .Times(AnyNumber())
|
| + .WillRepeatedly(Return(false));
|
| + const base::TimeDelta initial_delay =
|
| + suggestions_service()->blacklist_delay_for_testing();
|
|
|
| // Delay unchanged on success.
|
| - suggestions_service_->UpdateBlacklistDelay(true);
|
| - EXPECT_EQ(initial_delay, suggestions_service_->blacklist_delay());
|
| + suggestions_service()->FetchSuggestionsData();
|
| + base::RunLoop().RunUntilIdle();
|
| + RespondToFetchWithProfile(CreateSuggestionsProfile());
|
| + EXPECT_EQ(initial_delay,
|
| + suggestions_service()->blacklist_delay_for_testing());
|
|
|
| // Delay increases on failure.
|
| - suggestions_service_->UpdateBlacklistDelay(false);
|
| - EXPECT_GT(suggestions_service_->blacklist_delay(), initial_delay);
|
| + suggestions_service()->FetchSuggestionsData();
|
| + base::RunLoop().RunUntilIdle();
|
| + RespondToFetch(
|
| + "irrelevant", net::HTTP_BAD_REQUEST,
|
| + net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
|
| + EXPECT_GT(suggestions_service()->blacklist_delay_for_testing(),
|
| + initial_delay);
|
|
|
| // Delay resets on success.
|
| - suggestions_service_->UpdateBlacklistDelay(true);
|
| - EXPECT_EQ(initial_delay, suggestions_service_->blacklist_delay());
|
| + suggestions_service()->FetchSuggestionsData();
|
| + base::RunLoop().RunUntilIdle();
|
| + RespondToFetchWithProfile(CreateSuggestionsProfile());
|
| + EXPECT_EQ(initial_delay,
|
| + suggestions_service()->blacklist_delay_for_testing());
|
| }
|
|
|
| -TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) {
|
| - SuggestionsProfile suggestions =
|
| - CreateSuggestionsProfileWithExpiryTimestamps();
|
| - suggestions_service_->SetDefaultExpiryTimestamp(&suggestions,
|
| - kTestDefaultExpiry);
|
| - EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts());
|
| - EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts());
|
| +TEST_F(SuggestionsServiceTest, DoesNotOverrideDefaultExpiryTime) {
|
| + EXPECT_CALL(*thumbnail_manager(), Initialize(_));
|
| + EXPECT_CALL(*blacklist_store(), FilterSuggestions(_));
|
| + EXPECT_CALL(*blacklist_store(), GetTimeUntilReadyForUpload(_))
|
| + .WillOnce(Return(false));
|
| +
|
| + suggestions_service()->FetchSuggestionsData();
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + // Creates one suggestion without timestamp and adds a second with timestamp.
|
| + SuggestionsProfile profile = CreateSuggestionsProfile();
|
| + ChromeSuggestion* suggestion = profile.add_suggestions();
|
| + suggestion->set_title(kTestTitle);
|
| + suggestion->set_url(kTestUrl);
|
| + suggestion->set_expiry_ts(kTestSetExpiry);
|
| + RespondToFetchWithProfile(profile);
|
| +
|
| + SuggestionsProfile suggestions;
|
| + suggestions_store()->LoadSuggestions(&suggestions);
|
| + ASSERT_EQ(2, suggestions.suggestions_size());
|
| + // Suggestion[0] had no time stamp and should be ahead of the old suggestion.
|
| + EXPECT_LT(kTestSetExpiry, suggestions.suggestions(0).expiry_ts());
|
| + // Suggestion[1] had a very old time stamp but should not be updated.
|
| + EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(1).expiry_ts());
|
| }
|
|
|
| TEST_F(SuggestionsServiceTest, GetPageThumbnail) {
|
| @@ -730,13 +710,13 @@ TEST_F(SuggestionsServiceTest, GetPageThumbnail) {
|
| const GURL thumbnail_url("https://www.thumbnails.com/thumb.jpg");
|
| base::Callback<void(const GURL&, const gfx::Image&)> dummy_callback;
|
|
|
| - EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _));
|
| - suggestions_service_->GetPageThumbnail(test_url, dummy_callback);
|
| + EXPECT_CALL(*thumbnail_manager(), GetImageForURL(test_url, _));
|
| + suggestions_service()->GetPageThumbnail(test_url, dummy_callback);
|
|
|
| - EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url));
|
| - EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _));
|
| - suggestions_service_->GetPageThumbnailWithURL(test_url, thumbnail_url,
|
| - dummy_callback);
|
| + EXPECT_CALL(*thumbnail_manager(), AddImageURL(test_url, thumbnail_url));
|
| + EXPECT_CALL(*thumbnail_manager(), GetImageForURL(test_url, _));
|
| + suggestions_service()->GetPageThumbnailWithURL(test_url, thumbnail_url,
|
| + dummy_callback);
|
| }
|
|
|
| } // namespace suggestions
|
|
|