| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/string16.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/autocomplete_history_manager.h" | |
| 11 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 13 #include "chrome/browser/webdata/web_data_service.h" | |
| 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 15 #include "chrome/test/base/testing_browser_process.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "content/public/test/test_browser_thread.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "ui/gfx/rect.h" | |
| 21 #include "webkit/forms/form_data.h" | |
| 22 | |
| 23 using content::BrowserThread; | |
| 24 using content::WebContents; | |
| 25 using testing::_; | |
| 26 using webkit::forms::FormData; | |
| 27 | |
| 28 class MockWebDataService : public WebDataService { | |
| 29 public: | |
| 30 MOCK_METHOD1(AddFormFields, | |
| 31 void(const std::vector<webkit::forms::FormField>&)); // NOLINT | |
| 32 | |
| 33 protected: | |
| 34 virtual ~MockWebDataService() {} | |
| 35 }; | |
| 36 | |
| 37 class AutocompleteHistoryManagerTest : public ChromeRenderViewHostTestHarness { | |
| 38 protected: | |
| 39 AutocompleteHistoryManagerTest() | |
| 40 : ui_thread_(BrowserThread::UI, MessageLoopForUI::current()), | |
| 41 db_thread_(BrowserThread::DB) { | |
| 42 } | |
| 43 | |
| 44 virtual void SetUp() { | |
| 45 ChromeRenderViewHostTestHarness::SetUp(); | |
| 46 web_data_service_ = new MockWebDataService(); | |
| 47 autocomplete_manager_.reset(new AutocompleteHistoryManager( | |
| 48 contents(), &profile_, web_data_service_)); | |
| 49 } | |
| 50 | |
| 51 content::TestBrowserThread ui_thread_; | |
| 52 content::TestBrowserThread db_thread_; | |
| 53 | |
| 54 TestingProfile profile_; | |
| 55 scoped_refptr<MockWebDataService> web_data_service_; | |
| 56 scoped_ptr<AutocompleteHistoryManager> autocomplete_manager_; | |
| 57 }; | |
| 58 | |
| 59 // Tests that credit card numbers are not sent to the WebDatabase to be saved. | |
| 60 TEST_F(AutocompleteHistoryManagerTest, CreditCardNumberValue) { | |
| 61 FormData form; | |
| 62 form.name = ASCIIToUTF16("MyForm"); | |
| 63 form.method = ASCIIToUTF16("POST"); | |
| 64 form.origin = GURL("http://myform.com/form.html"); | |
| 65 form.action = GURL("http://myform.com/submit.html"); | |
| 66 form.user_submitted = true; | |
| 67 | |
| 68 // Valid Visa credit card number pulled from the paypal help site. | |
| 69 webkit::forms::FormField valid_cc; | |
| 70 valid_cc.label = ASCIIToUTF16("Credit Card"); | |
| 71 valid_cc.name = ASCIIToUTF16("ccnum"); | |
| 72 valid_cc.value = ASCIIToUTF16("4012888888881881"); | |
| 73 valid_cc.form_control_type = ASCIIToUTF16("text"); | |
| 74 form.fields.push_back(valid_cc); | |
| 75 | |
| 76 EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0); | |
| 77 autocomplete_manager_->OnFormSubmitted(form); | |
| 78 } | |
| 79 | |
| 80 // Contrary test to AutocompleteHistoryManagerTest.CreditCardNumberValue. The | |
| 81 // value being submitted is not a valid credit card number, so it will be sent | |
| 82 // to the WebDatabase to be saved. | |
| 83 TEST_F(AutocompleteHistoryManagerTest, NonCreditCardNumberValue) { | |
| 84 FormData form; | |
| 85 form.name = ASCIIToUTF16("MyForm"); | |
| 86 form.method = ASCIIToUTF16("POST"); | |
| 87 form.origin = GURL("http://myform.com/form.html"); | |
| 88 form.action = GURL("http://myform.com/submit.html"); | |
| 89 form.user_submitted = true; | |
| 90 | |
| 91 // Invalid credit card number. | |
| 92 webkit::forms::FormField invalid_cc; | |
| 93 invalid_cc.label = ASCIIToUTF16("Credit Card"); | |
| 94 invalid_cc.name = ASCIIToUTF16("ccnum"); | |
| 95 invalid_cc.value = ASCIIToUTF16("4580123456789012"); | |
| 96 invalid_cc.form_control_type = ASCIIToUTF16("text"); | |
| 97 form.fields.push_back(invalid_cc); | |
| 98 | |
| 99 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | |
| 100 autocomplete_manager_->OnFormSubmitted(form); | |
| 101 } | |
| 102 | |
| 103 // Tests that SSNs are not sent to the WebDatabase to be saved. | |
| 104 TEST_F(AutocompleteHistoryManagerTest, SSNValue) { | |
| 105 FormData form; | |
| 106 form.name = ASCIIToUTF16("MyForm"); | |
| 107 form.method = ASCIIToUTF16("POST"); | |
| 108 form.origin = GURL("http://myform.com/form.html"); | |
| 109 form.action = GURL("http://myform.com/submit.html"); | |
| 110 form.user_submitted = true; | |
| 111 | |
| 112 webkit::forms::FormField ssn; | |
| 113 ssn.label = ASCIIToUTF16("Social Security Number"); | |
| 114 ssn.name = ASCIIToUTF16("ssn"); | |
| 115 ssn.value = ASCIIToUTF16("078-05-1120"); | |
| 116 ssn.form_control_type = ASCIIToUTF16("text"); | |
| 117 form.fields.push_back(ssn); | |
| 118 | |
| 119 EXPECT_CALL(*web_data_service_, AddFormFields(_)).Times(0); | |
| 120 autocomplete_manager_->OnFormSubmitted(form); | |
| 121 } | |
| 122 | |
| 123 // Verify that autocomplete text is saved for search fields. | |
| 124 TEST_F(AutocompleteHistoryManagerTest, SearchField) { | |
| 125 FormData form; | |
| 126 form.name = ASCIIToUTF16("MyForm"); | |
| 127 form.method = ASCIIToUTF16("POST"); | |
| 128 form.origin = GURL("http://myform.com/form.html"); | |
| 129 form.action = GURL("http://myform.com/submit.html"); | |
| 130 form.user_submitted = true; | |
| 131 | |
| 132 // Search field. | |
| 133 webkit::forms::FormField search_field; | |
| 134 search_field.label = ASCIIToUTF16("Search"); | |
| 135 search_field.name = ASCIIToUTF16("search"); | |
| 136 search_field.value = ASCIIToUTF16("my favorite query"); | |
| 137 search_field.form_control_type = ASCIIToUTF16("search"); | |
| 138 form.fields.push_back(search_field); | |
| 139 | |
| 140 EXPECT_CALL(*(web_data_service_.get()), AddFormFields(_)).Times(1); | |
| 141 autocomplete_manager_->OnFormSubmitted(form); | |
| 142 } | |
| 143 | |
| 144 namespace { | |
| 145 | |
| 146 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { | |
| 147 public: | |
| 148 explicit MockAutofillExternalDelegate(TabContents* tab_contents) | |
| 149 : TestAutofillExternalDelegate(tab_contents, NULL) {} | |
| 150 virtual ~MockAutofillExternalDelegate() {} | |
| 151 | |
| 152 virtual void ApplyAutofillSuggestions( | |
| 153 const std::vector<string16>& autofill_values, | |
| 154 const std::vector<string16>& autofill_labels, | |
| 155 const std::vector<string16>& autofill_icons, | |
| 156 const std::vector<int>& autofill_unique_ids) OVERRIDE {}; | |
| 157 | |
| 158 MOCK_METHOD5(OnSuggestionsReturned, | |
| 159 void(int query_id, | |
| 160 const std::vector<string16>& autofill_values, | |
| 161 const std::vector<string16>& autofill_labels, | |
| 162 const std::vector<string16>& autofill_icons, | |
| 163 const std::vector<int>& autofill_unique_ids)); | |
| 164 | |
| 165 private: | |
| 166 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate); | |
| 167 }; | |
| 168 | |
| 169 class AutocompleteHistoryManagerStubSend : public AutocompleteHistoryManager { | |
| 170 public: | |
| 171 explicit AutocompleteHistoryManagerStubSend(WebContents* web_contents, | |
| 172 Profile* profile, | |
| 173 WebDataService* wds) | |
| 174 : AutocompleteHistoryManager(web_contents, profile, wds) {} | |
| 175 | |
| 176 // Intentionally swallow the message. | |
| 177 virtual bool Send(IPC::Message* message) { delete message; return true; } | |
| 178 }; | |
| 179 | |
| 180 } // namespace | |
| 181 | |
| 182 // Make sure our external delegate is called at the right time. | |
| 183 TEST_F(AutocompleteHistoryManagerTest, ExternalDelegate) { | |
| 184 // Local version with a stubbed out Send() | |
| 185 AutocompleteHistoryManagerStubSend autocomplete_history_manager( | |
| 186 contents(), | |
| 187 &profile_, web_data_service_); | |
| 188 | |
| 189 MockAutofillExternalDelegate external_delegate( | |
| 190 TabContents::FromWebContents(contents())); | |
| 191 EXPECT_CALL(external_delegate, OnSuggestionsReturned(_, _, _, _, _)); | |
| 192 autocomplete_history_manager.SetExternalDelegate(&external_delegate); | |
| 193 | |
| 194 // Should trigger a call to OnSuggestionsReturned, verified by the mock. | |
| 195 autocomplete_history_manager.SendSuggestions(NULL); | |
| 196 } | |
| OLD | NEW |