From 58348612a3e7286550dc6f38365a7b4b5b0e6fb3 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 24 Jan 2017 16:08:27 -0800 Subject: [PATCH] Factor out debugPaintPadding and test it (#7598) I plan to use this to implement similar logic in SliverPadding. To make this easier to test I extended the paints matcher to accept a function that takes a canvas. While I was at it I also made it accept a Finder, it'll go and find the render object for you. Also added support for paints..path and fixed some grammar in the error messages. Also improved the docs for debugPaint*. --- .../flutter/lib/src/painting/basic_types.dart | 1 + packages/flutter/lib/src/rendering/debug.dart | 41 ++++++++- .../lib/src/rendering/shifted_box.dart | 39 +------- .../material/mergeable_material_test.dart | 7 +- .../flutter/test/rendering/debug_test.dart | 16 +++- .../flutter/test/rendering/mock_canvas.dart | 88 ++++++++++++++++--- 6 files changed, 138 insertions(+), 54 deletions(-) diff --git a/packages/flutter/lib/src/painting/basic_types.dart b/packages/flutter/lib/src/painting/basic_types.dart index f43fce72cf..ba4a2bfd1f 100644 --- a/packages/flutter/lib/src/painting/basic_types.dart +++ b/packages/flutter/lib/src/painting/basic_types.dart @@ -17,6 +17,7 @@ export 'dart:ui' show Paint, PaintingStyle, Path, + PathFillType, Point, Radius, RRect, diff --git a/packages/flutter/lib/src/rendering/debug.dart b/packages/flutter/lib/src/rendering/debug.dart index 3efd8bceca..c815f9cabe 100644 --- a/packages/flutter/lib/src/rendering/debug.dart +++ b/packages/flutter/lib/src/rendering/debug.dart @@ -31,12 +31,22 @@ Color debugPaintSizeColor = _kDebugPaintSizeColor; /// The color to use when painting some boxes that just add space (e.g. an empty /// RenderConstrainedBox or RenderPadding). +/// +/// Used by, among other methods, [debugPaintPadding], which is called by +/// [RenderPadding.debugPaintSize] when [debugPaintSizeEnabled] is true. Color debugPaintSpacingColor = _kDebugPaintSpacingColor; /// The color to use when painting RenderPadding edges. +/// +/// Used by, among other methods, [debugPaintPadding], which is called by +/// [RenderPadding.debugPaintSize] when [debugPaintSizeEnabled] is true. Color debugPaintPaddingColor = _kDebugPaintPaddingColor; -/// The color to use when painting RenderPadding edges. +/// The color to use when painting RenderPadding edges. This color is painted on +/// top of [debugPaintPaddingColor]. +/// +/// Used by, among other methods, [debugPaintPadding], which is called by +/// [RenderPadding.debugPaintSize] when [debugPaintSizeEnabled] is true. Color debugPaintPaddingInnerEdgeColor = _kDebugPaintPaddingInnerEdgeColor; /// The color to use when painting the arrows used to show RenderPositionedBox alignment. @@ -105,6 +115,35 @@ List debugDescribeTransform(Matrix4 transform) { return matrix; } +void _debugDrawDoubleRect(Canvas canvas, Rect outerRect, Rect innerRect, Color color) { + final Path path = new Path() + ..fillType = PathFillType.evenOdd + ..addRect(outerRect) + ..addRect(innerRect); + final Paint paint = new Paint() + ..color = color; + canvas.drawPath(path, paint); +} + +/// Paint padding using the [debugPaintPaddingColor], +/// [debugPaintPaddingInnerEdgeColor], and [debugPaintSpacingColor] colors. +/// +/// Called by [RenderPadding.debugPaintSize] when [debugPaintSizeEnabled] is +/// true. +void debugPaintPadding(Canvas canvas, Rect outerRect, Rect innerRect, { double outlineWidth: 2.0 }) { + assert(() { + if (innerRect != null && !innerRect.isEmpty) { + _debugDrawDoubleRect(canvas, outerRect, innerRect, debugPaintPaddingColor); + _debugDrawDoubleRect(canvas, innerRect.inflate(outlineWidth).intersect(outerRect), innerRect, debugPaintPaddingInnerEdgeColor); + } else { + final Paint paint = new Paint() + ..color = debugPaintSpacingColor; + canvas.drawRect(outerRect, paint); + } + return true; + }); +} + /// Returns true if none of the rendering library debug variables have been changed. /// /// This function is used by the test framework to ensure that debug variables diff --git a/packages/flutter/lib/src/rendering/shifted_box.dart b/packages/flutter/lib/src/rendering/shifted_box.dart index d5d761ed83..265f365863 100644 --- a/packages/flutter/lib/src/rendering/shifted_box.dart +++ b/packages/flutter/lib/src/rendering/shifted_box.dart @@ -172,43 +172,8 @@ class RenderPadding extends RenderShiftedBox { void debugPaintSize(PaintingContext context, Offset offset) { super.debugPaintSize(context, offset); assert(() { - Paint paint; - if (child != null && !child.size.isEmpty) { - Path path; - paint = new Paint() - ..color = debugPaintPaddingColor; - path = new Path() - ..moveTo(offset.dx, offset.dy) - ..lineTo(offset.dx + size.width, offset.dy) - ..lineTo(offset.dx + size.width, offset.dy + size.height) - ..lineTo(offset.dx, offset.dy + size.height) - ..close() - ..moveTo(offset.dx + padding.left, offset.dy + padding.top) - ..lineTo(offset.dx + padding.left, offset.dy + size.height - padding.bottom) - ..lineTo(offset.dx + size.width - padding.right, offset.dy + size.height - padding.bottom) - ..lineTo(offset.dx + size.width - padding.right, offset.dy + padding.top) - ..close(); - context.canvas.drawPath(path, paint); - paint = new Paint() - ..color = debugPaintPaddingInnerEdgeColor; - const double kOutline = 2.0; - path = new Path() - ..moveTo(offset.dx + math.max(padding.left - kOutline, 0.0), offset.dy + math.max(padding.top - kOutline, 0.0)) - ..lineTo(offset.dx + math.min(size.width - padding.right + kOutline, size.width), offset.dy + math.max(padding.top - kOutline, 0.0)) - ..lineTo(offset.dx + math.min(size.width - padding.right + kOutline, size.width), offset.dy + math.min(size.height - padding.bottom + kOutline, size.height)) - ..lineTo(offset.dx + math.max(padding.left - kOutline, 0.0), offset.dy + math.min(size.height - padding.bottom + kOutline, size.height)) - ..close() - ..moveTo(offset.dx + padding.left, offset.dy + padding.top) - ..lineTo(offset.dx + padding.left, offset.dy + size.height - padding.bottom) - ..lineTo(offset.dx + size.width - padding.right, offset.dy + size.height - padding.bottom) - ..lineTo(offset.dx + size.width - padding.right, offset.dy + padding.top) - ..close(); - context.canvas.drawPath(path, paint); - } else { - paint = new Paint() - ..color = debugPaintSpacingColor; - context.canvas.drawRect(offset & size, paint); - } + final Rect outerRect = offset & size; + debugPaintPadding(context.canvas, outerRect, child != null ? padding.deflateRect(outerRect) : null); return true; }); } diff --git a/packages/flutter/test/material/mergeable_material_test.dart b/packages/flutter/test/material/mergeable_material_test.dart index d77aeeae65..16893ada8c 100644 --- a/packages/flutter/test/material/mergeable_material_test.dart +++ b/packages/flutter/test/material/mergeable_material_test.dart @@ -203,13 +203,14 @@ void main() { ) ); - RenderBox box = tester.renderObject(find.byType(MergeableMaterial)); - final BoxShadow boxShadow = kElevationToShadow[2][0]; final RRect rrect = kMaterialEdges[MaterialType.card].toRRect( new Rect.fromLTRB(0.0, 0.0, 800.0, 100.0) ); - expect(box, paints..rrect(rrect: rrect, color: boxShadow.color, hasMaskFilter: true)); + expect( + find.byType(MergeableMaterial), + paints..rrect(rrect: rrect, color: boxShadow.color, hasMaskFilter: true), + ); }); testWidgets('MergeableMaterial merge gap', (WidgetTester tester) async { diff --git a/packages/flutter/test/rendering/debug_test.dart b/packages/flutter/test/rendering/debug_test.dart index 581f810cf8..f810ab9b5b 100644 --- a/packages/flutter/test/rendering/debug_test.dart +++ b/packages/flutter/test/rendering/debug_test.dart @@ -6,8 +6,10 @@ import 'package:flutter/rendering.dart'; import 'package:test/test.dart'; import 'package:vector_math/vector_math_64.dart'; +import 'mock_canvas.dart'; + void main() { - test("Describe transform control test", () { + test('Describe transform control test', () { Matrix4 identity = new Matrix4.identity(); List description = debugDescribeTransform(identity); expect(description, equals([ @@ -17,4 +19,16 @@ void main() { ' [3] 0.0,0.0,0.0,1.0', ])); }); + + test('debugPaintPadding', () { + expect((Canvas canvas) { + debugPaintPadding(canvas, new Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), null); + }, paints..rect(color: debugPaintSpacingColor)); + expect((Canvas canvas) { + debugPaintPadding(canvas, new Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), new Rect.fromLTRB(11.0, 11.0, 19.0, 19.0)); + }, paints..path(color: debugPaintPaddingColor)..path(color: debugPaintPaddingInnerEdgeColor)); + expect((Canvas canvas) { + debugPaintPadding(canvas, new Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), new Rect.fromLTRB(15.0, 15.0, 15.0, 15.0)); + }, paints..rect(rect: new Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), color: debugPaintSpacingColor)); + }); } diff --git a/packages/flutter/test/rendering/mock_canvas.dart b/packages/flutter/test/rendering/mock_canvas.dart index 455786f22c..87622acc51 100644 --- a/packages/flutter/test/rendering/mock_canvas.dart +++ b/packages/flutter/test/rendering/mock_canvas.dart @@ -4,10 +4,22 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; -import 'package:test/test.dart'; +import 'package:flutter_test/flutter_test.dart'; -/// Matches [RenderObject]s that paint a display list that matches the canvas -/// calls described by the pattern. +/// Matches objects or functions that paint a display list that matches the +/// canvas calls described by the pattern. +/// +/// Specifically, this can be applied to [RenderObject]s, [Finder]s that +/// correspond to a single [RenderObject], and functions that have either of the +/// following signatures: +/// +/// ```dart +/// void function(PaintingContext context, Offset offset); +/// void function(Canvas canvas); +/// ``` +/// +/// In the case of functions that take a [PaintingContext] and an [Offset], the +/// [paints] matcher will always pass a zero offset. /// /// To specify the pattern, call the methods on the returned object. For example: /// @@ -34,6 +46,12 @@ PaintPattern get paints => new _TestRecordingCanvasPatternMatcher(); /// ``` typedef bool PaintPatternPredicate(Symbol methodName, List arguments); +/// The signature of [RenderObject.paint] functions. +typedef void _ContextPainterFunction(PaintingContext context, Offset offset); + +/// The signature of functions that paint directly on a canvas. +typedef void _CanvasPainterFunction(Canvas canvas); + /// Builder interface for patterns used to match display lists (canvas calls). /// /// The [paints] matcher returns a [PaintPattern] so that you can build the @@ -125,6 +143,21 @@ abstract class PaintPattern { /// [Canvas.drawCircle] call are ignored. void circle({ double x, double y, double radius, Color color, bool hasMaskFilter, PaintingStyle style }); + /// Indicates that a path is expected next. + /// + /// The next path is examined. Any arguments that are passed to this method + /// are compared to the actual [Canvas.drawPath] call's `paint` argument, and + /// any mismatches result in failure. + /// + /// There is currently no way to check the actual path itself. + // See https://github.com/flutter/flutter/issues/93 which tracks that issue. + /// + /// If no call to [Canvas.drawPath] was made, then this results in failure. + /// + /// Any calls made between the last matched call (if any) and the + /// [Canvas.drawPath] call are ignored. + void path({ Color color, bool hasMaskFilter, PaintingStyle style }); + /// Provides a custom matcher. /// /// Each method call after the last matched call (if any) will be passed to @@ -192,12 +225,16 @@ class _TestRecordingCanvasPatternMatcher extends Matcher implements PaintPattern _predicates.add(new _RRectPaintPredicate(rrect: rrect, color: color, hasMaskFilter: hasMaskFilter, style: style)); } - @override void circle({ double x, double y, double radius, Color color, bool hasMaskFilter, PaintingStyle style }) { _predicates.add(new _CirclePaintPredicate(x: x, y: y, radius: radius, color: color, hasMaskFilter: hasMaskFilter, style: style)); } + @override + void path({ Color color, bool hasMaskFilter, PaintingStyle style }) { + _predicates.add(new _PathPaintPredicate(color: color, hasMaskFilter: hasMaskFilter, style: style)); + } + @override void something(PaintPatternPredicate predicate) { _predicates.add(new _SomethingPaintPredicate(predicate)); @@ -205,12 +242,28 @@ class _TestRecordingCanvasPatternMatcher extends Matcher implements PaintPattern @override bool matches(Object object, Map matchState) { - if (object is! RenderObject) - return false; final _TestRecordingCanvas canvas = new _TestRecordingCanvas(); final _TestRecordingPaintingContext context = new _TestRecordingPaintingContext(canvas); - final RenderObject renderObject = object; - renderObject.paint(context, Offset.zero); + if (object is _ContextPainterFunction) { + final _ContextPainterFunction function = object; + function(context, Offset.zero); + } else if (object is _CanvasPainterFunction) { + final _CanvasPainterFunction function = object; + function(canvas); + } else { + if (object is Finder) { + TestAsyncUtils.guardSync(); + final Finder finder = object; + object = finder.evaluate().single.renderObject; + } + if (object is RenderObject) { + final RenderObject renderObject = object; + renderObject.paint(context, Offset.zero); + } else { + matchState[this] = 'was not one of the supported objects for the "paints" matcher.'; + return false; + } + } final StringBuffer description = new StringBuffer(); final bool result = _evaluatePredicates(canvas._invocations, description); if (!result) { @@ -226,7 +279,7 @@ class _TestRecordingCanvasPatternMatcher extends Matcher implements PaintPattern @override Description describe(Description description) { - description.add('RenderObject painting: '); + description.add('Object or closure painting: '); return description.addAll( '', ', ', '', _predicates.map((_PaintPredicate predicate) => predicate.toString()), @@ -387,7 +440,7 @@ abstract class _DrawCommandPaintPredicate extends _PaintPredicate { if (hasMaskFilter) throw 'called $methodName with a paint that did not have a mask filter, despite expecting one.'; else - throw 'called $methodName with a paint that did had a mask filter, despite not expecting one.'; + throw 'called $methodName with a paint that did have a mask filter, despite not expecting one.'; } if (style != null && paintArgument.style != style) throw 'called $methodName with a paint whose style, ${paintArgument.style}, was not exactly the expected style ($style).'; @@ -437,8 +490,13 @@ class _OneParameterPaintPredicate extends _DrawCommandPaintPredicate { @override void debugFillDescription(List description) { super.debugFillDescription(description); - if (expected != null) - description.add('${T.runtimeType}: $expected'); + if (expected != null) { + if (expected.toString().contains(T.toString())) { + description.add('$expected'); + } else { + description.add('$T: $expected'); + } + } } } @@ -509,6 +567,12 @@ class _CirclePaintPredicate extends _DrawCommandPaintPredicate { } } +class _PathPaintPredicate extends _DrawCommandPaintPredicate { + _PathPaintPredicate({ Color color, bool hasMaskFilter, PaintingStyle style }) : super( + #drawPath, 'a path', 2, 1, color: color, hasMaskFilter: hasMaskFilter, style: style + ); +} + class _SomethingPaintPredicate extends _PaintPredicate { _SomethingPaintPredicate(this.predicate);