[web:tests] switch to new HTML DOM matcher (flutter/engine#52354)
Remove the over-architected and under-utilized `canonicalizeHtml` and `HtmlComparisonMode`. Replace with a new `hasHtml` matcher.
The new matcher produces considerably better error messages when matching fails (see `matchers_test.dart` for multiple examples). It also allows focusing on particular parts of the DOM rather than looking for a full match. This way a test can test just the functionality it cares about, and it can skip specifying what unrelated parts should look like (i.e. it prevents over-testing). For example, let's say you want to test that an `<flt-semantics>` element has an `aria-label` attribute. You can use a DOM pattern that specified just that:
```
expect(
element,
hasHtml('<flt-semantics aria-label="hello"></flt-semantics>'),
)
```
This will check for the existence of `<flt-semantics>` and `aria-label="hello"`. It will ignore everything else that's unrelated to ARIA labels. For example, the element may have its geometry specified in the `style` attribute, an `id` attribute, and many other things, but if the test doesn't care, it doesn't need to specify those things (it can, if needs to though). This also applies to `style` properties. See more examples in the `matchers_test.dart` file.
This commit is contained in:
@@ -597,9 +597,9 @@ extension DomElementExtension on DomElement {
|
||||
external set _id(JSString id);
|
||||
set id(String id) => _id = id.toJS;
|
||||
|
||||
@JS('innerHtml')
|
||||
external set _innerHtml(JSString? html);
|
||||
set innerHtml(String? html) => _innerHtml = html?.toJS;
|
||||
@JS('innerHTML')
|
||||
external set _innerHTML(JSString? html);
|
||||
set innerHTML(String? html) => _innerHTML = html?.toJS;
|
||||
|
||||
@JS('outerHTML')
|
||||
external JSString? get _outerHTML;
|
||||
|
||||
@@ -7,8 +7,8 @@ library matchers;
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:html/dom.dart' as html_package;
|
||||
import 'package:html/parser.dart' as html_package;
|
||||
import 'package:html/dom.dart' as html;
|
||||
import 'package:html/parser.dart' as html;
|
||||
|
||||
import 'package:test/test.dart';
|
||||
|
||||
@@ -198,238 +198,6 @@ class _IsWithinDistance<T> extends Matcher {
|
||||
}
|
||||
}
|
||||
|
||||
/// Controls how test HTML is canonicalized by [canonicalizeHtml] function.
|
||||
///
|
||||
/// In all cases whitespace between elements is stripped.
|
||||
enum HtmlComparisonMode {
|
||||
/// Retains all attributes.
|
||||
///
|
||||
/// Useful when very precise HTML comparison is needed that includes both
|
||||
/// layout and non-layout style attributes. This mode is rarely needed. Most
|
||||
/// tests should use [layoutOnly] or [nonLayoutOnly].
|
||||
everything,
|
||||
|
||||
/// Retains only layout style attributes, such as "width".
|
||||
///
|
||||
/// Useful when testing layout because it filters out all the noise that does
|
||||
/// not affect layout.
|
||||
layoutOnly,
|
||||
|
||||
/// Retains only non-layout style attributes, such as "color".
|
||||
///
|
||||
/// Useful when testing styling because it filters out all the noise from the
|
||||
/// layout attributes.
|
||||
nonLayoutOnly,
|
||||
|
||||
/// Do not consider attributes when comparing HTML.
|
||||
noAttributes,
|
||||
}
|
||||
|
||||
/// Rewrites [htmlContent] by removing irrelevant style attributes.
|
||||
///
|
||||
/// If [throwOnUnusedStyleProperties] is `true`, throws instead of rewriting. Set
|
||||
/// [throwOnUnusedStyleProperties] to `true` to check that expected HTML strings do
|
||||
/// not contain irrelevant attributes. It is ok for actual HTML to contain all
|
||||
/// kinds of attributes. They only need to be filtered out before testing.
|
||||
String canonicalizeHtml(
|
||||
String htmlContent, {
|
||||
HtmlComparisonMode mode = HtmlComparisonMode.nonLayoutOnly,
|
||||
bool throwOnUnusedStyleProperties = false,
|
||||
List<String>? ignoredStyleProperties,
|
||||
}) {
|
||||
if (htmlContent.trim().isEmpty) {
|
||||
return '';
|
||||
}
|
||||
|
||||
String? unusedStyleProperty(String name) {
|
||||
if (throwOnUnusedStyleProperties) {
|
||||
fail('Provided HTML contains style property "$name" which '
|
||||
'is not used for comparison in the test. The HTML was:\n\n$htmlContent');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
html_package.Element cleanup(html_package.Element original) {
|
||||
String replacementTag = original.localName!;
|
||||
switch (replacementTag) {
|
||||
case 'flt-scene':
|
||||
replacementTag = 's';
|
||||
case 'flt-transform':
|
||||
replacementTag = 't';
|
||||
case 'flt-opacity':
|
||||
replacementTag = 'o';
|
||||
case 'flt-clip':
|
||||
final String? clipType = original.attributes['clip-type'];
|
||||
switch (clipType) {
|
||||
case 'rect':
|
||||
replacementTag = 'clip';
|
||||
case 'rrect':
|
||||
replacementTag = 'rclip';
|
||||
case 'physical-shape':
|
||||
replacementTag = 'pshape';
|
||||
default:
|
||||
throw Exception('Unknown clip type: $clipType');
|
||||
}
|
||||
case 'flt-clip-interior':
|
||||
replacementTag = 'clip-i';
|
||||
case 'flt-picture':
|
||||
replacementTag = 'pic';
|
||||
case 'flt-canvas':
|
||||
replacementTag = 'c';
|
||||
case 'flt-dom-canvas':
|
||||
replacementTag = 'd';
|
||||
case 'flt-semantics':
|
||||
replacementTag = 'sem';
|
||||
case 'flt-semantics-container':
|
||||
replacementTag = 'sem-c';
|
||||
case 'flt-semantics-img':
|
||||
replacementTag = 'sem-img';
|
||||
case 'flt-semantics-text-field':
|
||||
replacementTag = 'sem-tf';
|
||||
}
|
||||
|
||||
final html_package.Element replacement =
|
||||
html_package.Element.tag(replacementTag);
|
||||
|
||||
if (mode != HtmlComparisonMode.noAttributes) {
|
||||
// Sort the attributes so tests are not sensitive to their order, which
|
||||
// does not matter in terms of functionality.
|
||||
final List<String> attributeNames = original.attributes.keys.cast<String>().toList();
|
||||
attributeNames.sort();
|
||||
for (final String name in attributeNames) {
|
||||
final String value = original.attributes[name]!;
|
||||
if (name == 'style') {
|
||||
// The style attribute is handled separately because it contains substructure.
|
||||
continue;
|
||||
}
|
||||
|
||||
// These are the only attributes we're interested in testing. This list
|
||||
// can change over time.
|
||||
if (name.startsWith('aria-') || name.startsWith('flt-') || name == 'role') {
|
||||
replacement.attributes[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (original.attributes.containsKey('style')) {
|
||||
final String styleValue = original.attributes['style']!;
|
||||
|
||||
int attrCount = 0;
|
||||
final String processedAttributes = styleValue
|
||||
.split(';')
|
||||
.map((String attr) {
|
||||
attr = attr.trim();
|
||||
if (attr.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mode != HtmlComparisonMode.everything) {
|
||||
final bool forLayout = mode == HtmlComparisonMode.layoutOnly;
|
||||
final List<String> parts = attr.split(':');
|
||||
if (parts.length == 2) {
|
||||
final String name = parts.first;
|
||||
|
||||
if (ignoredStyleProperties != null && ignoredStyleProperties.contains(name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Whether the attribute is one that's set to the same value and
|
||||
// never changes. Such attributes are usually not interesting to
|
||||
// test.
|
||||
final bool isStaticAttribute = const <String>[
|
||||
'top',
|
||||
'left',
|
||||
'position',
|
||||
].contains(name);
|
||||
|
||||
if (isStaticAttribute) {
|
||||
return unusedStyleProperty(name);
|
||||
}
|
||||
|
||||
// Whether the attribute is set by the layout system.
|
||||
final bool isLayoutAttribute = const <String>[
|
||||
'top',
|
||||
'left',
|
||||
'bottom',
|
||||
'right',
|
||||
'position',
|
||||
'width',
|
||||
'height',
|
||||
'font-size',
|
||||
'transform',
|
||||
'transform-origin',
|
||||
'white-space',
|
||||
].contains(name);
|
||||
|
||||
if (forLayout && !isLayoutAttribute ||
|
||||
!forLayout && isLayoutAttribute) {
|
||||
return unusedStyleProperty(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attrCount++;
|
||||
return attr.trim();
|
||||
})
|
||||
.where((String? attr) => attr != null && attr.isNotEmpty)
|
||||
.join('; ');
|
||||
|
||||
if (attrCount > 0) {
|
||||
replacement.attributes['style'] = processedAttributes;
|
||||
}
|
||||
}
|
||||
} else if (throwOnUnusedStyleProperties && original.attributes.isNotEmpty) {
|
||||
fail('Provided HTML contains attributes. However, the comparison mode '
|
||||
'is $mode. The HTML was:\n\n$htmlContent');
|
||||
}
|
||||
|
||||
for (final html_package.Node child in original.nodes) {
|
||||
if (child is html_package.Text && child.text.trim().isEmpty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child is html_package.Element) {
|
||||
replacement.append(cleanup(child));
|
||||
} else {
|
||||
replacement.append(child.clone(true));
|
||||
}
|
||||
}
|
||||
|
||||
return replacement;
|
||||
}
|
||||
|
||||
final html_package.DocumentFragment originalDom =
|
||||
html_package.parseFragment(htmlContent);
|
||||
|
||||
final html_package.DocumentFragment cleanDom =
|
||||
html_package.DocumentFragment();
|
||||
for (final html_package.Element child in originalDom.children) {
|
||||
cleanDom.append(cleanup(child));
|
||||
}
|
||||
|
||||
return cleanDom.outerHtml;
|
||||
}
|
||||
|
||||
/// Tests that [element] has the HTML structure described by [expectedHtml].
|
||||
void expectHtml(DomElement element, String expectedHtml,
|
||||
{HtmlComparisonMode mode = HtmlComparisonMode.nonLayoutOnly}) {
|
||||
expectedHtml =
|
||||
canonicalizeHtml(expectedHtml, mode: mode, throwOnUnusedStyleProperties: true);
|
||||
final String actualHtml = canonicalizeHtml(element.outerHTML!, mode: mode);
|
||||
expect(actualHtml, expectedHtml);
|
||||
}
|
||||
|
||||
class SceneTester {
|
||||
SceneTester(this.scene);
|
||||
|
||||
final SurfaceScene scene;
|
||||
|
||||
void expectSceneHtml(String expectedHtml) {
|
||||
expectHtml(scene.webOnlyRootElement!, expectedHtml,
|
||||
mode: HtmlComparisonMode.noAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
/// A matcher for functions that throw [AssertionError].
|
||||
///
|
||||
/// This is equivalent to `throwsA(isInstanceOf<AssertionError>())`.
|
||||
@@ -454,3 +222,220 @@ final Matcher throwsAssertionError = throwsA(isAssertionError);
|
||||
/// * [throwsAssertionError], to test if a function throws any [AssertionError].
|
||||
/// * [isFlutterError], to test if any object is a [FlutterError].
|
||||
const Matcher isAssertionError = TypeMatcher<AssertionError>();
|
||||
|
||||
/// Matches a [DomElement] against an HTML pattern.
|
||||
///
|
||||
/// An HTML pattern is a piece of valid HTML. The expectation is that the DOM
|
||||
/// element has the exact element structure as the provided [htmlPattern]. The
|
||||
/// DOM element is expected to have the exact element and style attributes
|
||||
/// specified in the pattern.
|
||||
///
|
||||
/// The DOM element may have additional attributes not specified in the pattern.
|
||||
/// This allows testing specific features relevant to the test.
|
||||
///
|
||||
/// The DOM structure may not have additional elements that are not specified in
|
||||
/// the pattern.
|
||||
Matcher hasHtml(String htmlPattern) {
|
||||
final html.DocumentFragment originalDom = html.parseFragment(htmlPattern);
|
||||
if (originalDom.children.isEmpty) {
|
||||
fail(
|
||||
'Test HTML pattern is empty.\n'
|
||||
'The pattern must contain exacly one top-level element, but was: $htmlPattern');
|
||||
}
|
||||
if (originalDom.children.length > 1) {
|
||||
fail(
|
||||
'Test HTML pattern has more than one top-level element.\n'
|
||||
'The pattern must contain exacly one top-level element, but was: $htmlPattern');
|
||||
}
|
||||
return HtmlPatternMatcher(originalDom.children.single);
|
||||
}
|
||||
|
||||
enum _Breadcrumb { root, element, attribute, styleProperty }
|
||||
|
||||
class _Breadcrumbs {
|
||||
const _Breadcrumbs._(this.parent, this.kind, this.name);
|
||||
|
||||
final _Breadcrumbs? parent;
|
||||
final _Breadcrumb kind;
|
||||
final String name;
|
||||
|
||||
static const _Breadcrumbs root = _Breadcrumbs._(null, _Breadcrumb.root, '');
|
||||
|
||||
_Breadcrumbs element(String tagName) {
|
||||
return _Breadcrumbs._(this, _Breadcrumb.element, tagName);
|
||||
}
|
||||
|
||||
_Breadcrumbs attribute(String attributeName) {
|
||||
return _Breadcrumbs._(this, _Breadcrumb.attribute, attributeName);
|
||||
}
|
||||
|
||||
_Breadcrumbs styleProperty(String propertyName) {
|
||||
return _Breadcrumbs._(this, _Breadcrumb.styleProperty, propertyName);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return switch (kind) {
|
||||
_Breadcrumb.root => '<root>',
|
||||
_Breadcrumb.element => parent!.kind == _Breadcrumb.root ? '@$name' : '$parent > $name',
|
||||
_Breadcrumb.attribute => '$parent#$name',
|
||||
_Breadcrumb.styleProperty => '$parent#style($name)',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class HtmlPatternMatcher extends Matcher {
|
||||
const HtmlPatternMatcher(this.pattern);
|
||||
|
||||
final html.Element pattern;
|
||||
|
||||
@override
|
||||
bool matches(final Object? object, Map<Object?, Object?> matchState) {
|
||||
if (object is! DomElement) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final List<String> mismatches = <String>[];
|
||||
matchState['mismatches'] = mismatches;
|
||||
|
||||
final html.Element element = html.parseFragment(object.outerHTML).children.single;
|
||||
matchElements(_Breadcrumbs.root, mismatches, element, pattern);
|
||||
return mismatches.isEmpty;
|
||||
}
|
||||
|
||||
static bool _areTagsEqual(html.Element a, html.Element b) {
|
||||
const Map<String, String> synonyms = <String, String>{
|
||||
'sem': 'flt-semantics',
|
||||
'sem-c': 'flt-semantics-container',
|
||||
'sem-img': 'flt-semantics-img',
|
||||
'sem-tf': 'flt-semantics-text-field',
|
||||
};
|
||||
|
||||
String aName = a.localName!.toLowerCase();
|
||||
String bName = b.localName!.toLowerCase();
|
||||
|
||||
if (synonyms.containsKey(aName)) {
|
||||
aName = synonyms[aName]!;
|
||||
}
|
||||
|
||||
if (synonyms.containsKey(bName)) {
|
||||
bName = synonyms[bName]!;
|
||||
}
|
||||
|
||||
return aName == bName;
|
||||
}
|
||||
|
||||
void matchElements(_Breadcrumbs parent, List<String> mismatches, html.Element element, html.Element pattern) {
|
||||
final _Breadcrumbs breadcrumb = parent.element(pattern.localName!);
|
||||
|
||||
if (!_areTagsEqual(element, pattern)) {
|
||||
mismatches.add(
|
||||
'$breadcrumb: unexpected tag name <${element.localName}> (expected <${pattern.localName}>).'
|
||||
);
|
||||
// Don't bother matching anything else. If tags are different, it's likely
|
||||
// we're comparing apples to oranges at this point.
|
||||
return;
|
||||
}
|
||||
|
||||
matchAttributes(breadcrumb, mismatches, element, pattern);
|
||||
matchChildren(breadcrumb, mismatches, element, pattern);
|
||||
}
|
||||
|
||||
void matchAttributes(_Breadcrumbs parent, List<String> mismatches, html.Element element, html.Element pattern) {
|
||||
for (final MapEntry<Object, String> attribute in pattern.attributes.entries) {
|
||||
final String expectedName = attribute.key as String;
|
||||
final String expectedValue = attribute.value;
|
||||
final _Breadcrumbs breadcrumb = parent.attribute(expectedName);
|
||||
|
||||
if (expectedName == 'style') {
|
||||
// Style is a complex attribute that deserves a special comparison algorithm.
|
||||
matchStyle(parent, mismatches, element, pattern);
|
||||
} else {
|
||||
if (!element.attributes.containsKey(expectedName)) {
|
||||
mismatches.add('$breadcrumb: attribute $expectedName="$expectedValue" missing.');
|
||||
} else {
|
||||
final String? actualValue = element.attributes[expectedName];
|
||||
if (actualValue != expectedValue) {
|
||||
mismatches.add(
|
||||
'$breadcrumb: expected attribute value $expectedName="$expectedValue", '
|
||||
'but found $expectedName="$actualValue".'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Map<String, String> parseStyle(html.Element element) {
|
||||
final Map<String, String> result = <String, String>{};
|
||||
|
||||
final String rawStyle = element.attributes['style']!;
|
||||
for (final String attribute in rawStyle.split(';')) {
|
||||
final List<String> parts = attribute.split(':');
|
||||
final String name = parts[0].trim();
|
||||
final String value = parts.skip(1).join(':').trim();
|
||||
result[name] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void matchStyle(_Breadcrumbs parent, List<String> mismatches, html.Element element, html.Element pattern) {
|
||||
final Map<String, String> expected = parseStyle(pattern);
|
||||
final Map<String, String> actual = parseStyle(element);
|
||||
for (final MapEntry<String, String> entry in expected.entries) {
|
||||
final _Breadcrumbs breadcrumb = parent.styleProperty(entry.key);
|
||||
if (!actual.containsKey(entry.key)) {
|
||||
mismatches.add(
|
||||
'$breadcrumb: style property ${entry.key}="${entry.value}" missing.'
|
||||
);
|
||||
} else if (actual[entry.key] != entry.value) {
|
||||
mismatches.add(
|
||||
'$breadcrumb: expected style property ${entry.key}="${entry.value}", '
|
||||
'but found ${entry.key}="${actual[entry.key]}".'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void matchChildren(_Breadcrumbs parent, List<String> mismatches, html.Element element, html.Element pattern) {
|
||||
if (element.children.length != pattern.children.length) {
|
||||
mismatches.add(
|
||||
'$parent: expected ${pattern.children.length} children, but found ${element.children.length}.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pattern.children.length; i++) {
|
||||
final html.Element expectedChild = pattern.children[i];
|
||||
final html.Element actualChild = element.children[i];
|
||||
matchElements(parent, mismatches, actualChild, expectedChild);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Description describe(Description description) {
|
||||
description.add('the element to have the following pattern:\n');
|
||||
description.add(pattern.outerHtml);
|
||||
return description;
|
||||
}
|
||||
|
||||
@override
|
||||
Description describeMismatch(
|
||||
Object? object,
|
||||
Description mismatchDescription,
|
||||
Map<Object?, Object?> matchState,
|
||||
bool verbose,
|
||||
) {
|
||||
mismatchDescription.add('The following DOM structure did not match the expected pattern:\n');
|
||||
mismatchDescription.add('${(object! as DomElement).outerHTML!}\n\n');
|
||||
mismatchDescription.add('Specifically:\n');
|
||||
|
||||
final List<String> mismatches = matchState['mismatches']! as List<String>;
|
||||
for (final String mismatch in mismatches) {
|
||||
mismatchDescription.add(' - $mismatch\n');
|
||||
}
|
||||
|
||||
return mismatchDescription;
|
||||
}
|
||||
}
|
||||
|
||||
322
engine/src/flutter/lib/web_ui/test/engine/matchers_test.dart
Normal file
322
engine/src/flutter/lib/web_ui/test/engine/matchers_test.dart
Normal file
@@ -0,0 +1,322 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
@TestOn('chrome || safari || firefox')
|
||||
library;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine/dom.dart';
|
||||
|
||||
import '../common/matchers.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => testMain);
|
||||
}
|
||||
|
||||
void testMain() {
|
||||
group('expectDom', () {
|
||||
_expectDomTests();
|
||||
});
|
||||
}
|
||||
|
||||
void _expectDomTests() {
|
||||
test('trivial equal elements', () {
|
||||
expectDom(
|
||||
'<div></div>',
|
||||
hasHtml('<div></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial unequal elements', () {
|
||||
expectDom(
|
||||
'<div></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<span></span>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div></div>
|
||||
|
||||
Specifically:
|
||||
- @span: unexpected tag name <div> (expected <span>).''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial equal attributes', () {
|
||||
expectDom(
|
||||
'<div id="hello"></div>',
|
||||
hasHtml('<div id="hello"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial out-of-order equal attributes', () {
|
||||
expectDom(
|
||||
'<div hello="brave" new="world"></div>',
|
||||
hasHtml('<div new="world" hello="brave"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial unequal attributes', () {
|
||||
expectDom(
|
||||
'<div id="hello"></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div id="world"></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div id="hello"></div>
|
||||
|
||||
Specifically:
|
||||
- @div#id: expected attribute value id="world", but found id="hello".''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial missing attributes', () {
|
||||
expectDom(
|
||||
'<div></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div id="hello"></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div></div>
|
||||
|
||||
Specifically:
|
||||
- @div#id: attribute id="hello" missing.''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial additional attributes', () {
|
||||
expectDom(
|
||||
'<div id="hello"></div>',
|
||||
hasHtml('<div></div>'),
|
||||
);
|
||||
|
||||
expectDom(
|
||||
'<div id="hello" foo="bar"></div>',
|
||||
hasHtml('<div id="hello"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial equal style', () {
|
||||
expectDom(
|
||||
'<div style="width: 10px; height: 40px"></div>',
|
||||
hasHtml('<div style="width: 10px; height: 40px"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial additional style attribute', () {
|
||||
expectDom(
|
||||
'<div style="width: 10px; transform: scale(2, 3); height: 40px"></div>',
|
||||
hasHtml('<div style="width: 10px; height: 40px"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('out of order equal style', () {
|
||||
expectDom(
|
||||
'<div style="width: 10px; height: 40px"></div>',
|
||||
hasHtml('<div style="height: 40px; width: 10px"></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial unequal style attributes', () {
|
||||
expectDom(
|
||||
'<div style="width: 10px"></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div style="width: 12px"></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div style="width: 10px"></div>
|
||||
|
||||
Specifically:
|
||||
- @div#style(width): expected style property width="12px", but found width="10px".''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial missing style attribute', () {
|
||||
expectDom(
|
||||
'<div style="width: 12px"></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div style="width: 12px; height: 20px"></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div style="width: 12px"></div>
|
||||
|
||||
Specifically:
|
||||
- @div#style(height): style property height="20px" missing.''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('multiple attribute mismatches', () {
|
||||
expectDom(
|
||||
'<div id="other" style="width: 12px; transform: scale(2)"></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div id="this" foo="bar" style="width: 12px; transform: scale(2); height: 20px"></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div id="other" style="width: 12px; transform: scale(2)"></div>
|
||||
|
||||
Specifically:
|
||||
- @div#id: expected attribute value id="this", but found id="other".
|
||||
- @div#foo: attribute foo="bar" missing.
|
||||
- @div#style(height): style property height="20px" missing.''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial child elements', () {
|
||||
expectDom(
|
||||
'<div><span></span><p></p></div>',
|
||||
hasHtml('<div><span></span><p></p></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('trivial nested child elements', () {
|
||||
expectDom(
|
||||
'<div><p><span></span></p></div>',
|
||||
hasHtml('<div><p><span></span></p></div>'),
|
||||
);
|
||||
});
|
||||
|
||||
test('missing child elements', () {
|
||||
expectDom(
|
||||
'<div><span></span><p></p></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div><span></span><waldo></waldo><p></p></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div><span></span><p></p></div>
|
||||
|
||||
Specifically:
|
||||
- @div: expected 3 children, but found 2.''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('additional child elements', () {
|
||||
expectDom(
|
||||
'<div><span></span><waldo></waldo><p></p></div>',
|
||||
expectMismatch(
|
||||
hasHtml('<div><span></span><p></p></div>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<div><span></span><waldo></waldo><p></p></div>
|
||||
|
||||
Specifically:
|
||||
- @div: expected 2 children, but found 3.''',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('deep breadcrumbs', () {
|
||||
expectDom(
|
||||
'<a><b><c><d style="width: 1px"></d></c></b></a>',
|
||||
expectMismatch(
|
||||
hasHtml('<a><b><c><d style="width: 2px"></d></c></b></a>'),
|
||||
'''
|
||||
The following DOM structure did not match the expected pattern:
|
||||
<a><b><c><d style="width: 1px"></d></c></b></a>
|
||||
|
||||
Specifically:
|
||||
- @a > b > c > d#style(width): expected style property width="2px", but found width="1px".''',
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void expectDom(String domHtml, Matcher matcher) {
|
||||
final DomElement root = createDomElement('div');
|
||||
root.innerHTML = domHtml;
|
||||
expect(root.children.single, matcher);
|
||||
}
|
||||
|
||||
Matcher expectMismatch(Matcher matcher, String expectedMismatchDescription) {
|
||||
return _ExpectMismatch(matcher, expectedMismatchDescription);
|
||||
}
|
||||
|
||||
class _ExpectMismatch extends Matcher {
|
||||
const _ExpectMismatch(this._matcher, this.expectedMismatchDescription);
|
||||
|
||||
final Matcher _matcher;
|
||||
final String expectedMismatchDescription;
|
||||
|
||||
@override
|
||||
bool matches(Object? item, Map<Object?, Object?> matchState) {
|
||||
if (_matcher.matches(item, matchState)) {
|
||||
matchState['matched'] = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
final _TestDescription description = _TestDescription();
|
||||
_matcher.describeMismatch(
|
||||
item,
|
||||
description,
|
||||
matchState,
|
||||
false,
|
||||
);
|
||||
final String mismatchDescription = description.items.join();
|
||||
|
||||
if (mismatchDescription.trim() != expectedMismatchDescription.trim()) {
|
||||
matchState['mismatchDescription'] = mismatchDescription;
|
||||
matchState['expectedMismatchDescription'] = expectedMismatchDescription;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Description describe(Description description) =>
|
||||
description.add('not ').addDescriptionOf(_matcher);
|
||||
|
||||
@override
|
||||
Description describeMismatch(
|
||||
Object? object,
|
||||
Description mismatchDescription,
|
||||
Map<Object?, Object?> matchState,
|
||||
bool verbose,
|
||||
) {
|
||||
if (matchState.containsKey('matched')) {
|
||||
mismatchDescription.add('Expected a mismatch, but the HTML pattern matched.');
|
||||
}
|
||||
if (matchState.containsKey('mismatchDescription')) {
|
||||
mismatchDescription.add('Mismatch description was wrong.\n');
|
||||
mismatchDescription.add(' Expected: ${matchState['expectedMismatchDescription']}\n');
|
||||
mismatchDescription.add(' Actual : ${matchState['mismatchDescription']}\n');
|
||||
}
|
||||
|
||||
return mismatchDescription;
|
||||
}
|
||||
}
|
||||
|
||||
class _TestDescription implements Description {
|
||||
final List<String> items = <String>[];
|
||||
|
||||
@override
|
||||
int get length => items.length;
|
||||
|
||||
@override
|
||||
Description add(String text) {
|
||||
items.add(text);
|
||||
return this;
|
||||
}
|
||||
|
||||
@override
|
||||
Description addAll(String start, String separator, String end, Iterable<Object?> list) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Description addDescriptionOf(Object? value) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Description replace(String text) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
@@ -1768,12 +1768,24 @@ void _testTextField() {
|
||||
);
|
||||
|
||||
owner().updateSemantics(builder.build());
|
||||
|
||||
expectSemanticsTree(owner(), '''
|
||||
<sem style="$rootSemanticStyle">
|
||||
<input value="hello" />
|
||||
<input />
|
||||
</sem>''');
|
||||
|
||||
|
||||
final SemanticsObject node = owner().debugSemanticsTree![0]!;
|
||||
|
||||
// TODO(yjbanov): this used to attempt to test that value="hello" but the
|
||||
// test was a false positive. We should revise this test and
|
||||
// make sure it tests the right things:
|
||||
// https://github.com/flutter/flutter/issues/147200
|
||||
expect(
|
||||
(node.element as DomHTMLInputElement).value,
|
||||
isNull,
|
||||
);
|
||||
|
||||
expect(node.primaryRole?.role, PrimaryRole.textField);
|
||||
expect(
|
||||
reason: 'Text fields use custom focus management',
|
||||
|
||||
@@ -347,10 +347,9 @@ class SemanticsTester {
|
||||
|
||||
/// Verifies the HTML structure of the current semantics tree.
|
||||
void expectSemanticsTree(EngineSemanticsOwner owner, String semanticsHtml) {
|
||||
const List<String> ignoredStyleProperties = <String>['pointer-events'];
|
||||
expect(
|
||||
canonicalizeHtml(owner.semanticsHost.querySelector('flt-semantics')!.outerHTML!, ignoredStyleProperties: ignoredStyleProperties),
|
||||
canonicalizeHtml(semanticsHtml),
|
||||
owner.semanticsHost.querySelector('flt-semantics'),
|
||||
hasHtml(semanticsHtml),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -93,8 +93,18 @@ void testMain() {
|
||||
|
||||
expectSemanticsTree(owner(), '''
|
||||
<sem style="$rootSemanticStyle">
|
||||
<input value="hello" />
|
||||
<input />
|
||||
</sem>''');
|
||||
|
||||
// TODO(yjbanov): this used to attempt to test that value="hello" but the
|
||||
// test was a false positive. We should revise this test and
|
||||
// make sure it tests the right things:
|
||||
// https://github.com/flutter/flutter/issues/147200
|
||||
final SemanticsObject node = owner().debugSemanticsTree![0]!;
|
||||
expect(
|
||||
(node.element as DomHTMLInputElement).value,
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
// TODO(yjbanov): this test will need to be adjusted for Safari when we add
|
||||
|
||||
@@ -33,7 +33,7 @@ void testMain() {
|
||||
testLayerLifeCycle((ui.SceneBuilder sceneBuilder, ui.EngineLayer? oldLayer) {
|
||||
return sceneBuilder.pushOffset(10, 20, oldLayer: oldLayer as ui.OffsetEngineLayer?);
|
||||
}, () {
|
||||
return '''<s><flt-offset></flt-offset></s>''';
|
||||
return '''<flt-scene><flt-offset></flt-offset></flt-scene>''';
|
||||
});
|
||||
});
|
||||
|
||||
@@ -42,7 +42,7 @@ void testMain() {
|
||||
return sceneBuilder.pushTransform(
|
||||
(Matrix4.identity()..scale(EngineFlutterDisplay.instance.browserDevicePixelRatio)).toFloat64());
|
||||
}, () {
|
||||
return '''<s><flt-transform></flt-transform></s>''';
|
||||
return '''<flt-scene><flt-transform></flt-transform></flt-scene>''';
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,9 +52,9 @@ void testMain() {
|
||||
oldLayer: oldLayer as ui.ClipRectEngineLayer?);
|
||||
}, () {
|
||||
return '''
|
||||
<s>
|
||||
<clip><clip-i></clip-i></clip>
|
||||
</s>
|
||||
<flt-scene>
|
||||
<flt-clip><flt-clip-interior></flt-clip-interior></flt-clip>
|
||||
</flt-scene>
|
||||
''';
|
||||
});
|
||||
});
|
||||
@@ -67,9 +67,11 @@ void testMain() {
|
||||
clipBehavior: ui.Clip.none);
|
||||
}, () {
|
||||
return '''
|
||||
<s>
|
||||
<rclip><clip-i></clip-i></rclip>
|
||||
</s>
|
||||
<flt-scene>
|
||||
<flt-clip clip-type="rrect">
|
||||
<flt-clip-interior></flt-clip-interior>
|
||||
</flt-clip>
|
||||
</flt-scene>
|
||||
''';
|
||||
});
|
||||
});
|
||||
@@ -80,11 +82,11 @@ void testMain() {
|
||||
return sceneBuilder.pushClipPath(path, oldLayer: oldLayer as ui.ClipPathEngineLayer?);
|
||||
}, () {
|
||||
return '''
|
||||
<s>
|
||||
<flt-scene>
|
||||
<flt-clippath>
|
||||
<svg><defs><clipPath><path></path></clipPath></defs></svg>
|
||||
</flt-clippath>
|
||||
</s>
|
||||
</flt-scene>
|
||||
''';
|
||||
});
|
||||
});
|
||||
@@ -93,7 +95,7 @@ void testMain() {
|
||||
testLayerLifeCycle((ui.SceneBuilder sceneBuilder, ui.EngineLayer? oldLayer) {
|
||||
return sceneBuilder.pushOpacity(10, oldLayer: oldLayer as ui.OpacityEngineLayer?);
|
||||
}, () {
|
||||
return '''<s><o></o></s>''';
|
||||
return '''<flt-scene><flt-opacity></flt-opacity></flt-scene>''';
|
||||
});
|
||||
});
|
||||
test('pushBackdropFilter implements surface lifecycle', () {
|
||||
@@ -103,10 +105,13 @@ void testMain() {
|
||||
oldLayer: oldLayer as ui.BackdropFilterEngineLayer?,
|
||||
);
|
||||
}, () {
|
||||
return '<s><flt-backdrop>'
|
||||
'<flt-backdrop-filter></flt-backdrop-filter>'
|
||||
'<flt-backdrop-interior></flt-backdrop-interior>'
|
||||
'</flt-backdrop></s>';
|
||||
return '''
|
||||
<flt-scene>
|
||||
<flt-backdrop>
|
||||
<flt-backdrop-filter></flt-backdrop-filter>
|
||||
<flt-backdrop-interior></flt-backdrop-interior>
|
||||
</flt-backdrop>
|
||||
</flt-scene>''';
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -733,7 +738,7 @@ void testLayerLifeCycle(
|
||||
// Recycle: discards all the layers.
|
||||
sceneBuilder = SurfaceSceneBuilder();
|
||||
tester = SceneTester(sceneBuilder.build());
|
||||
tester.expectSceneHtml('<s></s>');
|
||||
tester.expectSceneHtml('<flt-scene></flt-scene>');
|
||||
|
||||
expect(surface3.rootElement, isNull); // offset3 should be recycled.
|
||||
|
||||
@@ -916,3 +921,16 @@ HtmlImage createTestImage({int width = 100, int height = 50}) {
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
class SceneTester {
|
||||
SceneTester(this.scene);
|
||||
|
||||
final SurfaceScene scene;
|
||||
|
||||
void expectSceneHtml(String expectedHtml) {
|
||||
expect(
|
||||
scene.webOnlyRootElement,
|
||||
hasHtml(expectedHtml),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user