This source file includes following definitions.
- MATCHER_P
- GetAllTypes
- GetConfiguration
- CheckBool
- CheckBool
- CheckConfigDataTypeArguments
- ClearTrackedCalls
- CallJavascriptFunction
- CallJavascriptFunction
- CallJavascriptFunction
- GetWebContents
- GetController
- SetController
- GetDeviceScaleFactor
- GetOverriddenTitle
- OverrideTitle
- GetLinkTransitionType
- SetLinkTransitionType
- GetBindings
- SetBindings
- OverrideJavaScriptFrame
- AddMessageHandler
- RegisterMessageCallback
- ProcessWebUIMessage
- CallJavascriptFunction
- CallJavascriptFunction
- CallJavascriptFunction
- arg2
- call_data
- profile_
- FocusUI
- GetProfile
- DisplayGaiaLoginInNewTabOrWindow
- SetUp
- SetDefaultExpectationsForConfigPage
- SetupInitializedProfileSyncService
- ExpectConfig
- ExpectDone
- ExpectSpinnerAndClose
- NotifySyncListeners
- GetTestUser
- GetTestUser
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/ui/webui/sync_setup_handler.h"
#include <vector>
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "chrome/browser/signin/fake_signin_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h"
#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/core/browser/fake_auth_status_provider.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync_driver/sync_prefs.h"
#include "content/public/browser/web_ui.h"
#include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/layout.h"
using ::testing::_;
using ::testing::Mock;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::Values;
typedef GoogleServiceAuthError AuthError;
namespace {
MATCHER_P(ModelTypeSetMatches, value, "") { return arg.Equals(value); }
const char kTestUser[] = "chrome.p13n.test@gmail.com";
syncer::ModelTypeSet GetAllTypes() {
return syncer::UserSelectableTypes();
}
enum SyncAllDataConfig {
SYNC_ALL_DATA,
CHOOSE_WHAT_TO_SYNC,
SYNC_NOTHING
};
enum EncryptAllConfig {
ENCRYPT_ALL_DATA,
ENCRYPT_PASSWORDS
};
std::string GetConfiguration(const base::DictionaryValue* extra_values,
SyncAllDataConfig sync_all,
syncer::ModelTypeSet types,
const std::string& passphrase,
EncryptAllConfig encrypt_all) {
base::DictionaryValue result;
if (extra_values)
result.MergeDictionary(extra_values);
result.SetBoolean("syncAllDataTypes", sync_all == SYNC_ALL_DATA);
result.SetBoolean("syncNothing", sync_all == SYNC_NOTHING);
result.SetBoolean("encryptAllData", encrypt_all == ENCRYPT_ALL_DATA);
result.SetBoolean("usePassphrase", !passphrase.empty());
if (!passphrase.empty())
result.SetString("passphrase", passphrase);
result.SetBoolean("appsSynced", types.Has(syncer::APPS));
result.SetBoolean("autofillSynced", types.Has(syncer::AUTOFILL));
result.SetBoolean("bookmarksSynced", types.Has(syncer::BOOKMARKS));
result.SetBoolean("extensionsSynced", types.Has(syncer::EXTENSIONS));
result.SetBoolean("passwordsSynced", types.Has(syncer::PASSWORDS));
result.SetBoolean("preferencesSynced", types.Has(syncer::PREFERENCES));
result.SetBoolean("tabsSynced", types.Has(syncer::PROXY_TABS));
result.SetBoolean("themesSynced", types.Has(syncer::THEMES));
result.SetBoolean("typedUrlsSynced", types.Has(syncer::TYPED_URLS));
std::string args;
base::JSONWriter::Write(&result, &args);
return args;
}
void CheckBool(const base::DictionaryValue* dictionary,
const std::string& key,
bool expected_value,
bool omit_if_false) {
if (omit_if_false && !expected_value) {
EXPECT_FALSE(dictionary->HasKey(key)) <<
"Did not expect to find value for " << key;
} else {
bool actual_value;
EXPECT_TRUE(dictionary->GetBoolean(key, &actual_value)) <<
"No value found for " << key;
EXPECT_EQ(actual_value, expected_value) <<
"Mismatch found for " << key;
}
}
void CheckBool(const base::DictionaryValue* dictionary,
const std::string& key,
bool expected_value) {
return CheckBool(dictionary, key, expected_value, false);
}
void CheckConfigDataTypeArguments(base::DictionaryValue* dictionary,
SyncAllDataConfig config,
syncer::ModelTypeSet types) {
CheckBool(dictionary, "syncAllDataTypes", config == SYNC_ALL_DATA);
CheckBool(dictionary, "syncNothing", config == SYNC_NOTHING);
CheckBool(dictionary, "appsSynced", types.Has(syncer::APPS));
CheckBool(dictionary, "autofillSynced", types.Has(syncer::AUTOFILL));
CheckBool(dictionary, "bookmarksSynced", types.Has(syncer::BOOKMARKS));
CheckBool(dictionary, "extensionsSynced", types.Has(syncer::EXTENSIONS));
CheckBool(dictionary, "passwordsSynced", types.Has(syncer::PASSWORDS));
CheckBool(dictionary, "preferencesSynced", types.Has(syncer::PREFERENCES));
CheckBool(dictionary, "tabsSynced", types.Has(syncer::PROXY_TABS));
CheckBool(dictionary, "themesSynced", types.Has(syncer::THEMES));
CheckBool(dictionary, "typedUrlsSynced", types.Has(syncer::TYPED_URLS));
}
}
class TestWebUI : public content::WebUI {
public:
virtual ~TestWebUI() {
ClearTrackedCalls();
}
void ClearTrackedCalls() {
for (std::vector<CallData>::iterator i = call_data_.begin();
i != call_data_.end();
++i) {
delete i->arg1;
delete i->arg2;
}
call_data_.clear();
}
virtual void CallJavascriptFunction(const std::string& function_name)
OVERRIDE {
call_data_.push_back(CallData());
call_data_.back().function_name = function_name;
}
virtual void CallJavascriptFunction(const std::string& function_name,
const base::Value& arg1) OVERRIDE {
call_data_.push_back(CallData());
call_data_.back().function_name = function_name;
call_data_.back().arg1 = arg1.DeepCopy();
}
virtual void CallJavascriptFunction(const std::string& function_name,
const base::Value& arg1,
const base::Value& arg2) OVERRIDE {
call_data_.push_back(CallData());
call_data_.back().function_name = function_name;
call_data_.back().arg1 = arg1.DeepCopy();
call_data_.back().arg2 = arg2.DeepCopy();
}
virtual content::WebContents* GetWebContents() const OVERRIDE {
return NULL;
}
virtual content::WebUIController* GetController() const OVERRIDE {
return NULL;
}
virtual void SetController(content::WebUIController* controller) OVERRIDE {}
virtual ui::ScaleFactor GetDeviceScaleFactor() const OVERRIDE {
return ui::SCALE_FACTOR_100P;
}
virtual const base::string16& GetOverriddenTitle() const OVERRIDE {
return temp_string_;
}
virtual void OverrideTitle(const base::string16& title) OVERRIDE {}
virtual content::PageTransition GetLinkTransitionType() const OVERRIDE {
return content::PAGE_TRANSITION_LINK;
}
virtual void SetLinkTransitionType(content::PageTransition type) OVERRIDE {}
virtual int GetBindings() const OVERRIDE {
return 0;
}
virtual void SetBindings(int bindings) OVERRIDE {}
virtual void OverrideJavaScriptFrame(
const std::string& frame_name) OVERRIDE {}
virtual void AddMessageHandler(
content::WebUIMessageHandler* handler) OVERRIDE {}
virtual void RegisterMessageCallback(
const std::string& message,
const MessageCallback& callback) OVERRIDE {}
virtual void ProcessWebUIMessage(const GURL& source_url,
const std::string& message,
const base::ListValue& args) OVERRIDE {}
virtual void CallJavascriptFunction(const std::string& function_name,
const base::Value& arg1,
const base::Value& arg2,
const base::Value& arg3) OVERRIDE {}
virtual void CallJavascriptFunction(const std::string& function_name,
const base::Value& arg1,
const base::Value& arg2,
const base::Value& arg3,
const base::Value& arg4) OVERRIDE {}
virtual void CallJavascriptFunction(
const std::string& function_name,
const std::vector<const base::Value*>& args) OVERRIDE {}
class CallData {
public:
CallData() : arg1(NULL), arg2(NULL) {}
std::string function_name;
base::Value* arg1;
base::Value* arg2;
};
const std::vector<CallData>& call_data() { return call_data_; }
private:
std::vector<CallData> call_data_;
base::string16 temp_string_;
};
class TestingSyncSetupHandler : public SyncSetupHandler {
public:
TestingSyncSetupHandler(content::WebUI* web_ui, Profile* profile)
: SyncSetupHandler(NULL),
profile_(profile) {
set_web_ui(web_ui);
}
virtual ~TestingSyncSetupHandler() {
set_web_ui(NULL);
}
virtual void FocusUI() OVERRIDE {}
virtual Profile* GetProfile() const OVERRIDE { return profile_; }
using SyncSetupHandler::is_configuring_sync;
private:
#if !defined(OS_CHROMEOS)
virtual void DisplayGaiaLoginInNewTabOrWindow() OVERRIDE {}
#endif
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(TestingSyncSetupHandler);
};
class SyncSetupHandlerTest : public testing::Test {
public:
SyncSetupHandlerTest() : error_(GoogleServiceAuthError::NONE) {}
virtual void SetUp() OVERRIDE {
error_ = GoogleServiceAuthError::AuthErrorNone();
TestingProfile::Builder builder;
builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
FakeSigninManagerBase::Build);
profile_ = builder.Build();
mock_signin_ = static_cast<SigninManagerBase*>(
SigninManagerFactory::GetForProfile(profile_.get()));
mock_signin_->SetAuthenticatedUsername(GetTestUser());
profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
GetTestUser());
mock_pss_ = static_cast<ProfileSyncServiceMock*>(
ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(),
ProfileSyncServiceMock::BuildMockProfileSyncService));
EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error_));
ON_CALL(*mock_pss_, GetPassphraseType()).WillByDefault(
Return(syncer::IMPLICIT_PASSPHRASE));
ON_CALL(*mock_pss_, GetPassphraseTime()).WillByDefault(
Return(base::Time()));
ON_CALL(*mock_pss_, GetExplicitPassphraseTime()).WillByDefault(
Return(base::Time()));
ON_CALL(*mock_pss_, GetRegisteredDataTypes())
.WillByDefault(Return(syncer::ModelTypeSet()));
mock_pss_->Initialize();
handler_.reset(new TestingSyncSetupHandler(&web_ui_, profile_.get()));
}
void SetDefaultExpectationsForConfigPage() {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).
WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, GetRegisteredDataTypes()).
WillRepeatedly(Return(GetAllTypes()));
EXPECT_CALL(*mock_pss_, GetPreferredDataTypes()).
WillRepeatedly(Return(GetAllTypes()));
EXPECT_CALL(*mock_pss_, GetActiveDataTypes()).
WillRepeatedly(Return(GetAllTypes()));
EXPECT_CALL(*mock_pss_, EncryptEverythingEnabled()).
WillRepeatedly(Return(false));
}
void SetupInitializedProfileSyncService() {
ASSERT_TRUE(mock_signin_->IsInitialized());
EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
}
void ExpectConfig() {
ASSERT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
std::string page;
ASSERT_TRUE(data.arg1->GetAsString(&page));
EXPECT_EQ(page, "configure");
}
void ExpectDone() {
ASSERT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
std::string page;
ASSERT_TRUE(data.arg1->GetAsString(&page));
EXPECT_EQ(page, "done");
}
void ExpectSpinnerAndClose() {
EXPECT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
std::string page;
ASSERT_TRUE(data.arg1->GetAsString(&page));
EXPECT_EQ(page, "spinner");
handler_->CloseSyncSetup();
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
}
void NotifySyncListeners() {
if (handler_->sync_startup_tracker_)
handler_->sync_startup_tracker_->OnStateChanged();
}
virtual std::string GetTestUser() {
return std::string(kTestUser);
}
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<Profile> profile_;
ProfileSyncServiceMock* mock_pss_;
GoogleServiceAuthError error_;
SigninManagerBase* mock_signin_;
TestWebUI web_ui_;
scoped_ptr<TestingSyncSetupHandler> handler_;
};
class SyncSetupHandlerFirstSigninTest : public SyncSetupHandlerTest {
virtual std::string GetTestUser() OVERRIDE { return std::string(); }
};
TEST_F(SyncSetupHandlerTest, Basic) {
}
#if !defined(OS_CHROMEOS)
TEST_F(SyncSetupHandlerFirstSigninTest, DisplayBasicLogin) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
SigninManager* manager = static_cast<SigninManager*>(mock_signin_);
manager->SignOut();
handler_->HandleStartSignin(NULL);
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
ASSERT_FALSE(handler_->is_configuring_sync());
handler_->CloseSyncSetup();
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
}
TEST_F(SyncSetupHandlerTest, ShowSyncSetupWhenNotSignedIn) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
handler_->HandleShowSetupUI(NULL);
ASSERT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
ASSERT_FALSE(handler_->is_configuring_sync());
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
}
#endif
TEST_F(SyncSetupHandlerTest, DisplayConfigureWithBackendDisabledAndCancel) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
error_ = GoogleServiceAuthError::AuthErrorNone();
EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
handler_->HandleShowSetupUI(NULL);
EXPECT_EQ(handler_.get(),
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
ExpectSpinnerAndClose();
}
TEST_F(SyncSetupHandlerTest,
DisplayConfigureWithBackendDisabledAndSyncStartupCompleted) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
error_ = GoogleServiceAuthError::AuthErrorNone();
EXPECT_CALL(*mock_pss_, sync_initialized())
.WillRepeatedly(Return(false));
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
EXPECT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data0 = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data0.function_name);
std::string page;
ASSERT_TRUE(data0.arg1->GetAsString(&page));
EXPECT_EQ(page, "spinner");
Mock::VerifyAndClearExpectations(mock_pss_);
SetDefaultExpectationsForConfigPage();
EXPECT_CALL(*mock_pss_, sync_initialized())
.WillRepeatedly(Return(true));
error_ = GoogleServiceAuthError::AuthErrorNone();
EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error_));
NotifySyncListeners();
EXPECT_EQ(2U, web_ui_.call_data().size());
const TestWebUI::CallData& data1 = web_ui_.call_data().back();
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data1.function_name);
ASSERT_TRUE(data1.arg1->GetAsString(&page));
EXPECT_EQ(page, "configure");
base::DictionaryValue* dictionary;
ASSERT_TRUE(data1.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "passphraseFailed", false);
CheckBool(dictionary, "showSyncEverythingPage", false);
CheckBool(dictionary, "syncAllDataTypes", true);
CheckBool(dictionary, "encryptAllData", false);
CheckBool(dictionary, "usePassphrase", false);
}
TEST_F(SyncSetupHandlerTest,
DisplayConfigureWithBackendDisabledAndCancelAfterSigninSuccess) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
error_ = GoogleServiceAuthError::AuthErrorNone();
EXPECT_CALL(*mock_pss_, sync_initialized())
.WillOnce(Return(false))
.WillRepeatedly(Return(true));
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
testing::InSequence seq;
EXPECT_CALL(*mock_pss_, DisableForUser());
EXPECT_CALL(*mock_pss_, SetSetupInProgress(false));
handler_->CloseSyncSetup();
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
}
TEST_F(SyncSetupHandlerTest,
DisplayConfigureWithBackendDisabledAndSigninFailed) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
error_ = GoogleServiceAuthError::AuthErrorNone();
EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
handler_->OpenSyncSetup();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
std::string page;
ASSERT_TRUE(data.arg1->GetAsString(&page));
EXPECT_EQ(page, "spinner");
Mock::VerifyAndClearExpectations(mock_pss_);
error_ = GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error_));
NotifySyncListeners();
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
}
#if !defined(OS_CHROMEOS)
class SyncSetupHandlerNonCrosTest : public SyncSetupHandlerTest {
public:
SyncSetupHandlerNonCrosTest() {}
};
TEST_F(SyncSetupHandlerNonCrosTest, HandleGaiaAuthFailure) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasUnrecoverableError())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
handler_->OpenSyncSetup();
ASSERT_FALSE(handler_->is_configuring_sync());
}
TEST_F(SyncSetupHandlerNonCrosTest, UnrecoverableErrorInitializingSync) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
handler_->OpenSyncSetup();
ASSERT_FALSE(handler_->is_configuring_sync());
}
TEST_F(SyncSetupHandlerNonCrosTest, GaiaErrorInitializingSync) {
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, HasSyncSetupCompleted())
.WillRepeatedly(Return(false));
handler_->OpenSyncSetup();
ASSERT_FALSE(handler_->is_configuring_sync());
}
#endif
TEST_F(SyncSetupHandlerTest, TestSyncEverything) {
std::string args = GetConfiguration(
NULL, SYNC_ALL_DATA, GetAllTypes(), std::string(), ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(true, _));
handler_->HandleConfigure(&list_args);
ExpectDone();
}
TEST_F(SyncSetupHandlerTest, TestSyncNothing) {
std::string args = GetConfiguration(
NULL, SYNC_NOTHING, GetAllTypes(), std::string(), ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, DisableForUser());
SetupInitializedProfileSyncService();
handler_->HandleConfigure(&list_args);
ASSERT_EQ(1U, web_ui_.call_data().size());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
EXPECT_EQ("SyncSetupOverlay.showSyncSetupPage", data.function_name);
}
TEST_F(SyncSetupHandlerTest, TurnOnEncryptAll) {
std::string args = GetConfiguration(
NULL, SYNC_ALL_DATA, GetAllTypes(), std::string(), ENCRYPT_ALL_DATA);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, EnableEncryptEverything());
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(true, _));
handler_->HandleConfigure(&list_args);
ExpectDone();
}
TEST_F(SyncSetupHandlerTest, TestPassphraseStillRequired) {
std::string args = GetConfiguration(
NULL, SYNC_ALL_DATA, GetAllTypes(), std::string(), ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(_, _));
SetDefaultExpectationsForConfigPage();
handler_->HandleConfigure(&list_args);
ExpectConfig();
}
TEST_F(SyncSetupHandlerTest, SuccessfullySetPassphrase) {
base::DictionaryValue dict;
dict.SetBoolean("isGooglePassphrase", true);
std::string args = GetConfiguration(&dict,
SYNC_ALL_DATA,
GetAllTypes(),
"gaiaPassphrase",
ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired()).WillOnce(Return(true));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(_, _));
EXPECT_CALL(*mock_pss_, SetDecryptionPassphrase("gaiaPassphrase")).
WillOnce(Return(true));
handler_->HandleConfigure(&list_args);
ExpectDone();
}
TEST_F(SyncSetupHandlerTest, SelectCustomEncryption) {
base::DictionaryValue dict;
dict.SetBoolean("isGooglePassphrase", false);
std::string args = GetConfiguration(&dict,
SYNC_ALL_DATA,
GetAllTypes(),
"custom_passphrase",
ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(_, _));
EXPECT_CALL(*mock_pss_,
SetEncryptionPassphrase("custom_passphrase",
ProfileSyncService::EXPLICIT));
handler_->HandleConfigure(&list_args);
ExpectDone();
}
TEST_F(SyncSetupHandlerTest, UnsuccessfullySetPassphrase) {
base::DictionaryValue dict;
dict.SetBoolean("isGooglePassphrase", true);
std::string args = GetConfiguration(&dict,
SYNC_ALL_DATA,
GetAllTypes(),
"invalid_passphrase",
ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_, OnUserChoseDatatypes(_, _));
EXPECT_CALL(*mock_pss_, SetDecryptionPassphrase("invalid_passphrase")).
WillOnce(Return(false));
SetDefaultExpectationsForConfigPage();
handler_->HandleConfigure(&list_args);
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "passphraseFailed", true);
}
TEST_F(SyncSetupHandlerTest, TestSyncIndividualTypes) {
syncer::ModelTypeSet user_selectable_types = GetAllTypes();
syncer::ModelTypeSet::Iterator it;
for (it = user_selectable_types.First(); it.Good(); it.Inc()) {
syncer::ModelTypeSet type_to_set;
type_to_set.Put(it.Get());
std::string args = GetConfiguration(NULL,
CHOOSE_WHAT_TO_SYNC,
type_to_set,
std::string(),
ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_,
OnUserChoseDatatypes(false, ModelTypeSetMatches(type_to_set)));
handler_->HandleConfigure(&list_args);
ExpectDone();
Mock::VerifyAndClearExpectations(mock_pss_);
web_ui_.ClearTrackedCalls();
}
}
TEST_F(SyncSetupHandlerTest, TestSyncAllManually) {
std::string args = GetConfiguration(NULL,
CHOOSE_WHAT_TO_SYNC,
GetAllTypes(),
std::string(),
ENCRYPT_PASSWORDS);
base::ListValue list_args;
list_args.Append(new base::StringValue(args));
EXPECT_CALL(*mock_pss_, IsPassphraseRequiredForDecryption())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
EXPECT_CALL(*mock_pss_,
OnUserChoseDatatypes(false, ModelTypeSetMatches(GetAllTypes())));
handler_->HandleConfigure(&list_args);
ExpectDone();
}
TEST_F(SyncSetupHandlerTest, ShowSyncSetup) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
ExpectConfig();
}
TEST_F(SyncSetupHandlerTest, ShowSigninOnAuthError) {
error_ = GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
SetupInitializedProfileSyncService();
mock_signin_->SetAuthenticatedUsername(kTestUser);
FakeAuthStatusProvider provider(
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get())->
signin_error_controller());
provider.SetAuthError(kTestUser, error_);
EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(false));
#if defined(OS_CHROMEOS)
EXPECT_EQ(NULL, LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
#else
handler_->OpenSyncSetup();
EXPECT_EQ(NULL,
LoginUIServiceFactory::GetForProfile(
profile_.get())->current_login_ui());
ASSERT_FALSE(handler_->is_configuring_sync());
#endif
}
TEST_F(SyncSetupHandlerTest, ShowSetupSyncEverything) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "showSyncEverythingPage", false);
CheckBool(dictionary, "syncAllDataTypes", true);
CheckBool(dictionary, "appsRegistered", true);
CheckBool(dictionary, "autofillRegistered", true);
CheckBool(dictionary, "bookmarksRegistered", true);
CheckBool(dictionary, "extensionsRegistered", true);
CheckBool(dictionary, "passwordsRegistered", true);
CheckBool(dictionary, "preferencesRegistered", true);
CheckBool(dictionary, "tabsRegistered", true);
CheckBool(dictionary, "themesRegistered", true);
CheckBool(dictionary, "typedUrlsRegistered", true);
CheckBool(dictionary, "showPassphrase", false);
CheckBool(dictionary, "usePassphrase", false);
CheckBool(dictionary, "passphraseFailed", false);
CheckBool(dictionary, "encryptAllData", false);
CheckConfigDataTypeArguments(dictionary, SYNC_ALL_DATA, GetAllTypes());
}
TEST_F(SyncSetupHandlerTest, ShowSetupManuallySyncAll) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
sync_driver::SyncPrefs sync_prefs(profile_->GetPrefs());
sync_prefs.SetKeepEverythingSynced(false);
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckConfigDataTypeArguments(dictionary, CHOOSE_WHAT_TO_SYNC, GetAllTypes());
}
TEST_F(SyncSetupHandlerTest, ShowSetupSyncForAllTypesIndividually) {
syncer::ModelTypeSet user_selectable_types = GetAllTypes();
syncer::ModelTypeSet::Iterator it;
for (it = user_selectable_types.First(); it.Good(); it.Inc()) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
sync_driver::SyncPrefs sync_prefs(profile_->GetPrefs());
sync_prefs.SetKeepEverythingSynced(false);
SetDefaultExpectationsForConfigPage();
syncer::ModelTypeSet types;
types.Put(it.Get());
EXPECT_CALL(*mock_pss_, GetPreferredDataTypes()).
WillRepeatedly(Return(types));
handler_->OpenSyncSetup();
ExpectConfig();
LoginUIServiceFactory::GetForProfile(profile_.get())->LoginUIClosed(
handler_.get());
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckConfigDataTypeArguments(dictionary, CHOOSE_WHAT_TO_SYNC, types);
Mock::VerifyAndClearExpectations(mock_pss_);
web_ui_.ClearTrackedCalls();
}
}
TEST_F(SyncSetupHandlerTest, ShowSetupGaiaPassphraseRequired) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "showPassphrase", true);
CheckBool(dictionary, "usePassphrase", false);
CheckBool(dictionary, "passphraseFailed", false);
}
TEST_F(SyncSetupHandlerTest, ShowSetupCustomPassphraseRequired) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(true));
EXPECT_CALL(*mock_pss_, GetPassphraseType())
.WillRepeatedly(Return(syncer::CUSTOM_PASSPHRASE));
SetupInitializedProfileSyncService();
SetDefaultExpectationsForConfigPage();
handler_->OpenSyncSetup();
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "showPassphrase", true);
CheckBool(dictionary, "usePassphrase", true);
CheckBool(dictionary, "passphraseFailed", false);
}
TEST_F(SyncSetupHandlerTest, ShowSetupEncryptAll) {
EXPECT_CALL(*mock_pss_, IsPassphraseRequired())
.WillRepeatedly(Return(false));
EXPECT_CALL(*mock_pss_, IsUsingSecondaryPassphrase())
.WillRepeatedly(Return(false));
SetupInitializedProfileSyncService();
SetDefaultExpectationsForConfigPage();
EXPECT_CALL(*mock_pss_, EncryptEverythingEnabled()).
WillRepeatedly(Return(true));
handler_->OpenSyncSetup();
ExpectConfig();
const TestWebUI::CallData& data = web_ui_.call_data()[0];
base::DictionaryValue* dictionary;
ASSERT_TRUE(data.arg2->GetAsDictionary(&dictionary));
CheckBool(dictionary, "encryptAllData", true);
}