Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / account_chooser_model_unittest.cc
blobe2f9b5c7f86aba481ace347c3655a30609fba111
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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16;
17 namespace autofill {
19 namespace {
21 class TestAccountChooserModel : public AccountChooserModel {
22 public:
23 TestAccountChooserModel(AccountChooserModelDelegate* delegate,
24 Profile* profile,
25 bool disable_wallet)
26 : AccountChooserModel(delegate, profile, disable_wallet) {}
27 ~TestAccountChooserModel() override {}
29 using AccountChooserModel::kWalletAccountsStartId;
30 using AccountChooserModel::kWalletAddAccountId;
31 using AccountChooserModel::kAutofillItemId;
33 private:
34 DISALLOW_COPY_AND_ASSIGN(TestAccountChooserModel);
37 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate {
38 public:
39 MockAccountChooserModelDelegate() {}
40 virtual ~MockAccountChooserModelDelegate() {}
42 MOCK_METHOD0(AccountChoiceChanged, void());
43 MOCK_METHOD0(AddAccount, void());
44 MOCK_METHOD0(UpdateAccountChooserView, void());
47 class AccountChooserModelTest : public testing::Test {
48 public:
49 AccountChooserModelTest() : model_(&delegate_, &profile_, false) {}
50 virtual ~AccountChooserModelTest() {}
52 TestingProfile* profile() { return &profile_; }
53 MockAccountChooserModelDelegate* delegate() { return &delegate_; }
54 TestAccountChooserModel* model() { return &model_; }
56 private:
57 content::TestBrowserThreadBundle thread_bundle_;
58 TestingProfile profile_;
59 testing::NiceMock<MockAccountChooserModelDelegate> delegate_;
60 TestAccountChooserModel model_;
63 } // namespace
65 TEST_F(AccountChooserModelTest, ObeysPref) {
66 // When "Pay without wallet" is false, use Wallet by default.
68 profile()->GetPrefs()->SetBoolean(
69 ::prefs::kAutofillDialogPayWithoutWallet, false);
70 TestAccountChooserModel model(delegate(), profile(), false);
71 EXPECT_TRUE(model.WalletIsSelected());
73 // When the user chose to "Pay without wallet", use Autofill.
75 profile()->GetPrefs()->SetBoolean(
76 ::prefs::kAutofillDialogPayWithoutWallet, true);
77 TestAccountChooserModel model(delegate(), profile(), false);
78 EXPECT_FALSE(model.WalletIsSelected());
80 // When the |disable_wallet| argument is true, use Autofill regardless
81 // of the pref.
83 profile()->GetPrefs()->SetBoolean(
84 ::prefs::kAutofillDialogPayWithoutWallet, false);
85 TestAccountChooserModel model(delegate(), profile(), true);
86 EXPECT_FALSE(model.WalletIsSelected());
88 // In incognito, use local data regardless of the pref.
90 Profile* incognito = profile()->GetOffTheRecordProfile();
91 profile()->GetPrefs()->SetBoolean(
92 ::prefs::kAutofillDialogPayWithoutWallet, false);
93 incognito->GetPrefs()->SetBoolean(
94 ::prefs::kAutofillDialogPayWithoutWallet, false);
96 TestAccountChooserModel model(delegate(), incognito, false);
97 EXPECT_FALSE(model.WalletIsSelected());
101 TEST_F(AccountChooserModelTest, IgnoresPrefChanges) {
102 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
103 ::prefs::kAutofillDialogPayWithoutWallet));
104 EXPECT_TRUE(model()->WalletIsSelected());
106 // Check that nothing changes while this dialog is running if a pref changes
107 // (this could cause subtle bugs or annoyances if a user closes another
108 // running dialog).
109 profile()->GetPrefs()->SetBoolean(
110 ::prefs::kAutofillDialogPayWithoutWallet, true);
111 EXPECT_TRUE(model()->WalletIsSelected());
114 TEST_F(AccountChooserModelTest, HandlesError) {
115 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
116 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1);
118 ASSERT_TRUE(model()->WalletIsSelected());
119 ASSERT_TRUE(model()->IsCommandIdEnabled(
120 TestAccountChooserModel::kWalletAccountsStartId));
122 model()->SetHadWalletError();
123 EXPECT_FALSE(model()->WalletIsSelected());
124 EXPECT_FALSE(model()->IsCommandIdEnabled(
125 TestAccountChooserModel::kWalletAccountsStartId));
128 TEST_F(AccountChooserModelTest, HandlesSigninError) {
129 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
130 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2);
132 // 0. "Unknown" wallet account, we don't know if the user is signed-in yet.
133 ASSERT_TRUE(model()->WalletIsSelected());
134 ASSERT_TRUE(model()->IsCommandIdEnabled(
135 TestAccountChooserModel::kWalletAccountsStartId));
136 ASSERT_TRUE(model()->WalletIsSelected());
137 ASSERT_FALSE(model()->HasAccountsToChoose());
138 ASSERT_EQ(3, model()->GetItemCount());
139 EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());
141 // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in).
142 // Calls UpdateAccountChooserView.
143 std::vector<std::string> accounts;
144 accounts.push_back("john.doe@gmail.com");
145 model()->SetWalletAccounts(accounts, 0U);
146 ASSERT_TRUE(model()->WalletIsSelected());
147 ASSERT_TRUE(model()->IsCommandIdEnabled(
148 TestAccountChooserModel::kWalletAccountsStartId));
149 ASSERT_TRUE(model()->WalletIsSelected());
150 ASSERT_TRUE(model()->HasAccountsToChoose());
151 EXPECT_EQ(3, model()->GetItemCount());
152 EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
154 // 2. Sign-in failure.
155 // Autofill data should be selected and be the only valid choice.
156 // Calls UpdateAccountChooserView.
157 // Calls AccountChoiceChanged.
158 model()->SetHadWalletSigninError();
159 EXPECT_FALSE(model()->WalletIsSelected());
160 EXPECT_TRUE(model()->IsCommandIdEnabled(
161 TestAccountChooserModel::kWalletAccountsStartId));
162 EXPECT_FALSE(model()->WalletIsSelected());
163 EXPECT_FALSE(model()->HasAccountsToChoose());
164 EXPECT_EQ(2, model()->GetItemCount());
165 EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());
168 TEST_F(AccountChooserModelTest, RespectsUserChoice) {
169 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);
171 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0);
172 EXPECT_FALSE(model()->WalletIsSelected());
174 EXPECT_CALL(*delegate(), AddAccount());
175 model()->ExecuteCommand(TestAccountChooserModel::kWalletAddAccountId, 0);
176 EXPECT_FALSE(model()->WalletIsSelected());
178 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId, 0);
179 EXPECT_TRUE(model()->WalletIsSelected());
182 TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) {
183 EXPECT_FALSE(model()->HasAccountsToChoose());
185 std::vector<std::string> accounts;
186 accounts.push_back("john.doe@gmail.com");
187 accounts.push_back("jane.smith@gmail.com");
188 model()->SetWalletAccounts(accounts, 0U);
189 EXPECT_TRUE(model()->HasAccountsToChoose());
190 EXPECT_TRUE(model()->WalletIsSelected());
191 ASSERT_EQ(4, model()->GetItemCount());
192 EXPECT_TRUE(model()->IsCommandIdEnabled(
193 TestAccountChooserModel::kWalletAccountsStartId));
194 EXPECT_TRUE(model()->IsCommandIdEnabled(
195 TestAccountChooserModel::kWalletAccountsStartId + 1));
196 EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
197 model()->SetWalletAccounts(accounts, 1U);
198 EXPECT_EQ(ASCIIToUTF16(accounts[1]), model()->GetActiveWalletAccountName());
200 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId, 0);
201 EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
202 model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId + 1,
204 EXPECT_EQ(ASCIIToUTF16(accounts[1]), model()->GetActiveWalletAccountName());
206 // Setting the wallet accounts forces the switch to wallet.
207 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0);
208 EXPECT_FALSE(model()->WalletIsSelected());
209 model()->SetWalletAccounts(accounts, 1U);
210 EXPECT_TRUE(model()->WalletIsSelected());
213 } // namespace autofill