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