OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.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/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "base/test/histogram_tester.h" | 21 #include "base/test/histogram_tester.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
24 #include "components/image_fetcher/image_fetcher.h" | 24 #include "components/image_fetcher/image_fetcher.h" |
25 #include "components/ntp_snippets/ntp_snippet.h" | 25 #include "components/ntp_snippets/ntp_snippet.h" |
26 #include "components/ntp_snippets/ntp_snippets_database.h" | 26 #include "components/ntp_snippets/ntp_snippets_database.h" |
27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
29 #include "components/ntp_snippets/switches.h" | 29 #include "components/ntp_snippets/switches.h" |
30 #include "components/prefs/pref_registry_simple.h" | |
30 #include "components/prefs/testing_pref_service.h" | 31 #include "components/prefs/testing_pref_service.h" |
31 #include "components/signin/core/browser/account_tracker_service.h" | 32 #include "components/signin/core/browser/account_tracker_service.h" |
32 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | 33 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" |
33 #include "components/signin/core/browser/fake_signin_manager.h" | 34 #include "components/signin/core/browser/fake_signin_manager.h" |
34 #include "components/signin/core/browser/test_signin_client.h" | 35 #include "components/signin/core/browser/test_signin_client.h" |
36 #include "components/signin/core/common/signin_pref_names.h" | |
35 #include "components/sync_driver/fake_sync_service.h" | 37 #include "components/sync_driver/fake_sync_service.h" |
36 #include "google_apis/google_api_keys.h" | 38 #include "google_apis/google_api_keys.h" |
37 #include "net/url_request/test_url_fetcher_factory.h" | 39 #include "net/url_request/test_url_fetcher_factory.h" |
38 #include "net/url_request/url_request_test_util.h" | 40 #include "net/url_request/url_request_test_util.h" |
39 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
41 | 43 |
42 using testing::ElementsAre; | 44 using testing::ElementsAre; |
43 using testing::Eq; | 45 using testing::Eq; |
44 using testing::Return; | 46 using testing::Return; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 MOCK_METHOD0(Unschedule, bool()); | 224 MOCK_METHOD0(Unschedule, bool()); |
223 }; | 225 }; |
224 | 226 |
225 class MockSyncService : public sync_driver::FakeSyncService { | 227 class MockSyncService : public sync_driver::FakeSyncService { |
226 public: | 228 public: |
227 MockSyncService() {} | 229 MockSyncService() {} |
228 virtual ~MockSyncService() {} | 230 virtual ~MockSyncService() {} |
229 MOCK_CONST_METHOD0(CanSyncStart, bool()); | 231 MOCK_CONST_METHOD0(CanSyncStart, bool()); |
230 MOCK_CONST_METHOD0(IsSyncActive, bool()); | 232 MOCK_CONST_METHOD0(IsSyncActive, bool()); |
231 MOCK_CONST_METHOD0(ConfigurationDone, bool()); | 233 MOCK_CONST_METHOD0(ConfigurationDone, bool()); |
234 MOCK_CONST_METHOD0(IsEncryptEverythingEnabled, bool()); | |
232 MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet()); | 235 MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet()); |
233 }; | 236 }; |
234 | 237 |
235 class MockServiceObserver : public NTPSnippetsServiceObserver { | 238 class MockServiceObserver : public NTPSnippetsServiceObserver { |
236 public: | 239 public: |
237 MOCK_METHOD0(NTPSnippetsServiceLoaded, void()); | 240 MOCK_METHOD0(NTPSnippetsServiceLoaded, void()); |
238 MOCK_METHOD0(NTPSnippetsServiceShutdown, void()); | 241 MOCK_METHOD0(NTPSnippetsServiceShutdown, void()); |
239 MOCK_METHOD0(NTPSnippetsServiceDisabled, void()); | 242 MOCK_METHOD1(NTPSnippetsServiceDisabledReasonChanged, |
243 void(DisabledReason disabled_reason)); | |
240 }; | 244 }; |
241 | 245 |
242 class WaitForDBLoad : public NTPSnippetsServiceObserver { | 246 class WaitForDBLoad : public NTPSnippetsServiceObserver { |
243 public: | 247 public: |
244 WaitForDBLoad(NTPSnippetsService* service) : service_(service) { | 248 WaitForDBLoad(NTPSnippetsService* service, bool check_state) |
249 : service_(service), check_state_(check_state) { | |
245 service_->AddObserver(this); | 250 service_->AddObserver(this); |
246 if (!service_->ready()) | 251 if (!service_->ready()) |
247 run_loop_.Run(); | 252 run_loop_.Run(); |
248 } | 253 } |
249 | 254 |
250 ~WaitForDBLoad() override { | 255 ~WaitForDBLoad() override { |
251 service_->RemoveObserver(this); | 256 service_->RemoveObserver(this); |
252 } | 257 } |
253 | 258 |
254 private: | 259 private: |
255 void NTPSnippetsServiceLoaded() override { | 260 void NTPSnippetsServiceLoaded() override { |
256 EXPECT_TRUE(service_->ready()); | 261 if (check_state_) |
262 EXPECT_TRUE(service_->ready()); | |
257 run_loop_.Quit(); | 263 run_loop_.Quit(); |
258 } | 264 } |
259 | 265 |
260 void NTPSnippetsServiceShutdown() override {} | 266 void NTPSnippetsServiceShutdown() override {} |
261 void NTPSnippetsServiceDisabled() override {} | 267 void NTPSnippetsServiceDisabledReasonChanged( |
268 DisabledReason disabled_reason) override {} | |
262 | 269 |
263 NTPSnippetsService* service_; | 270 NTPSnippetsService* service_; |
271 bool check_state_; | |
264 base::RunLoop run_loop_; | 272 base::RunLoop run_loop_; |
265 | 273 |
266 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); | 274 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); |
267 }; | 275 }; |
268 | 276 |
269 } // namespace | 277 } // namespace |
270 | 278 |
271 class NTPSnippetsServiceTest : public testing::Test { | 279 class NTPSnippetsServiceTest : public testing::Test { |
272 public: | 280 public: |
273 NTPSnippetsServiceTest() | 281 NTPSnippetsServiceTest() |
274 : fake_url_fetcher_factory_( | 282 : fake_url_fetcher_factory_( |
275 /*default_factory=*/&failing_url_fetcher_factory_), | 283 /*default_factory=*/&failing_url_fetcher_factory_), |
276 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, | 284 test_url_(base::StringPrintf(kTestContentSnippetsServerFormat, |
277 google_apis::GetAPIKey().c_str())), | 285 google_apis::GetAPIKey().c_str())), |
278 pref_service_(new TestingPrefServiceSimple()), | 286 pref_service_(new TestingPrefServiceSimple()), |
279 signin_client_(new TestSigninClient(nullptr)), | 287 signin_client_(new TestSigninClient(pref_service_.get())), |
280 account_tracker_(new AccountTrackerService()), | 288 account_tracker_(new AccountTrackerService()), |
281 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | |
282 account_tracker_.get())), | |
283 fake_token_service_(new FakeProfileOAuth2TokenService()) { | 289 fake_token_service_(new FakeProfileOAuth2TokenService()) { |
284 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 290 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
291 RegisterSigninManagerPrefs(); | |
292 | |
285 // Since no SuggestionsService is injected in tests, we need to force the | 293 // Since no SuggestionsService is injected in tests, we need to force the |
286 // service to fetch from all hosts. | 294 // service to fetch from all hosts. |
287 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 295 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
288 switches::kDontRestrict); | 296 switches::kDontRestrict); |
289 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); | 297 EXPECT_TRUE(database_dir_.CreateUniqueTempDir()); |
290 } | 298 } |
291 | 299 |
292 ~NTPSnippetsServiceTest() override { | 300 ~NTPSnippetsServiceTest() override { |
293 if (service_) | 301 if (service_) |
294 service_->Shutdown(); | 302 service_->Shutdown(); |
295 | 303 |
296 // We need to run the message loop after deleting the database, because | 304 // We need to run the message loop after deleting the database, because |
297 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task | 305 // ProtoDatabaseImpl deletes the actual LevelDB asynchronously on the task |
298 // runner. Without this, we'd get reports of memory leaks. | 306 // runner. Without this, we'd get reports of memory leaks. |
299 service_.reset(); | 307 service_.reset(); |
300 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
301 } | 309 } |
302 | 310 |
303 void SetUp() override { | 311 void SetUp() override { |
304 ResetSyncServiceMock(); | 312 ResetSyncServiceMock(); |
305 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 313 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
306 CreateSnippetsService(/*enabled=*/true); | 314 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true); |
307 } | 315 } |
308 | 316 |
309 void CreateSnippetsService(bool enabled) { | 317 void CreateSnippetsService(bool enabled, bool sign_in) { |
310 if (service_) | 318 if (service_) |
311 service_->Shutdown(); | 319 service_->Shutdown(); |
312 | 320 |
313 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 321 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
314 base::ThreadTaskRunnerHandle::Get()); | 322 base::ThreadTaskRunnerHandle::Get()); |
315 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 323 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
316 new net::TestURLRequestContextGetter(task_runner.get()); | 324 new net::TestURLRequestContextGetter(task_runner.get()); |
317 | 325 |
318 // Delete the current service, so that the database is destroyed before we | 326 // Delete the current service, so that the database is destroyed before we |
319 // create the new one, otherwise opening the new database will fail. | 327 // create the new one, otherwise opening the new database will fail. |
320 service_.reset(); | 328 service_.reset(); |
321 | 329 |
330 fake_signin_manager_.reset(new FakeSigninManagerBase( | |
Bernhard Bauer
2016/06/16 14:03:35
I would store this in a raw FakeSigninManagerBase*
dgn
2016/06/16 14:34:10
Done.
| |
331 signin_client_.get(), account_tracker_.get())); | |
332 | |
333 NTPSnippetsFetcher* snippets_fetcher = new NTPSnippetsFetcher( | |
334 fake_signin_manager_.get(), fake_token_service_.get(), | |
335 std::move(request_context_getter), base::Bind(&ParseJson), | |
336 /*is_stable_channel=*/true); | |
337 | |
338 if (sign_in) { | |
339 static_cast<FakeSigninManagerBase*>(fake_signin_manager_.get()) | |
340 ->SignIn("foo@bar.com"); | |
341 snippets_fetcher->setPersonalizationForTesting( | |
342 NTPSnippetsFetcher::Personalization::kNonPersonal); | |
343 } | |
344 | |
322 service_.reset(new NTPSnippetsService( | 345 service_.reset(new NTPSnippetsService( |
323 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr, | 346 enabled, pref_service_.get(), fake_signin_manager_.get(), |
324 std::string("fr"), &scheduler_, | 347 mock_sync_service_.get(), nullptr, std::string("fr"), &scheduler_, |
325 base::WrapUnique(new NTPSnippetsFetcher( | 348 base::WrapUnique(snippets_fetcher), /*image_fetcher=*/nullptr, |
326 fake_signin_manager_.get(), fake_token_service_.get(), | 349 base::WrapUnique( |
327 std::move(request_context_getter), base::Bind(&ParseJson), | 350 new NTPSnippetsDatabase(database_dir_.path(), task_runner)))); |
328 /*is_stable_channel=*/true)), | 351 |
329 /*image_fetcher=*/nullptr, | |
330 base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(), | |
331 task_runner)))); | |
332 if (enabled) | 352 if (enabled) |
333 WaitForDBLoad(service_.get()); | 353 WaitForDBLoad(service_.get(), sign_in); |
334 } | 354 } |
335 | 355 |
336 protected: | 356 protected: |
337 const GURL& test_url() { return test_url_; } | 357 const GURL& test_url() { return test_url_; } |
338 NTPSnippetsService* service() { return service_.get(); } | 358 NTPSnippetsService* service() { return service_.get(); } |
339 MockScheduler& mock_scheduler() { return scheduler_; } | 359 MockScheduler& mock_scheduler() { return scheduler_; } |
340 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); } | 360 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); } |
341 | 361 |
342 // Provide the json to be returned by the fake fetcher. | 362 // Provide the json to be returned by the fake fetcher. |
343 void SetUpFetchResponse(const std::string& json) { | 363 void SetUpFetchResponse(const std::string& json) { |
(...skipping 10 matching lines...) Expand all Loading... | |
354 // Call before the service is set up to initialize a sync service. | 374 // Call before the service is set up to initialize a sync service. |
355 // Subsequent calls reset the return values of the mocked methods. | 375 // Subsequent calls reset the return values of the mocked methods. |
356 void ResetSyncServiceMock() { | 376 void ResetSyncServiceMock() { |
357 if (!mock_sync_service_) { | 377 if (!mock_sync_service_) { |
358 // Use a NiceMock to avoid the "uninteresting call" warnings. | 378 // Use a NiceMock to avoid the "uninteresting call" warnings. |
359 mock_sync_service_.reset(new testing::NiceMock<MockSyncService>); | 379 mock_sync_service_.reset(new testing::NiceMock<MockSyncService>); |
360 } | 380 } |
361 | 381 |
362 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); | 382 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); |
363 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); | 383 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); |
384 ON_CALL(*mock_sync_service_, IsEncryptEverythingEnabled()) | |
385 .WillByDefault(Return(false)); | |
364 ON_CALL(*mock_sync_service_, ConfigurationDone()) | 386 ON_CALL(*mock_sync_service_, ConfigurationDone()) |
365 .WillByDefault(Return(true)); | 387 .WillByDefault(Return(true)); |
366 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) | 388 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) |
367 .WillByDefault( | 389 .WillByDefault( |
368 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); | 390 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); |
369 } | 391 } |
370 | 392 |
393 void RegisterSigninManagerPrefs() { | |
394 pref_service_->registry()->RegisterStringPref( | |
395 prefs::kGoogleServicesAccountId, std::string()); | |
396 pref_service_->registry()->RegisterStringPref( | |
397 prefs::kGoogleServicesLastAccountId, std::string()); | |
398 pref_service_->registry()->RegisterStringPref( | |
399 prefs::kGoogleServicesLastUsername, std::string()); | |
400 } | |
401 | |
371 private: | 402 private: |
372 base::MessageLoop message_loop_; | 403 base::MessageLoop message_loop_; |
373 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; | 404 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; |
374 // Instantiation of factory automatically sets itself as URLFetcher's factory. | 405 // Instantiation of factory automatically sets itself as URLFetcher's factory. |
375 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | 406 net::FakeURLFetcherFactory fake_url_fetcher_factory_; |
376 const GURL test_url_; | 407 const GURL test_url_; |
377 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 408 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
378 std::unique_ptr<TestSigninClient> signin_client_; | 409 std::unique_ptr<TestSigninClient> signin_client_; |
379 std::unique_ptr<AccountTrackerService> account_tracker_; | 410 std::unique_ptr<AccountTrackerService> account_tracker_; |
380 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default. | 411 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default. |
381 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 412 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
382 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 413 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
383 MockScheduler scheduler_; | 414 MockScheduler scheduler_; |
384 // Last so that the dependencies are deleted after the service. | 415 // Last so that the dependencies are deleted after the service. |
385 std::unique_ptr<NTPSnippetsService> service_; | 416 std::unique_ptr<NTPSnippetsService> service_; |
386 | 417 |
387 base::ScopedTempDir database_dir_; | 418 base::ScopedTempDir database_dir_; |
388 | 419 |
389 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 420 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
390 }; | 421 }; |
391 | 422 |
392 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 423 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
393 public: | 424 public: |
394 void SetUp() override { | 425 void SetUp() override { |
395 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); | 426 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); |
396 CreateSnippetsService(/*enabled=*/false); | 427 CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true); |
397 } | 428 } |
398 }; | 429 }; |
399 | 430 |
400 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) { | 431 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) { |
401 // SetUp() checks that Schedule is called. | 432 // SetUp() checks that Schedule is called. |
402 } | 433 } |
403 | 434 |
404 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) { | 435 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) { |
405 // SetUp() checks that Unschedule is called. | 436 // SetUp() checks that Unschedule is called. |
406 } | 437 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
518 // Discard the snippet. | 549 // Discard the snippet. |
519 EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl)); | 550 EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl)); |
520 EXPECT_THAT(service()->snippets(), IsEmpty()); | 551 EXPECT_THAT(service()->snippets(), IsEmpty()); |
521 | 552 |
522 // Make sure that fetching the same snippet again does not re-add it. | 553 // Make sure that fetching the same snippet again does not re-add it. |
523 LoadFromJSONString(json_str); | 554 LoadFromJSONString(json_str); |
524 EXPECT_THAT(service()->snippets(), IsEmpty()); | 555 EXPECT_THAT(service()->snippets(), IsEmpty()); |
525 | 556 |
526 // The snippet should stay discarded even after re-creating the service. | 557 // The snippet should stay discarded even after re-creating the service. |
527 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 558 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
528 CreateSnippetsService(/*enabled=*/true); | 559 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true); |
529 LoadFromJSONString(json_str); | 560 LoadFromJSONString(json_str); |
530 EXPECT_THAT(service()->snippets(), IsEmpty()); | 561 EXPECT_THAT(service()->snippets(), IsEmpty()); |
531 | 562 |
532 // The snippet can be added again after clearing discarded snippets. | 563 // The snippet can be added again after clearing discarded snippets. |
533 service()->ClearDiscardedSnippets(); | 564 service()->ClearDiscardedSnippets(); |
534 EXPECT_THAT(service()->snippets(), IsEmpty()); | 565 EXPECT_THAT(service()->snippets(), IsEmpty()); |
535 LoadFromJSONString(json_str); | 566 LoadFromJSONString(json_str); |
536 EXPECT_THAT(service()->snippets(), SizeIs(1)); | 567 EXPECT_THAT(service()->snippets(), SizeIs(1)); |
537 } | 568 } |
538 | 569 |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 // Discarded snippets shouldn't influence NumArticlesFetched. | 865 // Discarded snippets shouldn't influence NumArticlesFetched. |
835 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), | 866 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), |
836 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), | 867 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), |
837 base::Bucket(/*min=*/1, /*count=*/3))); | 868 base::Bucket(/*min=*/1, /*count=*/3))); |
838 EXPECT_THAT( | 869 EXPECT_THAT( |
839 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), | 870 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), |
840 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 871 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
841 // Recreating the service and loading from prefs shouldn't count as fetched | 872 // Recreating the service and loading from prefs shouldn't count as fetched |
842 // articles. | 873 // articles. |
843 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 874 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
844 CreateSnippetsService(/*enabled=*/true); | 875 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true); |
845 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); | 876 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); |
846 } | 877 } |
847 | 878 |
848 TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) { | 879 TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) { |
849 const std::string creation = | 880 const std::string creation = |
850 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()); | 881 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()); |
851 const std::string expiry = | 882 const std::string expiry = |
852 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()); | 883 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()); |
853 const std::vector<std::string> source_urls = { | 884 const std::vector<std::string> source_urls = { |
854 "http://mashable.com/2016/05/11/stolen", | 885 "http://mashable.com/2016/05/11/stolen", |
(...skipping 14 matching lines...) Expand all Loading... | |
869 EXPECT_THAT(service()->snippets(), IsEmpty()); | 900 EXPECT_THAT(service()->snippets(), IsEmpty()); |
870 | 901 |
871 // The same article from the AOL domain should now be detected as discarded. | 902 // The same article from the AOL domain should now be detected as discarded. |
872 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 903 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
873 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 904 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
874 ASSERT_THAT(service()->snippets(), IsEmpty()); | 905 ASSERT_THAT(service()->snippets(), IsEmpty()); |
875 } | 906 } |
876 | 907 |
877 TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) { | 908 TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) { |
878 // The default test setup has a compatible sync state. | 909 // The default test setup has a compatible sync state. |
879 EXPECT_EQ(DisabledReason::NONE, service()->GetDisabledReason()); | 910 EXPECT_EQ(DisabledReason::NONE, service()->GetNewDisabledReason()); |
880 | 911 |
881 // History sync disabled. | 912 // History sync disabled. |
882 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) | 913 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) |
883 .WillByDefault(Return(syncer::ModelTypeSet())); | 914 .WillByDefault(Return(syncer::ModelTypeSet())); |
884 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, | 915 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, |
885 service()->GetDisabledReason()); | 916 service()->GetNewDisabledReason()); |
886 ResetSyncServiceMock(); | 917 |
918 // Encryption enabled. | |
919 ON_CALL(*mock_sync_service(), IsEncryptEverythingEnabled()) | |
920 .WillByDefault(Return(true)); | |
921 EXPECT_EQ(DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED, | |
922 service()->GetNewDisabledReason()); | |
887 | 923 |
888 // Not done loading. | 924 // Not done loading. |
889 ON_CALL(*mock_sync_service(), ConfigurationDone()) | 925 ON_CALL(*mock_sync_service(), ConfigurationDone()) |
890 .WillByDefault(Return(false)); | 926 .WillByDefault(Return(false)); |
891 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) | 927 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) |
892 .WillByDefault(Return(syncer::ModelTypeSet())); | 928 .WillByDefault(Return(syncer::ModelTypeSet())); |
893 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN, | 929 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN, |
894 service()->GetDisabledReason()); | 930 service()->GetNewDisabledReason()); |
895 ResetSyncServiceMock(); | |
896 | 931 |
897 // Sync disabled. | 932 // Sync disabled. |
898 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); | 933 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); |
899 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, | 934 EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason()); |
900 service()->GetDisabledReason()); | |
901 ResetSyncServiceMock(); | |
902 | 935 |
903 // No service. | 936 // No sync service. |
904 service()->sync_service_ = nullptr; | 937 service()->sync_service_ = nullptr; |
905 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, | 938 EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason()); |
906 service()->GetDisabledReason()); | 939 |
940 // Signed out. | |
941 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/false); | |
942 EXPECT_EQ(DisabledReason::SIGNED_OUT, service()->GetNewDisabledReason()); | |
943 | |
944 CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true); | |
945 EXPECT_EQ(DisabledReason::EXPLICITLY_DISABLED, | |
946 service()->GetNewDisabledReason()); | |
907 } | 947 } |
908 | 948 |
909 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { | 949 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { |
910 MockServiceObserver mock_observer; | 950 MockServiceObserver mock_observer; |
911 service()->AddObserver(&mock_observer); | 951 service()->AddObserver(&mock_observer); |
912 | 952 |
913 // Simulate user disabled sync. | 953 // Simulate user disabled sync. |
914 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); | 954 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); |
915 // The service should notify observers it's been disabled and clear the | 955 // The service should notify observers it's been disabled and clear the |
916 // snippets instead of pulling new ones. | 956 // snippets instead of pulling new ones. |
917 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled()); | 957 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabledReasonChanged( |
958 DisabledReason::SYNC_DISABLED)); | |
918 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 959 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
919 service()->OnStateChanged(); | 960 service()->OnStateChanged(); |
920 base::RunLoop().RunUntilIdle(); | 961 base::RunLoop().RunUntilIdle(); |
921 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); | 962 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); |
922 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. | 963 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. |
923 | 964 |
924 // Simulate user sign in. | 965 // Simulate user sign in. |
925 ResetSyncServiceMock(); | 966 ResetSyncServiceMock(); |
926 // The service should be ready again and load snippets. | 967 // The service should be ready again and load snippets. |
927 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 968 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
969 EXPECT_CALL(mock_observer, | |
970 NTPSnippetsServiceDisabledReasonChanged(DisabledReason::NONE)); | |
928 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 971 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
929 service()->OnStateChanged(); | 972 service()->OnStateChanged(); |
930 base::RunLoop().RunUntilIdle(); | 973 base::RunLoop().RunUntilIdle(); |
931 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 974 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
932 EXPECT_FALSE(service()->snippets().empty()); | 975 EXPECT_FALSE(service()->snippets().empty()); |
933 | 976 |
934 service()->RemoveObserver(&mock_observer); | 977 service()->RemoveObserver(&mock_observer); |
935 } | 978 } |
936 | 979 |
937 } // namespace ntp_snippets | 980 } // namespace ntp_snippets |
OLD | NEW |