account_chooser_model_unittest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
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"
13
14namespace autofill {
15
16namespace {
17
18class TestAccountChooserModel : public AccountChooserModel {
19 public:
20  TestAccountChooserModel(AccountChooserModelDelegate* delegate,
21                          PrefService* prefs,
22                          const AutofillMetrics& metric_logger)
23      : AccountChooserModel(delegate, prefs, metric_logger) {}
24  virtual ~TestAccountChooserModel() {}
25
26  using AccountChooserModel::kWalletAccountsStartId;
27  using AccountChooserModel::kAutofillItemId;
28
29 private:
30  DISALLOW_COPY_AND_ASSIGN(TestAccountChooserModel);
31};
32
33class MockAccountChooserModelDelegate : public AccountChooserModelDelegate {
34 public:
35  MockAccountChooserModelDelegate() {}
36  virtual ~MockAccountChooserModelDelegate() {}
37
38  MOCK_METHOD0(AccountChoiceChanged, void());
39  MOCK_METHOD0(UpdateAccountChooserView, void());
40  MOCK_METHOD0(AccountChooserWillShow, void());
41};
42
43class AccountChooserModelTest : public testing::Test {
44 public:
45  AccountChooserModelTest()
46      : model_(&delegate_, profile_.GetPrefs(), metric_logger_) {}
47  virtual ~AccountChooserModelTest() {}
48
49  Profile* profile() { return &profile_; }
50  MockAccountChooserModelDelegate* delegate() { return &delegate_; }
51  TestAccountChooserModel* model() { return &model_; }
52  const AutofillMetrics& metric_logger() { return metric_logger_; }
53
54 private:
55  TestingProfile profile_;
56  testing::NiceMock<MockAccountChooserModelDelegate> delegate_;
57  TestAccountChooserModel model_;
58  AutofillMetrics metric_logger_;
59};
60
61}  // namespace
62
63TEST_F(AccountChooserModelTest, ObeysPref) {
64  // When "Pay without wallet" is false, use Wallet by default.
65  {
66    profile()->GetPrefs()->SetBoolean(
67        ::prefs::kAutofillDialogPayWithoutWallet, false);
68    TestAccountChooserModel model(delegate(), profile()->GetPrefs(),
69                                  metric_logger());
70    EXPECT_TRUE(model.WalletIsSelected());
71  }
72  // When the user chose to "Pay without wallet", use Autofill.
73  {
74    profile()->GetPrefs()->SetBoolean(
75        ::prefs::kAutofillDialogPayWithoutWallet, true);
76    TestAccountChooserModel model(delegate(), profile()->GetPrefs(),
77                                  metric_logger());
78    EXPECT_FALSE(model.WalletIsSelected());
79  }
80}
81
82TEST_F(AccountChooserModelTest, IgnoresPrefChanges) {
83  ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
84      ::prefs::kAutofillDialogPayWithoutWallet));
85  EXPECT_TRUE(model()->WalletIsSelected());
86
87  // Check that nothing changes while this dialog is running if a pref changes
88  // (this could cause subtle bugs or annoyances if a user closes another
89  // running dialog).
90  profile()->GetPrefs()->SetBoolean(
91      ::prefs::kAutofillDialogPayWithoutWallet, true);
92  EXPECT_TRUE(model()->WalletIsSelected());
93}
94
95TEST_F(AccountChooserModelTest, HandlesError) {
96  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
97  EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1);
98
99  ASSERT_TRUE(model()->WalletIsSelected());
100  ASSERT_TRUE(model()->IsCommandIdEnabled(
101      TestAccountChooserModel::kWalletAccountsStartId));
102
103  model()->SetHadWalletError();
104  EXPECT_FALSE(model()->WalletIsSelected());
105  EXPECT_FALSE(model()->IsCommandIdEnabled(
106      TestAccountChooserModel::kWalletAccountsStartId));
107}
108
109TEST_F(AccountChooserModelTest, HandlesSigninError) {
110  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1);
111  EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2);
112
113  // 0. "Unknown" wallet account, we don't know if the user is signed-in yet.
114  ASSERT_TRUE(model()->WalletIsSelected());
115  ASSERT_TRUE(model()->IsCommandIdEnabled(
116      TestAccountChooserModel::kWalletAccountsStartId));
117  ASSERT_TRUE(model()->WalletIsSelected());
118  ASSERT_FALSE(model()->HasAccountsToChoose());
119  ASSERT_EQ(2, model()->GetItemCount());
120  EXPECT_EQ(string16(), model()->GetActiveWalletAccountName());
121
122  // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in).
123  // Calls UpdateAccountChooserView.
124  std::vector<std::string> accounts;
125  accounts.push_back("john.doe@gmail.com");
126  model()->SetWalletAccounts(accounts);
127  ASSERT_TRUE(model()->WalletIsSelected());
128  ASSERT_TRUE(model()->IsCommandIdEnabled(
129      TestAccountChooserModel::kWalletAccountsStartId));
130  ASSERT_TRUE(model()->WalletIsSelected());
131  ASSERT_TRUE(model()->HasAccountsToChoose());
132  EXPECT_EQ(2, model()->GetItemCount());
133  EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
134
135  // 2. Sign-in failure.
136  // Autofill data should be selected and be the only valid choice.
137  // Calls UpdateAccountChooserView.
138  // Calls AccountChoiceChanged.
139  model()->SetHadWalletSigninError();
140  EXPECT_FALSE(model()->WalletIsSelected());
141  EXPECT_TRUE(model()->IsCommandIdEnabled(
142      TestAccountChooserModel::kWalletAccountsStartId));
143  EXPECT_FALSE(model()->WalletIsSelected());
144  EXPECT_FALSE(model()->HasAccountsToChoose());
145  EXPECT_EQ(1, model()->GetItemCount());
146  EXPECT_EQ(string16(), model()->GetActiveWalletAccountName());
147}
148
149TEST_F(AccountChooserModelTest, RespectsUserChoice) {
150  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);
151
152  model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0);
153  EXPECT_FALSE(model()->WalletIsSelected());
154
155  model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId, 0);
156  EXPECT_TRUE(model()->WalletIsSelected());
157}
158
159TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) {
160  EXPECT_FALSE(model()->HasAccountsToChoose());
161
162  std::vector<std::string> accounts;
163  accounts.push_back("john.doe@gmail.com");
164  accounts.push_back("jane.smith@gmail.com");
165  model()->SetWalletAccounts(accounts);
166  EXPECT_TRUE(model()->HasAccountsToChoose());
167  EXPECT_TRUE(model()->WalletIsSelected());
168  ASSERT_EQ(3, model()->GetItemCount());
169  EXPECT_TRUE(model()->IsCommandIdEnabled(
170      TestAccountChooserModel::kWalletAccountsStartId));
171  EXPECT_TRUE(model()->IsCommandIdEnabled(
172      TestAccountChooserModel::kWalletAccountsStartId + 1));
173  EXPECT_EQ(ASCIIToUTF16(accounts[0]), model()->GetActiveWalletAccountName());
174
175  model()->ExecuteCommand(TestAccountChooserModel::kWalletAccountsStartId + 1,
176                          0);
177  EXPECT_EQ(ASCIIToUTF16(accounts[1]), model()->GetActiveWalletAccountName());
178}
179
180}  // namespace autofill
181