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" | |
6 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
7 #include "chrome/common/pref_names.h" | |
8 #include "chrome/test/base/testing_profile.h" | |
9 #include "testing/gmock/include/gmock/gmock.h" | 6 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
11 | 8 |
12 namespace autofill { | 9 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 // When "Pay without wallet" is false, use Wallet by default. | |
43 { | |
44 profile()->GetPrefs()->SetBoolean( | |
45 prefs::kAutofillDialogPayWithoutWallet, false); | |
46 AccountChooserModel model(delegate(), profile()->GetPrefs()); | |
47 EXPECT_TRUE(model.WalletIsSelected()); | |
48 } | |
49 // When the user chose to "Pay without wallet", use Autofill. | |
50 { | |
51 profile()->GetPrefs()->SetBoolean( | |
52 prefs::kAutofillDialogPayWithoutWallet, true); | |
53 AccountChooserModel model(delegate(), profile()->GetPrefs()); | |
54 EXPECT_FALSE(model.WalletIsSelected()); | |
55 } | |
56 } | |
57 | |
58 TEST_F(AccountChooserModelTest, IgnoresPrefChanges) { | |
59 ASSERT_FALSE(profile()->GetPrefs()->GetBoolean( | |
60 prefs::kAutofillDialogPayWithoutWallet)); | |
61 EXPECT_TRUE(model()->WalletIsSelected()); | |
62 | |
63 // 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 | |
65 // running dialog). | |
66 profile()->GetPrefs()->SetBoolean( | |
67 prefs::kAutofillDialogPayWithoutWallet, true); | |
68 EXPECT_TRUE(model()->WalletIsSelected()); | |
69 } | |
70 | |
71 TEST_F(AccountChooserModelTest, HandlesError) { | |
72 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); | |
73 | |
74 ASSERT_TRUE(model()->WalletIsSelected()); | |
75 ASSERT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | |
76 | |
77 model()->SetHadWalletError(); | |
78 EXPECT_FALSE(model()->WalletIsSelected()); | |
79 EXPECT_FALSE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | |
80 } | |
81 | |
82 TEST_F(AccountChooserModelTest, HandlesSigninError) { | |
83 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); | |
84 | |
85 ASSERT_TRUE(model()->WalletIsSelected()); | |
86 ASSERT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | |
87 | |
88 model()->SetHadWalletSigninError(); | |
89 EXPECT_FALSE(model()->WalletIsSelected()); | |
90 EXPECT_TRUE(model()->IsCommandIdEnabled(AccountChooserModel::kWalletItemId)); | |
91 } | |
92 | |
93 TEST_F(AccountChooserModelTest, RespectsUserChoice) { | |
94 EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); | |
95 | |
96 model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); | |
97 EXPECT_FALSE(model()->WalletIsSelected()); | |
98 | |
99 model()->ExecuteCommand(AccountChooserModel::kWalletItemId, 0); | |
100 EXPECT_TRUE(model()->WalletIsSelected()); | |
101 } | |
102 | |
103 } // namespace autofill | 10 } // namespace autofill |
OLD | NEW |