summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc
blob: 653dc6349f116e315fba1c913603ac27b4c0ae50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2014 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 "chrome/browser/autofill/autofill_cc_infobar_delegate.h"

#include "base/memory/scoped_ptr.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/browser/autofill_metrics.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;

namespace autofill {

namespace {

class MockAutofillMetrics : public AutofillMetrics {
 public:
  MockAutofillMetrics() {}
  MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));

 private:
  DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
};

class TestPersonalDataManager : public PersonalDataManager {
 public:
  TestPersonalDataManager() : PersonalDataManager("en-US") {}

  using PersonalDataManager::set_database;
  using PersonalDataManager::SetPrefService;

  // Overridden to avoid a trip to the database.
  virtual void LoadProfiles() OVERRIDE {}
  virtual void LoadCreditCards() OVERRIDE {}

  MOCK_METHOD1(SaveImportedCreditCard,
               std::string(const CreditCard& imported_credit_card));

 private:
  DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
};

}  // namespace

class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness {
 public:
  virtual ~AutofillCCInfobarDelegateTest();

  virtual void SetUp() OVERRIDE;
  virtual void TearDown() OVERRIDE;

 protected:
  scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
      MockAutofillMetrics* metric_logger);

  scoped_ptr<TestPersonalDataManager> personal_data_;
};

AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}

void AutofillCCInfobarDelegateTest::SetUp() {
  ChromeRenderViewHostTestHarness::SetUp();

  // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
  test::DisableSystemServices(profile()->GetPrefs());

  PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);

  ChromeAutofillClient::CreateForWebContents(web_contents());
  ChromeAutofillClient* autofill_client =
      ChromeAutofillClient::FromWebContents(web_contents());

  personal_data_.reset(new TestPersonalDataManager());
  personal_data_->set_database(autofill_client->GetDatabase());
  personal_data_->SetPrefService(profile()->GetPrefs());
}

void AutofillCCInfobarDelegateTest::TearDown() {
  personal_data_.reset();
  ChromeRenderViewHostTestHarness::TearDown();
}

scoped_ptr<ConfirmInfoBarDelegate>
AutofillCCInfobarDelegateTest::CreateDelegate(
    MockAutofillMetrics* metric_logger) {
  EXPECT_CALL(*metric_logger,
              LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));

  CreditCard credit_card;
  return AutofillCCInfoBarDelegate::Create(
      metric_logger,
      base::Bind(
          base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard),
          base::Unretained(personal_data_.get()),
          credit_card));
}

// Test that credit card infobar metrics are logged correctly.
TEST_F(AutofillCCInfobarDelegateTest, Metrics) {
  MockAutofillMetrics metric_logger;
  ::testing::InSequence dummy;

  // Accept the infobar.
  {
    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
    ASSERT_TRUE(infobar);
    EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED));

    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
        .Times(0);
    EXPECT_TRUE(infobar->Accept());
  }

  // Cancel the infobar.
  {
    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
    ASSERT_TRUE(infobar);
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
        .Times(1);
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
        .Times(0);
    EXPECT_TRUE(infobar->Cancel());
  }

  // Dismiss the infobar.
  {
    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
    ASSERT_TRUE(infobar);
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
        .Times(1);
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
        .Times(0);
    infobar->InfoBarDismissed();
  }

  // Ignore the infobar.
  {
    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
    ASSERT_TRUE(infobar);
    EXPECT_CALL(metric_logger,
                LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
        .Times(1);
  }
}

}  // namespace autofill