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 "chrome/browser/ui/autofill/account_chooser_model.h" |
| 7 #include "chrome/common/pref_names.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace autofill { |
| 13 |
| 14 namespace { |
| 15 |
| 16 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { |
| 17 public: |
| 18 MockAccountChooserModelDelegate() {} |
| 19 virtual ~MockAccountChooserModelDelegate() {} |
| 20 |
| 21 MOCK_METHOD0(AccountChoiceChanged, void()); |
| 22 }; |
| 23 |
| 24 class AccountChooserModelTest : public testing::Test { |
| 25 public: |
| 26 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} |
| 27 virtual ~AccountChooserModelTest() {} |
| 28 |
| 29 Profile* profile() { return &profile_; } |
| 30 MockAccountChooserModelDelegate* delegate() { return &delegate_; } |
| 31 AccountChooserModel* model() { return &model_; } |
| 32 |
| 33 private: |
| 34 TestingProfile profile_; |
| 35 MockAccountChooserModelDelegate delegate_; |
| 36 AccountChooserModel model_; |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 TEST_F(AccountChooserModelTest, ObeysPref) { |
| 42 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
| 43 |
| 44 profile()->GetPrefs()->SetBoolean( |
| 45 prefs::kAutofillDialogPayWithoutWallet, false); |
| 46 EXPECT_TRUE(model()->WalletIsSelected()); |
| 47 |
| 48 profile()->GetPrefs()->SetBoolean( |
| 49 prefs::kAutofillDialogPayWithoutWallet, true); |
| 50 EXPECT_FALSE(model()->WalletIsSelected()); |
| 51 } |
| 52 |
| 53 TEST_F(AccountChooserModelTest, HandlesError) { |
| 54 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
| 55 |
| 56 profile()->GetPrefs()->SetBoolean( |
| 57 prefs::kAutofillDialogPayWithoutWallet, false); |
| 58 EXPECT_TRUE(model()->WalletIsSelected()); |
| 59 |
| 60 model()->SetHadWalletError(); |
| 61 EXPECT_FALSE(model()->WalletIsSelected()); |
| 62 EXPECT_FALSE(model()->IsCommandIdEnabled(0)); |
| 63 } |
| 64 |
| 65 TEST_F(AccountChooserModelTest, HandlesSigninError) { |
| 66 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
| 67 |
| 68 profile()->GetPrefs()->SetBoolean( |
| 69 prefs::kAutofillDialogPayWithoutWallet, false); |
| 70 EXPECT_TRUE(model()->WalletIsSelected()); |
| 71 |
| 72 model()->SetHadWalletSigninError(); |
| 73 EXPECT_FALSE(model()->WalletIsSelected()); |
| 74 EXPECT_TRUE(model()->IsCommandIdEnabled(0)); |
| 75 } |
| 76 |
| 77 TEST_F(AccountChooserModelTest, RespectsUserChoice) { |
| 78 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(3); |
| 79 |
| 80 profile()->GetPrefs()->SetBoolean( |
| 81 prefs::kAutofillDialogPayWithoutWallet, false); |
| 82 EXPECT_TRUE(model()->WalletIsSelected()); |
| 83 |
| 84 model()->ExecuteCommand(1, 0); |
| 85 EXPECT_FALSE(model()->WalletIsSelected()); |
| 86 |
| 87 model()->ExecuteCommand(0, 0); |
| 88 EXPECT_TRUE(model()->WalletIsSelected()); |
| 89 } |
| 90 |
| 91 TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) { |
| 92 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(7); |
| 93 |
| 94 // 1. Initial state -- autofill data only. |
| 95 profile()->GetPrefs()->SetBoolean( |
| 96 prefs::kAutofillDialogPayWithoutWallet, true); |
| 97 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 98 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 99 EXPECT_EQ(1, model()->GetItemCount()); |
| 100 |
| 101 const std::string kAccount1 = "john.doe@gmail.com"; |
| 102 const std::string kAccount2 = "john.android@gmail.com"; |
| 103 |
| 104 // 2. Sign-in to the content area. |
| 105 model()->ForceSelectWalletAccount(); |
| 106 model()->SetCurrentlySignedInAccount(kAccount1); |
| 107 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 108 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 109 EXPECT_EQ(2, model()->GetItemCount()); |
| 110 EXPECT_EQ(kAccount1, model()->GetCurrentlySignedInAccount()); |
| 111 |
| 112 // 3. Some sort of sign-in error happened. |
| 113 model()->SetHadWalletSigninError(); |
| 114 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 115 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 116 EXPECT_EQ(1, model()->GetItemCount()); |
| 117 |
| 118 // An unrelated available accounts change. |
| 119 // Note that it does NOT send AccountChoiceChanged. |
| 120 std::vector<std::string> accounts; |
| 121 accounts.push_back(kAccount2); |
| 122 model()->SetAvailableAccounts(accounts); |
| 123 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 124 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 125 EXPECT_EQ(2, model()->GetItemCount()); |
| 126 |
| 127 // 4. Sign-in with the new account. |
| 128 model()->ForceSelectWalletAccount(); |
| 129 model()->SetCurrentlySignedInAccount(kAccount2); |
| 130 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 131 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 132 EXPECT_EQ(2, model()->GetItemCount()); |
| 133 EXPECT_EQ(kAccount2, model()->GetCurrentlySignedInAccount()); |
| 134 |
| 135 // 5. Switch to the autofill. |
| 136 model()->ExecuteCommand(1, 0); |
| 137 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 138 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 139 EXPECT_TRUE(model()->IsCommandIdEnabled(0)); // accounts are available. |
| 140 EXPECT_TRUE(model()->IsCommandIdEnabled(2)); // accounts are available. |
| 141 |
| 142 // 6. Sign-in with an old account. |
| 143 model()->ForceSelectWalletAccount(); |
| 144 model()->SetCurrentlySignedInAccount(kAccount1); |
| 145 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 146 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 147 EXPECT_EQ(3, model()->GetItemCount()); |
| 148 EXPECT_EQ(kAccount1, model()->GetCurrentlySignedInAccount()); |
| 149 |
| 150 // 7. Some wallet error. |
| 151 model()->SetHadWalletError(); |
| 152 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 153 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 154 EXPECT_EQ(2, model()->GetItemCount()); |
| 155 EXPECT_FALSE(model()->IsCommandIdEnabled(2)); // no available accounts. |
| 156 } |
| 157 |
| 158 } // namespace autofill |
OLD | NEW |