| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/suggestions/suggestions_service.h" | 5 #include "components/suggestions/suggestions_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | |
| 10 #include <sstream> | |
| 11 #include <utility> | 9 #include <utility> |
| 12 | 10 |
| 13 #include "base/bind.h" | 11 #include "base/bind.h" |
| 14 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 15 #include "base/macros.h" | 13 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 15 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 18 #include "components/suggestions/blacklist_store.h" | 16 #include "components/suggestions/blacklist_store.h" |
| 19 #include "components/suggestions/image_manager.h" | 17 #include "components/suggestions/image_manager.h" |
| 20 #include "components/suggestions/proto/suggestions.pb.h" | 18 #include "components/suggestions/proto/suggestions.pb.h" |
| 21 #include "components/suggestions/suggestions_store.h" | 19 #include "components/suggestions/suggestions_store.h" |
| 22 #include "components/suggestions/suggestions_utils.h" | 20 #include "components/sync_driver/fake_sync_service.h" |
| 21 #include "components/sync_driver/sync_service.h" |
| 23 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
| 24 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 25 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 26 #include "net/url_request/test_url_fetcher_factory.h" | 25 #include "net/url_request/test_url_fetcher_factory.h" |
| 27 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 30 |
| 32 using std::string; | |
| 33 using testing::DoAll; | 31 using testing::DoAll; |
| 34 using ::testing::AnyNumber; | 32 using ::testing::AnyNumber; |
| 35 using ::testing::Eq; | 33 using ::testing::Eq; |
| 36 using ::testing::Return; | 34 using ::testing::Return; |
| 37 using testing::SetArgPointee; | 35 using testing::SetArgPointee; |
| 38 using ::testing::NiceMock; | 36 using ::testing::NiceMock; |
| 39 using ::testing::StrictMock; | 37 using ::testing::StrictMock; |
| 40 using ::testing::_; | 38 using ::testing::_; |
| 41 | 39 |
| 42 namespace { | 40 namespace { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 suggestion->set_url(kTestUrl); | 106 suggestion->set_url(kTestUrl); |
| 109 suggestion->set_expiry_ts(kTestSetExpiry); | 107 suggestion->set_expiry_ts(kTestSetExpiry); |
| 110 | 108 |
| 111 suggestion = profile.add_suggestions(); | 109 suggestion = profile.add_suggestions(); |
| 112 suggestion->set_title(kTestTitle); | 110 suggestion->set_title(kTestTitle); |
| 113 suggestion->set_url(kTestUrl); | 111 suggestion->set_url(kTestUrl); |
| 114 | 112 |
| 115 return profile; | 113 return profile; |
| 116 } | 114 } |
| 117 | 115 |
| 116 class MockSyncService : public sync_driver::FakeSyncService { |
| 117 public: |
| 118 MockSyncService() {} |
| 119 virtual ~MockSyncService() {} |
| 120 MOCK_CONST_METHOD0(CanSyncStart, bool()); |
| 121 MOCK_CONST_METHOD0(IsSyncActive, bool()); |
| 122 MOCK_CONST_METHOD0(ConfigurationDone, bool()); |
| 123 MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet()); |
| 124 }; |
| 125 |
| 118 class TestSuggestionsStore : public suggestions::SuggestionsStore { | 126 class TestSuggestionsStore : public suggestions::SuggestionsStore { |
| 119 public: | 127 public: |
| 120 TestSuggestionsStore() { | 128 TestSuggestionsStore() { |
| 121 cached_suggestions = CreateSuggestionsProfile(); | 129 cached_suggestions = CreateSuggestionsProfile(); |
| 122 } | 130 } |
| 123 bool LoadSuggestions(SuggestionsProfile* suggestions) override { | 131 bool LoadSuggestions(SuggestionsProfile* suggestions) override { |
| 124 suggestions->CopyFrom(cached_suggestions); | 132 suggestions->CopyFrom(cached_suggestions); |
| 125 return cached_suggestions.suggestions_size(); | 133 return cached_suggestions.suggestions_size(); |
| 126 } | 134 } |
| 127 bool StoreSuggestions(const SuggestionsProfile& suggestions) | 135 bool StoreSuggestions(const SuggestionsProfile& suggestions) override { |
| 128 override { | |
| 129 cached_suggestions.CopyFrom(suggestions); | 136 cached_suggestions.CopyFrom(suggestions); |
| 130 return true; | 137 return true; |
| 131 } | 138 } |
| 132 void ClearSuggestions() override { | 139 void ClearSuggestions() override { |
| 133 cached_suggestions = SuggestionsProfile(); | 140 cached_suggestions = SuggestionsProfile(); |
| 134 } | 141 } |
| 135 | 142 |
| 136 SuggestionsProfile cached_suggestions; | 143 SuggestionsProfile cached_suggestions; |
| 137 }; | 144 }; |
| 138 | 145 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 157 MOCK_METHOD1(GetCandidateForUpload, bool(GURL*)); | 164 MOCK_METHOD1(GetCandidateForUpload, bool(GURL*)); |
| 158 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); | 165 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); |
| 159 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); | 166 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); |
| 160 MOCK_METHOD0(ClearBlacklist, void()); | 167 MOCK_METHOD0(ClearBlacklist, void()); |
| 161 }; | 168 }; |
| 162 | 169 |
| 163 class SuggestionsServiceTest : public testing::Test { | 170 class SuggestionsServiceTest : public testing::Test { |
| 164 public: | 171 public: |
| 165 void CheckCallback(const SuggestionsProfile& suggestions_profile) { | 172 void CheckCallback(const SuggestionsProfile& suggestions_profile) { |
| 166 ++suggestions_data_callback_count_; | 173 ++suggestions_data_callback_count_; |
| 174 if (suggestions_profile.suggestions_size() == 0) |
| 175 ++suggestions_empty_data_count_; |
| 167 } | 176 } |
| 168 | 177 |
| 169 void CheckSuggestionsData() { | 178 void CheckSuggestionsData() { |
| 170 SuggestionsProfile suggestions_profile; | 179 SuggestionsProfile suggestions_profile; |
| 171 test_suggestions_store_->LoadSuggestions(&suggestions_profile); | 180 test_suggestions_store_->LoadSuggestions(&suggestions_profile); |
| 172 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | 181 EXPECT_EQ(1, suggestions_profile.suggestions_size()); |
| 173 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | 182 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); |
| 174 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | 183 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); |
| 175 EXPECT_EQ(kTestFaviconUrl, | 184 EXPECT_EQ(kTestFaviconUrl, |
| 176 suggestions_profile.suggestions(0).favicon_url()); | 185 suggestions_profile.suggestions(0).favicon_url()); |
| 177 EXPECT_EQ(kTestImpressionUrl, | 186 EXPECT_EQ(kTestImpressionUrl, |
| 178 suggestions_profile.suggestions(0).impression_url()); | 187 suggestions_profile.suggestions(0).impression_url()); |
| 179 EXPECT_EQ(kTestClickUrl, suggestions_profile.suggestions(0).click_url()); | 188 EXPECT_EQ(kTestClickUrl, suggestions_profile.suggestions(0).click_url()); |
| 180 } | 189 } |
| 181 | 190 |
| 182 void SetBlacklistFailure() { | |
| 183 blacklisting_failed_ = true; | |
| 184 } | |
| 185 | |
| 186 void SetUndoBlacklistFailure() { | |
| 187 undo_blacklisting_failed_ = true; | |
| 188 } | |
| 189 | |
| 190 void ExpectEmptySuggestionsProfile(const SuggestionsProfile& profile) { | |
| 191 EXPECT_EQ(0, profile.suggestions_size()); | |
| 192 ++suggestions_empty_data_count_; | |
| 193 } | |
| 194 | |
| 195 int suggestions_data_callback_count_; | 191 int suggestions_data_callback_count_; |
| 196 int suggestions_empty_data_count_; | 192 int suggestions_empty_data_count_; |
| 197 bool blacklisting_failed_; | 193 bool blacklisting_failed_; |
| 198 bool undo_blacklisting_failed_; | 194 bool undo_blacklisting_failed_; |
| 199 | 195 |
| 200 protected: | 196 protected: |
| 201 SuggestionsServiceTest() | 197 SuggestionsServiceTest() |
| 202 : suggestions_data_callback_count_(0), | 198 : suggestions_data_callback_count_(0), |
| 203 suggestions_empty_data_count_(0), | 199 suggestions_empty_data_count_(0), |
| 204 blacklisting_failed_(false), | 200 blacklisting_failed_(false), |
| 205 undo_blacklisting_failed_(false), | 201 undo_blacklisting_failed_(false), |
| 206 factory_(NULL, base::Bind(&CreateURLFetcher)), | 202 factory_(nullptr, base::Bind(&CreateURLFetcher)), |
| 207 mock_thumbnail_manager_(NULL), | 203 mock_sync_service_(nullptr), |
| 208 mock_blacklist_store_(NULL), | 204 mock_thumbnail_manager_(nullptr), |
| 209 test_suggestions_store_(NULL) { | 205 mock_blacklist_store_(nullptr), |
| 206 test_suggestions_store_(nullptr) { |
| 210 token_service_.UpdateCredentials(kAccountId, "refresh_token"); | 207 token_service_.UpdateCredentials(kAccountId, "refresh_token"); |
| 211 token_service_.set_auto_post_fetch_response_on_message_loop(true); | 208 token_service_.set_auto_post_fetch_response_on_message_loop(true); |
| 212 } | 209 } |
| 213 | 210 |
| 214 ~SuggestionsServiceTest() override {} | 211 ~SuggestionsServiceTest() override {} |
| 215 | 212 |
| 216 void SetOAuth2FeatureEnabled(bool enabled) { | 213 void SetOAuth2FeatureEnabled(bool enabled) { |
| 217 base::FeatureList::ClearInstanceForTesting(); | 214 base::FeatureList::ClearInstanceForTesting(); |
| 218 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); | 215 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 219 if (enabled) { | 216 if (enabled) { |
| 220 feature_list->InitializeFromCommandLine( | 217 feature_list->InitializeFromCommandLine( |
| 221 "SuggestionsServiceOAuth2", std::string()); | 218 "SuggestionsServiceOAuth2", std::string()); |
| 222 } | 219 } |
| 223 base::FeatureList::SetInstance(std::move(feature_list)); | 220 base::FeatureList::SetInstance(std::move(feature_list)); |
| 224 } | 221 } |
| 225 | 222 |
| 226 void SetUp() override { | 223 void SetUp() override { |
| 227 request_context_ = | 224 request_context_ = |
| 228 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); | 225 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); |
| 229 } | 226 } |
| 230 | 227 |
| 231 void FetchSuggestionsDataHelper(SyncState sync_state) { | 228 SuggestionsService* CreateSuggestionsServiceWithMocks() { |
| 232 scoped_ptr<SuggestionsService> suggestions_service( | 229 mock_sync_service_.reset(new MockSyncService); |
| 233 CreateSuggestionsServiceWithMocks()); | 230 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); |
| 234 EXPECT_TRUE(suggestions_service != NULL); | 231 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); |
| 232 ON_CALL(*mock_sync_service_, ConfigurationDone()) |
| 233 .WillByDefault(Return(true)); |
| 234 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) |
| 235 .WillByDefault( |
| 236 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); |
| 235 | 237 |
| 236 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | |
| 237 | |
| 238 // Set up net::FakeURLFetcherFactory. | |
| 239 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | |
| 240 suggestions_profile.SerializeAsString(), | |
| 241 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 242 | |
| 243 // Expectations. | |
| 244 EXPECT_CALL(*mock_thumbnail_manager_, | |
| 245 Initialize(EqualsProto(suggestions_profile))); | |
| 246 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | |
| 247 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | |
| 248 .WillOnce(Return(false)); | |
| 249 | |
| 250 // Send the request. The data will be returned to the callback. | |
| 251 suggestions_service->FetchSuggestionsData( | |
| 252 sync_state, base::Bind(&SuggestionsServiceTest::CheckCallback, | |
| 253 base::Unretained(this))); | |
| 254 | |
| 255 // Ensure that CheckCallback() ran once. | |
| 256 EXPECT_EQ(1, suggestions_data_callback_count_); | |
| 257 | |
| 258 // Let the network request run. | |
| 259 io_message_loop_.RunUntilIdle(); | |
| 260 | |
| 261 CheckSuggestionsData(); | |
| 262 } | |
| 263 | |
| 264 SuggestionsService* CreateSuggestionsServiceWithMocks() { | |
| 265 // These objects are owned by the returned SuggestionsService, but we keep | 238 // These objects are owned by the returned SuggestionsService, but we keep |
| 266 // the pointer around for testing. | 239 // the pointer around for testing. |
| 267 test_suggestions_store_ = new TestSuggestionsStore(); | 240 test_suggestions_store_ = new TestSuggestionsStore(); |
| 268 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); | 241 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); |
| 269 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); | 242 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); |
| 270 return new SuggestionsService( | 243 return new SuggestionsService( |
| 271 nullptr /* signin_manager */, | 244 nullptr /* signin_manager */, |
| 272 &token_service_, | 245 &token_service_, |
| 246 mock_sync_service_.get(), |
| 273 request_context_.get(), | 247 request_context_.get(), |
| 274 scoped_ptr<SuggestionsStore>(test_suggestions_store_), | 248 scoped_ptr<SuggestionsStore>(test_suggestions_store_), |
| 275 scoped_ptr<ImageManager>(mock_thumbnail_manager_), | 249 scoped_ptr<ImageManager>(mock_thumbnail_manager_), |
| 276 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); | 250 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); |
| 277 } | 251 } |
| 278 | 252 |
| 279 void Blacklist(SuggestionsService* suggestions_service, GURL url) { | 253 void Blacklist(SuggestionsService* suggestions_service, GURL url) { |
| 280 suggestions_service->BlacklistURL( | 254 blacklisting_failed_ = !suggestions_service->BlacklistURL(url); |
| 281 url, base::Bind(&SuggestionsServiceTest::CheckCallback, | |
| 282 base::Unretained(this)), | |
| 283 base::Bind(&SuggestionsServiceTest::SetBlacklistFailure, | |
| 284 base::Unretained(this))); | |
| 285 } | 255 } |
| 286 | 256 |
| 287 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { | 257 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { |
| 288 suggestions_service->UndoBlacklistURL( | 258 undo_blacklisting_failed_ = !suggestions_service->UndoBlacklistURL(url); |
| 289 url, base::Bind(&SuggestionsServiceTest::CheckCallback, | |
| 290 base::Unretained(this)), | |
| 291 base::Bind(&SuggestionsServiceTest::SetUndoBlacklistFailure, | |
| 292 base::Unretained(this))); | |
| 293 } | 259 } |
| 294 | 260 |
| 295 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either | 261 // Helper for Undo failure tests. Depending on |is_uploaded|, tests either |
| 296 // the case where the URL is no longer in the local blacklist or the case | 262 // the case where the URL is no longer in the local blacklist or the case |
| 297 // in which it's not yet candidate for upload. | 263 // in which it's not yet candidate for upload. |
| 298 void UndoBlacklistURLFailsHelper(bool is_uploaded) { | 264 void UndoBlacklistURLFailsHelper(bool is_uploaded) { |
| 299 scoped_ptr<SuggestionsService> suggestions_service( | 265 scoped_ptr<SuggestionsService> suggestions_service( |
| 300 CreateSuggestionsServiceWithMocks()); | 266 CreateSuggestionsServiceWithMocks()); |
| 301 EXPECT_TRUE(suggestions_service != NULL); | 267 EXPECT_TRUE(suggestions_service != nullptr); |
| 302 // Ensure scheduling the request doesn't happen before undo. | 268 // Ensure scheduling the request doesn't happen before undo. |
| 303 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 269 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
| 304 suggestions_service->set_blacklist_delay(delay); | 270 suggestions_service->set_blacklist_delay(delay); |
| 271 |
| 272 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 273 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 274 |
| 305 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 275 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 306 GURL blacklisted_url(kBlacklistedUrl); | 276 GURL blacklisted_url(kBlacklistedUrl); |
| 307 | 277 |
| 308 // Blacklist expectations. | 278 // Blacklist expectations. |
| 309 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 279 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 310 .WillOnce(Return(true)); | 280 .WillOnce(Return(true)); |
| 311 EXPECT_CALL(*mock_thumbnail_manager_, | 281 EXPECT_CALL(*mock_thumbnail_manager_, |
| 312 Initialize(EqualsProto(suggestions_profile))); | 282 Initialize(EqualsProto(suggestions_profile))); |
| 313 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 283 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 314 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 284 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 336 } | 306 } |
| 337 | 307 |
| 338 bool HasPendingSuggestionsRequest(SuggestionsService* suggestions_service) { | 308 bool HasPendingSuggestionsRequest(SuggestionsService* suggestions_service) { |
| 339 return !!suggestions_service->pending_request_.get(); | 309 return !!suggestions_service->pending_request_.get(); |
| 340 } | 310 } |
| 341 | 311 |
| 342 protected: | 312 protected: |
| 343 base::MessageLoopForIO io_message_loop_; | 313 base::MessageLoopForIO io_message_loop_; |
| 344 net::FakeURLFetcherFactory factory_; | 314 net::FakeURLFetcherFactory factory_; |
| 345 FakeProfileOAuth2TokenService token_service_; | 315 FakeProfileOAuth2TokenService token_service_; |
| 316 scoped_ptr<MockSyncService> mock_sync_service_; |
| 346 // Only used if the SuggestionsService is built with mocks. Not owned. | 317 // Only used if the SuggestionsService is built with mocks. Not owned. |
| 347 MockImageManager* mock_thumbnail_manager_; | 318 MockImageManager* mock_thumbnail_manager_; |
| 348 MockBlacklistStore* mock_blacklist_store_; | 319 MockBlacklistStore* mock_blacklist_store_; |
| 349 TestSuggestionsStore* test_suggestions_store_; | 320 TestSuggestionsStore* test_suggestions_store_; |
| 350 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 321 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 351 | 322 |
| 352 private: | 323 private: |
| 353 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 324 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 354 }; | 325 }; |
| 355 | 326 |
| 356 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { | 327 TEST_F(SuggestionsServiceTest, FetchSuggestionsData) { |
| 357 FetchSuggestionsDataHelper(INITIALIZED_ENABLED_HISTORY); | 328 scoped_ptr<SuggestionsService> suggestions_service( |
| 329 CreateSuggestionsServiceWithMocks()); |
| 330 ASSERT_TRUE(suggestions_service != nullptr); |
| 331 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 332 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 333 |
| 334 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 335 |
| 336 // Set up net::FakeURLFetcherFactory. |
| 337 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), |
| 338 suggestions_profile.SerializeAsString(), |
| 339 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 340 |
| 341 // Expectations. |
| 342 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)); |
| 343 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 344 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 345 .WillOnce(Return(false)); |
| 346 |
| 347 // Send the request. The data should be returned to the callback. |
| 348 suggestions_service->FetchSuggestionsData(); |
| 349 |
| 350 // Let the network request run. |
| 351 io_message_loop_.RunUntilIdle(); |
| 352 |
| 353 // Ensure that CheckCallback() ran once. |
| 354 EXPECT_EQ(1, suggestions_data_callback_count_); |
| 355 |
| 356 CheckSuggestionsData(); |
| 358 } | 357 } |
| 359 | 358 |
| 360 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { | 359 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncNotInitializedEnabled) { |
| 361 FetchSuggestionsDataHelper(NOT_INITIALIZED_ENABLED); | 360 scoped_ptr<SuggestionsService> suggestions_service( |
| 361 CreateSuggestionsServiceWithMocks()); |
| 362 ASSERT_TRUE(suggestions_service != nullptr); |
| 363 EXPECT_CALL(*mock_sync_service_, IsSyncActive()) |
| 364 .WillRepeatedly(Return(false)); |
| 365 |
| 366 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 367 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 368 |
| 369 // Try to fetch suggestions. Since sync is not active, no network request |
| 370 // should be sent. |
| 371 suggestions_service->FetchSuggestionsData(); |
| 372 |
| 373 // Let any network request run. |
| 374 io_message_loop_.RunUntilIdle(); |
| 375 |
| 376 // Ensure that CheckCallback() didn't run. |
| 377 EXPECT_EQ(0, suggestions_data_callback_count_); |
| 378 |
| 379 // |test_suggestions_store_| should still contain the default values. |
| 380 SuggestionsProfile suggestions; |
| 381 test_suggestions_store_->LoadSuggestions(&suggestions); |
| 382 EXPECT_EQ(CreateSuggestionsProfile().SerializeAsString(), |
| 383 suggestions.SerializeAsString()); |
| 362 } | 384 } |
| 363 | 385 |
| 364 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { | 386 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataSyncDisabled) { |
| 365 scoped_ptr<SuggestionsService> suggestions_service( | 387 scoped_ptr<SuggestionsService> suggestions_service( |
| 366 CreateSuggestionsServiceWithMocks()); | 388 CreateSuggestionsServiceWithMocks()); |
| 367 EXPECT_TRUE(suggestions_service != NULL); | 389 ASSERT_TRUE(suggestions_service != nullptr); |
| 390 EXPECT_CALL(*mock_sync_service_, CanSyncStart()) |
| 391 .WillRepeatedly(Return(false)); |
| 368 | 392 |
| 369 // Send the request. Cache is cleared and empty data will be returned to the | 393 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 370 // callback. | 394 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 371 suggestions_service->FetchSuggestionsData( | |
| 372 SYNC_OR_HISTORY_SYNC_DISABLED, | |
| 373 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | |
| 374 base::Unretained(this))); | |
| 375 | 395 |
| 376 // Ensure that ExpectEmptySuggestionsProfile ran once. | 396 // Tell SuggestionsService that the sync state changed. The cache should be |
| 397 // cleared and empty data returned to the callback. |
| 398 suggestions_service->OnStateChanged(); |
| 399 |
| 400 // Ensure that CheckCallback ran once with empty data. |
| 401 EXPECT_EQ(1, suggestions_data_callback_count_); |
| 377 EXPECT_EQ(1, suggestions_empty_data_count_); | 402 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 403 |
| 404 // Try to fetch suggestions. Since sync is not active, no network request |
| 405 // should be sent. |
| 406 suggestions_service->FetchSuggestionsData(); |
| 407 |
| 408 // Let any network request run. |
| 409 io_message_loop_.RunUntilIdle(); |
| 410 |
| 411 // Ensure that CheckCallback didn't run again. |
| 412 EXPECT_EQ(1, suggestions_data_callback_count_); |
| 378 } | 413 } |
| 379 | 414 |
| 380 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { | 415 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { |
| 381 SetOAuth2FeatureEnabled(true); | 416 SetOAuth2FeatureEnabled(true); |
| 382 | 417 |
| 383 token_service_.RevokeCredentials(kAccountId); | 418 token_service_.RevokeCredentials(kAccountId); |
| 384 | 419 |
| 385 scoped_ptr<SuggestionsService> suggestions_service( | 420 scoped_ptr<SuggestionsService> suggestions_service( |
| 386 CreateSuggestionsServiceWithMocks()); | 421 CreateSuggestionsServiceWithMocks()); |
| 387 ASSERT_TRUE(suggestions_service != NULL); | 422 ASSERT_TRUE(suggestions_service != nullptr); |
| 388 | 423 |
| 389 // We should get served from cache. | 424 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 390 // TODO(treib,mathp): Is this the correct behavior when the credentials have | 425 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 391 // expired? Currently, the SuggestionsService immediately serves from cache, | 426 |
| 392 // before even attempting any online auth. | |
| 393 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)); | |
| 394 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | |
| 395 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 427 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 396 .WillOnce(Return(false)); | 428 .WillOnce(Return(false)); |
| 397 | 429 |
| 398 suggestions_service->FetchSuggestionsData( | 430 suggestions_service->FetchSuggestionsData(); |
| 399 INITIALIZED_ENABLED_HISTORY, | |
| 400 base::Bind(&SuggestionsServiceTest::CheckCallback, | |
| 401 base::Unretained(this))); | |
| 402 | 431 |
| 403 EXPECT_EQ(1, suggestions_data_callback_count_); | 432 // No network request should be sent. |
| 404 | |
| 405 // But no network request should be sent. | |
| 406 io_message_loop_.RunUntilIdle(); | 433 io_message_loop_.RunUntilIdle(); |
| 407 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); | 434 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); |
| 435 EXPECT_EQ(0, suggestions_data_callback_count_); |
| 408 } | 436 } |
| 409 | 437 |
| 410 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { | 438 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { |
| 411 scoped_ptr<SuggestionsService> suggestions_service( | 439 scoped_ptr<SuggestionsService> suggestions_service( |
| 412 CreateSuggestionsServiceWithMocks()); | 440 CreateSuggestionsServiceWithMocks()); |
| 413 EXPECT_TRUE(suggestions_service != NULL); | 441 ASSERT_TRUE(suggestions_service != nullptr); |
| 414 | 442 |
| 415 // Fake a request error. | 443 // Fake a request error. |
| 416 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | 444 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), |
| 417 "irrelevant", net::HTTP_OK, | 445 "irrelevant", net::HTTP_OK, |
| 418 net::URLRequestStatus::FAILED); | 446 net::URLRequestStatus::FAILED); |
| 419 | 447 |
| 420 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 448 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 421 .WillOnce(Return(false)); | 449 .WillOnce(Return(false)); |
| 422 | 450 |
| 423 // Send the request. Empty data will be returned to the callback. | 451 // Send the request. Empty data will be returned to the callback. |
| 424 suggestions_service->IssueRequestIfNoneOngoing( | 452 suggestions_service->IssueRequestIfNoneOngoing( |
| 425 SuggestionsService::BuildSuggestionsURL()); | 453 SuggestionsService::BuildSuggestionsURL()); |
| 426 | 454 |
| 427 // (Testing only) wait until suggestion fetch is complete. | 455 // (Testing only) wait until suggestion fetch is complete. |
| 428 io_message_loop_.RunUntilIdle(); | 456 io_message_loop_.RunUntilIdle(); |
| 429 } | 457 } |
| 430 | 458 |
| 431 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { | 459 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingResponseNotOK) { |
| 432 scoped_ptr<SuggestionsService> suggestions_service( | 460 scoped_ptr<SuggestionsService> suggestions_service( |
| 433 CreateSuggestionsServiceWithMocks()); | 461 CreateSuggestionsServiceWithMocks()); |
| 434 EXPECT_TRUE(suggestions_service != NULL); | 462 ASSERT_TRUE(suggestions_service != nullptr); |
| 435 | 463 |
| 436 // Fake a non-200 response code. | 464 // Fake a non-200 response code. |
| 437 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), | 465 factory_.SetFakeResponse(SuggestionsService::BuildSuggestionsURL(), |
| 438 "irrelevant", net::HTTP_BAD_REQUEST, | 466 "irrelevant", net::HTTP_BAD_REQUEST, |
| 439 net::URLRequestStatus::SUCCESS); | 467 net::URLRequestStatus::SUCCESS); |
| 440 | 468 |
| 441 // Expect that an upload to the blacklist is scheduled. | 469 // Expect that an upload to the blacklist is scheduled. |
| 442 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 470 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 443 .WillOnce(Return(false)); | 471 .WillOnce(Return(false)); |
| 444 | 472 |
| 445 // Send the request. Empty data will be returned to the callback. | 473 // Send the request. Empty data will be returned to the callback. |
| 446 suggestions_service->IssueRequestIfNoneOngoing( | 474 suggestions_service->IssueRequestIfNoneOngoing( |
| 447 SuggestionsService::BuildSuggestionsURL()); | 475 SuggestionsService::BuildSuggestionsURL()); |
| 448 | 476 |
| 449 // (Testing only) wait until suggestion fetch is complete. | 477 // (Testing only) wait until suggestion fetch is complete. |
| 450 io_message_loop_.RunUntilIdle(); | 478 io_message_loop_.RunUntilIdle(); |
| 451 | 479 |
| 452 // Expect no suggestions in the cache. | 480 // Expect no suggestions in the cache. |
| 453 SuggestionsProfile empty_suggestions; | 481 SuggestionsProfile empty_suggestions; |
| 454 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions)); | 482 EXPECT_FALSE(test_suggestions_store_->LoadSuggestions(&empty_suggestions)); |
| 455 } | 483 } |
| 456 | 484 |
| 457 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 485 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 458 scoped_ptr<SuggestionsService> suggestions_service( | 486 scoped_ptr<SuggestionsService> suggestions_service( |
| 459 CreateSuggestionsServiceWithMocks()); | 487 CreateSuggestionsServiceWithMocks()); |
| 460 EXPECT_TRUE(suggestions_service != NULL); | 488 EXPECT_TRUE(suggestions_service != nullptr); |
| 461 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 489 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
| 462 suggestions_service->set_blacklist_delay(no_delay); | 490 suggestions_service->set_blacklist_delay(no_delay); |
| 463 | 491 |
| 492 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 493 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 494 |
| 464 GURL blacklisted_url(kBlacklistedUrl); | 495 GURL blacklisted_url(kBlacklistedUrl); |
| 465 GURL request_url( | 496 GURL request_url( |
| 466 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); | 497 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); |
| 467 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 498 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 468 factory_.SetFakeResponse(request_url, | 499 factory_.SetFakeResponse(request_url, |
| 469 suggestions_profile.SerializeAsString(), | 500 suggestions_profile.SerializeAsString(), |
| 470 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 501 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 471 EXPECT_CALL(*mock_thumbnail_manager_, | 502 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2); |
| 472 Initialize(EqualsProto(suggestions_profile))); | |
| 473 | 503 |
| 474 // Expected calls to the blacklist store. | 504 // Expected calls to the blacklist store. |
| 475 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 505 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 476 .WillOnce(Return(true)); | 506 .WillOnce(Return(true)); |
| 477 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 507 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); |
| 478 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 508 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 479 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) | 509 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) |
| 480 .WillOnce(Return(false)); | 510 .WillOnce(Return(false)); |
| 481 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) | 511 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) |
| 482 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true))); | 512 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true))); |
| 483 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url))) | 513 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url))) |
| 484 .WillOnce(Return(true)); | 514 .WillOnce(Return(true)); |
| 485 | 515 |
| 486 Blacklist(suggestions_service.get(), blacklisted_url); | 516 Blacklist(suggestions_service.get(), blacklisted_url); |
| 517 EXPECT_EQ(1, suggestions_data_callback_count_); |
| 487 | 518 |
| 488 // Wait on the upload task. This only works when the scheduling task is not | 519 // Wait on the upload task. This only works when the scheduling task is not |
| 489 // for future execution (note how both the SuggestionsService's scheduling | 520 // for future execution (note how both the SuggestionsService's scheduling |
| 490 // delay and the BlacklistStore's candidacy delay are zero). Then wait on | 521 // delay and the BlacklistStore's candidacy delay are zero). Then wait on |
| 491 // the blacklist request, then again on the next blacklist scheduling task. | 522 // the blacklist request, then again on the next blacklist scheduling task. |
| 492 base::MessageLoop::current()->RunUntilIdle(); | 523 base::MessageLoop::current()->RunUntilIdle(); |
| 493 io_message_loop_.RunUntilIdle(); | 524 io_message_loop_.RunUntilIdle(); |
| 494 base::MessageLoop::current()->RunUntilIdle(); | 525 base::MessageLoop::current()->RunUntilIdle(); |
| 495 | 526 |
| 496 EXPECT_EQ(1, suggestions_data_callback_count_); | 527 EXPECT_EQ(2, suggestions_data_callback_count_); |
| 497 EXPECT_FALSE(blacklisting_failed_); | 528 EXPECT_FALSE(blacklisting_failed_); |
| 498 CheckSuggestionsData(); | 529 CheckSuggestionsData(); |
| 499 } | 530 } |
| 500 | 531 |
| 501 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { | 532 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { |
| 502 scoped_ptr<SuggestionsService> suggestions_service( | 533 scoped_ptr<SuggestionsService> suggestions_service( |
| 503 CreateSuggestionsServiceWithMocks()); | 534 CreateSuggestionsServiceWithMocks()); |
| 504 EXPECT_TRUE(suggestions_service != NULL); | 535 ASSERT_TRUE(suggestions_service != nullptr); |
| 536 |
| 537 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 538 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 539 |
| 505 GURL blacklisted_url(kBlacklistedUrl); | 540 GURL blacklisted_url(kBlacklistedUrl); |
| 506 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 541 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 507 .WillOnce(Return(false)); | 542 .WillOnce(Return(false)); |
| 508 | 543 |
| 509 Blacklist(suggestions_service.get(), blacklisted_url); | 544 Blacklist(suggestions_service.get(), blacklisted_url); |
| 510 | 545 |
| 511 EXPECT_TRUE(blacklisting_failed_); | 546 EXPECT_TRUE(blacklisting_failed_); |
| 512 EXPECT_EQ(0, suggestions_data_callback_count_); | 547 EXPECT_EQ(0, suggestions_data_callback_count_); |
| 513 } | 548 } |
| 514 | 549 |
| 515 // Initial blacklist request fails, triggering a second which succeeds. | 550 // Initial blacklist request fails, triggering a second which succeeds. |
| 516 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { | 551 TEST_F(SuggestionsServiceTest, BlacklistURLRequestFails) { |
| 517 scoped_ptr<SuggestionsService> suggestions_service( | 552 scoped_ptr<SuggestionsService> suggestions_service( |
| 518 CreateSuggestionsServiceWithMocks()); | 553 CreateSuggestionsServiceWithMocks()); |
| 519 EXPECT_TRUE(suggestions_service != NULL); | 554 ASSERT_TRUE(suggestions_service != nullptr); |
| 520 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); | 555 base::TimeDelta no_delay = base::TimeDelta::FromSeconds(0); |
| 521 suggestions_service->set_blacklist_delay(no_delay); | 556 suggestions_service->set_blacklist_delay(no_delay); |
| 522 | 557 |
| 558 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 559 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 560 |
| 523 GURL blacklisted_url(kBlacklistedUrl); | 561 GURL blacklisted_url(kBlacklistedUrl); |
| 524 GURL request_url( | 562 GURL request_url( |
| 525 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); | 563 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url)); |
| 526 GURL blacklisted_url_alt(kBlacklistedUrlAlt); | 564 GURL blacklisted_url_alt(kBlacklistedUrlAlt); |
| 527 GURL request_url_alt( | 565 GURL request_url_alt( |
| 528 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url_alt)); | 566 SuggestionsService::BuildSuggestionsBlacklistURL(blacklisted_url_alt)); |
| 529 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 567 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 530 | 568 |
| 531 // Note: we want to set the response for the blacklist URL to first | 569 // Note: we want to set the response for the blacklist URL to first |
| 532 // succeed, then fail. This doesn't seem possible. For simplicity of testing, | 570 // succeed, then fail. This doesn't seem possible. For simplicity of testing, |
| 533 // we'll pretend the URL changed in the BlacklistStore between the first and | 571 // we'll pretend the URL changed in the BlacklistStore between the first and |
| 534 // the second request, and adjust expectations accordingly. | 572 // the second request, and adjust expectations accordingly. |
| 535 factory_.SetFakeResponse(request_url, "irrelevant", net::HTTP_OK, | 573 factory_.SetFakeResponse(request_url, "irrelevant", net::HTTP_OK, |
| 536 net::URLRequestStatus::FAILED); | 574 net::URLRequestStatus::FAILED); |
| 537 factory_.SetFakeResponse(request_url_alt, | 575 factory_.SetFakeResponse(request_url_alt, |
| 538 suggestions_profile.SerializeAsString(), | 576 suggestions_profile.SerializeAsString(), |
| 539 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 577 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 540 | 578 |
| 541 // Expectations. | 579 // Expectations. |
| 542 EXPECT_CALL(*mock_thumbnail_manager_, | 580 EXPECT_CALL(*mock_thumbnail_manager_, Initialize(_)).Times(2); |
| 543 Initialize(EqualsProto(suggestions_profile))); | |
| 544 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 581 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 545 .WillOnce(Return(true)); | 582 .WillOnce(Return(true)); |
| 546 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | 583 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); |
| 547 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 584 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 548 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) | 585 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) |
| 549 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) | 586 .WillOnce(DoAll(SetArgPointee<0>(no_delay), Return(true))) |
| 550 .WillOnce(Return(false)); | 587 .WillOnce(Return(false)); |
| 551 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) | 588 EXPECT_CALL(*mock_blacklist_store_, GetCandidateForUpload(_)) |
| 552 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true))) | 589 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url), Return(true))) |
| 553 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url_alt), Return(true))); | 590 .WillOnce(DoAll(SetArgPointee<0>(blacklisted_url_alt), Return(true))); |
| 554 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url_alt))) | 591 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklisted_url_alt))) |
| 555 .WillOnce(Return(true)); | 592 .WillOnce(Return(true)); |
| 556 | 593 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 567 io_message_loop_.RunUntilIdle(); | 604 io_message_loop_.RunUntilIdle(); |
| 568 base::MessageLoop::current()->RunUntilIdle(); | 605 base::MessageLoop::current()->RunUntilIdle(); |
| 569 io_message_loop_.RunUntilIdle(); | 606 io_message_loop_.RunUntilIdle(); |
| 570 base::MessageLoop::current()->RunUntilIdle(); | 607 base::MessageLoop::current()->RunUntilIdle(); |
| 571 CheckSuggestionsData(); | 608 CheckSuggestionsData(); |
| 572 } | 609 } |
| 573 | 610 |
| 574 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { | 611 TEST_F(SuggestionsServiceTest, UndoBlacklistURL) { |
| 575 scoped_ptr<SuggestionsService> suggestions_service( | 612 scoped_ptr<SuggestionsService> suggestions_service( |
| 576 CreateSuggestionsServiceWithMocks()); | 613 CreateSuggestionsServiceWithMocks()); |
| 577 EXPECT_TRUE(suggestions_service != NULL); | 614 ASSERT_TRUE(suggestions_service != nullptr); |
| 578 // Ensure scheduling the request doesn't happen before undo. | 615 // Ensure scheduling the request doesn't happen before undo. |
| 579 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 616 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
| 580 suggestions_service->set_blacklist_delay(delay); | 617 suggestions_service->set_blacklist_delay(delay); |
| 618 |
| 619 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 620 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 621 |
| 581 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 622 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 582 GURL blacklisted_url(kBlacklistedUrl); | 623 GURL blacklisted_url(kBlacklistedUrl); |
| 583 | 624 |
| 584 // Blacklist expectations. | 625 // Blacklist expectations. |
| 585 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 626 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 586 .WillOnce(Return(true)); | 627 .WillOnce(Return(true)); |
| 587 EXPECT_CALL(*mock_thumbnail_manager_, | 628 EXPECT_CALL(*mock_thumbnail_manager_, |
| 588 Initialize(EqualsProto(suggestions_profile))) | 629 Initialize(EqualsProto(suggestions_profile))) |
| 589 .Times(AnyNumber()); | 630 .Times(AnyNumber()); |
| 590 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)) | 631 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 602 UndoBlacklist(suggestions_service.get(), blacklisted_url); | 643 UndoBlacklist(suggestions_service.get(), blacklisted_url); |
| 603 | 644 |
| 604 EXPECT_EQ(2, suggestions_data_callback_count_); | 645 EXPECT_EQ(2, suggestions_data_callback_count_); |
| 605 EXPECT_FALSE(blacklisting_failed_); | 646 EXPECT_FALSE(blacklisting_failed_); |
| 606 EXPECT_FALSE(undo_blacklisting_failed_); | 647 EXPECT_FALSE(undo_blacklisting_failed_); |
| 607 } | 648 } |
| 608 | 649 |
| 609 TEST_F(SuggestionsServiceTest, ClearBlacklist) { | 650 TEST_F(SuggestionsServiceTest, ClearBlacklist) { |
| 610 scoped_ptr<SuggestionsService> suggestions_service( | 651 scoped_ptr<SuggestionsService> suggestions_service( |
| 611 CreateSuggestionsServiceWithMocks()); | 652 CreateSuggestionsServiceWithMocks()); |
| 612 EXPECT_TRUE(suggestions_service != NULL); | 653 ASSERT_TRUE(suggestions_service != nullptr); |
| 613 // Ensure scheduling the request doesn't happen before undo. | 654 // Ensure scheduling the request doesn't happen before undo. |
| 614 base::TimeDelta delay = base::TimeDelta::FromHours(1); | 655 base::TimeDelta delay = base::TimeDelta::FromHours(1); |
| 615 suggestions_service->set_blacklist_delay(delay); | 656 suggestions_service->set_blacklist_delay(delay); |
| 657 |
| 658 auto subscription = suggestions_service->AddCallback(base::Bind( |
| 659 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
| 660 |
| 616 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); | 661 SuggestionsProfile suggestions_profile = CreateSuggestionsProfile(); |
| 617 GURL blacklisted_url(kBlacklistedUrl); | 662 GURL blacklisted_url(kBlacklistedUrl); |
| 618 | 663 |
| 619 factory_.SetFakeResponse( | 664 factory_.SetFakeResponse( |
| 620 SuggestionsService::BuildSuggestionsBlacklistClearURL(), | 665 SuggestionsService::BuildSuggestionsBlacklistClearURL(), |
| 621 suggestions_profile.SerializeAsString(), net::HTTP_OK, | 666 suggestions_profile.SerializeAsString(), net::HTTP_OK, |
| 622 net::URLRequestStatus::SUCCESS); | 667 net::URLRequestStatus::SUCCESS); |
| 623 | 668 |
| 624 // Blacklist expectations. | 669 // Blacklist expectations. |
| 625 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) | 670 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklisted_url))) |
| 626 .WillOnce(Return(true)); | 671 .WillOnce(Return(true)); |
| 627 EXPECT_CALL(*mock_thumbnail_manager_, | 672 EXPECT_CALL(*mock_thumbnail_manager_, |
| 628 Initialize(EqualsProto(suggestions_profile))) | 673 Initialize(EqualsProto(suggestions_profile))) |
| 629 .Times(AnyNumber()); | 674 .Times(AnyNumber()); |
| 630 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber()); | 675 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(AnyNumber()); |
| 631 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 676 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
| 632 .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true))); | 677 .WillOnce(DoAll(SetArgPointee<0>(delay), Return(true))); |
| 633 EXPECT_CALL(*mock_blacklist_store_, ClearBlacklist()); | 678 EXPECT_CALL(*mock_blacklist_store_, ClearBlacklist()); |
| 634 | 679 |
| 635 Blacklist(suggestions_service.get(), blacklisted_url); | 680 Blacklist(suggestions_service.get(), blacklisted_url); |
| 636 suggestions_service->ClearBlacklist(base::Bind( | 681 suggestions_service->ClearBlacklist(); |
| 637 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | |
| 638 | 682 |
| 639 EXPECT_EQ(2, suggestions_data_callback_count_); | 683 EXPECT_EQ(2, suggestions_data_callback_count_); |
| 640 EXPECT_FALSE(blacklisting_failed_); | 684 EXPECT_FALSE(blacklisting_failed_); |
| 641 } | 685 } |
| 642 | 686 |
| 643 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) { | 687 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfNotInBlacklist) { |
| 644 UndoBlacklistURLFailsHelper(true); | 688 UndoBlacklistURLFailsHelper(true); |
| 645 } | 689 } |
| 646 | 690 |
| 647 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) { | 691 TEST_F(SuggestionsServiceTest, UndoBlacklistURLFailsIfAlreadyCandidate) { |
| 648 UndoBlacklistURLFailsHelper(false); | 692 UndoBlacklistURLFailsHelper(false); |
| 649 } | 693 } |
| 650 | 694 |
| 651 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { | 695 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { |
| 652 scoped_ptr<GURL> request_url; | 696 scoped_ptr<GURL> request_url; |
| 653 scoped_ptr<net::FakeURLFetcher> fetcher; | 697 scoped_ptr<net::FakeURLFetcher> fetcher; |
| 654 GURL retrieved_url; | 698 GURL retrieved_url; |
| 655 | 699 |
| 656 // Not a blacklist request. | 700 // Not a blacklist request. |
| 657 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); | 701 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); |
| 658 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, | 702 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, |
| 659 net::URLRequestStatus::SUCCESS); | 703 net::URLRequestStatus::SUCCESS); |
| 660 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | 704 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
| 661 | 705 |
| 662 // An actual blacklist request. | 706 // An actual blacklist request. |
| 663 string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; | 707 string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; |
| 664 string encoded_blacklisted_url = | 708 string encoded_blacklisted_url = |
| 665 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; | 709 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; |
| 666 string blacklist_request_prefix( | 710 string blacklist_request_prefix( |
| 667 SuggestionsService::BuildSuggestionsBlacklistURLPrefix()); | 711 SuggestionsService::BuildSuggestionsBlacklistURLPrefix()); |
| 668 request_url.reset( | 712 request_url.reset( |
| 669 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); | 713 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); |
| 670 fetcher.reset(); | 714 fetcher.reset(); |
| 671 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, | 715 fetcher = CreateURLFetcher(*request_url, nullptr, "", net::HTTP_OK, |
| 672 net::URLRequestStatus::SUCCESS); | 716 net::URLRequestStatus::SUCCESS); |
| 673 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | 717 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
| 674 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); | 718 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); |
| 675 } | 719 } |
| 676 | 720 |
| 677 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { | 721 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { |
| 678 scoped_ptr<SuggestionsService> suggestions_service( | 722 scoped_ptr<SuggestionsService> suggestions_service( |
| 679 CreateSuggestionsServiceWithMocks()); | 723 CreateSuggestionsServiceWithMocks()); |
| 680 base::TimeDelta initial_delay = suggestions_service->blacklist_delay(); | 724 base::TimeDelta initial_delay = suggestions_service->blacklist_delay(); |
| 681 | 725 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 699 CreateSuggestionsProfileWithExpiryTimestamps(); | 743 CreateSuggestionsProfileWithExpiryTimestamps(); |
| 700 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, | 744 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, |
| 701 kTestDefaultExpiry); | 745 kTestDefaultExpiry); |
| 702 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); | 746 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); |
| 703 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); | 747 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); |
| 704 } | 748 } |
| 705 | 749 |
| 706 TEST_F(SuggestionsServiceTest, GetPageThumbnail) { | 750 TEST_F(SuggestionsServiceTest, GetPageThumbnail) { |
| 707 scoped_ptr<SuggestionsService> suggestions_service( | 751 scoped_ptr<SuggestionsService> suggestions_service( |
| 708 CreateSuggestionsServiceWithMocks()); | 752 CreateSuggestionsServiceWithMocks()); |
| 709 ASSERT_TRUE(suggestions_service != NULL); | 753 ASSERT_TRUE(suggestions_service != nullptr); |
| 710 | 754 |
| 711 GURL test_url(kTestUrl); | 755 GURL test_url(kTestUrl); |
| 712 GURL thumbnail_url("https://www.thumbnails.com/thumb.jpg"); | 756 GURL thumbnail_url("https://www.thumbnails.com/thumb.jpg"); |
| 713 base::Callback<void(const GURL&, const SkBitmap*)> dummy_callback; | 757 base::Callback<void(const GURL&, const SkBitmap*)> dummy_callback; |
| 714 | 758 |
| 715 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 759 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
| 716 suggestions_service->GetPageThumbnail(test_url, dummy_callback); | 760 suggestions_service->GetPageThumbnail(test_url, dummy_callback); |
| 717 | 761 |
| 718 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); | 762 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); |
| 719 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 763 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
| 720 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, | 764 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, |
| 721 dummy_callback); | 765 dummy_callback); |
| 722 | 766 |
| 723 } | 767 } |
| 724 | 768 |
| 725 } // namespace suggestions | 769 } // namespace suggestions |
| OLD | NEW |