Commit 94eaf496 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

[Reland] More integration tests for autofill in //ios/web_view.

One test that verifies fetching, filling, and clearing.

Bug: 778764
Change-Id: I77653f323941a66bc1ae3c490cebb4d92de29b02
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Reviewed-on: https://chromium-review.googlesource.com/841342
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#532462}
Reviewed-on: https://chromium-review.googlesource.com/894483
Cr-Commit-Position: refs/heads/master@{#533300}
parent 1e61ca28
......@@ -19,85 +19,201 @@
#error "This file requires ARC support."
#endif
using testing::kWaitForActionTimeout;
using testing::WaitUntilConditionOrTimeout;
namespace ios_web_view {
namespace {
NSString* const kTestFormName = @"FormName";
NSString* const kTestFormID = @"FormID";
NSString* const kTestFieldName = @"FieldName";
NSString* const kTestFieldID = @"FieldID";
NSString* const kTestFieldValue = @"FieldValue";
NSString* const kTestSubmitID = @"SubmitID";
NSString* const kTestFormHtml =
[NSString stringWithFormat:
@"<form name='%@'>"
"<input type='text' name='%@' value='%@'/>"
"<input type='submit'/>"
@"<form name='%@' id='%@'>"
"<input type='text' name='%@' id='%@'/>"
"<input type='submit' id='%@'/>"
"</form>",
kTestFormName,
kTestFormID,
kTestFieldName,
kTestFieldValue];
kTestFieldID,
kTestSubmitID];
} // namespace
// Tests autofill features in CWVWebViews.
class WebViewAutofillTest : public WebViewIntTest {
protected:
void SetUp() override {
WebViewIntTest::SetUp();
WebViewAutofillTest() : autofill_controller_(web_view_.autofillController) {}
bool LoadTestPage() WARN_UNUSED_RESULT {
std::string html = base::SysNSStringToUTF8(kTestFormHtml);
GURL url = GetUrlForPageWithHtmlBody(html);
ASSERT_TRUE(test::LoadUrl(web_view_, net::NSURLWithGURL(url)));
return test::LoadUrl(web_view_, net::NSURLWithGURL(url));
}
bool SubmitForm() WARN_UNUSED_RESULT {
NSString* submit_script =
[NSString stringWithFormat:@"document.getElementById('%@').click();",
kTestSubmitID];
NSError* submit_error = nil;
test::EvaluateJavaScript(web_view_, submit_script, &submit_error);
return !submit_error;
}
bool SetFormFieldValue(NSString* value) WARN_UNUSED_RESULT {
NSString* set_value_script = [NSString
stringWithFormat:@"document.getElementById('%@').value = '%@';",
kTestFieldID, value];
NSError* set_value_error = nil;
test::EvaluateJavaScript(web_view_, set_value_script, &set_value_error);
return !set_value_error;
}
NSArray<CWVAutofillSuggestion*>* FetchSuggestions() {
__block bool suggestions_fetched = false;
__block NSArray<CWVAutofillSuggestion*>* fetched_suggestions = nil;
[autofill_controller_
fetchSuggestionsForFormWithName:kTestFormName
fieldName:kTestFieldName
completionHandler:^(
NSArray<CWVAutofillSuggestion*>* suggestions) {
fetched_suggestions = suggestions;
suggestions_fetched = true;
}];
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool {
return suggestions_fetched;
}));
return fetched_suggestions;
}
CWVAutofillController* autofill_controller_;
};
// Tests that CWVAutofillControllerDelegate receives callbacks.
TEST_F(WebViewAutofillTest, TestDelegateCallbacks) {
CWVAutofillController* autofill_controller = [web_view_ autofillController];
ASSERT_TRUE(LoadTestPage());
ASSERT_TRUE(SetFormFieldValue(kTestFieldValue));
id delegate = OCMProtocolMock(@protocol(CWVAutofillControllerDelegate));
autofill_controller.delegate = delegate;
autofill_controller_.delegate = delegate;
[[delegate expect] autofillController:autofill_controller
[[delegate expect] autofillController:autofill_controller_
didFocusOnFieldWithName:kTestFieldName
formName:kTestFormName
value:kTestFieldValue];
test::EvaluateJavaScript(web_view_,
@"var event = new Event('focus');"
"document.forms[0][0].dispatchEvent(event);",
nil);
NSString* focus_script =
[NSString stringWithFormat:
@"var event = new Event('focus');"
"document.getElementById('%@').dispatchEvent(event);",
kTestFieldID];
NSError* focus_error = nil;
test::EvaluateJavaScript(web_view_, focus_script, &focus_error);
ASSERT_NSEQ(nil, focus_error);
[delegate verify];
[[delegate expect] autofillController:autofill_controller
[[delegate expect] autofillController:autofill_controller_
didBlurOnFieldWithName:kTestFieldName
formName:kTestFormName
value:kTestFieldValue];
test::EvaluateJavaScript(web_view_,
@"var event = new Event('blur');"
"document.forms[0][0].dispatchEvent(event);",
nil);
NSString* blur_script =
[NSString stringWithFormat:
@"var event = new Event('blur');"
"document.getElementById('%@').dispatchEvent(event);",
kTestFieldID];
NSError* blur_error = nil;
test::EvaluateJavaScript(web_view_, blur_script, &blur_error);
ASSERT_NSEQ(nil, blur_error);
[delegate verify];
[[delegate expect] autofillController:autofill_controller
[[delegate expect] autofillController:autofill_controller_
didInputInFieldWithName:kTestFieldName
formName:kTestFormName
value:kTestFieldValue];
// The 'input' event listener defined in form.js is only called during the
// bubbling phase.
test::EvaluateJavaScript(web_view_,
@"var event = new Event('input', {'bubbles': true});"
"document.forms[0][0].dispatchEvent(event);",
nil);
NSString* input_script =
[NSString stringWithFormat:
@"var event = new Event('input', {'bubbles': true});"
"document.getElementById('%@').dispatchEvent(event);",
kTestFieldID];
NSError* input_error = nil;
test::EvaluateJavaScript(web_view_, input_script, &input_error);
ASSERT_NSEQ(nil, input_error);
[delegate verify];
[[delegate expect] autofillController:autofill_controller
[[delegate expect] autofillController:autofill_controller_
didSubmitFormWithName:kTestFormName
userInitiated:NO
isMainFrame:YES];
// The 'submit' event listener defined in form.js is only called during the
// bubbling phase.
test::EvaluateJavaScript(
web_view_,
@"var event = new Event('submit', {'bubbles': true});"
"document.forms[0].dispatchEvent(event);",
nil);
NSString* submit_script =
[NSString stringWithFormat:
@"var event = new Event('submit', {'bubbles': true});"
"document.getElementById('%@').dispatchEvent(event);",
kTestFormID];
NSError* submit_error = nil;
test::EvaluateJavaScript(web_view_, submit_script, &submit_error);
ASSERT_NSEQ(nil, submit_error);
[delegate verify];
}
// Tests that CWVAutofillController can fetch, fill, and clear suggestions.
TEST_F(WebViewAutofillTest, TestSuggestionFetchFillClear) {
ASSERT_TRUE(LoadTestPage());
ASSERT_TRUE(SetFormFieldValue(kTestFieldValue));
ASSERT_TRUE(SubmitForm());
ASSERT_TRUE(LoadTestPage());
NSArray<CWVAutofillSuggestion*>* fetched_suggestions = FetchSuggestions();
EXPECT_EQ(1U, fetched_suggestions.count);
CWVAutofillSuggestion* fetched_suggestion = fetched_suggestions.firstObject;
EXPECT_NSEQ(kTestFieldValue, fetched_suggestion.value);
EXPECT_NSEQ(kTestFormName, fetched_suggestion.formName);
EXPECT_NSEQ(kTestFieldName, fetched_suggestion.fieldName);
// The input element needs to be focused before it can be filled or cleared.
NSString* focus_script = [NSString
stringWithFormat:@"document.getElementById('%@').focus()", kTestFieldID];
NSError* focus_error = nil;
test::EvaluateJavaScript(web_view_, focus_script, &focus_error);
ASSERT_NSEQ(nil, focus_error);
__block bool suggestion_filled = false;
[autofill_controller_ fillSuggestion:fetched_suggestion
completionHandler:^{
suggestion_filled = true;
}];
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool {
return suggestion_filled;
}));
NSString* filled_script = [NSString
stringWithFormat:@"document.getElementById('%@').value", kTestFieldID];
NSError* filled_error = nil;
NSString* filled_value =
test::EvaluateJavaScript(web_view_, filled_script, &filled_error);
ASSERT_NSEQ(nil, filled_error);
EXPECT_NSEQ(fetched_suggestion.value, filled_value);
__block bool form_cleared = false;
[autofill_controller_ clearFormWithName:kTestFormName
completionHandler:^{
form_cleared = true;
}];
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool {
return form_cleared;
}));
NSString* cleared_script = [NSString
stringWithFormat:@"document.getElementById('%@').value", kTestFieldID];
NSError* cleared_error = nil;
NSString* current_value =
test::EvaluateJavaScript(web_view_, cleared_script, &cleared_error);
ASSERT_NSEQ(nil, cleared_error);
EXPECT_NSEQ(@"", current_value);
}
} // namespace ios_web_view
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment