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
3 // found in the LICENSE file.
5 #include "base/prefs/pref_registry_simple.h"
6 #include "base/prefs/testing_pref_service.h"
7 #include "components/autofill/core/browser/autofill_test_utils.h"
8 #include "components/autofill/core/browser/options_util.h"
9 #include "components/autofill/core/browser/test_personal_data_manager.h"
10 #include "components/autofill/core/common/autofill_pref_names.h"
11 #include "components/sync_driver/data_type_manager.h"
12 #include "components/sync_driver/sync_service.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "testing/gmock/include/gmock/gmock.h"
16 using testing::Return
;
22 class SyncServiceMock
: public sync_driver::SyncService
{
24 MOCK_CONST_METHOD0(HasSyncSetupCompleted
, bool());
25 MOCK_CONST_METHOD0(IsSyncActive
, bool());
26 MOCK_CONST_METHOD0(IsSyncAllowed
, bool());
27 MOCK_CONST_METHOD0(GetActiveDataTypes
, syncer::ModelTypeSet());
28 MOCK_METHOD1(AddObserver
, void(sync_driver::SyncServiceObserver
*));
29 MOCK_METHOD1(RemoveObserver
, void(sync_driver::SyncServiceObserver
*));
30 MOCK_CONST_METHOD1(HasObserver
,
31 bool(const sync_driver::SyncServiceObserver
*));
32 MOCK_METHOD1(OnDataTypeRequestsSyncStartup
, void(syncer::ModelType type
));
33 MOCK_CONST_METHOD0(CanSyncStart
, bool());
34 MOCK_METHOD1(RequestStop
, void(sync_driver::SyncService::SyncStopDataFate
));
35 MOCK_METHOD0(RequestStart
, void());
36 MOCK_CONST_METHOD0(GetPreferredDataTypes
, syncer::ModelTypeSet());
37 MOCK_METHOD2(OnUserChoseDatatypes
,
38 void(bool sync_everything
, syncer::ModelTypeSet chosen_types
));
39 MOCK_METHOD1(DeactivateDataType
, void(syncer::ModelType type
));
40 MOCK_METHOD0(SetSyncSetupCompleted
, void());
41 MOCK_CONST_METHOD0(FirstSetupInProgress
, bool());
42 MOCK_METHOD1(SetSetupInProgress
, void(bool));
43 MOCK_CONST_METHOD0(setup_in_progress
, bool());
44 MOCK_CONST_METHOD0(ConfigurationDone
, bool());
45 MOCK_CONST_METHOD0(GetAuthError
, const GoogleServiceAuthError
&());
46 MOCK_CONST_METHOD0(HasUnrecoverableError
, bool());
47 MOCK_CONST_METHOD0(backend_initialized
, bool());
48 MOCK_METHOD0(GetOpenTabsUIDelegate
, sync_driver::OpenTabsUIDelegate
*());
49 MOCK_CONST_METHOD0(IsPassphraseRequiredForDecryption
, bool());
50 MOCK_CONST_METHOD0(GetExplicitPassphraseTime
, base::Time());
51 MOCK_CONST_METHOD0(IsUsingSecondaryPassphrase
, bool());
52 MOCK_METHOD0(EnableEncryptEverything
, void());
53 MOCK_METHOD2(SetEncryptionPassphrase
,
54 void(const std::string
& passphrase
, PassphraseType type
));
55 MOCK_METHOD1(SetDecryptionPassphrase
, bool(const std::string
& passphrase
));
56 MOCK_CONST_METHOD1(IsCryptographerReady
,
57 bool(const syncer::BaseTransaction
* trans
));
58 MOCK_CONST_METHOD0(GetUserShare
, syncer::UserShare
*());
60 // DataTypeEncryptionHandler mocks.
61 MOCK_CONST_METHOD0(IsPassphraseRequired
, bool());
62 MOCK_CONST_METHOD0(GetEncryptedDataTypes
, syncer::ModelTypeSet());
65 scoped_ptr
<SyncServiceMock
> CreateSyncService(bool has_autofill_profile
,
66 bool has_autofill_wallet_data
,
67 bool is_enabled_and_logged_in
) {
68 scoped_ptr
<SyncServiceMock
> sync(new SyncServiceMock());
70 ON_CALL(*sync
, IsSyncAllowed()).WillByDefault(Return(true));
72 syncer::ModelTypeSet type_set
;
73 if (has_autofill_profile
)
74 type_set
.Put(syncer::AUTOFILL_PROFILE
);
75 if (has_autofill_wallet_data
)
76 type_set
.Put(syncer::AUTOFILL_WALLET_DATA
);
77 ON_CALL(*sync
, GetActiveDataTypes()).WillByDefault(Return(type_set
));
78 ON_CALL(*sync
, GetPreferredDataTypes()).WillByDefault(Return(type_set
));
80 ON_CALL(*sync
, CanSyncStart())
81 .WillByDefault(Return(is_enabled_and_logged_in
));
86 scoped_ptr
<TestingPrefServiceSimple
> CreatePrefService(
87 bool autofill_enabled
,
88 bool autofill_wallet_import_enabled
,
89 bool autofill_wallet_sync_experiment_enabled
) {
90 scoped_ptr
<TestingPrefServiceSimple
> prefs(new TestingPrefServiceSimple());
91 prefs
->registry()->RegisterBooleanPref(prefs::kAutofillEnabled
,
93 prefs
->registry()->RegisterBooleanPref(prefs::kAutofillWalletImportEnabled
,
94 autofill_wallet_import_enabled
);
95 prefs
->registry()->RegisterBooleanPref(
96 prefs::kAutofillWalletSyncExperimentEnabled
,
97 autofill_wallet_sync_experiment_enabled
);
102 scoped_ptr
<TestPersonalDataManager
> CreatePersonalDataManager(
104 bool has_server_data
) {
105 scoped_ptr
<TestPersonalDataManager
> pdm(new TestPersonalDataManager());
106 pdm
->SetTestingPrefService(prefs
);
107 if (has_server_data
) {
108 // This will cause pdm->HasServerData() to return true.
109 pdm
->AddTestingServerCreditCard(test::GetVerifiedCreditCard());
117 // Verify that true is returned when all inputs are complete.
118 TEST(WalletIntegrationAvailableTest
, AllInputsComplete
) {
119 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(true, true, true);
120 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
121 CreatePrefService(true, true, true);
122 scoped_ptr
<TestPersonalDataManager
> pdm
=
123 CreatePersonalDataManager(prefs
.get(), true);
125 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
128 // Verify that false is returned when SyncService is missing or incomplete.
129 TEST(WalletIntegrationAvailableTest
, MissingOrIncompleteSyncService
) {
130 // Setup |prefs| and |pdm| to do their part to make
131 // WalletIntegrationAvailable() return true.
132 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
133 CreatePrefService(true, true, true);
134 scoped_ptr
<TestPersonalDataManager
> pdm
=
135 CreatePersonalDataManager(prefs
.get(), true);
137 // Incomplete SyncService data should return false.
138 EXPECT_FALSE(WalletIntegrationAvailable(NULL
, *prefs
, *pdm
));
140 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(false, false, false);
141 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
143 sync
= CreateSyncService(false, false, true);
144 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
146 sync
= CreateSyncService(true, false, false);
147 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
149 // Complete SyncService data should return true.
150 sync
= CreateSyncService(true, true, true);
151 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
154 // Verify that false is returned when
155 // !prefs::kAutofillWalletSyncExperimentEnabled.
156 TEST(WalletIntegrationAvailableTest
, ExperimentalWalletIntegrationDisabled
) {
157 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(true, true, true);
158 // Set kAutofillWalletSyncExperimentEnabled to false.
159 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
160 CreatePrefService(true, true, false);
161 scoped_ptr
<TestPersonalDataManager
> pdm
=
162 CreatePersonalDataManager(prefs
.get(), true);
164 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
167 // Verify that false is returned if server data is missing.
168 TEST(WalletIntegrationAvailableTest
, NoServerData
) {
169 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(true, true, true);
170 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
171 CreatePrefService(true, true, true);
172 // Set server data as missing.
173 scoped_ptr
<TestPersonalDataManager
> pdm
=
174 CreatePersonalDataManager(prefs
.get(), false);
176 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
179 // Verify that true is returned when !prefs::kAutofillWalletImportEnabled,
180 // even if server data is missing.
181 TEST(WalletIntegrationAvailableTest
, WalletImportDisabled
) {
182 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(true, true, true);
183 // Set kAutofillWalletImportEnabled to false.
184 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
185 CreatePrefService(true, false, true);
186 // Set server data as missing.
187 scoped_ptr
<TestPersonalDataManager
> pdm
=
188 CreatePersonalDataManager(prefs
.get(), false);
190 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
193 // Verify that true is returned when data hasn't been synced yet, even if
194 // server data is missing.
195 TEST(WalletIntegrationAvailableTest
, WalletDataNotSyncedYet
) {
196 // Set wallet data as not synced yet.
197 scoped_ptr
<SyncServiceMock
> sync
= CreateSyncService(true, false, true);
198 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
199 CreatePrefService(true, true, true);
200 // Set server data as missing.
201 scoped_ptr
<TestPersonalDataManager
> pdm
=
202 CreatePersonalDataManager(prefs
.get(), false);
204 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
207 } // namespace autofill