account_chooser_model_unittest.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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::kActiveWalletItemId; 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}; 41 42class AccountChooserModelTest : public testing::Test { 43 public: 44 AccountChooserModelTest() 45 : model_(&delegate_, profile_.GetPrefs(), metric_logger_) {} 46 virtual ~AccountChooserModelTest() {} 47 48 Profile* profile() { return &profile_; } 49 MockAccountChooserModelDelegate* delegate() { return &delegate_; } 50 TestAccountChooserModel* model() { return &model_; } 51 const AutofillMetrics& metric_logger() { return metric_logger_; } 52 53 private: 54 TestingProfile profile_; 55 MockAccountChooserModelDelegate delegate_; 56 TestAccountChooserModel model_; 57 AutofillMetrics metric_logger_; 58}; 59 60} // namespace 61 62TEST_F(AccountChooserModelTest, ObeysPref) { 63 // When "Pay without wallet" is false, use Wallet by default. 64 { 65 profile()->GetPrefs()->SetBoolean( 66 ::prefs::kAutofillDialogPayWithoutWallet, false); 67 TestAccountChooserModel model(delegate(), profile()->GetPrefs(), 68 metric_logger()); 69 EXPECT_TRUE(model.WalletIsSelected()); 70 } 71 // When the user chose to "Pay without wallet", use Autofill. 72 { 73 profile()->GetPrefs()->SetBoolean( 74 ::prefs::kAutofillDialogPayWithoutWallet, true); 75 TestAccountChooserModel model(delegate(), profile()->GetPrefs(), 76 metric_logger()); 77 EXPECT_FALSE(model.WalletIsSelected()); 78 } 79} 80 81TEST_F(AccountChooserModelTest, IgnoresPrefChanges) { 82 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean( 83 ::prefs::kAutofillDialogPayWithoutWallet)); 84 EXPECT_TRUE(model()->WalletIsSelected()); 85 86 // Check that nothing changes while this dialog is running if a pref changes 87 // (this could cause subtle bugs or annoyances if a user closes another 88 // running dialog). 89 profile()->GetPrefs()->SetBoolean( 90 ::prefs::kAutofillDialogPayWithoutWallet, true); 91 EXPECT_TRUE(model()->WalletIsSelected()); 92} 93 94TEST_F(AccountChooserModelTest, HandlesError) { 95 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); 96 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1); 97 98 ASSERT_TRUE(model()->WalletIsSelected()); 99 ASSERT_TRUE(model()->IsCommandIdEnabled( 100 TestAccountChooserModel::kActiveWalletItemId)); 101 102 model()->SetHadWalletError(); 103 EXPECT_FALSE(model()->WalletIsSelected()); 104 EXPECT_FALSE(model()->IsCommandIdEnabled( 105 TestAccountChooserModel::kActiveWalletItemId)); 106} 107 108TEST_F(AccountChooserModelTest, HandlesSigninError) { 109 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); 110 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2); 111 112 // 0. "Unknown" wallet account, we don't know if the user is signed-in yet. 113 ASSERT_TRUE(model()->WalletIsSelected()); 114 ASSERT_TRUE(model()->IsCommandIdEnabled( 115 TestAccountChooserModel::kActiveWalletItemId)); 116 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); 117 ASSERT_FALSE(model()->HasAccountsToChoose()); 118 ASSERT_EQ(2, model()->GetItemCount()); 119 EXPECT_EQ(string16(), model()->active_wallet_account_name()); 120 121 // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in). 122 // Calls UpdateAccountChooserView. 123 const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); 124 model()->SetActiveWalletAccountName(kAccount1); 125 ASSERT_TRUE(model()->WalletIsSelected()); 126 ASSERT_TRUE(model()->IsCommandIdEnabled( 127 TestAccountChooserModel::kActiveWalletItemId)); 128 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); 129 ASSERT_TRUE(model()->HasAccountsToChoose()); 130 EXPECT_EQ(2, model()->GetItemCount()); 131 EXPECT_EQ(kAccount1, model()->active_wallet_account_name()); 132 133 // 2. Sign-in failure. 134 // Autofill data should be selected and be the only valid choice. 135 // Calls UpdateAccountChooserView. 136 // Calls AccountChoiceChanged. 137 model()->SetHadWalletSigninError(); 138 EXPECT_FALSE(model()->WalletIsSelected()); 139 EXPECT_TRUE(model()->IsCommandIdEnabled( 140 TestAccountChooserModel::kActiveWalletItemId)); 141 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); 142 EXPECT_FALSE(model()->HasAccountsToChoose()); 143 EXPECT_EQ(1, model()->GetItemCount()); 144 EXPECT_EQ(string16(), model()->active_wallet_account_name()); 145} 146 147TEST_F(AccountChooserModelTest, RespectsUserChoice) { 148 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); 149 150 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0); 151 EXPECT_FALSE(model()->WalletIsSelected()); 152 153 model()->ExecuteCommand(TestAccountChooserModel::kActiveWalletItemId, 0); 154 EXPECT_TRUE(model()->WalletIsSelected()); 155} 156 157} // namespace autofill 158