1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#include <stddef.h>
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/web/public/test/web_js_test.h"
#import "ios/web/public/test/web_test_with_web_state.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
class FillJsTest : public web::WebJsTest<web::WebTestWithWebState> {
public:
FillJsTest()
: web::WebJsTest<web::WebTestWithWebState>(@[ @"form_util_js" ]) {}
};
} // namespace
TEST_F(FillJsTest, GetCanonicalActionForForm) {
struct TestData {
NSString* html_action;
NSString* expected_action;
} test_data[] = {
// Empty action.
{nil, @"baseurl/"},
// Absolute urls.
{@"http://foo1.com/bar", @"http://foo1.com/bar"},
{@"http://foo2.com/bar#baz", @"http://foo2.com/bar"},
{@"http://foo3.com/bar?baz", @"http://foo3.com/bar"},
// Relative urls.
{@"action.php", @"baseurl/action.php"},
{@"action.php?abc", @"baseurl/action.php"},
// Non-http protocols.
{@"data:abc", @"data:abc"},
{@"javascript:login()", @"javascript:login()"},
};
for (size_t i = 0; i < base::size(test_data); i++) {
TestData& data = test_data[i];
NSString* html_action =
data.html_action == nil
? @""
: [NSString stringWithFormat:@"action='%@'", data.html_action];
NSString* html = [NSString stringWithFormat:
@"<html><body>"
"<form %@></form>"
"</body></html>",
html_action];
LoadHtmlAndInject(html);
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getCanonicalActionForForm(document.body.children[0])");
NSString* base_url = base::SysUTF8ToNSString(BaseUrl());
NSString* expected_action =
[data.expected_action stringByReplacingOccurrencesOfString:@"baseurl/"
withString:base_url];
EXPECT_NSEQ(expected_action, result)
<< " in test " << i << ": "
<< base::SysNSStringToUTF8(data.html_action);
}
}
// Tests the extraction of the aria-label attribute.
TEST_F(FillJsTest, GetAriaLabel) {
LoadHtmlAndInject(@"<input id='input' type='text' aria-label='the label'/>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"the label";
EXPECT_NSEQ(result, expected_result);
}
// Tests that aria-labelledby works. Simple case: only one id referenced.
TEST_F(FillJsTest, GetAriaLabelledBySingle) {
LoadHtmlAndInject(
@"<html><body>"
"<div id='billing'>Billing</div>"
"<div>"
" <div id='name'>Name</div>"
" <input id='input' type='text' aria-labelledby='name'/>"
"</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"Name";
EXPECT_NSEQ(result, expected_result);
}
// Tests that aria-labelledby works: Complex case: multiple ids referenced.
TEST_F(FillJsTest, GetAriaLabelledByMulti) {
LoadHtmlAndInject(
@"<html><body>"
"<div id='billing'>Billing</div>"
"<div>"
" <div id='name'>Name</div>"
" <input id='input' type='text' aria-labelledby='billing name'/>"
"</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"Billing Name";
EXPECT_NSEQ(result, expected_result);
}
// Tests that aria-labelledby takes precedence over aria-label
TEST_F(FillJsTest, GetAriaLabelledByTakesPrecedence) {
LoadHtmlAndInject(
@"<html><body>"
"<div id='billing'>Billing</div>"
"<div>"
" <div id='name'>Name</div>"
" <input id='input' type='text' aria-label='ignored' "
" aria-labelledby='name'/>"
"</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"Name";
EXPECT_NSEQ(result, expected_result);
}
// Tests that an invalid aria-labelledby reference gets ignored (as opposed to
// crashing, for example).
TEST_F(FillJsTest, GetAriaLabelledByInvalid) {
LoadHtmlAndInject(
@"<html><body>"
"<div id='billing'>Billing</div>"
"<div>"
" <div id='name'>Name</div>"
" <input id='input' type='text' aria-labelledby='div1 div2'/>"
"</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"";
EXPECT_NSEQ(result, expected_result);
}
// Tests that invalid aria-labelledby references fall back to aria-label.
TEST_F(FillJsTest, GetAriaLabelledByFallback) {
LoadHtmlAndInject(
@"<html><body>"
"<div id='billing'>Billing</div>"
"<div>"
" <div id='name'>Name</div>"
" <input id='input' type='text' aria-label='valid' "
" aria-labelledby='div1 div2'/>"
"</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaLabel(document.getElementById('input'));");
NSString* expected_result = @"valid";
EXPECT_NSEQ(result, expected_result);
}
// Tests that aria-describedby works: Simple case: a single id referenced.
TEST_F(FillJsTest, GetAriaDescriptionSingle) {
LoadHtmlAndInject(
@"<html><body>"
"<input id='input' type='text' aria-describedby='div1'/>"
"<div id='div1'>aria description</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaDescription(document.getElementById('input'));");
NSString* expected_result = @"aria description";
EXPECT_NSEQ(result, expected_result);
}
// Tests that aria-describedby works: Complex case: multiple ids referenced.
TEST_F(FillJsTest, GetAriaDescriptionMulti) {
LoadHtmlAndInject(
@"<html><body>"
"<input id='input' type='text' aria-describedby='div1 div2'/>"
"<div id='div2'>description</div>"
"<div id='div1'>aria</div>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaDescription(document.getElementById('input'));");
NSString* expected_result = @"aria description";
EXPECT_NSEQ(result, expected_result);
}
// Tests that invalid aria-describedby returns the empty string.
TEST_F(FillJsTest, GetAriaDescriptionInvalid) {
LoadHtmlAndInject(
@"<html><body>"
"<input id='input' type='text' aria-describedby='invalid'/>"
"</body></html>");
id result = ExecuteJavaScript(
@"__gCrWeb.fill.getAriaDescription(document.getElementById('input'));");
NSString* expected_result = @"";
EXPECT_NSEQ(result, expected_result);
}
|