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