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