OLD | NEW |
(Empty) | |
| 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/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 "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace autofill { |
| 14 |
| 15 namespace { |
| 16 |
| 17 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { |
| 18 public: |
| 19 MockAccountChooserModelDelegate() {} |
| 20 virtual ~MockAccountChooserModelDelegate() {} |
| 21 |
| 22 MOCK_METHOD0(AccountChoiceChanged, void()); |
| 23 MOCK_METHOD0(UpdateAccountChooserView, void()); |
| 24 }; |
| 25 |
| 26 class AccountChooserModelTest : public testing::Test { |
| 27 public: |
| 28 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} |
| 29 virtual ~AccountChooserModelTest() {} |
| 30 |
| 31 Profile* profile() { return &profile_; } |
| 32 MockAccountChooserModelDelegate* delegate() { return &delegate_; } |
| 33 AccountChooserModel* model() { return &model_; } |
| 34 |
| 35 private: |
| 36 TestingProfile profile_; |
| 37 MockAccountChooserModelDelegate delegate_; |
| 38 AccountChooserModel model_; |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 TEST_F(AccountChooserModelTest, ObeysPref) { |
| 44 // When "Pay without wallet" is false, use Wallet by default. |
| 45 { |
| 46 profile()->GetPrefs()->SetBoolean( |
| 47 prefs::kAutofillDialogPayWithoutWallet, false); |
| 48 AccountChooserModel model(delegate(), profile()->GetPrefs()); |
| 49 EXPECT_TRUE(model.WalletIsSelected()); |
| 50 } |
| 51 // When the user chose to "Pay without wallet", use Autofill. |
| 52 { |
| 53 profile()->GetPrefs()->SetBoolean( |
| 54 prefs::kAutofillDialogPayWithoutWallet, true); |
| 55 AccountChooserModel model(delegate(), profile()->GetPrefs()); |
| 56 EXPECT_FALSE(model.WalletIsSelected()); |
| 57 } |
| 58 } |
| 59 |
| 60 TEST_F(AccountChooserModelTest, IgnoresPrefChanges) { |
| 61 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean( |
| 62 prefs::kAutofillDialogPayWithoutWallet)); |
| 63 EXPECT_TRUE(model()->WalletIsSelected()); |
| 64 |
| 65 // Check that nothing changes while this dialog is running if a pref changes |
| 66 // (this could cause subtle bugs or annoyances if a user closes another |
| 67 // running dialog). |
| 68 profile()->GetPrefs()->SetBoolean( |
| 69 prefs::kAutofillDialogPayWithoutWallet, true); |
| 70 EXPECT_TRUE(model()->WalletIsSelected()); |
| 71 } |
| 72 |
| 73 TEST_F(AccountChooserModelTest, HandlesError) { |
| 74 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
| 75 |
| 76 ASSERT_TRUE(model()->WalletIsSelected()); |
| 77 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 78 AccountChooserModel::kActiveWalletItemId)); |
| 79 |
| 80 model()->SetHadWalletError(); |
| 81 EXPECT_FALSE(model()->WalletIsSelected()); |
| 82 EXPECT_FALSE(model()->IsCommandIdEnabled( |
| 83 AccountChooserModel::kActiveWalletItemId)); |
| 84 } |
| 85 |
| 86 TEST_F(AccountChooserModelTest, HandlesSigninError) { |
| 87 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
| 88 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1); |
| 89 |
| 90 // 0. "Unknown" wallet account, we don't know if the user is signed-in yet. |
| 91 ASSERT_TRUE(model()->WalletIsSelected()); |
| 92 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 93 AccountChooserModel::kActiveWalletItemId)); |
| 94 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 95 ASSERT_FALSE(model()->HasAccountsToChoose()); |
| 96 ASSERT_EQ(2, model()->GetItemCount()); |
| 97 EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
| 98 |
| 99 // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in). |
| 100 // Calls UpdateAccountChooserView. |
| 101 const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); |
| 102 model()->SetActiveWalletAccountName(kAccount1); |
| 103 ASSERT_TRUE(model()->WalletIsSelected()); |
| 104 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 105 AccountChooserModel::kActiveWalletItemId)); |
| 106 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 107 ASSERT_TRUE(model()->HasAccountsToChoose()); |
| 108 EXPECT_EQ(2, model()->GetItemCount()); |
| 109 EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
| 110 |
| 111 // 2. Sign-in failure. |
| 112 // Autofill data should be selected and be the only valid choice. |
| 113 // Calls AccountChoiceChanged. |
| 114 model()->SetHadWalletSigninError(); |
| 115 EXPECT_FALSE(model()->WalletIsSelected()); |
| 116 EXPECT_TRUE(model()->IsCommandIdEnabled( |
| 117 AccountChooserModel::kActiveWalletItemId)); |
| 118 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 119 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 120 EXPECT_EQ(2, model()->GetItemCount()); |
| 121 EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
| 122 } |
| 123 |
| 124 TEST_F(AccountChooserModelTest, RespectsUserChoice) { |
| 125 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
| 126 |
| 127 model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); |
| 128 EXPECT_FALSE(model()->WalletIsSelected()); |
| 129 |
| 130 model()->ExecuteCommand(AccountChooserModel::kActiveWalletItemId, 0); |
| 131 EXPECT_TRUE(model()->WalletIsSelected()); |
| 132 } |
| 133 |
| 134 TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) { |
| 135 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(5); |
| 136 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(4); |
| 137 |
| 138 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 139 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 140 AccountChooserModel::kActiveWalletItemId)); |
| 141 |
| 142 // Simulate successful user info fetch / passive sign-in. |
| 143 // The active wallet account name should be set to the expected value. |
| 144 // Calls UpdateAccountChooserView. |
| 145 const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); |
| 146 model()->SetActiveWalletAccountName(kAccount1); |
| 147 EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 148 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 149 EXPECT_EQ(2, model()->GetItemCount()); |
| 150 EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
| 151 |
| 152 // Process a sign-in error. |
| 153 // The active wallet account name should be cleared. |
| 154 // Calls AccountChoiceChanged. |
| 155 model()->SetHadWalletSigninError(); |
| 156 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 157 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 158 EXPECT_EQ(2, model()->GetItemCount()); |
| 159 EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
| 160 |
| 161 // Process the change of the set of additional accounts. |
| 162 // Calls UpdateAccountChooserView. |
| 163 const string16 kAccount2 = ASCIIToUTF16("john.android@gmail.com"); |
| 164 std::vector<string16> additional_accounts; |
| 165 additional_accounts.push_back(kAccount2); |
| 166 model()->SetAdditionalAccounts(additional_accounts); |
| 167 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 168 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 169 EXPECT_EQ(3, model()->GetItemCount()); |
| 170 |
| 171 // Sign in with the new account. |
| 172 // Calls AccountChoiceChanged. |
| 173 model()->ForceSelectWalletAccount(); |
| 174 // Calls UpdateAccountChooserView. |
| 175 model()->SetActiveWalletAccountName(kAccount2); |
| 176 EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 177 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 178 EXPECT_EQ(2, model()->GetItemCount()); |
| 179 EXPECT_EQ(kAccount2, model()->GetActiveWalletAccountName()); |
| 180 |
| 181 // Switch to the autofill. |
| 182 // Calls AccountChoiceChanged. |
| 183 model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); |
| 184 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 185 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 186 EXPECT_TRUE(model()->IsCommandIdEnabled( |
| 187 AccountChooserModel::kActiveWalletItemId)); |
| 188 EXPECT_TRUE(model()->IsCommandIdEnabled( |
| 189 AccountChooserModel::kAutofillItemId)); |
| 190 |
| 191 // Sign in with the old account. |
| 192 // Calls AccountChoiceChanged. |
| 193 model()->ForceSelectWalletAccount(); |
| 194 // Calls UpdateAccountChooserView. |
| 195 model()->SetActiveWalletAccountName(kAccount1); |
| 196 EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 197 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 198 EXPECT_EQ(3, model()->GetItemCount()); |
| 199 EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
| 200 |
| 201 // Some wallet error. |
| 202 // Calls AccountChoiceChanged. |
| 203 model()->SetHadWalletError(); |
| 204 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 205 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 206 EXPECT_EQ(3, model()->GetItemCount()); |
| 207 EXPECT_TRUE(model()->IsCommandIdEnabled( |
| 208 AccountChooserModel::kAutofillItemId)); |
| 209 EXPECT_FALSE(model()->IsCommandIdEnabled( |
| 210 AccountChooserModel::kActiveWalletItemId)); |
| 211 } |
| 212 |
| 213 } // namespace autofill |
OLD | NEW |