OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | 6 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
7 #include "chrome/common/pref_names.h" | 7 #include "chrome/common/pref_names.h" |
8 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace autofill { | 12 namespace autofill { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { | 16 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { |
17 public: | 17 public: |
18 MockAccountChooserModelDelegate() {} | 18 MockAccountChooserModelDelegate() {} |
19 virtual ~MockAccountChooserModelDelegate() {} | 19 virtual ~MockAccountChooserModelDelegate() {} |
20 | 20 |
21 MOCK_METHOD0(AccountChoiceChanged, void()); | 21 MOCK_METHOD0(AccountChoiceChanged, void()); |
| 22 MOCK_METHOD0(UpdateAccountChooserView, void()); |
22 }; | 23 }; |
23 | 24 |
24 class AccountChooserModelTest : public testing::Test { | 25 class AccountChooserModelTest : public testing::Test { |
25 public: | 26 public: |
26 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} | 27 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} |
27 virtual ~AccountChooserModelTest() {} | 28 virtual ~AccountChooserModelTest() {} |
28 | 29 |
29 Profile* profile() { return &profile_; } | 30 Profile* profile() { return &profile_; } |
30 MockAccountChooserModelDelegate* delegate() { return &delegate_; } | 31 MockAccountChooserModelDelegate* delegate() { return &delegate_; } |
31 AccountChooserModel* model() { return &model_; } | 32 AccountChooserModel* model() { return &model_; } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 prefs::kAutofillDialogPayWithoutWallet, false); | 82 prefs::kAutofillDialogPayWithoutWallet, false); |
82 EXPECT_TRUE(model()->WalletIsSelected()); | 83 EXPECT_TRUE(model()->WalletIsSelected()); |
83 | 84 |
84 model()->ExecuteCommand(1, 0); | 85 model()->ExecuteCommand(1, 0); |
85 EXPECT_FALSE(model()->WalletIsSelected()); | 86 EXPECT_FALSE(model()->WalletIsSelected()); |
86 | 87 |
87 model()->ExecuteCommand(0, 0); | 88 model()->ExecuteCommand(0, 0); |
88 EXPECT_TRUE(model()->WalletIsSelected()); | 89 EXPECT_TRUE(model()->WalletIsSelected()); |
89 } | 90 } |
90 | 91 |
| 92 TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) { |
| 93 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(7); |
| 94 |
| 95 // 1. Initial state -- autofill data only. |
| 96 profile()->GetPrefs()->SetBoolean( |
| 97 prefs::kAutofillDialogPayWithoutWallet, true); |
| 98 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 99 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 100 EXPECT_EQ(1, model()->GetItemCount()); |
| 101 |
| 102 const std::string kAccount1 = "john.doe@gmail.com"; |
| 103 const std::string kAccount2 = "john.android@gmail.com"; |
| 104 |
| 105 // 2. Sign-in to the content area. |
| 106 model()->ForceWalletAccountSelected(); |
| 107 model()->SetCurrentlySignedInAccount(kAccount1); |
| 108 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 109 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 110 EXPECT_EQ(2, model()->GetItemCount()); |
| 111 EXPECT_EQ(kAccount1, model()->GetCurrentlySignedInAccount()); |
| 112 |
| 113 // 3. Some sort of sign-in error happened. |
| 114 model()->SetHadWalletSigninError(); |
| 115 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 116 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 117 EXPECT_EQ(1, model()->GetItemCount()); |
| 118 |
| 119 // An unrelated available accounts change. |
| 120 // Note that it does NOT send AccountChoiceChanged. |
| 121 std::vector<std::string> accounts; |
| 122 accounts.push_back(kAccount2); |
| 123 model()->SetAvailableAccounts(accounts); |
| 124 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 125 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 126 EXPECT_EQ(2, model()->GetItemCount()); |
| 127 |
| 128 // 4. Sign-in with the new account. |
| 129 model()->ForceWalletAccountSelected(); |
| 130 model()->SetCurrentlySignedInAccount(kAccount2); |
| 131 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 132 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 133 EXPECT_EQ(2, model()->GetItemCount()); |
| 134 EXPECT_EQ(kAccount2, model()->GetCurrentlySignedInAccount()); |
| 135 |
| 136 // 5. Switch to the autofill. |
| 137 model()->ExecuteCommand(1, 0); |
| 138 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 139 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 140 EXPECT_TRUE(model()->IsCommandIdEnabled(0)); // accounts are available. |
| 141 EXPECT_TRUE(model()->IsCommandIdEnabled(2)); // accounts are available. |
| 142 |
| 143 // 6. Sign-in with an old account. |
| 144 model()->ForceWalletAccountSelected(); |
| 145 model()->SetCurrentlySignedInAccount(kAccount1); |
| 146 EXPECT_TRUE(model()->IsCurrentlySignedInAccountSelected()); |
| 147 EXPECT_TRUE(model()->HasAccountsToChoose()); |
| 148 EXPECT_EQ(3, model()->GetItemCount()); |
| 149 EXPECT_EQ(kAccount1, model()->GetCurrentlySignedInAccount()); |
| 150 |
| 151 // 7. Some wallet error. |
| 152 model()->SetHadWalletError(); |
| 153 EXPECT_FALSE(model()->IsCurrentlySignedInAccountSelected()); |
| 154 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 155 EXPECT_EQ(2, model()->GetItemCount()); |
| 156 EXPECT_FALSE(model()->IsCommandIdEnabled(2)); // no available accounts. |
| 157 } |
| 158 |
91 } // namespace autofill | 159 } // namespace autofill |
OLD | NEW |