| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/passwords/password_controller.h" | 5 #import "ios/chrome/browser/passwords/password_controller.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 static NSString* kInputFieldValueVerificationScript = | 742 static NSString* kInputFieldValueVerificationScript = |
| 743 @"var result='';" | 743 @"var result='';" |
| 744 "var inputs = document.getElementsByTagName('input');" | 744 "var inputs = document.getElementsByTagName('input');" |
| 745 "for(var i = 0; i < inputs.length; i++){" | 745 "for(var i = 0; i < inputs.length; i++){" |
| 746 " var input = inputs[i];" | 746 " var input = inputs[i];" |
| 747 " if (input.value) {" | 747 " if (input.value) {" |
| 748 " result += input.id + '=' + input.value +';';" | 748 " result += input.id + '=' + input.value +';';" |
| 749 " }" | 749 " }" |
| 750 "}; result"; | 750 "}; result"; |
| 751 | 751 |
| 752 // Test html content and expected result for __gCrWeb.hasPasswordField call. |
| 753 struct TestDataForPasswordFormDetection { |
| 754 NSString* page_content; |
| 755 BOOL contains_password; |
| 756 }; |
| 757 |
| 758 // Tests that the existence of (or the lack of) a password field in the page is |
| 759 // detected correctly. |
| 760 TEST_F(PasswordControllerTest, HasPasswordField) { |
| 761 TestDataForPasswordFormDetection test_data[] = { |
| 762 // Form without a password field. |
| 763 {@"<form><input type='text' name='password'></form>", NO}, |
| 764 // Form with a password field. |
| 765 {@"<form><input type='password' name='password'></form>", YES}}; |
| 766 for (size_t i = 0; i < arraysize(test_data); i++) { |
| 767 TestDataForPasswordFormDetection& data = test_data[i]; |
| 768 LoadHtml(data.page_content); |
| 769 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()"); |
| 770 EXPECT_NSEQ(@(data.contains_password), result) |
| 771 << " in test " << i << ": " |
| 772 << base::SysNSStringToUTF8(data.page_content); |
| 773 } |
| 774 } |
| 775 |
| 776 // Tests that the existence a password field in a nested iframe/ is detected |
| 777 // correctly. |
| 778 TEST_F(PasswordControllerTest, HasPasswordFieldinFrame) { |
| 779 TestDataForPasswordFormDetection data = { |
| 780 // Form with a password field in a nested iframe. |
| 781 @"<iframe name='pf'></iframe>" |
| 782 "<script>" |
| 783 " var doc = frames['pf'].document.open();" |
| 784 " doc.write('<form><input type=\\'password\\'></form>');" |
| 785 " doc.close();" |
| 786 "</script>", |
| 787 YES |
| 788 }; |
| 789 LoadHtml(data.page_content); |
| 790 id result = ExecuteJavaScript(@"__gCrWeb.hasPasswordField()"); |
| 791 EXPECT_NSEQ(@(data.contains_password), result) |
| 792 << base::SysNSStringToUTF8(data.page_content); |
| 793 } |
| 794 |
| 752 struct FillPasswordFormTestData { | 795 struct FillPasswordFormTestData { |
| 753 const std::string origin; | 796 const std::string origin; |
| 754 const std::string action; | 797 const std::string action; |
| 755 const char* username_field; | 798 const char* username_field; |
| 756 const char* username_value; | 799 const char* username_value; |
| 757 const char* password_field; | 800 const char* password_field; |
| 758 const char* password_value; | 801 const char* password_value; |
| 759 const BOOL should_succeed; | 802 const BOOL should_succeed; |
| 760 NSString* expected_result; | 803 NSString* expected_result; |
| 761 }; | 804 }; |
| 762 | 805 |
| 763 // Test that filling password forms works correctly. | 806 // Tests that filling password forms works correctly. |
| 764 TEST_F(PasswordControllerTest, FillPasswordForm) { | 807 TEST_F(PasswordControllerTest, FillPasswordForm) { |
| 765 LoadHtml(kHtmlWithMultiplePasswordForms); | 808 LoadHtml(kHtmlWithMultiplePasswordForms); |
| 766 | 809 |
| 810 // TODO(crbug.com/614092): can we remove this assertion? This call is the only |
| 811 // reason why hasPasswordField is a public API on gCrWeb. If the page does |
| 812 // not contain a password field, shouldn't one of the expectations of the |
| 813 // remaining tests also fail? |
| 767 EXPECT_NSEQ(@YES, ExecuteJavaScript(@"__gCrWeb.hasPasswordField()")); | 814 EXPECT_NSEQ(@YES, ExecuteJavaScript(@"__gCrWeb.hasPasswordField()")); |
| 768 | 815 |
| 769 const std::string base_url = BaseUrl(); | 816 const std::string base_url = BaseUrl(); |
| 770 // clang-format off | 817 // clang-format off |
| 771 FillPasswordFormTestData test_data[] = { | 818 FillPasswordFormTestData test_data[] = { |
| 772 // Basic test: one-to-one match on the first password form. | 819 // Basic test: one-to-one match on the first password form. |
| 773 { | 820 { |
| 774 base_url, | 821 base_url, |
| 775 base_url, | 822 base_url, |
| 776 "u0", | 823 "u0", |
| (...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 // Tests that an HTTPS page with a password field does not update the SSL status | 1377 // Tests that an HTTPS page with a password field does not update the SSL status |
| 1331 // to indicate DISPLAYED_PASSWORD_FIELD_ON_HTTP. | 1378 // to indicate DISPLAYED_PASSWORD_FIELD_ON_HTTP. |
| 1332 TEST_F(PasswordControllerTest, HTTPSPassword) { | 1379 TEST_F(PasswordControllerTest, HTTPSPassword) { |
| 1333 LoadHtml(kHtmlWithPasswordForm, GURL("https://chromium.test")); | 1380 LoadHtml(kHtmlWithPasswordForm, GURL("https://chromium.test")); |
| 1334 | 1381 |
| 1335 web::SSLStatus ssl_status = | 1382 web::SSLStatus ssl_status = |
| 1336 web_state()->GetNavigationManager()->GetLastCommittedItem()->GetSSL(); | 1383 web_state()->GetNavigationManager()->GetLastCommittedItem()->GetSSL(); |
| 1337 EXPECT_FALSE(ssl_status.content_status & | 1384 EXPECT_FALSE(ssl_status.content_status & |
| 1338 web::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP); | 1385 web::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP); |
| 1339 } | 1386 } |
| OLD | NEW |