OLD | NEW |
(Empty) | |
| 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 #import "components/autofill/ios/browser/js_autofill_manager.h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/ios/ios_util.h" |
| 10 #import "base/test/ios/wait_util.h" |
| 11 #include "components/autofill/core/common/autofill_constants.h" |
| 12 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 13 #import "ios/web/public/test/js_test_util.h" |
| 14 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| 15 #import "ios/web/public/web_state/web_state.h" |
| 16 #import "testing/gtest_mac.h" |
| 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 |
| 24 // TODO(crbug.com/619982): MobileSafari corrected HTMLInputElement.maxLength |
| 25 // with the specification ( https://bugs.webkit.org/show_bug.cgi?id=154906 ). |
| 26 // Add support for old and new default maxLength value until we dropped Xcode 7. |
| 27 NSNumber* GetDefaultMaxLength() { |
| 28 return base::ios::IsRunningOnIOS10OrLater() ? @-1 : @524288; |
| 29 } |
| 30 |
| 31 // Text fixture to test JsAutofillManager. |
| 32 class JsAutofillManagerTest : public ChromeWebTest { |
| 33 protected: |
| 34 // Loads the given HTML and initializes the Autofill JS scripts. |
| 35 void LoadHtml(NSString* html) { |
| 36 ChromeWebTest::LoadHtml(html); |
| 37 CRWJSInjectionManager* manager = [web_state()->GetJSInjectionReceiver() |
| 38 instanceOfClass:[JsAutofillManager class]]; |
| 39 manager_ = static_cast<JsAutofillManager*>(manager); |
| 40 [manager_ inject]; |
| 41 } |
| 42 // Testable autofill manager. |
| 43 JsAutofillManager* manager_; |
| 44 }; |
| 45 |
| 46 // Tests that |hasBeenInjected| returns YES after |inject| call. |
| 47 TEST_F(JsAutofillManagerTest, InitAndInject) { |
| 48 LoadHtml(@"<html></html>"); |
| 49 EXPECT_TRUE([manager_ hasBeenInjected]); |
| 50 } |
| 51 |
| 52 // Tests forms extraction method |
| 53 // (fetchFormsWithRequirements:minimumRequiredFieldsCount:completionHandler:). |
| 54 TEST_F(JsAutofillManagerTest, ExtractForms) { |
| 55 LoadHtml(@"<html><body><form name='testform' method='post'>" |
| 56 "<input type='text' name='firstname'/>" |
| 57 "<input type='text' name='lastname'/>" |
| 58 "<input type='email' name='email'/>" |
| 59 "</form></body></html>"); |
| 60 |
| 61 NSDictionary* expected = @{ |
| 62 @"name" : @"testform", |
| 63 @"method" : @"post", |
| 64 @"fields" : @[ |
| 65 @{ |
| 66 @"name" : @"firstname", |
| 67 @"form_control_type" : @"text", |
| 68 @"max_length" : GetDefaultMaxLength(), |
| 69 @"should_autocomplete" : @true, |
| 70 @"is_checkable" : @false, |
| 71 @"is_focusable" : @true, |
| 72 @"value" : @"", |
| 73 @"label" : @"" |
| 74 }, |
| 75 @{ |
| 76 @"name" : @"lastname", |
| 77 @"form_control_type" : @"text", |
| 78 @"max_length" : GetDefaultMaxLength(), |
| 79 @"should_autocomplete" : @true, |
| 80 @"is_checkable" : @false, |
| 81 @"is_focusable" : @true, |
| 82 @"value" : @"", |
| 83 @"label" : @"" |
| 84 }, |
| 85 @{ |
| 86 @"name" : @"email", |
| 87 @"form_control_type" : @"email", |
| 88 @"max_length" : GetDefaultMaxLength(), |
| 89 @"should_autocomplete" : @true, |
| 90 @"is_checkable" : @false, |
| 91 @"is_focusable" : @true, |
| 92 @"value" : @"", |
| 93 @"label" : @"" |
| 94 } |
| 95 ] |
| 96 }; |
| 97 |
| 98 __block BOOL block_was_called = NO; |
| 99 __block NSString* result; |
| 100 [manager_ fetchFormsWithMinimumRequiredFieldsCount: |
| 101 autofill::kRequiredFieldsForPredictionRoutines |
| 102 completionHandler:^(NSString* actualResult) { |
| 103 block_was_called = YES; |
| 104 result = [actualResult copy]; |
| 105 }]; |
| 106 base::test::ios::WaitUntilCondition(^bool() { |
| 107 return block_was_called; |
| 108 }); |
| 109 |
| 110 NSDictionary* resultDict = [NSJSONSerialization |
| 111 JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding] |
| 112 options:0 |
| 113 error:nil]; |
| 114 EXPECT_NSNE(nil, resultDict); |
| 115 |
| 116 NSDictionary* forms = [resultDict[@"forms"] firstObject]; |
| 117 [expected enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL* stop) { |
| 118 EXPECT_NSEQ(forms[key], obj); |
| 119 }]; |
| 120 } |
| 121 |
| 122 // Tests form filling (fillActiveFormField:completionHandler:) method. |
| 123 TEST_F(JsAutofillManagerTest, FillActiveFormField) { |
| 124 LoadHtml(@"<html><body><form name='testform' method='post'>" |
| 125 "<input type='email' name='email'/>" |
| 126 "</form></body></html>"); |
| 127 |
| 128 NSString* get_element_javascript = @"document.getElementsByName('email')[0]"; |
| 129 NSString* focus_element_javascript = |
| 130 [NSString stringWithFormat:@"%@.focus()", get_element_javascript]; |
| 131 [manager_ executeJavaScript:focus_element_javascript completionHandler:nil]; |
| 132 [manager_ |
| 133 fillActiveFormField:@"{\"name\":\"email\",\"value\":\"newemail@com\"}" |
| 134 completionHandler:^{ |
| 135 }]; |
| 136 |
| 137 NSString* element_value_javascript = |
| 138 [NSString stringWithFormat:@"%@.value", get_element_javascript]; |
| 139 EXPECT_NSEQ(@"newemail@com", |
| 140 web::ExecuteJavaScript(manager_, element_value_javascript)); |
| 141 } |
| 142 |
| 143 } // namespace |
OLD | NEW |