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 "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/ui/autofill/account_chooser_model.h" |
7 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
8 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace autofill { | 13 namespace autofill { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { | 17 class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { |
17 public: | 18 public: |
18 MockAccountChooserModelDelegate() {} | 19 MockAccountChooserModelDelegate() {} |
19 virtual ~MockAccountChooserModelDelegate() {} | 20 virtual ~MockAccountChooserModelDelegate() {} |
20 | 21 |
21 MOCK_METHOD0(AccountChoiceChanged, void()); | 22 MOCK_METHOD0(AccountChoiceChanged, void()); |
| 23 MOCK_METHOD0(UpdateAccountChooserView, void()); |
22 }; | 24 }; |
23 | 25 |
| 26 class TestAccountChooserModel : public AccountChooserModel { |
| 27 public: |
| 28 TestAccountChooserModel(AccountChooserModelDelegate* delegate, |
| 29 PrefService* prefs) |
| 30 : AccountChooserModel(delegate, prefs) { |
| 31 } |
| 32 |
| 33 using AccountChooserModel::kActiveWalletItemId; |
| 34 using AccountChooserModel::kAutofillItemId; |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
24 class AccountChooserModelTest : public testing::Test { | 39 class AccountChooserModelTest : public testing::Test { |
25 public: | 40 public: |
26 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} | 41 AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} |
27 virtual ~AccountChooserModelTest() {} | 42 virtual ~AccountChooserModelTest() {} |
28 | 43 |
29 Profile* profile() { return &profile_; } | 44 Profile* profile() { return &profile_; } |
30 MockAccountChooserModelDelegate* delegate() { return &delegate_; } | 45 MockAccountChooserModelDelegate* delegate() { return &delegate_; } |
31 AccountChooserModel* model() { return &model_; } | 46 TestAccountChooserModel* model() { return &model_; } |
32 | 47 |
33 private: | 48 private: |
34 TestingProfile profile_; | 49 TestingProfile profile_; |
35 MockAccountChooserModelDelegate delegate_; | 50 MockAccountChooserModelDelegate delegate_; |
36 AccountChooserModel model_; | 51 TestAccountChooserModel model_; |
37 }; | 52 }; |
38 | 53 |
39 } // namespace | |
40 | |
41 TEST_F(AccountChooserModelTest, ObeysPref) { | 54 TEST_F(AccountChooserModelTest, ObeysPref) { |
42 // When "Pay without wallet" is false, use Wallet by default. | 55 // When "Pay without wallet" is false, use Wallet by default. |
43 { | 56 { |
44 profile()->GetPrefs()->SetBoolean( | 57 profile()->GetPrefs()->SetBoolean( |
45 prefs::kAutofillDialogPayWithoutWallet, false); | 58 prefs::kAutofillDialogPayWithoutWallet, false); |
46 AccountChooserModel model(delegate(), profile()->GetPrefs()); | 59 AccountChooserModel model(delegate(), profile()->GetPrefs()); |
47 EXPECT_TRUE(model.WalletIsSelected()); | 60 EXPECT_TRUE(model.WalletIsSelected()); |
48 } | 61 } |
49 // When the user chose to "Pay without wallet", use Autofill. | 62 // When the user chose to "Pay without wallet", use Autofill. |
50 { | 63 { |
(...skipping 12 matching lines...) Expand all Loading... |
63 // Check that nothing changes while this dialog is running if a pref changes | 76 // Check that nothing changes while this dialog is running if a pref changes |
64 // (this could cause subtle bugs or annoyances if a user closes another | 77 // (this could cause subtle bugs or annoyances if a user closes another |
65 // running dialog). | 78 // running dialog). |
66 profile()->GetPrefs()->SetBoolean( | 79 profile()->GetPrefs()->SetBoolean( |
67 prefs::kAutofillDialogPayWithoutWallet, true); | 80 prefs::kAutofillDialogPayWithoutWallet, true); |
68 EXPECT_TRUE(model()->WalletIsSelected()); | 81 EXPECT_TRUE(model()->WalletIsSelected()); |
69 } | 82 } |
70 | 83 |
71 TEST_F(AccountChooserModelTest, HandlesError) { | 84 TEST_F(AccountChooserModelTest, HandlesError) { |
72 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); | 85 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
| 86 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1); |
73 | 87 |
74 ASSERT_TRUE(model()->WalletIsSelected()); | 88 ASSERT_TRUE(model()->WalletIsSelected()); |
75 ASSERT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | 89 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 90 TestAccountChooserModel::kActiveWalletItemId)); |
76 | 91 |
77 model()->SetHadWalletError(); | 92 model()->SetHadWalletError(); |
78 EXPECT_FALSE(model()->WalletIsSelected()); | 93 EXPECT_FALSE(model()->WalletIsSelected()); |
79 EXPECT_FALSE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | 94 EXPECT_FALSE(model()->IsCommandIdEnabled( |
| 95 TestAccountChooserModel::kActiveWalletItemId)); |
80 } | 96 } |
81 | 97 |
82 TEST_F(AccountChooserModelTest, HandlesSigninError) { | 98 TEST_F(AccountChooserModelTest, HandlesSigninError) { |
83 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); | 99 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
| 100 EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(2); |
84 | 101 |
| 102 // 0. "Unknown" wallet account, we don't know if the user is signed-in yet. |
85 ASSERT_TRUE(model()->WalletIsSelected()); | 103 ASSERT_TRUE(model()->WalletIsSelected()); |
86 ASSERT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | 104 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 105 TestAccountChooserModel::kActiveWalletItemId)); |
| 106 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 107 ASSERT_FALSE(model()->HasAccountsToChoose()); |
| 108 ASSERT_EQ(2, model()->GetItemCount()); |
| 109 EXPECT_EQ(string16(), model()->active_wallet_account_name()); |
87 | 110 |
| 111 // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in). |
| 112 // Calls UpdateAccountChooserView. |
| 113 const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); |
| 114 model()->SetActiveWalletAccountName(kAccount1); |
| 115 ASSERT_TRUE(model()->WalletIsSelected()); |
| 116 ASSERT_TRUE(model()->IsCommandIdEnabled( |
| 117 TestAccountChooserModel::kActiveWalletItemId)); |
| 118 ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
| 119 ASSERT_TRUE(model()->HasAccountsToChoose()); |
| 120 EXPECT_EQ(2, model()->GetItemCount()); |
| 121 EXPECT_EQ(kAccount1, model()->active_wallet_account_name()); |
| 122 |
| 123 // 2. Sign-in failure. |
| 124 // Autofill data should be selected and be the only valid choice. |
| 125 // Calls UpdateAccountChooserView. |
| 126 // Calls AccountChoiceChanged. |
88 model()->SetHadWalletSigninError(); | 127 model()->SetHadWalletSigninError(); |
89 EXPECT_FALSE(model()->WalletIsSelected()); | 128 EXPECT_FALSE(model()->WalletIsSelected()); |
90 EXPECT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | 129 EXPECT_TRUE(model()->IsCommandIdEnabled( |
| 130 TestAccountChooserModel::kActiveWalletItemId)); |
| 131 EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
| 132 EXPECT_FALSE(model()->HasAccountsToChoose()); |
| 133 EXPECT_EQ(1, model()->GetItemCount()); |
| 134 EXPECT_EQ(string16(), model()->active_wallet_account_name()); |
91 } | 135 } |
92 | 136 |
93 TEST_F(AccountChooserModelTest, RespectsUserChoice) { | 137 TEST_F(AccountChooserModelTest, RespectsUserChoice) { |
94 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); | 138 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
95 | 139 |
96 model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); | 140 model()->ExecuteCommand(TestAccountChooserModel::kAutofillItemId, 0); |
97 EXPECT_FALSE(model()->WalletIsSelected()); | 141 EXPECT_FALSE(model()->WalletIsSelected()); |
98 | 142 |
99 model()->ExecuteCommand(AccountChooserModel::kWalletItemId, 0); | 143 model()->ExecuteCommand(TestAccountChooserModel::kActiveWalletItemId, 0); |
100 EXPECT_TRUE(model()->WalletIsSelected()); | 144 EXPECT_TRUE(model()->WalletIsSelected()); |
101 } | 145 } |
102 | 146 |
103 } // namespace autofill | 147 } // namespace autofill |
OLD | NEW |