Chromium Code Reviews| Index: components/autofill/core/browser/autofill_manager_unittest.cc |
| diff --git a/components/autofill/core/browser/autofill_manager_unittest.cc b/components/autofill/core/browser/autofill_manager_unittest.cc |
| index a25be3cc4b752d9026b45c6be215629f00e82a40..366a655aa7c5870b02e1ca4c4e6abaf53feb7aab 100644 |
| --- a/components/autofill/core/browser/autofill_manager_unittest.cc |
| +++ b/components/autofill/core/browser/autofill_manager_unittest.cc |
| @@ -701,10 +701,21 @@ class TestAutofillExternalDelegate : public AutofillExternalDelegate { |
| suggestion_vector.push_back(suggestion2); |
| CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]); |
| } |
| + // Check that the autofill suggestions were sent, and that they match a page |
| + // but contain no results. |
| void CheckNoSuggestions(int expected_page_id) { |
| - EXPECT_TRUE(on_suggestions_returned_seen()); |
| CheckSuggestions(expected_page_id, 0, nullptr); |
| } |
| + // Check that the autofill suggestions were sent, and that they match a page |
| + // and contain a specific number of suggestions. |
| + void CheckSuggestionCount(int expected_page_id, |
| + size_t expected_num_suggestions) { |
| + // Ensure that these results are from the most recent query. |
| + EXPECT_TRUE(on_suggestions_returned_seen_); |
| + |
| + EXPECT_EQ(expected_page_id, query_id_); |
| + ASSERT_EQ(expected_num_suggestions, suggestions_.size()); |
| + } |
| bool on_query_seen() const { |
| return on_query_seen_; |
| @@ -961,6 +972,16 @@ class AutofillManagerTest : public testing::Test { |
| form->fields[0], *card); |
| } |
| + // Convenience method for using and retrieving a mock autocomplete history |
| + // manager. |
| + MockAutocompleteHistoryManager* UseMockAutocompleteHistoryManager() { |
| + autofill_manager_->autocomplete_history_manager_.reset( |
|
Mathieu
2016/08/04 20:57:19
to avoid the cast, you can:
MockAutocompleteHisto
asredzki
2016/08/04 21:10:10
Done.
|
| + new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| + autofill_manager_->client())); |
| + return static_cast<MockAutocompleteHistoryManager*>( |
| + autofill_manager_->autocomplete_history_manager_.get()); |
|
Mathieu
2016/08/04 20:57:19
indent is off here?
asredzki
2016/08/04 21:10:10
Done.
|
| + } |
| + |
| // Convenience method to cast the FullCardRequest into a CardUnmaskDelegate. |
| CardUnmaskDelegate* full_card_unmask_delegate() { |
| DCHECK(autofill_manager_->full_card_request_); |
| @@ -1062,9 +1083,10 @@ TEST_F(AutofillManagerTest, OnFormsSeen_DifferentFormStructures) { |
| download_manager_->VerifyLastQueriedForms(forms); |
| } |
| -// Test that no suggestions are returned for a field with an unrecognized |
| -// autocomplete attribute. |
| +// Test that no autofill suggestions are returned for a field with an |
| +// unrecognized autocomplete attribute. |
| TEST_F(AutofillManagerTest, GetProfileSuggestions_UnrecognizedAttribute) { |
| + // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| form.origin = GURL("https://myform.com/form.html"); |
| @@ -1085,21 +1107,27 @@ TEST_F(AutofillManagerTest, GetProfileSuggestions_UnrecognizedAttribute) { |
| std::vector<FormData> forms(1, form); |
| FormsSeen(forms); |
| - // Suggestions should be returned for the first two fields |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
|
Mathieu
2016/08/04 20:57:18
I don't like "Use", perhaps "Recreate"?
asredzki
2016/08/04 21:10:10
Done.
|
| + |
| + // Ensure that autocomplete manager is not called for suggestions either. |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| + |
| + // Suggestions should be returned for the first two fields. |
| GetAutofillSuggestions(form, form.fields[0]); |
| - EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); |
| + external_delegate_->CheckSuggestionCount(kDefaultPageID, 2); |
| GetAutofillSuggestions(form, form.fields[1]); |
| - EXPECT_TRUE(external_delegate_->on_suggestions_returned_seen()); |
| + external_delegate_->CheckSuggestionCount(kDefaultPageID, 2); |
| - // Suggestions should not be returned for the third field because of its |
| + // No suggestions should not be provided for the third field because of its |
| // unrecognized autocomplete attribute. |
| GetAutofillSuggestions(form, form.fields[2]); |
| - EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); |
| + external_delegate_->CheckNoSuggestions(kDefaultPageID); |
| } |
| // Test that no suggestions are returned when there are less than three fields |
| // and none of them have an autocomplete attribute. |
| TEST_F(AutofillManagerTest, GetProfileSuggestions_SmallFormNoAutocomplete) { |
| + // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| form.origin = GURL("https://myform.com/form.html"); |
| @@ -1113,6 +1141,10 @@ TEST_F(AutofillManagerTest, GetProfileSuggestions_SmallFormNoAutocomplete) { |
| std::vector<FormData> forms(1, form); |
| FormsSeen(forms); |
| + // Ensure that autocomplete manager is called for both fields. |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(2); |
| + |
| GetAutofillSuggestions(form, form.fields[0]); |
| EXPECT_FALSE(external_delegate_->on_suggestions_returned_seen()); |
| @@ -1124,6 +1156,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestions_SmallFormNoAutocomplete) { |
| // attribute, suggestions are only made for the one that has the attribute. |
| TEST_F(AutofillManagerTest, |
| GetProfileSuggestions_SmallFormWithOneAutocomplete) { |
| + // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| form.origin = GURL("https://myform.com/form.html"); |
| @@ -1156,6 +1189,7 @@ TEST_F(AutofillManagerTest, |
| // suggestions are made for both fields. |
| TEST_F(AutofillManagerTest, |
| GetProfileSuggestions_SmallFormWithTwoAutocomplete) { |
| + // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| form.origin = GURL("https://myform.com/form.html"); |
| @@ -1184,7 +1218,7 @@ TEST_F(AutofillManagerTest, |
| // Test that we return all address profile suggestions when all form fields are |
| // empty. |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_EmptyValue) { |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| @@ -1204,7 +1238,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestionsEmptyValue) { |
| // Test that we return only matching address profile suggestions when the |
| // selected form field has been partially filled out. |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsMatchCharacter) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_MatchCharacter) { |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| @@ -1296,7 +1330,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestions_AlreadyAutofilledNoLabels) { |
| } |
| // Test that we return no suggestions when the form has no relevant fields. |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_UnknownFields) { |
| // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| @@ -1321,7 +1355,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestionsUnknownFields) { |
| } |
| // Test that we cull duplicate profile suggestions. |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_WithDuplicates) { |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| @@ -1345,7 +1379,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestionsWithDuplicates) { |
| } |
| // Test that we return no suggestions when autofill is disabled. |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_AutofillDisabledByUser) { |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| @@ -1466,7 +1500,7 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestions_StopCharsWithInput) { |
| // Test that we return only matching credit card profile suggestions when the |
| // selected form field has been partially filled out. |
| -TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) { |
| +TEST_F(AutofillManagerTest, GetCreditCardSuggestions_MatchCharacter) { |
| // Set up our form data. |
| FormData form; |
| CreateTestCreditCardFormData(&form, true, false); |
| @@ -1487,7 +1521,7 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestionsMatchCharacter) { |
| // Test that we return credit card profile suggestions when the selected form |
| // field is not the credit card number field. |
| -TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) { |
| +TEST_F(AutofillManagerTest, GetCreditCardSuggestions_NonCCNumber) { |
| // Set up our form data. |
| FormData form; |
| CreateTestCreditCardFormData(&form, true, false); |
| @@ -1520,7 +1554,7 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonCCNumber) { |
| // Test that we return a warning explaining that credit card profile suggestions |
| // are unavailable when the form is not secure. |
| -TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) { |
| +TEST_F(AutofillManagerTest, GetCreditCardSuggestions_NonHTTPS) { |
| // Set up our form data. |
| FormData form; |
| CreateTestCreditCardFormData(&form, false, false); |
| @@ -1546,7 +1580,7 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestionsNonHTTPS) { |
| // Test that we return all credit card suggestions in the case that two cards |
| // have the same obfuscated number. |
| -TEST_F(AutofillManagerTest, GetCreditCardSuggestionsRepeatedObfuscatedNumber) { |
| +TEST_F(AutofillManagerTest, GetCreditCardSuggestions_RepeatedObfuscatedNumber) { |
| // Add a credit card with the same obfuscated number as Elvis's. |
| // |credit_card| will be owned by the mock PersonalDataManager. |
| CreditCard* credit_card = new CreditCard; |
| @@ -1727,7 +1761,7 @@ TEST_F(AutofillManagerTest, GetFieldSuggestionsWithDuplicateValues) { |
| Suggestion("Elvis", "", "", 1)); |
| } |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_FancyPhone) { |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| @@ -1754,7 +1788,7 @@ TEST_F(AutofillManagerTest, GetProfileSuggestionsFancyPhone) { |
| Suggestion("12345678901", "3734 Elvis Presley Blvd.", "", 3)); |
| } |
| -TEST_F(AutofillManagerTest, GetProfileSuggestionsForPhonePrefixOrSuffix) { |
| +TEST_F(AutofillManagerTest, GetProfileSuggestions_ForPhonePrefixOrSuffix) { |
| // Set up our form data. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| @@ -2966,15 +3000,12 @@ TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) { |
| autofill_manager_.reset( |
| new TestAutofillManager(autofill_driver_.get(), &client, NULL)); |
| autofill_manager_->set_autofill_enabled(false); |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), &client)); |
| // Set up our form data. |
| FormData form; |
| test::CreateTestAddressFormData(&form); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| + |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| EXPECT_CALL(*m, OnWillSubmitForm(_)); |
| FormSubmitted(form); |
| } |
| @@ -2996,13 +3027,8 @@ TEST_F(AutofillManagerTest, AutocompleteSuggestions_SomeWhenAutofillDisabled) { |
| const FormFieldData& field = form.fields[0]; |
| // Expect Autocomplete manager to be called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), &client)); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)); |
| GetAutofillSuggestions(form, field); |
| } |
| @@ -3026,13 +3052,9 @@ TEST_F(AutofillManagerTest, |
| field.should_autocomplete = false; |
| // Autocomplete manager is not called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), &client)); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| + |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| GetAutofillSuggestions(form, field); |
| } |
| @@ -3049,14 +3071,8 @@ TEST_F(AutofillManagerTest, AutocompleteSuggestions_NoneWhenAutofillPresent) { |
| const FormFieldData& field = form.fields[0]; |
| // Autocomplete manager is not called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| - autofill_manager_->client())); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| GetAutofillSuggestions(form, field); |
| @@ -3082,14 +3098,8 @@ TEST_F(AutofillManagerTest, AutocompleteSuggestions_SomeWhenAutofillEmpty) { |
| test::CreateTestFormField("Email", "email", "donkey", "email", &field); |
| // Autocomplete manager is called for suggestions because Autofill is empty. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| - autofill_manager_->client())); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)); |
| GetAutofillSuggestions(form, field); |
| } |
| @@ -3115,12 +3125,7 @@ TEST_F(AutofillManagerTest, |
| field.should_autocomplete = true; |
| // Autocomplete manager is not called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| - autofill_manager_->client())); |
| - MockAutocompleteHistoryManager* m = |
| - static_cast<MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)); |
| GetAutofillSuggestions(form, field); |
| @@ -3146,11 +3151,7 @@ TEST_F(AutofillManagerTest, |
| field.should_autocomplete = true; |
| // Autocomplete manager is not called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), &client)); |
| - MockAutocompleteHistoryManager* m = |
| - static_cast<MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| GetAutofillSuggestions(form, field); |
| @@ -3173,14 +3174,8 @@ TEST_F( |
| test::CreateTestFormField("Email", "email", "donkey", "email", &field); |
| // Autocomplete manager is not called for suggestions. |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| - autofill_manager_->client())); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| GetAutofillSuggestions(form, field); |
| } |
| @@ -3192,13 +3187,8 @@ TEST_F(AutofillManagerTest, AutocompleteOffRespectedForAutocomplete) { |
| autofill_manager_->set_autofill_enabled(false); |
| autofill_manager_->SetExternalDelegate(external_delegate_.get()); |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), &client)); |
| - MockAutocompleteHistoryManager* m = static_cast< |
| - MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*m, |
| - OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnGetAutocompleteSuggestions(_, _, _, _)).Times(0); |
| // Set up our form data. |
| FormData form; |
| @@ -4125,7 +4115,8 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestions_UnrecognizedAttribute) { |
| // Test to verify suggestions appears for forms having credit card number split |
| // across fields. |
| -TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) { |
| +TEST_F(AutofillManagerTest, |
| + GetCreditCardSuggestions_ForNumberSpitAcrossFields) { |
| // Set up our form data with credit card number split across fields. |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |
| @@ -4182,15 +4173,9 @@ TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) { |
| // Test that inputs detected to be CVC inputs are forced to |
| // !should_autocomplete for AutocompleteHistoryManager::OnWillSubmitForm. |
| TEST_F(AutofillManagerTest, DontSaveCvcInAutocompleteHistory) { |
| - autofill_manager_->autocomplete_history_manager_.reset( |
| - new MockAutocompleteHistoryManager(autofill_driver_.get(), |
| - &autofill_client_)); |
| FormData form_seen_by_ahm; |
| - MockAutocompleteHistoryManager* mock_ahm = |
| - static_cast<MockAutocompleteHistoryManager*>( |
| - autofill_manager_->autocomplete_history_manager_.get()); |
| - EXPECT_CALL(*mock_ahm, OnWillSubmitForm(_)) |
| - .WillOnce(SaveArg<0>(&form_seen_by_ahm)); |
| + MockAutocompleteHistoryManager* m = UseMockAutocompleteHistoryManager(); |
| + EXPECT_CALL(*m, OnWillSubmitForm(_)).WillOnce(SaveArg<0>(&form_seen_by_ahm)); |
| FormData form; |
| form.name = ASCIIToUTF16("MyForm"); |