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

Unified 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: fix compilation: origins 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_service_unittest.cc b/components/ntp_snippets/ntp_snippets_service_unittest.cc
index e55dc70720b63c10ca0f06ec288977e854754d48..b162b6879133bb4aefd940146f70f1d5457b4b96 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -27,11 +27,13 @@
#include "components/ntp_snippets/ntp_snippets_fetcher.h"
#include "components/ntp_snippets/ntp_snippets_scheduler.h"
#include "components/ntp_snippets/switches.h"
+#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
#include "components/signin/core/browser/fake_signin_manager.h"
#include "components/signin/core/browser/test_signin_client.h"
+#include "components/signin/core/common/signin_pref_names.h"
#include "components/sync_driver/fake_sync_service.h"
#include "google_apis/google_api_keys.h"
#include "net/url_request/test_url_fetcher_factory.h"
@@ -229,6 +231,7 @@ class MockSyncService : public sync_driver::FakeSyncService {
MOCK_CONST_METHOD0(CanSyncStart, bool());
MOCK_CONST_METHOD0(IsSyncActive, bool());
MOCK_CONST_METHOD0(ConfigurationDone, bool());
+ MOCK_CONST_METHOD0(IsEncryptEverythingEnabled, bool());
MOCK_CONST_METHOD0(GetActiveDataTypes, syncer::ModelTypeSet());
};
@@ -236,12 +239,14 @@ class MockServiceObserver : public NTPSnippetsServiceObserver {
public:
MOCK_METHOD0(NTPSnippetsServiceLoaded, void());
MOCK_METHOD0(NTPSnippetsServiceShutdown, void());
- MOCK_METHOD0(NTPSnippetsServiceDisabled, void());
+ MOCK_METHOD1(NTPSnippetsServiceDisabledReasonChanged,
+ void(DisabledReason disabled_reason));
};
class WaitForDBLoad : public NTPSnippetsServiceObserver {
public:
- WaitForDBLoad(NTPSnippetsService* service) : service_(service) {
+ WaitForDBLoad(NTPSnippetsService* service, bool check_state)
+ : service_(service), check_state_(check_state) {
service_->AddObserver(this);
if (!service_->ready())
run_loop_.Run();
@@ -253,14 +258,17 @@ class WaitForDBLoad : public NTPSnippetsServiceObserver {
private:
void NTPSnippetsServiceLoaded() override {
- EXPECT_TRUE(service_->ready());
+ if (check_state_)
+ EXPECT_TRUE(service_->ready());
run_loop_.Quit();
}
void NTPSnippetsServiceShutdown() override {}
- void NTPSnippetsServiceDisabled() override {}
+ void NTPSnippetsServiceDisabledReasonChanged(
+ DisabledReason disabled_reason) override {}
NTPSnippetsService* service_;
+ bool check_state_;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
@@ -276,12 +284,12 @@ class NTPSnippetsServiceTest : public testing::Test {
test_url_(base::StringPrintf(kTestContentSnippetsServerFormat,
google_apis::GetAPIKey().c_str())),
pref_service_(new TestingPrefServiceSimple()),
- signin_client_(new TestSigninClient(nullptr)),
+ signin_client_(new TestSigninClient(pref_service_.get())),
account_tracker_(new AccountTrackerService()),
- fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(),
- account_tracker_.get())),
fake_token_service_(new FakeProfileOAuth2TokenService()) {
NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry());
+ RegisterSigninManagerPrefs();
+
// Since no SuggestionsService is injected in tests, we need to force the
// service to fetch from all hosts.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
@@ -303,10 +311,10 @@ class NTPSnippetsServiceTest : public testing::Test {
void SetUp() override {
ResetSyncServiceMock();
EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
- CreateSnippetsService(/*enabled=*/true);
+ CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true);
}
- void CreateSnippetsService(bool enabled) {
+ void CreateSnippetsService(bool enabled, bool sign_in) {
if (service_)
service_->Shutdown();
@@ -319,18 +327,30 @@ class NTPSnippetsServiceTest : public testing::Test {
// create the new one, otherwise opening the new database will fail.
service_.reset();
+ 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.
+ signin_client_.get(), account_tracker_.get()));
+
+ NTPSnippetsFetcher* snippets_fetcher = new NTPSnippetsFetcher(
+ fake_signin_manager_.get(), fake_token_service_.get(),
+ std::move(request_context_getter), base::Bind(&ParseJson),
+ /*is_stable_channel=*/true);
+
+ if (sign_in) {
+ static_cast<FakeSigninManagerBase*>(fake_signin_manager_.get())
+ ->SignIn("foo@bar.com");
+ snippets_fetcher->setPersonalizationForTesting(
+ NTPSnippetsFetcher::Personalization::kNonPersonal);
+ }
+
service_.reset(new NTPSnippetsService(
- enabled, pref_service_.get(), mock_sync_service_.get(), nullptr,
- std::string("fr"), &scheduler_,
- base::WrapUnique(new NTPSnippetsFetcher(
- fake_signin_manager_.get(), fake_token_service_.get(),
- std::move(request_context_getter), base::Bind(&ParseJson),
- /*is_stable_channel=*/true)),
- /*image_fetcher=*/nullptr,
- base::WrapUnique(new NTPSnippetsDatabase(database_dir_.path(),
- task_runner))));
+ enabled, pref_service_.get(), fake_signin_manager_.get(),
+ mock_sync_service_.get(), nullptr, std::string("fr"), &scheduler_,
+ base::WrapUnique(snippets_fetcher), /*image_fetcher=*/nullptr,
+ base::WrapUnique(
+ new NTPSnippetsDatabase(database_dir_.path(), task_runner))));
+
if (enabled)
- WaitForDBLoad(service_.get());
+ WaitForDBLoad(service_.get(), sign_in);
}
protected:
@@ -361,6 +381,8 @@ class NTPSnippetsServiceTest : public testing::Test {
ON_CALL(*mock_sync_service_, CanSyncStart()).WillByDefault(Return(true));
ON_CALL(*mock_sync_service_, IsSyncActive()).WillByDefault(Return(true));
+ ON_CALL(*mock_sync_service_, IsEncryptEverythingEnabled())
+ .WillByDefault(Return(false));
ON_CALL(*mock_sync_service_, ConfigurationDone())
.WillByDefault(Return(true));
ON_CALL(*mock_sync_service_, GetActiveDataTypes())
@@ -368,6 +390,15 @@ class NTPSnippetsServiceTest : public testing::Test {
Return(syncer::ModelTypeSet(syncer::HISTORY_DELETE_DIRECTIVES)));
}
+ void RegisterSigninManagerPrefs() {
+ pref_service_->registry()->RegisterStringPref(
+ prefs::kGoogleServicesAccountId, std::string());
+ pref_service_->registry()->RegisterStringPref(
+ prefs::kGoogleServicesLastAccountId, std::string());
+ pref_service_->registry()->RegisterStringPref(
+ prefs::kGoogleServicesLastUsername, std::string());
+ }
+
private:
base::MessageLoop message_loop_;
FailingFakeURLFetcherFactory failing_url_fetcher_factory_;
@@ -393,7 +424,7 @@ class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
public:
void SetUp() override {
EXPECT_CALL(mock_scheduler(), Unschedule()).Times(1);
- CreateSnippetsService(/*enabled=*/false);
+ CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true);
}
};
@@ -525,7 +556,7 @@ TEST_F(NTPSnippetsServiceTest, Discard) {
// The snippet should stay discarded even after re-creating the service.
EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
- CreateSnippetsService(/*enabled=*/true);
+ CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true);
LoadFromJSONString(json_str);
EXPECT_THAT(service()->snippets(), IsEmpty());
@@ -841,7 +872,7 @@ TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) {
// Recreating the service and loading from prefs shouldn't count as fetched
// articles.
EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
- CreateSnippetsService(/*enabled=*/true);
+ CreateSnippetsService(/*enabled=*/true, /*sign_in=*/true);
tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
}
@@ -876,14 +907,19 @@ TEST_F(NTPSnippetsServiceTest, DiscardShouldRespectAllKnownUrls) {
TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) {
// The default test setup has a compatible sync state.
- EXPECT_EQ(DisabledReason::NONE, service()->GetDisabledReason());
+ EXPECT_EQ(DisabledReason::NONE, service()->GetNewDisabledReason());
// History sync disabled.
ON_CALL(*mock_sync_service(), GetActiveDataTypes())
.WillByDefault(Return(syncer::ModelTypeSet()));
EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
- service()->GetDisabledReason());
- ResetSyncServiceMock();
+ service()->GetNewDisabledReason());
+
+ // Encryption enabled.
+ ON_CALL(*mock_sync_service(), IsEncryptEverythingEnabled())
+ .WillByDefault(Return(true));
+ EXPECT_EQ(DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED,
+ service()->GetNewDisabledReason());
// Not done loading.
ON_CALL(*mock_sync_service(), ConfigurationDone())
@@ -891,19 +927,23 @@ TEST_F(NTPSnippetsServiceTest, SyncStateCompatibility) {
ON_CALL(*mock_sync_service(), GetActiveDataTypes())
.WillByDefault(Return(syncer::ModelTypeSet()));
EXPECT_EQ(DisabledReason::HISTORY_SYNC_STATE_UNKNOWN,
- service()->GetDisabledReason());
- ResetSyncServiceMock();
+ service()->GetNewDisabledReason());
// Sync disabled.
ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
- EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
- service()->GetDisabledReason());
- ResetSyncServiceMock();
+ EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason());
- // No service.
+ // No sync service.
service()->sync_service_ = nullptr;
- EXPECT_EQ(DisabledReason::HISTORY_SYNC_DISABLED,
- service()->GetDisabledReason());
+ EXPECT_EQ(DisabledReason::SYNC_DISABLED, service()->GetNewDisabledReason());
+
+ // Signed out.
+ CreateSnippetsService(/*enabled=*/true, /*sign_in=*/false);
+ EXPECT_EQ(DisabledReason::SIGNED_OUT, service()->GetNewDisabledReason());
+
+ CreateSnippetsService(/*enabled=*/false, /*sign_in=*/true);
+ EXPECT_EQ(DisabledReason::EXPLICITLY_DISABLED,
+ service()->GetNewDisabledReason());
}
TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
@@ -914,7 +954,8 @@ TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
ON_CALL(*mock_sync_service(), CanSyncStart()).WillByDefault(Return(false));
// The service should notify observers it's been disabled and clear the
// snippets instead of pulling new ones.
- EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabled());
+ EXPECT_CALL(mock_observer, NTPSnippetsServiceDisabledReasonChanged(
+ DisabledReason::SYNC_DISABLED));
SetUpFetchResponse(GetTestJson({GetSnippet()}));
service()->OnStateChanged();
base::RunLoop().RunUntilIdle();
@@ -925,6 +966,8 @@ TEST_F(NTPSnippetsServiceTest, HistorySyncStateChanges) {
ResetSyncServiceMock();
// The service should be ready again and load snippets.
EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
+ EXPECT_CALL(mock_observer,
+ NTPSnippetsServiceDisabledReasonChanged(DisabledReason::NONE));
SetUpFetchResponse(GetTestJson({GetSnippet()}));
service()->OnStateChanged();
base::RunLoop().RunUntilIdle();

Powered by Google App Engine
This is Rietveld 408576698