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/fake_sync_service.h"
13 #include "components/sync_driver/sync_service.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "sync/internal_api/public/engine/sync_status.h"
16 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
17 #include "testing/gmock/include/gmock/gmock.h"
19 using testing::Return
;
25 class TestSyncService
: public sync_driver::FakeSyncService
{
27 TestSyncService(syncer::ModelTypeSet type_set
,
29 : type_set_(type_set
),
30 can_sync_start_(can_sync_start
) {}
31 ~TestSyncService() override
{}
33 // FakeSyncService overrides.
34 bool IsSyncAllowed() const override
{ return true; }
35 syncer::ModelTypeSet
GetActiveDataTypes() const override
{
38 syncer::ModelTypeSet
GetPreferredDataTypes() const override
{
41 bool CanSyncStart() const override
{ return can_sync_start_
; }
44 syncer::ModelTypeSet type_set_
;
48 scoped_ptr
<TestSyncService
> CreateSyncService(bool has_autofill_profile
,
49 bool has_autofill_wallet_data
,
50 bool is_enabled_and_logged_in
) {
51 syncer::ModelTypeSet type_set
;
52 if (has_autofill_profile
)
53 type_set
.Put(syncer::AUTOFILL_PROFILE
);
54 if (has_autofill_wallet_data
)
55 type_set
.Put(syncer::AUTOFILL_WALLET_DATA
);
56 return make_scoped_ptr(
57 new TestSyncService(type_set
, is_enabled_and_logged_in
));
60 scoped_ptr
<TestingPrefServiceSimple
> CreatePrefService(
61 bool autofill_enabled
,
62 bool autofill_wallet_import_enabled
,
63 bool autofill_wallet_sync_experiment_enabled
) {
64 scoped_ptr
<TestingPrefServiceSimple
> prefs(new TestingPrefServiceSimple());
65 prefs
->registry()->RegisterBooleanPref(prefs::kAutofillEnabled
,
67 prefs
->registry()->RegisterBooleanPref(prefs::kAutofillWalletImportEnabled
,
68 autofill_wallet_import_enabled
);
69 prefs
->registry()->RegisterBooleanPref(
70 prefs::kAutofillWalletSyncExperimentEnabled
,
71 autofill_wallet_sync_experiment_enabled
);
76 scoped_ptr
<TestPersonalDataManager
> CreatePersonalDataManager(
78 bool has_server_data
) {
79 scoped_ptr
<TestPersonalDataManager
> pdm(new TestPersonalDataManager());
80 pdm
->SetTestingPrefService(prefs
);
81 if (has_server_data
) {
82 // This will cause pdm->HasServerData() to return true.
83 pdm
->AddTestingServerCreditCard(test::GetVerifiedCreditCard());
91 // Verify that true is returned when all inputs are complete.
92 TEST(WalletIntegrationAvailableTest
, AllInputsComplete
) {
93 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(true, true, true);
94 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
95 CreatePrefService(true, true, true);
96 scoped_ptr
<TestPersonalDataManager
> pdm
=
97 CreatePersonalDataManager(prefs
.get(), true);
99 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
102 // Verify that false is returned when SyncService is missing or incomplete.
103 TEST(WalletIntegrationAvailableTest
, MissingOrIncompleteSyncService
) {
104 // Setup |prefs| and |pdm| to do their part to make
105 // WalletIntegrationAvailable() return true.
106 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
107 CreatePrefService(true, true, true);
108 scoped_ptr
<TestPersonalDataManager
> pdm
=
109 CreatePersonalDataManager(prefs
.get(), true);
111 // Incomplete SyncService data should return false.
112 EXPECT_FALSE(WalletIntegrationAvailable(NULL
, *prefs
, *pdm
));
114 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(false, false, false);
115 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
117 sync
= CreateSyncService(false, false, true);
118 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
120 sync
= CreateSyncService(true, false, false);
121 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
123 // Complete SyncService data should return true.
124 sync
= CreateSyncService(true, true, true);
125 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
128 // Verify that false is returned when
129 // !prefs::kAutofillWalletSyncExperimentEnabled.
130 TEST(WalletIntegrationAvailableTest
, ExperimentalWalletIntegrationDisabled
) {
131 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(true, true, true);
132 // Set kAutofillWalletSyncExperimentEnabled to false.
133 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
134 CreatePrefService(true, true, false);
135 scoped_ptr
<TestPersonalDataManager
> pdm
=
136 CreatePersonalDataManager(prefs
.get(), true);
138 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
141 // Verify that false is returned if server data is missing.
142 TEST(WalletIntegrationAvailableTest
, NoServerData
) {
143 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(true, true, true);
144 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
145 CreatePrefService(true, true, true);
146 // Set server data as missing.
147 scoped_ptr
<TestPersonalDataManager
> pdm
=
148 CreatePersonalDataManager(prefs
.get(), false);
150 EXPECT_FALSE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
153 // Verify that true is returned when !prefs::kAutofillWalletImportEnabled,
154 // even if server data is missing.
155 TEST(WalletIntegrationAvailableTest
, WalletImportDisabled
) {
156 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(true, true, true);
157 // Set kAutofillWalletImportEnabled to false.
158 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
159 CreatePrefService(true, false, true);
160 // Set server data as missing.
161 scoped_ptr
<TestPersonalDataManager
> pdm
=
162 CreatePersonalDataManager(prefs
.get(), false);
164 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
167 // Verify that true is returned when data hasn't been synced yet, even if
168 // server data is missing.
169 TEST(WalletIntegrationAvailableTest
, WalletDataNotSyncedYet
) {
170 // Set wallet data as not synced yet.
171 scoped_ptr
<TestSyncService
> sync
= CreateSyncService(true, false, true);
172 scoped_ptr
<TestingPrefServiceSimple
> prefs
=
173 CreatePrefService(true, true, true);
174 // Set server data as missing.
175 scoped_ptr
<TestPersonalDataManager
> pdm
=
176 CreatePersonalDataManager(prefs
.get(), false);
178 EXPECT_TRUE(WalletIntegrationAvailable(sync
.get(), *prefs
, *pdm
));
181 } // namespace autofill