1 // Copyright 2013 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_service.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/ui/autofill/account_chooser_model.h"
8 #include "chrome/common/pref_names.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/autofill/core/browser/autofill_metrics.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16
;
20 class TestAccountChooserModel
: public AccountChooserModel
{
22 TestAccountChooserModel(AccountChooserModelDelegate
* delegate
,
25 : AccountChooserModel(delegate
, profile
, disable_wallet
) {}
26 ~TestAccountChooserModel() override
{}
28 using AccountChooserModel::kWalletAccountsStartId
;
29 using AccountChooserModel::kWalletAddAccountId
;
30 using AccountChooserModel::kAutofillItemId
;
33 DISALLOW_COPY_AND_ASSIGN(TestAccountChooserModel
);
36 class MockAccountChooserModelDelegate
: public AccountChooserModelDelegate
{
38 MockAccountChooserModelDelegate() {}
39 virtual ~MockAccountChooserModelDelegate() {}
41 MOCK_METHOD0(AccountChoiceChanged
, void());
42 MOCK_METHOD0(AddAccount
, void());
43 MOCK_METHOD0(UpdateAccountChooserView
, void());
46 class AccountChooserModelTest
: public testing::Test
{
48 AccountChooserModelTest() : model_(&delegate_
, &profile_
, false) {}
49 virtual ~AccountChooserModelTest() {}
51 TestingProfile
* profile() { return &profile_
; }
52 MockAccountChooserModelDelegate
* delegate() { return &delegate_
; }
53 TestAccountChooserModel
* model() { return &model_
; }
56 TestingProfile profile_
;
57 testing::NiceMock
<MockAccountChooserModelDelegate
> delegate_
;
58 TestAccountChooserModel model_
;
63 TEST_F(AccountChooserModelTest
, ObeysPref
) {
64 // When "Pay without wallet" is false, use Wallet by default.
66 profile()->GetPrefs()->SetBoolean(
67 ::prefs::kAutofillDialogPayWithoutWallet
, false);
68 TestAccountChooserModel
model(delegate(), profile(), false);
69 EXPECT_TRUE(model
.WalletIsSelected());
71 // When the user chose to "Pay without wallet", use Autofill.
73 profile()->GetPrefs()->SetBoolean(
74 ::prefs::kAutofillDialogPayWithoutWallet
, true);
75 TestAccountChooserModel
model(delegate(), profile(), false);
76 EXPECT_FALSE(model
.WalletIsSelected());
78 // When the |disable_wallet| argument is true, use Autofill regardless
81 profile()->GetPrefs()->SetBoolean(
82 ::prefs::kAutofillDialogPayWithoutWallet
, false);
83 TestAccountChooserModel
model(delegate(), profile(), true);
84 EXPECT_FALSE(model
.WalletIsSelected());
86 // In incognito, use local data regardless of the pref.
88 Profile
* incognito
= profile()->GetOffTheRecordProfile();
89 profile()->GetPrefs()->SetBoolean(
90 ::prefs::kAutofillDialogPayWithoutWallet
, false);
91 incognito
->GetPrefs()->SetBoolean(
92 ::prefs::kAutofillDialogPayWithoutWallet
, false);
94 TestAccountChooserModel
model(delegate(), incognito
, false);
95 EXPECT_FALSE(model
.WalletIsSelected());
99 TEST_F(AccountChooserModelTest
, IgnoresPrefChanges
) {
100 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
101 ::prefs::kAutofillDialogPayWithoutWallet
));
102 EXPECT_TRUE(model()->WalletIsSelected());
104 // Check that nothing changes while this dialog is running if a pref changes
105 // (this could cause subtle bugs or annoyances if a user closes another
107 profile()->GetPrefs()->SetBoolean(
108 ::prefs::kAutofillDialogPayWithoutWallet
, true);
109 EXPECT_TRUE(model()->WalletIsSelected());
112 TEST_F(AccountChooserModelTest
, HandlesError
) {
113 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
114 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1);
116 ASSERT_TRUE(model()->WalletIsSelected());
117 ASSERT_TRUE(model()->IsCommandIdEnabled(
118 TestAccountChooserModel::kWalletAccountsStartId
));
120 model()->SetHadWalletError();
121 EXPECT_FALSE(model()->WalletIsSelected());
122 EXPECT_FALSE(model()->IsCommandIdEnabled(
123 TestAccountChooserModel::kWalletAccountsStartId
));
126 TEST_F(AccountChooserModelTest
, HandlesSigninError
) {
127 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
128 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2);
130 // 0. "Unknown" wallet account, we don't know if the user is signed-in yet.
131 ASSERT_TRUE(model()->WalletIsSelected());
132 ASSERT_TRUE(model()->IsCommandIdEnabled(
133 TestAccountChooserModel::kWalletAccountsStartId
));
134 ASSERT_TRUE(model()->WalletIsSelected());
135 ASSERT_FALSE(model()->HasAccountsToChoose());
136 ASSERT_EQ(3, model()->GetItemCount());
137 EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());
139 // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in).
140 // Calls UpdateAccountChooserView.
141 std::vector
<std::string
> accounts
;
142 accounts
.push_back("john.doe@gmail.com");
143 model()->SetWalletAccounts(accounts
, 0U);
144 ASSERT_TRUE(model()->WalletIsSelected());
145 ASSERT_TRUE(model()->IsCommandIdEnabled(
146 TestAccountChooserModel::kWalletAccountsStartId
));
147 ASSERT_TRUE(model()->WalletIsSelected());
148 ASSERT_TRUE(model()->HasAccountsToChoose());
149 EXPECT_EQ(3, model()->GetItemCount());
150 EXPECT_EQ(ASCIIToUTF16(accounts
[0]), model()->GetActiveWalletAccountName());
152 // 2. Sign-in failure.
153 // Autofill data should be selected and be the only valid choice.
154 // Calls UpdateAccountChooserView.
155 // Calls AccountChoiceChanged.
156 model()->SetHadWalletSigninError();
157 EXPECT_FALSE(model()->WalletIsSelected());
158 EXPECT_TRUE(model()->IsCommandIdEnabled(
159 TestAccountChooserModel::kWalletAccountsStartId
));
160 EXPECT_FALSE(model()->WalletIsSelected());
161 EXPECT_FALSE(model()->HasAccountsToChoose());
162 EXPECT_EQ(2, model()->GetItemCount());
163 EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());
166 TEST_F(AccountChooserModelTest
, RespectsUserChoice
) {
167 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);
169 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId
, 0);
170 EXPECT_FALSE(model()->WalletIsSelected());
172 EXPECT_CALL(*delegate(), AddAccount());
173 model()->ExecuteCommand(TestAccountChooserModel::kWalletAddAccountId
, 0);
174 EXPECT_FALSE(model()->WalletIsSelected());
176 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId
, 0);
177 EXPECT_TRUE(model()->WalletIsSelected());
180 TEST_F(AccountChooserModelTest
, HandlesMultipleAccounts
) {
181 EXPECT_FALSE(model()->HasAccountsToChoose());
183 std::vector
<std::string
> accounts
;
184 accounts
.push_back("john.doe@gmail.com");
185 accounts
.push_back("jane.smith@gmail.com");
186 model()->SetWalletAccounts(accounts
, 0U);
187 EXPECT_TRUE(model()->HasAccountsToChoose());
188 EXPECT_TRUE(model()->WalletIsSelected());
189 ASSERT_EQ(4, model()->GetItemCount());
190 EXPECT_TRUE(model()->IsCommandIdEnabled(
191 TestAccountChooserModel::kWalletAccountsStartId
));
192 EXPECT_TRUE(model()->IsCommandIdEnabled(
193 TestAccountChooserModel::kWalletAccountsStartId
+ 1));
194 EXPECT_EQ(ASCIIToUTF16(accounts
[0]), model()->GetActiveWalletAccountName());
195 model()->SetWalletAccounts(accounts
, 1U);
196 EXPECT_EQ(ASCIIToUTF16(accounts
[1]), model()->GetActiveWalletAccountName());
198 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId
, 0);
199 EXPECT_EQ(ASCIIToUTF16(accounts
[0]), model()->GetActiveWalletAccountName());
200 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId
+ 1,
202 EXPECT_EQ(ASCIIToUTF16(accounts
[1]), model()->GetActiveWalletAccountName());
204 // Setting the wallet accounts forces the switch to wallet.
205 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId
, 0);
206 EXPECT_FALSE(model()->WalletIsSelected());
207 model()->SetWalletAccounts(accounts
, 1U);
208 EXPECT_TRUE(model()->WalletIsSelected());
211 } // namespace autofill