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_impl.h" | 5 #include "components/suggestions/suggestions_service_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "components/signin/core/browser/account_tracker_service.h" |
17 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 18 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
| 19 #include "components/signin/core/browser/fake_signin_manager.h" |
| 20 #include "components/signin/core/browser/test_signin_client.h" |
18 #include "components/suggestions/blacklist_store.h" | 21 #include "components/suggestions/blacklist_store.h" |
19 #include "components/suggestions/image_manager.h" | 22 #include "components/suggestions/image_manager.h" |
20 #include "components/suggestions/proto/suggestions.pb.h" | 23 #include "components/suggestions/proto/suggestions.pb.h" |
21 #include "components/suggestions/suggestions_store.h" | 24 #include "components/suggestions/suggestions_store.h" |
22 #include "components/sync/driver/fake_sync_service.h" | 25 #include "components/sync/driver/fake_sync_service.h" |
23 #include "components/sync/driver/sync_service.h" | 26 #include "components/sync/driver/sync_service.h" |
| 27 #include "components/sync_preferences/testing_pref_service_syncable.h" |
24 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
25 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
26 #include "net/http/http_status_code.h" | 30 #include "net/http/http_status_code.h" |
27 #include "net/url_request/test_url_fetcher_factory.h" | 31 #include "net/url_request/test_url_fetcher_factory.h" |
28 #include "net/url_request/url_request_status.h" | 32 #include "net/url_request/url_request_status.h" |
29 #include "net/url_request/url_request_test_util.h" | 33 #include "net/url_request/url_request_test_util.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "ui/gfx/image/image.h" | 36 #include "ui/gfx/image/image.h" |
33 | 37 |
| 38 using sync_preferences::TestingPrefServiceSyncable; |
| 39 using testing::_; |
| 40 using testing::AnyNumber; |
34 using testing::DoAll; | 41 using testing::DoAll; |
35 using ::testing::AnyNumber; | 42 using testing::Eq; |
36 using ::testing::Eq; | 43 using testing::NiceMock; |
37 using ::testing::Return; | 44 using testing::Return; |
38 using testing::SetArgPointee; | 45 using testing::SetArgPointee; |
39 using ::testing::NiceMock; | 46 using testing::StrictMock; |
40 using ::testing::StrictMock; | |
41 using ::testing::_; | |
42 | 47 |
43 namespace { | 48 namespace { |
44 | 49 |
45 // SuggestionsService::AccessTokenFetcher provides an empty account ID if its | 50 const char kAccountId[] = "account"; |
46 // SigninManager is null. | |
47 const char kAccountId[] = ""; | |
48 const char kTestTitle[] = "a title"; | 51 const char kTestTitle[] = "a title"; |
49 const char kTestUrl[] = "http://go.com"; | 52 const char kTestUrl[] = "http://go.com"; |
50 const char kTestFaviconUrl[] = | 53 const char kTestFaviconUrl[] = |
51 "https://s2.googleusercontent.com/s2/favicons?domain_url=" | 54 "https://s2.googleusercontent.com/s2/favicons?domain_url=" |
52 "http://go.com&alt=s&sz=32"; | 55 "http://go.com&alt=s&sz=32"; |
53 const char kBlacklistedUrl[] = "http://blacklist.com"; | 56 const char kBlacklistedUrl[] = "http://blacklist.com"; |
54 const char kBlacklistedUrlAlt[] = "http://blacklist-atl.com"; | 57 const char kBlacklistedUrlAlt[] = "http://blacklist-atl.com"; |
55 const int64_t kTestDefaultExpiry = 1402200000000000; | 58 const int64_t kTestDefaultExpiry = 1402200000000000; |
56 const int64_t kTestSetExpiry = 1404792000000000; | 59 const int64_t kTestSetExpiry = 1404792000000000; |
57 | 60 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 int suggestions_empty_data_count_; | 191 int suggestions_empty_data_count_; |
189 bool blacklisting_failed_; | 192 bool blacklisting_failed_; |
190 bool undo_blacklisting_failed_; | 193 bool undo_blacklisting_failed_; |
191 | 194 |
192 protected: | 195 protected: |
193 SuggestionsServiceTest() | 196 SuggestionsServiceTest() |
194 : suggestions_data_callback_count_(0), | 197 : suggestions_data_callback_count_(0), |
195 suggestions_empty_data_count_(0), | 198 suggestions_empty_data_count_(0), |
196 blacklisting_failed_(false), | 199 blacklisting_failed_(false), |
197 undo_blacklisting_failed_(false), | 200 undo_blacklisting_failed_(false), |
| 201 signin_client_(&pref_service_), |
| 202 signin_manager_(&signin_client_, &account_tracker_), |
198 factory_(nullptr, base::Bind(&CreateURLFetcher)), | 203 factory_(nullptr, base::Bind(&CreateURLFetcher)), |
199 mock_sync_service_(nullptr), | 204 mock_sync_service_(nullptr), |
200 mock_thumbnail_manager_(nullptr), | 205 mock_thumbnail_manager_(nullptr), |
201 mock_blacklist_store_(nullptr), | 206 mock_blacklist_store_(nullptr), |
202 test_suggestions_store_(nullptr) { | 207 test_suggestions_store_(nullptr) { |
| 208 SigninManagerBase::RegisterProfilePrefs(pref_service_.registry()); |
| 209 SigninManagerBase::RegisterPrefs(pref_service_.registry()); |
| 210 |
| 211 signin_manager_.SignIn(kAccountId); |
203 token_service_.UpdateCredentials(kAccountId, "refresh_token"); | 212 token_service_.UpdateCredentials(kAccountId, "refresh_token"); |
204 token_service_.set_auto_post_fetch_response_on_message_loop(true); | 213 token_service_.set_auto_post_fetch_response_on_message_loop(true); |
205 } | 214 } |
206 | 215 |
207 ~SuggestionsServiceTest() override {} | 216 ~SuggestionsServiceTest() override {} |
208 | 217 |
209 void SetUp() override { | 218 void SetUp() override { |
210 request_context_ = | 219 request_context_ = |
211 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); | 220 new net::TestURLRequestContextGetter(io_message_loop_.task_runner()); |
212 } | 221 } |
213 | 222 |
214 std::unique_ptr<SuggestionsServiceImpl> CreateSuggestionsServiceWithMocks() { | 223 std::unique_ptr<SuggestionsServiceImpl> CreateSuggestionsServiceWithMocks() { |
215 mock_sync_service_.reset(new MockSyncService); | 224 mock_sync_service_.reset(new MockSyncService); |
216 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); | 225 EXPECT_CALL(*mock_sync_service_, CanSyncStart()) |
217 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); | 226 .Times(AnyNumber()) |
218 ON_CALL(*mock_sync_service_, ConfigurationDone()) | 227 .WillRepeatedly(Return(true)); |
219 .WillByDefault(Return(true)); | 228 EXPECT_CALL(*mock_sync_service_, IsSyncActive()) |
220 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) | 229 .Times(AnyNumber()) |
221 .WillByDefault( | 230 .WillRepeatedly(Return(true)); |
| 231 EXPECT_CALL(*mock_sync_service_, ConfigurationDone()) |
| 232 .Times(AnyNumber()) |
| 233 .WillRepeatedly(Return(true)); |
| 234 EXPECT_CALL(*mock_sync_service_, GetActiveDataTypes()) |
| 235 .Times(AnyNumber()) |
| 236 .WillRepeatedly( |
222 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); | 237 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); |
223 | 238 |
224 // These objects are owned by the returned SuggestionsService, but we keep | 239 // These objects are owned by the returned SuggestionsService, but we keep |
225 // the pointer around for testing. | 240 // the pointer around for testing. |
226 test_suggestions_store_ = new TestSuggestionsStore(); | 241 test_suggestions_store_ = new TestSuggestionsStore(); |
227 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); | 242 mock_thumbnail_manager_ = new StrictMock<MockImageManager>(); |
228 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); | 243 mock_blacklist_store_ = new StrictMock<MockBlacklistStore>(); |
229 return base::MakeUnique<SuggestionsServiceImpl>( | 244 return base::MakeUnique<SuggestionsServiceImpl>( |
230 nullptr /* signin_manager */, &token_service_, mock_sync_service_.get(), | 245 &signin_manager_, &token_service_, mock_sync_service_.get(), |
231 request_context_.get(), base::WrapUnique(test_suggestions_store_), | 246 request_context_.get(), base::WrapUnique(test_suggestions_store_), |
232 base::WrapUnique(mock_thumbnail_manager_), | 247 base::WrapUnique(mock_thumbnail_manager_), |
233 base::WrapUnique(mock_blacklist_store_)); | 248 base::WrapUnique(mock_blacklist_store_)); |
234 } | 249 } |
235 | 250 |
236 void Blacklist(SuggestionsService* suggestions_service, GURL url) { | 251 void Blacklist(SuggestionsService* suggestions_service, GURL url) { |
237 blacklisting_failed_ = !suggestions_service->BlacklistURL(url); | 252 blacklisting_failed_ = !suggestions_service->BlacklistURL(url); |
238 } | 253 } |
239 | 254 |
240 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { | 255 void UndoBlacklist(SuggestionsService* suggestions_service, GURL url) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 EXPECT_TRUE(undo_blacklisting_failed_); | 303 EXPECT_TRUE(undo_blacklisting_failed_); |
289 } | 304 } |
290 | 305 |
291 bool HasPendingSuggestionsRequest( | 306 bool HasPendingSuggestionsRequest( |
292 SuggestionsServiceImpl* suggestions_service) { | 307 SuggestionsServiceImpl* suggestions_service) { |
293 return !!suggestions_service->pending_request_.get(); | 308 return !!suggestions_service->pending_request_.get(); |
294 } | 309 } |
295 | 310 |
296 protected: | 311 protected: |
297 base::MessageLoopForIO io_message_loop_; | 312 base::MessageLoopForIO io_message_loop_; |
| 313 TestingPrefServiceSyncable pref_service_; |
| 314 AccountTrackerService account_tracker_; |
| 315 TestSigninClient signin_client_; |
| 316 FakeSigninManagerBase signin_manager_; |
298 net::FakeURLFetcherFactory factory_; | 317 net::FakeURLFetcherFactory factory_; |
299 FakeProfileOAuth2TokenService token_service_; | 318 FakeProfileOAuth2TokenService token_service_; |
300 std::unique_ptr<MockSyncService> mock_sync_service_; | 319 std::unique_ptr<MockSyncService> mock_sync_service_; |
301 // Only used if the SuggestionsService is built with mocks. Not owned. | 320 // Only used if the SuggestionsService is built with mocks. Not owned. |
302 MockImageManager* mock_thumbnail_manager_; | 321 MockImageManager* mock_thumbnail_manager_; |
303 MockBlacklistStore* mock_blacklist_store_; | 322 MockBlacklistStore* mock_blacklist_store_; |
304 TestSuggestionsStore* test_suggestions_store_; | 323 TestSuggestionsStore* test_suggestions_store_; |
305 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 324 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
306 | 325 |
307 private: | 326 private: |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 suggestions_service->FetchSuggestionsData(); | 409 suggestions_service->FetchSuggestionsData(); |
391 | 410 |
392 // Let any network request run. | 411 // Let any network request run. |
393 base::RunLoop().RunUntilIdle(); | 412 base::RunLoop().RunUntilIdle(); |
394 | 413 |
395 // Ensure that CheckCallback didn't run again. | 414 // Ensure that CheckCallback didn't run again. |
396 EXPECT_EQ(1, suggestions_data_callback_count_); | 415 EXPECT_EQ(1, suggestions_data_callback_count_); |
397 } | 416 } |
398 | 417 |
399 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { | 418 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoAccessToken) { |
400 token_service_.RevokeCredentials(kAccountId); | 419 token_service_.set_auto_post_fetch_response_on_message_loop(false); |
401 | 420 |
402 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( | 421 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
403 CreateSuggestionsServiceWithMocks()); | 422 CreateSuggestionsServiceWithMocks()); |
404 ASSERT_TRUE(suggestions_service != nullptr); | 423 ASSERT_TRUE(suggestions_service != nullptr); |
405 | 424 |
406 auto subscription = suggestions_service->AddCallback(base::Bind( | 425 auto subscription = suggestions_service->AddCallback(base::Bind( |
407 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); | 426 &SuggestionsServiceTest::CheckCallback, base::Unretained(this))); |
408 | 427 |
409 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) | 428 EXPECT_CALL(*mock_blacklist_store_, GetTimeUntilReadyForUpload(_)) |
410 .WillOnce(Return(false)); | 429 .WillOnce(Return(false)); |
411 | 430 |
412 suggestions_service->FetchSuggestionsData(); | 431 suggestions_service->FetchSuggestionsData(); |
413 | 432 |
| 433 token_service_.IssueErrorForAllPendingRequests(GoogleServiceAuthError( |
| 434 GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS)); |
| 435 |
414 // No network request should be sent. | 436 // No network request should be sent. |
415 base::RunLoop().RunUntilIdle(); | 437 base::RunLoop().RunUntilIdle(); |
416 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); | 438 EXPECT_FALSE(HasPendingSuggestionsRequest(suggestions_service.get())); |
417 EXPECT_EQ(0, suggestions_data_callback_count_); | 439 EXPECT_EQ(0, suggestions_data_callback_count_); |
418 } | 440 } |
419 | 441 |
420 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { | 442 TEST_F(SuggestionsServiceTest, IssueRequestIfNoneOngoingError) { |
421 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( | 443 std::unique_ptr<SuggestionsServiceImpl> suggestions_service( |
422 CreateSuggestionsServiceWithMocks()); | 444 CreateSuggestionsServiceWithMocks()); |
423 ASSERT_TRUE(suggestions_service != nullptr); | 445 ASSERT_TRUE(suggestions_service != nullptr); |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 757 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
736 suggestions_service->GetPageThumbnail(test_url, dummy_callback); | 758 suggestions_service->GetPageThumbnail(test_url, dummy_callback); |
737 | 759 |
738 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); | 760 EXPECT_CALL(*mock_thumbnail_manager_, AddImageURL(test_url, thumbnail_url)); |
739 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); | 761 EXPECT_CALL(*mock_thumbnail_manager_, GetImageForURL(test_url, _)); |
740 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, | 762 suggestions_service->GetPageThumbnailWithURL(test_url, thumbnail_url, |
741 dummy_callback); | 763 dummy_callback); |
742 } | 764 } |
743 | 765 |
744 } // namespace suggestions | 766 } // namespace suggestions |
OLD | NEW |