diff options
Diffstat (limited to 'chrome/browser/autofill/autofill_manager_unittest.cc')
-rw-r--r-- | chrome/browser/autofill/autofill_manager_unittest.cc | 273 |
1 files changed, 239 insertions, 34 deletions
diff --git a/chrome/browser/autofill/autofill_manager_unittest.cc b/chrome/browser/autofill/autofill_manager_unittest.cc index 264dc80..182148d 100644 --- a/chrome/browser/autofill/autofill_manager_unittest.cc +++ b/chrome/browser/autofill/autofill_manager_unittest.cc @@ -4,6 +4,7 @@ #include <vector> +#include "app/l10n_util.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/scoped_vector.h" @@ -23,6 +24,7 @@ #include "chrome/common/pref_names.h" #include "chrome/common/render_messages.h" #include "googleurl/src/gurl.h" +#include "grit/generated_resources.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/glue/form_data.h" #include "webkit/glue/form_field.h" @@ -59,6 +61,14 @@ class TestPersonalDataManager : public PersonalDataManager { web_profiles_->push_back(profile); } + void ClearAutoFillProfiles() { + web_profiles_.reset(); + } + + void ClearCreditCards() { + credit_cards_.reset(); + } + private: void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) { AutoFillProfile* profile = new AutoFillProfile; @@ -110,7 +120,8 @@ class TestAutoFillManager : public AutoFillManager { public: TestAutoFillManager(TabContents* tab_contents, TestPersonalDataManager* personal_manager) - : AutoFillManager(tab_contents, NULL) { + : AutoFillManager(tab_contents, NULL), + autofill_enabled_(true) { test_personal_data_ = personal_manager; set_personal_data_manager(personal_manager); // Download manager requests are disabled for purposes of this unit-test. @@ -118,7 +129,11 @@ class TestAutoFillManager : public AutoFillManager { set_disable_download_manager_requests(true); } - virtual bool IsAutoFillEnabled() const { return true; } + virtual bool IsAutoFillEnabled() const { return autofill_enabled_; } + + void set_autofill_enabled(bool autofill_enabled) { + autofill_enabled_ = autofill_enabled; + } AutoFillProfile* GetLabeledProfile(const char* label) { return test_personal_data_->GetLabeledProfile(label); @@ -130,6 +145,7 @@ class TestAutoFillManager : public AutoFillManager { private: TestPersonalDataManager* test_personal_data_; + bool autofill_enabled_; DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); }; @@ -293,11 +309,12 @@ TEST_F(AutoFillManagerTest, GetProfileSuggestionsEmptyValue) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -338,11 +355,12 @@ TEST_F(AutoFillManagerTest, GetProfileSuggestionsMatchCharacter) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "E", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -394,8 +412,109 @@ TEST_F(AutoFillManagerTest, GetProfileSuggestionsUnknownFields) { autofill_test::CreateTestFormField( "Username", "username", "", "text", &field); + rvh()->ResetAutoFillState(kPageID); EXPECT_FALSE( - autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + autofill_manager_->GetAutoFillSuggestions(false, field)); +} + +// Test that we return no suggestions when autofill is disabled. +TEST_F(AutoFillManagerTest, GetProfileSuggestionsAutofillDisabledByUser) { + FormData form; + CreateTestAddressFormData(&form); + + // Set up our FormStructures. + std::vector<FormData> forms; + forms.push_back(form); + autofill_manager_->FormsSeen(forms); + + // Disable AutoFill. + autofill_manager_->set_autofill_enabled(false); + + // The page ID sent to the AutoFillManager from the RenderView, used to send + // an IPC message back to the renderer. + const int kPageID = 1; + + webkit_glue::FormField field; + autofill_test::CreateTestFormField( + "First Name", "firstname", "", "text", &field); + rvh()->ResetAutoFillState(kPageID); + EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(false, field)); +} + +// Test that we return a warning explaining that autofill suggestions are +// unavailable when the form method is GET rather than POST. +TEST_F(AutoFillManagerTest, GetProfileSuggestionsMethodGet) { + FormData form; + CreateTestAddressFormData(&form); + form.method = ASCIIToUTF16("GET"); + + // Set up our FormStructures. + std::vector<FormData> forms; + forms.push_back(form); + autofill_manager_->FormsSeen(forms); + + // The page ID sent to the AutoFillManager from the RenderView, used to send + // an IPC message back to the renderer. + const int kPageID = 1; + + webkit_glue::FormField field; + autofill_test::CreateTestFormField( + "First Name", "firstname", "", "text", &field); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); + + // No suggestions provided, so send an empty vector as the results. + // This triggers the combined message send. + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); + + // Test that we sent the right message to the renderer. + int page_id = 0; + std::vector<string16> values; + std::vector<string16> labels; + std::vector<string16> icons; + EXPECT_TRUE(GetAutoFillSuggestionsMessage(&page_id, &values, &labels, + &icons)); + EXPECT_EQ(kPageID, page_id); + ASSERT_EQ(1U, values.size()); + EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED), + values[0]); + ASSERT_EQ(1U, labels.size()); + EXPECT_EQ(string16(), labels[0]); + ASSERT_EQ(1U, icons.size()); + EXPECT_EQ(string16(), icons[0]); + + // Now add some Autocomplete suggestions. We should return the autocomplete + // suggestions and the warning; these will be culled by the renderer. + process()->sink().ClearMessages(); + const int kPageID2 = 2; + rvh()->ResetAutoFillState(kPageID2); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); + + std::vector<string16> suggestions; + suggestions.push_back(ASCIIToUTF16("Jay")); + suggestions.push_back(ASCIIToUTF16("Jason")); + rvh()->AutocompleteSuggestionsReturned(suggestions); + + EXPECT_TRUE(GetAutoFillSuggestionsMessage(&page_id, &values, &labels, + &icons)); + EXPECT_EQ(kPageID2, page_id); + ASSERT_EQ(3U, values.size()); + EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED), + values[0]); + EXPECT_EQ(ASCIIToUTF16("Jay"), values[1]); + EXPECT_EQ(ASCIIToUTF16("Jason"), values[2]); + ASSERT_EQ(3U, labels.size()); + EXPECT_EQ(string16(), labels[0]); + EXPECT_EQ(string16(), labels[1]); + EXPECT_EQ(string16(), labels[2]); + ASSERT_EQ(3U, icons.size()); + EXPECT_EQ(string16(), icons[0]); + EXPECT_EQ(string16(), icons[1]); + EXPECT_EQ(string16(), icons[2]); + + // Now clear the test profiles and try again -- we shouldn't return a warning. + test_personal_data_->ClearAutoFillProfiles(); + EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(false, field)); } // Test that we return all credit card profile suggestions when all form fields @@ -416,11 +535,12 @@ TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsEmptyValue) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "Card Number", "cardnumber", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -459,11 +579,12 @@ TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsMatchCharacter) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "Card Number", "cardnumber", "4", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -499,11 +620,12 @@ TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsNonCCNumber) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "Name on Card", "nameoncard", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -524,8 +646,8 @@ TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsNonCCNumber) { EXPECT_EQ(ASCIIToUTF16("masterCardCC"), icons[1]); } -// Test that we return no credit card profile suggestions when the form is not -// https. +// Test that we return a warning explaining that credit card profile suggestions +// are unavailable when the form is not https. TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsNonHTTPS) { FormData form; CreateTestCreditCardFormData(&form, false); @@ -542,8 +664,61 @@ TEST_F(AutoFillManagerTest, GetCreditCardSuggestionsNonHTTPS) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "Card Number", "cardnumber", "", "text", &field); - EXPECT_FALSE( - autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); + + // No suggestions provided, so send an empty vector as the results. + // This triggers the combined message send. + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); + + // Test that we sent the right message to the renderer. + int page_id = 0; + std::vector<string16> values; + std::vector<string16> labels; + std::vector<string16> icons; + EXPECT_TRUE(GetAutoFillSuggestionsMessage(&page_id, &values, &labels, + &icons)); + EXPECT_EQ(kPageID, page_id); + ASSERT_EQ(1U, values.size()); + EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION), + values[0]); + ASSERT_EQ(1U, labels.size()); + EXPECT_EQ(string16(), labels[0]); + ASSERT_EQ(1U, icons.size()); + EXPECT_EQ(string16(), icons[0]); + + // Now add some Autocomplete suggestions. We should show the autocomplete + // suggestions and the warning. + process()->sink().ClearMessages(); + const int kPageID2 = 2; + rvh()->ResetAutoFillState(kPageID2); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); + + std::vector<string16> suggestions; + suggestions.push_back(ASCIIToUTF16("Jay")); + suggestions.push_back(ASCIIToUTF16("Jason")); + rvh()->AutocompleteSuggestionsReturned(suggestions); + + EXPECT_TRUE(GetAutoFillSuggestionsMessage(&page_id, &values, &labels, + &icons)); + EXPECT_EQ(kPageID2, page_id); + ASSERT_EQ(3U, values.size()); + EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION), + values[0]); + EXPECT_EQ(ASCIIToUTF16("Jay"), values[1]); + EXPECT_EQ(ASCIIToUTF16("Jason"), values[2]); + ASSERT_EQ(3U, labels.size()); + EXPECT_EQ(string16(), labels[0]); + EXPECT_EQ(string16(), labels[1]); + EXPECT_EQ(string16(), labels[2]); + ASSERT_EQ(3U, icons.size()); + EXPECT_EQ(string16(), icons[0]); + EXPECT_EQ(string16(), icons[1]); + EXPECT_EQ(string16(), icons[2]); + + // Clear the test credit cards and try again -- we shouldn't return a warning. + test_personal_data_->ClearCreditCards(); + EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(false, field)); } // Test that we return profile and credit card suggestions for combined forms. @@ -564,11 +739,12 @@ TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestions) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right address suggestions to the renderer. int page_id = 0; @@ -593,11 +769,12 @@ TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestions) { process()->sink().ClearMessages(); autofill_test::CreateTestFormField( "Card Number", "cardnumber", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the credit card suggestions to the renderer. page_id = 0; @@ -616,7 +793,9 @@ TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestions) { } // Test that for non-https forms with both address and credit card fields, we -// only return address suggestions. +// only return address suggestions. Instead of credit card suggestions, we +// should return a warning explaining that credit card profile suggestions are +// unavailable when the form is not https. TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) { FormData form; CreateTestAddressFormData(&form); @@ -634,11 +813,12 @@ TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right address suggestions to the renderer. int page_id = 0; @@ -660,10 +840,31 @@ TEST_F(AutoFillManagerTest, GetAddressAndCreditCardSuggestionsNonHttps) { EXPECT_EQ(string16(), icons[0]); EXPECT_EQ(string16(), icons[1]); + process()->sink().ClearMessages(); autofill_test::CreateTestFormField( "Card Number", "cardnumber", "", "text", &field); - EXPECT_FALSE( - autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); + + // No suggestions provided, so send an empty vector as the results. + // This triggers the combined message send. + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); + + // Test that we sent the right message to the renderer. + EXPECT_TRUE(GetAutoFillSuggestionsMessage(&page_id, &values, &labels, + &icons)); + EXPECT_EQ(kPageID, page_id); + ASSERT_EQ(1U, values.size()); + EXPECT_EQ(l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION), + values[0]); + ASSERT_EQ(1U, labels.size()); + EXPECT_EQ(string16(), labels[0]); + ASSERT_EQ(1U, icons.size()); + EXPECT_EQ(string16(), icons[0]); + + // Clear the test credit cards and try again -- we shouldn't return a warning. + test_personal_data_->ClearCreditCards(); + EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(false, field)); } // Test that we correctly combine autofill and autocomplete suggestions. @@ -683,14 +884,15 @@ TEST_F(AutoFillManagerTest, GetCombinedAutoFillAndAutocompleteSuggestions) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, false, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(false, field)); // Add some Autocomplete suggestions. // This triggers the combined message send. std::vector<string16> suggestions; suggestions.push_back(ASCIIToUTF16("Jay")); suggestions.push_back(ASCIIToUTF16("Jason")); - rvh()->AutocompleteSuggestionsReturned(kPageID, suggestions); + rvh()->AutocompleteSuggestionsReturned(suggestions); // Test that we sent the right message to the renderer. int page_id = 0; @@ -735,11 +937,12 @@ TEST_F(AutoFillManagerTest, GetFieldSuggestionsFieldIsAutoFilled) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, true, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(true, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; @@ -778,14 +981,15 @@ TEST_F(AutoFillManagerTest, GetFieldSuggestionsForAutocompleteOnly) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "Some Field", "somefield", "", "text", &field); - EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(kPageID, true, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_FALSE(autofill_manager_->GetAutoFillSuggestions(true, field)); // Add some Autocomplete suggestions. // This triggers the combined message send. std::vector<string16> suggestions; suggestions.push_back(ASCIIToUTF16("one")); suggestions.push_back(ASCIIToUTF16("two")); - rvh()->AutocompleteSuggestionsReturned(kPageID, suggestions); + rvh()->AutocompleteSuggestionsReturned(suggestions); // Test that we sent the right message to the renderer. int page_id = 0; @@ -830,11 +1034,12 @@ TEST_F(AutoFillManagerTest, GetFieldSuggestionsWithDuplicateValues) { webkit_glue::FormField field; autofill_test::CreateTestFormField( "First Name", "firstname", "", "text", &field); - EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(kPageID, true, field)); + rvh()->ResetAutoFillState(kPageID); + EXPECT_TRUE(autofill_manager_->GetAutoFillSuggestions(true, field)); // No suggestions provided, so send an empty vector as the results. // This triggers the combined message send. - rvh()->AutocompleteSuggestionsReturned(kPageID, std::vector<string16>()); + rvh()->AutocompleteSuggestionsReturned(std::vector<string16>()); // Test that we sent the right message to the renderer. int page_id = 0; |