From 6b1f84fe1070e62afd8fc9cd4ab7709f9215f4a1 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Wed, 14 Sep 2022 09:46:00 -0700 Subject: [PATCH] Reland "Update accessibility contrast test coverage (#109784)" (#110502) This reverts commit 41b50c22b0a5bee445abb0557455d4ffe887d9f7. --- .../flutter_test/lib/src/accessibility.dart | 119 ++++++++++-------- .../flutter_test/test/accessibility_test.dart | 55 ++++++++ 2 files changed, 123 insertions(+), 51 deletions(-) diff --git a/packages/flutter_test/lib/src/accessibility.dart b/packages/flutter_test/lib/src/accessibility.dart index 32fb119e5b..53bc01e874 100644 --- a/packages/flutter_test/lib/src/accessibility.dart +++ b/packages/flutter_test/lib/src/accessibility.dart @@ -331,61 +331,79 @@ class MinimumTextContrastGuideline extends AccessibilityGuideline { if (shouldSkipNode(data)) { return result; } + final String text = data.label.isEmpty ? data.value : data.label; + final Iterable elements = find.text(text).hitTestable().evaluate(); + for (final Element element in elements) { + result += await _evaluateElement(node, element, tester, image, byteData); + } + return result; + } + Future _evaluateElement( + SemanticsNode node, + Element element, + WidgetTester tester, + ui.Image image, + ByteData byteData, + ) async { // Look up inherited text properties to determine text size and weight. late bool isBold; double? fontSize; - final String text = data.label.isEmpty ? data.value : data.label; - final List elements = find.text(text).hitTestable().evaluate().toList(); late final Rect paintBounds; + late final Rect paintBoundsWithOffset; - if (elements.length == 1) { - final Element element = elements.single; - final RenderObject? renderBox = element.renderObject; - if (renderBox is! RenderBox) { - throw StateError('Unexpected renderObject type: $renderBox'); - } + final RenderObject? renderBox = element.renderObject; + if (renderBox is! RenderBox) { + throw StateError('Unexpected renderObject type: $renderBox'); + } - const Offset offset = Offset(4.0, 4.0); - paintBounds = Rect.fromPoints( - renderBox.localToGlobal(renderBox.paintBounds.topLeft - offset), - renderBox.localToGlobal(renderBox.paintBounds.bottomRight + offset), - ); - final Widget widget = element.widget; - final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(element); - if (widget is Text) { - final TextStyle? style = widget.style; - final TextStyle effectiveTextStyle = style == null || style.inherit - ? defaultTextStyle.style.merge(widget.style) - : style; - isBold = effectiveTextStyle.fontWeight == FontWeight.bold; - fontSize = effectiveTextStyle.fontSize; - } else if (widget is EditableText) { - isBold = widget.style.fontWeight == FontWeight.bold; - fontSize = widget.style.fontSize; - } else { - throw StateError('Unexpected widget type: ${widget.runtimeType}'); - } - } else if (elements.length > 1) { - return Evaluation.fail( - 'Multiple nodes with the same label: ${data.label}\n', - ); + const Offset offset = Offset(4.0, 4.0); + paintBoundsWithOffset = Rect.fromPoints( + renderBox.localToGlobal(renderBox.paintBounds.topLeft - offset), + renderBox.localToGlobal(renderBox.paintBounds.bottomRight + offset), + ); + + paintBounds = Rect.fromPoints( + renderBox.localToGlobal(renderBox.paintBounds.topLeft), + renderBox.localToGlobal(renderBox.paintBounds.bottomRight), + ); + + final Offset? nodeOffset = node.transform != null ? MatrixUtils.getAsTranslation(node.transform!) : null; + + final Rect nodeBounds = node.rect.shift(nodeOffset ?? Offset.zero); + final Rect intersection = nodeBounds.intersect(paintBounds); + if (intersection.width <= 0 || intersection.height <= 0) { + // Skip this element since it doesn't correspond to the given semantic + // node. + return const Evaluation.pass(); + } + + final Widget widget = element.widget; + final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(element); + if (widget is Text) { + final TextStyle? style = widget.style; + final TextStyle effectiveTextStyle = style == null || style.inherit + ? defaultTextStyle.style.merge(widget.style) + : style; + isBold = effectiveTextStyle.fontWeight == FontWeight.bold; + fontSize = effectiveTextStyle.fontSize; + } else if (widget is EditableText) { + isBold = widget.style.fontWeight == FontWeight.bold; + fontSize = widget.style.fontSize; } else { - // If we can't find the text node then assume the label does not - // correspond to actual text. - return result; + throw StateError('Unexpected widget type: ${widget.runtimeType}'); } - if (isNodeOffScreen(paintBounds, tester.binding.window)) { - return result; + if (isNodeOffScreen(paintBoundsWithOffset, tester.binding.window)) { + return const Evaluation.pass(); } - final Map colorHistogram = _colorsWithinRect(byteData, paintBounds, image.width, image.height); + final Map colorHistogram = _colorsWithinRect(byteData, paintBoundsWithOffset, image.width, image.height); // Node was too far off screen. if (colorHistogram.isEmpty) { - return result; + return const Evaluation.pass(); } final _ContrastReport report = _ContrastReport(colorHistogram); @@ -394,19 +412,18 @@ class MinimumTextContrastGuideline extends AccessibilityGuideline { final double targetContrastRatio = this.targetContrastRatio(fontSize, bold: isBold); if (contrastRatio - targetContrastRatio >= _tolerance) { - return result + const Evaluation.pass(); + return const Evaluation.pass(); } - return result + - Evaluation.fail( - '$node:\n' - 'Expected contrast ratio of at least $targetContrastRatio ' - 'but found ${contrastRatio.toStringAsFixed(2)} ' - 'for a font size of $fontSize.\n' - 'The computed colors was:\n' - 'light - ${report.lightColor}, dark - ${report.darkColor}\n' - 'See also: ' - 'https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html', - ); + return Evaluation.fail( + '$node:\n' + 'Expected contrast ratio of at least $targetContrastRatio ' + 'but found ${contrastRatio.toStringAsFixed(2)} ' + 'for a font size of $fontSize.\n' + 'The computed colors was:\n' + 'light - ${report.lightColor}, dark - ${report.darkColor}\n' + 'See also: ' + 'https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html', + ); } /// Returns whether node should be skipped. diff --git a/packages/flutter_test/test/accessibility_test.dart b/packages/flutter_test/test/accessibility_test.dart index 399a33e77f..a8aee694ed 100644 --- a/packages/flutter_test/test/accessibility_test.dart +++ b/packages/flutter_test/test/accessibility_test.dart @@ -23,6 +23,61 @@ void main() { handle.dispose(); }); + testWidgets('Multiple text with same label', (WidgetTester tester) async { + final SemanticsHandle handle = tester.ensureSemantics(); + await tester.pumpWidget( + _boilerplate( + Column( + children: const [ + Text( + 'this is a test', + style: TextStyle(fontSize: 14.0, color: Colors.black), + ), + Text( + 'this is a test', + style: TextStyle(fontSize: 14.0, color: Colors.black), + ), + ], + ), + ), + ); + await expectLater(tester, meetsGuideline(textContrastGuideline)); + handle.dispose(); + }); + + testWidgets( + 'Multiple text with same label but Nodes excluded from ' + 'semantic tree have failing contrast should pass a11y guideline ', + (WidgetTester tester) async { + final SemanticsHandle handle = tester.ensureSemantics(); + await tester.pumpWidget( + _boilerplate( + Column( + children: const [ + Text( + 'this is a test', + style: TextStyle(fontSize: 14.0, color: Colors.black), + ), + SizedBox(height: 50), + Text( + 'this is a test', + style: TextStyle(fontSize: 14.0, color: Colors.black), + ), + SizedBox(height: 50), + ExcludeSemantics( + child: Text( + 'this is a test', + style: TextStyle(fontSize: 14.0, color: Colors.white), + ), + ), + ], + ), + ), + ); + await expectLater(tester, meetsGuideline(textContrastGuideline)); + handle.dispose(); + }); + testWidgets('white text on black background - Text Widget - direct style', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics();