autocomplete_history_manager_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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 <vector>
6
7#include "base/memory/ref_counted.h"
8#include "base/message_loop/message_loop_proxy.h"
9#include "base/prefs/pref_service.h"
10#include "base/run_loop.h"
11#include "base/strings/string16.h"
12#include "base/strings/utf_string_conversions.h"
13#include "base/synchronization/waitable_event.h"
14#include "chrome/browser/webdata/web_data_service_factory.h"
15#include "chrome/test/base/chrome_render_view_host_test_harness.h"
16#include "chrome/test/base/testing_profile.h"
17#include "components/autofill/core/browser/autocomplete_history_manager.h"
18#include "components/autofill/core/browser/autofill_external_delegate.h"
19#include "components/autofill/core/browser/autofill_manager.h"
20#include "components/autofill/core/browser/autofill_test_utils.h"
21#include "components/autofill/core/browser/test_autofill_driver.h"
22#include "components/autofill/core/browser/test_autofill_manager_delegate.h"
23#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
24#include "components/autofill/core/common/form_data.h"
25#include "components/webdata/common/web_data_service_test_util.h"
26#include "content/public/test/test_browser_thread.h"
27#include "content/public/test/test_utils.h"
28#include "testing/gmock/include/gmock/gmock.h"
29#include "testing/gtest/include/gtest/gtest.h"
30#include "ui/gfx/rect.h"
31
32using testing::_;
33
34namespace autofill {
35
36namespace {
37
38class MockWebDataService : public AutofillWebDataService {
39 public:
40  MockWebDataService()
41      : AutofillWebDataService(base::MessageLoopProxy::current(),
42                               base::MessageLoopProxy::current()) {}
43
44  MOCK_METHOD1(AddFormFields, void(const std::vector<FormFieldData>&));
45
46 protected:
47  virtual ~MockWebDataService() {}
48};
49
50class MockAutofillManagerDelegate
51    : public autofill::TestAutofillManagerDelegate {
52 public:
53  MockAutofillManagerDelegate(
54      scoped_refptr<MockWebDataService> web_data_service)
55      : web_data_service_(web_data_service),
56        prefs_(test::PrefServiceForTesting()) {
57  }
58  virtual ~MockAutofillManagerDelegate() {}
59  virtual scoped_refptr<AutofillWebDataService>
60      GetDatabase() OVERRIDE { return web_data_service_; }
61  virtual PrefService* GetPrefs() OVERRIDE { return prefs_.get(); }
62
63 private:
64  scoped_refptr<MockWebDataService> web_data_service_;
65  scoped_ptr<PrefService> prefs_;
66
67  DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate);
68};
69
70}  // namespace
71
72class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness {
73 protected:
74  virtual void SetUp() OVERRIDE {
75    ChromeRenderViewHostTestHarness::SetUp();
76    web_data_service_ = new MockWebDataService();
77    manager_delegate_.reset(new MockAutofillManagerDelegate(web_data_service_));
78    autofill_driver_.reset(new TestAutofillDriver(web_contents()));
79    autocomplete_manager_.reset(
80        new AutocompleteHistoryManager(autofill_driver_.get(),
81                                       manager_delegate_.get()));
82  }
83
84  virtual void TearDown() OVERRIDE {
85    autocomplete_manager_.reset();
86    ChromeRenderViewHostTestHarness::TearDown();
87  }
88
89  scoped_refptr<MockWebDataService> web_data_service_;
90  scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_;
91  scoped_ptr<AutofillDriver> autofill_driver_;
92  scoped_ptr<MockAutofillManagerDelegate> manager_delegate_;
93};
94
95// Tests that credit card numbers are not sent to the WebDatabase to be saved.
96TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) {
97  FormData form;
98  form.name = ASCIIToUTF16("MyForm");
99  form.method = ASCIIToUTF16("POST");
100  form.origin = GURL("http://myform.com/form.html");
101  form.action = GURL("http://myform.com/submit.html");
102  form.user_submitted = true;
103
104  // Valid Visa credit card number pulled from the paypal help site.
105  FormFieldData valid_cc;
106  valid_cc.label = ASCIIToUTF16("Credit Card");
107  valid_cc.name = ASCIIToUTF16("ccnum");
108  valid_cc.value = ASCIIToUTF16("4012888888881881");
109  valid_cc.form_control_type = "text";
110  form.fields.push_back(valid_cc);
111
112  EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0);
113  autocomplete_manager_->OnFormSubmitted(form);
114}
115
116// Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue.  The
117// value being submitted is not a valid credit card number, so it will be sent
118// to the WebDatabase to be saved.
119TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) {
120  FormData form;
121  form.name = ASCIIToUTF16("MyForm");
122  form.method = ASCIIToUTF16("POST");
123  form.origin = GURL("http://myform.com/form.html");
124  form.action = GURL("http://myform.com/submit.html");
125  form.user_submitted = true;
126
127  // Invalid credit card number.
128  FormFieldData invalid_cc;
129  invalid_cc.label = ASCIIToUTF16("Credit Card");
130  invalid_cc.name = ASCIIToUTF16("ccnum");
131  invalid_cc.value = ASCIIToUTF16("4580123456789012");
132  invalid_cc.form_control_type = "text";
133  form.fields.push_back(invalid_cc);
134
135  EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1);
136  autocomplete_manager_->OnFormSubmitted(form);
137}
138
139// Tests that SSNs are not sent to the WebDatabase to be saved.
140TEST_F(AutocompleteHistoryManagerTest, SSNValue) {
141  FormData form;
142  form.name = ASCIIToUTF16("MyForm");
143  form.method = ASCIIToUTF16("POST");
144  form.origin = GURL("http://myform.com/form.html");
145  form.action = GURL("http://myform.com/submit.html");
146  form.user_submitted = true;
147
148  FormFieldData ssn;
149  ssn.label = ASCIIToUTF16("Social Security Number");
150  ssn.name = ASCIIToUTF16("ssn");
151  ssn.value = ASCIIToUTF16("078-05-1120");
152  ssn.form_control_type = "text";
153  form.fields.push_back(ssn);
154
155  EXPECT_CALL(*web_data_service_.get(), AddFormFields(_)).Times(0);
156  autocomplete_manager_->OnFormSubmitted(form);
157}
158
159// Verify that autocomplete text is saved for search fields.
160TEST_F(AutocompleteHistoryManagerTest, SearchField) {
161  FormData form;
162  form.name = ASCIIToUTF16("MyForm");
163  form.method = ASCIIToUTF16("POST");
164  form.origin = GURL("http://myform.com/form.html");
165  form.action = GURL("http://myform.com/submit.html");
166  form.user_submitted = true;
167
168  // Search field.
169  FormFieldData search_field;
170  search_field.label = ASCIIToUTF16("Search");
171  search_field.name = ASCIIToUTF16("search");
172  search_field.value = ASCIIToUTF16("my favorite query");
173  search_field.form_control_type = "search";
174  form.fields.push_back(search_field);
175
176  EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1);
177  autocomplete_manager_->OnFormSubmitted(form);
178}
179
180namespace {
181
182class MockAutofillExternalDelegate : public AutofillExternalDelegate {
183 public:
184  MockAutofillExternalDelegate(AutofillManager* autofill_manager,
185                               AutofillDriver* autofill_driver)
186      : AutofillExternalDelegate(autofill_manager, autofill_driver) {}
187  virtual ~MockAutofillExternalDelegate() {}
188
189  MOCK_METHOD5(OnSuggestionsReturned,
190               void(int query_id,
191                    const std::vector<base::string16>& autofill_values,
192                    const std::vector<base::string16>& autofill_labels,
193                    const std::vector<base::string16>& autofill_icons,
194                    const std::vector<int>& autofill_unique_ids));
195
196 private:
197  DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate);
198};
199
200class AutocompleteHistoryManagerNoIPC : public AutocompleteHistoryManager {
201  public:
202   AutocompleteHistoryManagerNoIPC(AutofillDriver* driver,
203                                   AutofillManagerDelegate* delegate)
204       : AutocompleteHistoryManager(driver, delegate) {
205     // Ensure that IPC is not sent during the test.
206     set_send_ipc(false);
207   }
208
209  using AutocompleteHistoryManager::SendSuggestions;
210};
211
212}  // namespace
213
214// Make sure our external delegate is called at the right time.
215TEST_F(AutocompleteHistoryManagerTest, ExternalDelegate) {
216  AutocompleteHistoryManagerNoIPC autocomplete_history_manager(
217      autofill_driver_.get(), manager_delegate_.get());
218
219  scoped_ptr<AutofillManager> autofill_manager(new AutofillManager(
220      autofill_driver_.get(),
221      manager_delegate_.get(),
222      "en-US",
223      AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER));
224
225  MockAutofillExternalDelegate external_delegate(autofill_manager.get(),
226                                                 autofill_driver_.get());
227  autocomplete_history_manager.SetExternalDelegate(&external_delegate);
228
229  // Should trigger a call to OnSuggestionsReturned, verified by the mock.
230  EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _));
231  autocomplete_history_manager.SendSuggestions(NULL);
232}
233
234}  // namespace autofill
235