autofill_dialog_models_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "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"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace autofill {
13
14namespace {
15
16class MockAccountChooserModelDelegate : public AccountChooserModelDelegate {
17 public:
18  MockAccountChooserModelDelegate() {}
19  virtual ~MockAccountChooserModelDelegate() {}
20
21  MOCK_METHOD0(AccountChoiceChanged, void());
22};
23
24class 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
41TEST_F(AccountChooserModelTest, ObeysPref) {
42  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);
43
44  profile()->GetPrefs()->SetBoolean(
45      prefs::kAutofillDialogPayWithoutWallet, false);
46  EXPECT_TRUE(model()->WalletIsSelected());
47
48  profile()->GetPrefs()->SetBoolean(
49      prefs::kAutofillDialogPayWithoutWallet, true);
50  EXPECT_FALSE(model()->WalletIsSelected());
51}
52
53TEST_F(AccountChooserModelTest, HandlesError) {
54  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(2);
55
56  profile()->GetPrefs()->SetBoolean(
57      prefs::kAutofillDialogPayWithoutWallet, false);
58  EXPECT_TRUE(model()->WalletIsSelected());
59
60  model()->SetHadWalletError();
61  EXPECT_FALSE(model()->WalletIsSelected());
62  EXPECT_FALSE(model()->IsCommandIdEnabled(0));
63}
64
65TEST_F(AccountChooserModelTest, RespectsUserChoice) {
66  EXPECT_CALL(*delegate(), AccountChoiceChanged()).Times(3);
67
68  profile()->GetPrefs()->SetBoolean(
69      prefs::kAutofillDialogPayWithoutWallet, false);
70  EXPECT_TRUE(model()->WalletIsSelected());
71
72  model()->ExecuteCommand(1, 0);
73  EXPECT_FALSE(model()->WalletIsSelected());
74
75  model()->ExecuteCommand(0, 0);
76  EXPECT_TRUE(model()->WalletIsSelected());
77}
78
79}  // namespace autofill
80