Index: chrome/browser/ui/autofill/account_chooser_model_unittest.cc |
diff --git a/chrome/browser/ui/autofill/account_chooser_model_unittest.cc b/chrome/browser/ui/autofill/account_chooser_model_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b7d4ccaa654b7c3bb0dce9186672134c3c815b7f |
--- /dev/null |
+++ b/chrome/browser/ui/autofill/account_chooser_model_unittest.cc |
@@ -0,0 +1,213 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/prefs/pref_service.h" |
+#include "base/utf_string_conversions.h" |
+#include "chrome/browser/ui/autofill/account_chooser_model.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace autofill { |
+ |
+namespace { |
+ |
+class MockAccountChooserModelDelegate : public AccountChooserModelDelegate { |
+ public: |
+ MockAccountChooserModelDelegate() {} |
+ virtual ~MockAccountChooserModelDelegate() {} |
+ |
+ MOCK_METHOD0(AccountChoiceChanged, void()); |
+ MOCK_METHOD0(UpdateAccountChooserView, void()); |
+}; |
+ |
+class AccountChooserModelTest : public testing::Test { |
+ public: |
+ AccountChooserModelTest() : model_(&delegate_, profile_.GetPrefs()) {} |
+ virtual ~AccountChooserModelTest() {} |
+ |
+ Profile* profile() { return &profile_; } |
+ MockAccountChooserModelDelegate* delegate() { return &delegate_; } |
+ AccountChooserModel* model() { return &model_; } |
+ |
+ private: |
+ TestingProfile profile_; |
+ MockAccountChooserModelDelegate delegate_; |
+ AccountChooserModel model_; |
+}; |
+ |
+} // namespace |
+ |
+TEST_F(AccountChooserModelTest, ObeysPref) { |
+ // When "Pay without wallet" is false, use Wallet by default. |
+ { |
+ profile()->GetPrefs()->SetBoolean( |
+ prefs::kAutofillDialogPayWithoutWallet, false); |
+ AccountChooserModel model(delegate(), profile()->GetPrefs()); |
+ EXPECT_TRUE(model.WalletIsSelected()); |
+ } |
+ // When the user chose to "Pay without wallet", use Autofill. |
+ { |
+ profile()->GetPrefs()->SetBoolean( |
+ prefs::kAutofillDialogPayWithoutWallet, true); |
+ AccountChooserModel model(delegate(), profile()->GetPrefs()); |
+ EXPECT_FALSE(model.WalletIsSelected()); |
+ } |
+} |
+ |
+TEST_F(AccountChooserModelTest, IgnoresPrefChanges) { |
+ ASSERT_FALSE(profile()->GetPrefs()->GetBoolean( |
+ prefs::kAutofillDialogPayWithoutWallet)); |
+ EXPECT_TRUE(model()->WalletIsSelected()); |
+ |
+ // Check that nothing changes while this dialog is running if a pref changes |
+ // (this could cause subtle bugs or annoyances if a user closes another |
+ // running dialog). |
+ profile()->GetPrefs()->SetBoolean( |
+ prefs::kAutofillDialogPayWithoutWallet, true); |
+ EXPECT_TRUE(model()->WalletIsSelected()); |
+} |
+ |
+TEST_F(AccountChooserModelTest, HandlesError) { |
+ EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
+ |
+ ASSERT_TRUE(model()->WalletIsSelected()); |
+ ASSERT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ |
+ model()->SetHadWalletError(); |
+ EXPECT_FALSE(model()->WalletIsSelected()); |
+ EXPECT_FALSE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+} |
+ |
+TEST_F(AccountChooserModelTest, HandlesSigninError) { |
+ EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(1); |
+ EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(1); |
+ |
+ // 0. "Unknown" wallet account, we don't know if the user is signed-in yet. |
+ ASSERT_TRUE(model()->WalletIsSelected()); |
+ ASSERT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ ASSERT_FALSE(model()->HasAccountsToChoose()); |
+ ASSERT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
+ |
+ // 1. "Known" wallet account (e.g. after active/passive/automatic sign-in). |
+ // Calls UpdateAccountChooserView. |
+ const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); |
+ model()->SetActiveWalletAccountName(kAccount1); |
+ ASSERT_TRUE(model()->WalletIsSelected()); |
+ ASSERT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ ASSERT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
+ |
+ // 2. Sign-in failure. |
+ // Autofill data should be selected and be the only valid choice. |
+ // Calls AccountChoiceChanged. |
+ model()->SetHadWalletSigninError(); |
+ EXPECT_FALSE(model()->WalletIsSelected()); |
+ EXPECT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_FALSE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
+} |
+ |
+TEST_F(AccountChooserModelTest, RespectsUserChoice) { |
+ EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2); |
+ |
+ model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); |
+ EXPECT_FALSE(model()->WalletIsSelected()); |
+ |
+ model()->ExecuteCommand(AccountChooserModel::kActiveWalletItemId, 0); |
+ EXPECT_TRUE(model()->WalletIsSelected()); |
+} |
+ |
+TEST_F(AccountChooserModelTest, HandlesMultipleAccounts) { |
+ EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(5); |
+ EXPECT_CALL(*delegate(), UpdateAccountChooserView()).Times(4); |
+ |
+ ASSERT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ ASSERT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ |
+ // Simulate successful user info fetch / passive sign-in. |
+ // The active wallet account name should be set to the expected value. |
+ // Calls UpdateAccountChooserView. |
+ const string16 kAccount1 = ASCIIToUTF16("john.doe@gmail.com"); |
+ model()->SetActiveWalletAccountName(kAccount1); |
+ EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
+ |
+ // Process a sign-in error. |
+ // The active wallet account name should be cleared. |
+ // Calls AccountChoiceChanged. |
+ model()->SetHadWalletSigninError(); |
+ EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_FALSE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(string16(), model()->GetActiveWalletAccountName()); |
+ |
+ // Process the change of the set of additional accounts. |
+ // Calls UpdateAccountChooserView. |
+ const string16 kAccount2 = ASCIIToUTF16("john.android@gmail.com"); |
+ std::vector<string16> additional_accounts; |
+ additional_accounts.push_back(kAccount2); |
+ model()->SetAdditionalAccounts(additional_accounts); |
+ EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(3, model()->GetItemCount()); |
+ |
+ // Sign in with the new account. |
+ // Calls AccountChoiceChanged. |
+ model()->ForceSelectWalletAccount(); |
+ // Calls UpdateAccountChooserView. |
+ model()->SetActiveWalletAccountName(kAccount2); |
+ EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(2, model()->GetItemCount()); |
+ EXPECT_EQ(kAccount2, model()->GetActiveWalletAccountName()); |
+ |
+ // Switch to the autofill. |
+ // Calls AccountChoiceChanged. |
+ model()->ExecuteCommand(AccountChooserModel::kAutofillItemId, 0); |
+ EXPECT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+ EXPECT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kAutofillItemId)); |
+ |
+ // Sign in with the old account. |
+ // Calls AccountChoiceChanged. |
+ model()->ForceSelectWalletAccount(); |
+ // Calls UpdateAccountChooserView. |
+ model()->SetActiveWalletAccountName(kAccount1); |
+ EXPECT_TRUE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_TRUE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(3, model()->GetItemCount()); |
+ EXPECT_EQ(kAccount1, model()->GetActiveWalletAccountName()); |
+ |
+ // Some wallet error. |
+ // Calls AccountChoiceChanged. |
+ model()->SetHadWalletError(); |
+ EXPECT_FALSE(model()->IsActiveWalletAccountSelected()); |
+ EXPECT_FALSE(model()->HasAccountsToChoose()); |
+ EXPECT_EQ(3, model()->GetItemCount()); |
+ EXPECT_TRUE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kAutofillItemId)); |
+ EXPECT_FALSE(model()->IsCommandIdEnabled( |
+ AccountChooserModel::kActiveWalletItemId)); |
+} |
+ |
+} // namespace autofill |