Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2061803002: 📰 The Status card reports disabled sync states (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@simplifyBridge
Patch Set: Address comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(
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 fake_signin_manager_.get()->SignIn("foo@bar.com");
340 snippets_fetcher->SetPersonalizationForTesting(
341 NTPSnippetsFetcher::Personalization::kNonPersonal);
342 }
343
322 service_.reset(new NTPSnippetsService( 344 service_.reset(new NTPSnippetsService(
323 enabled, pref_service_.get(), mock_sync_service_.get(), nullptr, 345 enabled, pref_service_.get(), fake_signin_manager_.get(),
324 std::string("fr"), &scheduler_, 346 mock_sync_service_.get(), nullptr, std::string("fr"), &scheduler_,
325 base::WrapUnique(new NTPSnippetsFetcher( 347 base::WrapUnique(snippets_fetcher), /*image_fetcher=*/nullptr,
326 fake_signin_manager_.get(), fake_token_service_.get(), 348 base::WrapUnique(
327 std::move(request_context_getter), base::Bind(&ParseJson), 349 new NTPSnippetsDatabase(database_dir_.path(), task_runner))));
328 /*is_stable_channel=*/true)), 350
329 /*image_fetcher=*/nullptr,
330 base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(),
331 task_runner))));
332 if (enabled) 351 if (enabled)
333 WaitForDBLoad(service_.get()); 352 WaitForDBLoad(service_.get(), sign_in);
334 } 353 }
335 354
336 protected: 355 protected:
337 const GURL& test_url() { return test_url_; } 356 const GURL& test_url() { return test_url_; }
338 NTPSnippetsService* service() { return service_.get(); } 357 NTPSnippetsService* service() { return service_.get(); }
339 MockScheduler& mock_scheduler() { return scheduler_; } 358 MockScheduler& mock_scheduler() { return scheduler_; }
340 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); } 359 MockSyncService* mock_sync_service() { return mock_sync_service_.get(); }
341 360
342 // Provide the json to be returned by the fake fetcher. 361 // Provide the json to be returned by the fake fetcher.
343 void SetUpFetchResponse(const std::string& json) { 362 void SetUpFetchResponse(const std::string& json) {
(...skipping 10 matching lines...) Expand all
354 // Call before the service is set up to initialize a sync service. 373 // Call before the service is set up to initialize a sync service.
355 // Subsequent calls reset the return values of the mocked methods. 374 // Subsequent calls reset the return values of the mocked methods.
356 void ResetSyncServiceMock() { 375 void ResetSyncServiceMock() {
357 if (!mock_sync_service_) { 376 if (!mock_sync_service_) {
358 // Use a NiceMock to avoid the "uninteresting call" warnings. 377 // Use a NiceMock to avoid the "uninteresting call" warnings.
359 mock_sync_service_.reset(new testing::NiceMock<MockSyncService>); 378 mock_sync_service_.reset(new testing::NiceMock<MockSyncService>);
360 } 379 }
361 380
362 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true)); 381 ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true));
363 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true)); 382 ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true));
383 ON_CALL(*mock_sync_service_, IsEncryptEverythingEnabled())
384 .WillByDefault(Return(false));
364 ON_CALL(*mock_sync_service_, ConfigurationDone()) 385 ON_CALL(*mock_sync_service_, ConfigurationDone())
365 .WillByDefault(Return(true)); 386 .WillByDefault(Return(true));
366 ON_CALL(*mock_sync_service_, GetActiveDataTypes()) 387 ON_CALL(*mock_sync_service_, GetActiveDataTypes())
367 .WillByDefault( 388 .WillByDefault(
368 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES))); 389 Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES)));
369 } 390 }
370 391
392 void RegisterSigninManagerPrefs() {
393 pref_service_->registry()->RegisterStringPref(
394 prefs::kGoogleServicesAccountId, std::string());
395 pref_service_->registry()->RegisterStringPref(
396 prefs::kGoogleServicesLastAccountId, std::string());
397 pref_service_->registry()->RegisterStringPref(
398 prefs::kGoogleServicesLastUsername, std::string());
399 }
400
371 private: 401 private:
372 base::MessageLoop message_loop_; 402 base::MessageLoop message_loop_;
373 FailingFakeURLFetcherFactory failing_url_fetcher_factory_; 403 FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
374 // Instantiation of factory automatically sets itself as URLFetcher's factory. 404 // Instantiation of factory automatically sets itself as URLFetcher's factory.
375 net::FakeURLFetcherFactory fake_url_fetcher_factory_; 405 net::FakeURLFetcherFactory fake_url_fetcher_factory_;
376 const GURL test_url_; 406 const GURL test_url_;
377 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 407 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
378 std::unique_ptr<TestSigninClient> signin_client_; 408 std::unique_ptr<TestSigninClient> signin_client_;
379 std::unique_ptr<AccountTrackerService> account_tracker_; 409 std::unique_ptr<AccountTrackerService> account_tracker_;
380 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default. 410 std::unique_ptr<MockSyncService> mock_sync_service_; // Null by default.
381 std::unique_ptr<SigninManagerBase> fake_signin_manager_; 411 std::unique_ptr<FakeSigninManagerBase> fake_signin_manager_;
382 std::unique_ptr<OAuth2TokenService> fake_token_service_; 412 std::unique_ptr<OAuth2TokenService> fake_token_service_;
383 MockScheduler scheduler_; 413 MockScheduler scheduler_;
384 // Last so that the dependencies are deleted after the service. 414 // Last so that the dependencies are deleted after the service.
385 std::unique_ptr<NTPSnippetsService> service_; 415 std::unique_ptr<NTPSnippetsService> service_;
386 416
387 base::ScopedTempDir database_dir_; 417 base::ScopedTempDir database_dir_;
388 418
389 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 419 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
390 }; 420 };
391 421
392 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { 422 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
393 public: 423 public:
394 void SetUp() override { 424 void SetUp() override {
395 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1); 425 EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
396 CreateSnippetsService(/*enabled=*/false); 426 CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true);
397 } 427 }
398 }; 428 };
399 429
400 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) { 430 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) {
401 // SetUp() checks that Schedule is called. 431 // SetUp() checks that Schedule is called.
402 } 432 }
403 433
404 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) { 434 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) {
405 // SetUp() checks that Unschedule is called. 435 // SetUp() checks that Unschedule is called.
406 } 436 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 // Discard the snippet. 548 // Discard the snippet.
519 EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl)); 549 EXPECT_TRUE(service()->DiscardSnippet(kSnippetUrl));
520 EXPECT_THAT(service()->snippets(), IsEmpty()); 550 EXPECT_THAT(service()->snippets(), IsEmpty());
521 551
522 // Make sure that fetching the same snippet again does not re-add it. 552 // Make sure that fetching the same snippet again does not re-add it.
523 LoadFromJSONString(json_str); 553 LoadFromJSONString(json_str);
524 EXPECT_THAT(service()->snippets(), IsEmpty()); 554 EXPECT_THAT(service()->snippets(), IsEmpty());
525 555
526 // The snippet should stay discarded even after re-creating the service. 556 // The snippet should stay discarded even after re-creating the service.
527 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 557 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
528 CreateSnippetsService(/*enabled=*/true); 558 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true);
529 LoadFromJSONString(json_str); 559 LoadFromJSONString(json_str);
530 EXPECT_THAT(service()->snippets(), IsEmpty()); 560 EXPECT_THAT(service()->snippets(), IsEmpty());
531 561
532 // The snippet can be added again after clearing discarded snippets. 562 // The snippet can be added again after clearing discarded snippets.
533 service()->ClearDiscardedSnippets(); 563 service()->ClearDiscardedSnippets();
534 EXPECT_THAT(service()->snippets(), IsEmpty()); 564 EXPECT_THAT(service()->snippets(), IsEmpty());
535 LoadFromJSONString(json_str); 565 LoadFromJSONString(json_str);
536 EXPECT_THAT(service()->snippets(), SizeIs(1)); 566 EXPECT_THAT(service()->snippets(), SizeIs(1));
537 } 567 }
538 568
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 // Discarded snippets shouldn't influence NumArticlesFetched. 864 // Discarded snippets shouldn't influence NumArticlesFetched.
835 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"), 865 EXPECT_THAT(tester.GetAllSamples("NewTabPage.Snippets.NumArticlesFetched"),
836 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1), 866 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1),
837 base::Bucket(/*min=*/1, /*count=*/3))); 867 base::Bucket(/*min=*/1, /*count=*/3)));
838 EXPECT_THAT( 868 EXPECT_THAT(
839 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 869 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
840 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 870 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
841 // Recreating the service and loading from prefs shouldn't count as fetched 871 // Recreating the service and loading from prefs shouldn't count as fetched
842 // articles. 872 // articles.
843 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 873 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
844 CreateSnippetsService(/*enabled=*/true); 874 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true);
845 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); 875 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
846 } 876 }
847 877
848 TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) { 878 TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) {
849 const std::string creation = 879 const std::string creation =
850 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()); 880 NTPSnippet::TimeToJsonString(GetDefaultCreationTime());
851 const std::string expiry = 881 const std::string expiry =
852 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()); 882 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime());
853 const std::vector<std::string> source_urls = { 883 const std::vector<std::string> source_urls = {
854 "http://mashable.com/2016/05/11/stolen", 884 "http://mashable.com/2016/05/11/stolen",
(...skipping 14 matching lines...) Expand all
869 EXPECT_THAT(service()->snippets(), IsEmpty()); 899 EXPECT_THAT(service()->snippets(), IsEmpty());
870 900
871 // The same article from the AOL domain should now be detected as discarded. 901 // The same article from the AOL domain should now be detected as discarded.
872 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( 902 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
873 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); 903 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)}));
874 ASSERT_THAT(service()->snippets(), IsEmpty()); 904 ASSERT_THAT(service()->snippets(), IsEmpty());
875 } 905 }
876 906
877 TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) { 907 TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) {
878 // The default test setup has a compatible sync state. 908 // The default test setup has a compatible sync state.
879 EXPECT_EQ(DisabledReason::NONE, service()->GetDisabledReason()); 909 EXPECT_EQ(DisabledReason::NONE, service()->GetNewDisabledReason());
880 910
881 // History sync disabled. 911 // History sync disabled.
882 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) 912 ON_CALL(*mock_sync_service(), GetActiveDataTypes())
883 .WillByDefault(Return(syncer::ModelTypeSet())); 913 .WillByDefault(Return(syncer::ModelTypeSet()));
884 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, 914 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
885 service()->GetDisabledReason()); 915 service()->GetNewDisabledReason());
886 ResetSyncServiceMock(); 916
917 // Encryption enabled.
918 ON_CALL(*mock_sync_service(), IsEncryptEverythingEnabled())
919 .WillByDefault(Return(true));
920 EXPECT_EQ(DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED,
921 service()->GetNewDisabledReason());
887 922
888 // Not done loading. 923 // Not done loading.
889 ON_CALL(*mock_sync_service(), ConfigurationDone()) 924 ON_CALL(*mock_sync_service(), ConfigurationDone())
890 .WillByDefault(Return(false)); 925 .WillByDefault(Return(false));
891 ON_CALL(*mock_sync_service(), GetActiveDataTypes()) 926 ON_CALL(*mock_sync_service(), GetActiveDataTypes())
892 .WillByDefault(Return(syncer::ModelTypeSet())); 927 .WillByDefault(Return(syncer::ModelTypeSet()));
893 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN, 928 EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN,
894 service()->GetDisabledReason()); 929 service()->GetNewDisabledReason());
895 ResetSyncServiceMock();
896 930
897 // Sync disabled. 931 // Sync disabled.
898 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); 932 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
899 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, 933 EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason());
900 service()->GetDisabledReason());
901 ResetSyncServiceMock();
902 934
903 // No service. 935 // No sync service.
904 service()->sync_service_ = nullptr; 936 service()->sync_service_ = nullptr;
905 EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED, 937 EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason());
906 service()->GetDisabledReason()); 938
939 // Signed out.
940 CreateSnippetsService(/*enabled=*/true, /*sign_in=*/false);
941 EXPECT_EQ(DisabledReason::SIGNED_OUT, service()->GetNewDisabledReason());
942
943 CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true);
944 EXPECT_EQ(DisabledReason::EXPLICITLY_DISABLED,
945 service()->GetNewDisabledReason());
907 } 946 }
908 947
909 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) { 948 TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
910 MockServiceObserver mock_observer; 949 MockServiceObserver mock_observer;
911 service()->AddObserver(&mock_observer); 950 service()->AddObserver(&mock_observer);
912 951
913 // Simulate user disabled sync. 952 // Simulate user disabled sync.
914 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false)); 953 ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
915 // The service should notify observers it's been disabled and clear the 954 // The service should notify observers it's been disabled and clear the
916 // snippets instead of pulling new ones. 955 // snippets instead of pulling new ones.
917 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled()); 956 EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabledReasonChanged(
957 DisabledReason::SYNC_DISABLED));
918 SetUpFetchResponse(GetTestJson({GetSnippet()})); 958 SetUpFetchResponse(GetTestJson({GetSnippet()}));
919 service()->OnStateChanged(); 959 service()->OnStateChanged();
920 base::RunLoop().RunUntilIdle(); 960 base::RunLoop().RunUntilIdle();
921 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); 961 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_);
922 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made. 962 EXPECT_THAT(service()->snippets(), IsEmpty()); // No fetch should be made.
923 963
924 // Simulate user sign in. 964 // Simulate user sign in.
925 ResetSyncServiceMock(); 965 ResetSyncServiceMock();
926 // The service should be ready again and load snippets. 966 // The service should be ready again and load snippets.
927 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 967 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
968 EXPECT_CALL(mock_observer,
969 NTPSnippetsServiceDisabledReasonChanged(DisabledReason::NONE));
928 SetUpFetchResponse(GetTestJson({GetSnippet()})); 970 SetUpFetchResponse(GetTestJson({GetSnippet()}));
929 service()->OnStateChanged(); 971 service()->OnStateChanged();
930 base::RunLoop().RunUntilIdle(); 972 base::RunLoop().RunUntilIdle();
931 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); 973 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
932 EXPECT_FALSE(service()->snippets().empty()); 974 EXPECT_FALSE(service()->snippets().empty());
933 975
934 service()->RemoveObserver(&mock_observer); 976 service()->RemoveObserver(&mock_observer);
935 } 977 }
936 978
937 } // namespace ntp_snippets 979 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698