summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/autofill/account_chooser_model_unittest.cc
blob: e2f9b5c7f86aba481ace347c3655a30609fba111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/autofill/account_chooser_model.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/browser/autofill_metrics.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;

namespace autofill {

namespace {

class TestAccountChooserModel : public AccountChooserModel {
 public:
  TestAccountChooserModel(AccountChooserModelDelegate* delegate,
                          Profile* profile,
                          bool disable_wallet)
      : AccountChooserModel(delegate, profile, disable_wallet) {}
  ~TestAccountChooserModel() override {}

  using AccountChooserModel::kWalletAccountsStartId;
  using AccountChooserModel::kWalletAddAccountId;
  using AccountChooserModel::kAutofillItemId;

 private:
  DISALLOW_COPY_AND_ASSIGN(TestAccountChooserModel);
};

class MockAccountChooserModelDelegate : public AccountChooserModelDelegate {
 public:
  MockAccountChooserModelDelegate() {}
  virtual ~MockAccountChooserModelDelegate() {}

  MOCK_METHOD0(AccountChoiceChanged, void());
  MOCK_METHOD0(AddAccount, void());
  MOCK_METHOD0(UpdateAccountChooserView, void());
};

class AccountChooserModelTest : public testing::Test {
 public:
  AccountChooserModelTest() : model_(&delegate_, &profile_, false) {}
  virtual ~AccountChooserModelTest() {}

  TestingProfile* profile() { return &profile_; }
  MockAccountChooserModelDelegate* delegate() { return &delegate_; }
  TestAccountChooserModel* model() { return &model_; }

 private:
  content::TestBrowserThreadBundle thread_bundle_;
  TestingProfile profile_;
  testing::NiceMock<MockAccountChooserModelDelegate> delegate_;
  TestAccountChooserModel model_;
};

}  // namespace

TEST_F(AccountChooserModelTest, ObeysPref) {
  // When "Pay without wallet" is false, use Wallet by default.
  {
    profile()->GetPrefs()->SetBoolean(
        ::prefs::kAutofillDialogPayWithoutWallet, false);
    TestAccountChooserModel model(delegate(), profile(), false);
    EXPECT_TRUE(model.WalletIsSelected());
  }
  // When the user chose to "Pay without wallet", use Autofill.
  {
    profile()->GetPrefs()->SetBoolean(
        ::prefs::kAutofillDialogPayWithoutWallet, true);
    TestAccountChooserModel model(delegate(), profile(), false);
    EXPECT_FALSE(model.WalletIsSelected());
  }
  // When the |disable_wallet| argument is true, use Autofill regardless
  // of the pref.
  {
    profile()->GetPrefs()->SetBoolean(
        ::prefs::kAutofillDialogPayWithoutWallet, false);
    TestAccountChooserModel model(delegate(), profile(), true);
    EXPECT_FALSE(model.WalletIsSelected());
  }
  // In incognito, use local data regardless of the pref.
  {
    Profile* incognito = profile()->GetOffTheRecordProfile();
    profile()->GetPrefs()->SetBoolean(
        ::prefs::kAutofillDialogPayWithoutWallet, false);
    incognito->GetPrefs()->SetBoolean(
        ::prefs::kAutofillDialogPayWithoutWallet, false);

    TestAccountChooserModel model(delegate(), incognito, false);
    EXPECT_FALSE(model.WalletIsSelected());
  }
}

TEST_F(AccountChooserModelTest, IgnoresPrefChanges) {
  ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet));
  EXPECT_TRUE(model()->WalletIsSelected());

  // Check that nothing changes while this dialog is running if a pref changes
  // (this could cause subtle bugs or annoyances if a user closes another
  // running dialog).
  profile()->GetPrefs()->SetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet, true);
  EXPECT_TRUE(model()->WalletIsSelected());
}

TEST_F(AccountChooserModelTest, HandlesError) {
  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
  EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1);

  ASSERT_TRUE(model()->WalletIsSelected());
  ASSERT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));

  model()->SetHadWalletError();
  EXPECT_FALSE(model()->WalletIsSelected());
  EXPECT_FALSE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));
}

TEST_F(AccountChooserModelTest, HandlesSigninError) {
  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
  EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2);

  // 0. "Unknown" wallet account, we don't know if the user is signed-in yet.
  ASSERT_TRUE(model()->WalletIsSelected());
  ASSERT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));
  ASSERT_TRUE(model()->WalletIsSelected());
  ASSERT_FALSE(model()->HasAccountsToChoose());
  ASSERT_EQ(3, model()->GetItemCount());
  EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());

  // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in).
  // Calls UpdateAccountChooserView.
  std::vector<std::string> accounts;
  accounts.push_back("john.doe@gmail.com");
  model()->SetWalletAccounts(accounts, 0U);
  ASSERT_TRUE(model()->WalletIsSelected());
  ASSERT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));
  ASSERT_TRUE(model()->WalletIsSelected());
  ASSERT_TRUE(model()->HasAccountsToChoose());
  EXPECT_EQ(3, model()->GetItemCount());
  EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());

  // 2. Sign-in failure.
  // Autofill data should be selected and be the only valid choice.
  // Calls UpdateAccountChooserView.
  // Calls AccountChoiceChanged.
  model()->SetHadWalletSigninError();
  EXPECT_FALSE(model()->WalletIsSelected());
  EXPECT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));
  EXPECT_FALSE(model()->WalletIsSelected());
  EXPECT_FALSE(model()->HasAccountsToChoose());
  EXPECT_EQ(2, model()->GetItemCount());
  EXPECT_EQ(base::string16(), model()->GetActiveWalletAccountName());
}

TEST_F(AccountChooserModelTest, RespectsUserChoice) {
  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);

  model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0);
  EXPECT_FALSE(model()->WalletIsSelected());

  EXPECT_CALL(*delegate(), AddAccount());
  model()->ExecuteCommand(TestAccountChooserModel::kWalletAddAccountId, 0);
  EXPECT_FALSE(model()->WalletIsSelected());

  model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId, 0);
  EXPECT_TRUE(model()->WalletIsSelected());
}

TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) {
  EXPECT_FALSE(model()->HasAccountsToChoose());

  std::vector<std::string> accounts;
  accounts.push_back("john.doe@gmail.com");
  accounts.push_back("jane.smith@gmail.com");
  model()->SetWalletAccounts(accounts, 0U);
  EXPECT_TRUE(model()->HasAccountsToChoose());
  EXPECT_TRUE(model()->WalletIsSelected());
  ASSERT_EQ(4, model()->GetItemCount());
  EXPECT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId));
  EXPECT_TRUE(model()->IsCommandIdEnabled(
      TestAccountChooserModel::kWalletAccountsStartId + 1));
  EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
  model()->SetWalletAccounts(accounts, 1U);
  EXPECT_EQ(ASCIIToUTF16(accounts[1]), model()->GetActiveWalletAccountName());

  model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId, 0);
  EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
  model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId + 1,
                          0);
  EXPECT_EQ(ASCIIToUTF16(accounts[1]), model()->GetActiveWalletAccountName());

  // Setting the wallet accounts forces the switch to wallet.
  model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0);
  EXPECT_FALSE(model()->WalletIsSelected());
  model()->SetWalletAccounts(accounts, 1U);
  EXPECT_TRUE(model()->WalletIsSelected());
}

}  // namespace autofill