From 812955c39df0cc7cbda8241602291204a9af902b Mon Sep 17 00:00:00 2001 From: Dan Field Date: Fri, 5 Apr 2019 13:58:46 -0700 Subject: [PATCH] Improve path metrics tests and docs (flutter/engine#7851) * Improve path metrics tests and docs --- engine/src/flutter/lib/ui/painting.dart | 7 ++-- .../src/flutter/testing/dart/path_test.dart | 38 ++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index e7b0e01c9e..6703dfd010 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -2257,7 +2257,9 @@ class PathMetricIterator implements Iterator { /// iterator is at the contour for which they were created. It will also only be /// valid for the path as it was specified when [Path.computeMetrics] was called. /// If additional contours are added or any contours are updated, the metrics -/// need to be recomputed. +/// need to be recomputed. Previously created metrics will still refer to a +/// snapshot of the path at the time they were computed, rather than to the +/// actual metrics for the new mutations to the path. class PathMetric { PathMetric._(this._measure) : assert(_measure != null), @@ -2364,9 +2366,6 @@ class _PathMeasure extends NativeFieldWrapperClass2 { // // A path can have a next contour if [Path.moveTo] was called after drawing began. // Return true if one exists, or false. - // - // Before Skia introduced an SkPathContourMeasureIter, this didn't work like - // a normal iterator. Now it does. bool _nextContour() { final bool next = _nativeNextContour(); if (next) { diff --git a/engine/src/flutter/testing/dart/path_test.dart b/engine/src/flutter/testing/dart/path_test.dart index 6a0c99f1ae..a65b0bcb82 100644 --- a/engine/src/flutter/testing/dart/path_test.dart +++ b/engine/src/flutter/testing/dart/path_test.dart @@ -207,14 +207,42 @@ void main() { ..lineTo(10, 15); final List metrics = path.computeMetrics().toList(); expect(metrics.length, 2); - print(metrics); expect(metrics[0].length, 20); expect(metrics[0].isClosed, true); - expect(() => metrics[0].getTangentForOffset(4.0), isNotNull); - expect(() => metrics[0].extractPath(4.0, 10.0), isNotNull); + expect(metrics[0].getTangentForOffset(4.0).vector, const Offset(0.0, 1.0)); + expect(metrics[0].extractPath(4.0, 10.0).computeMetrics().first.length, 6.0); expect(metrics[1].length, 10); expect(metrics[1].isClosed, false); - expect(() => metrics[1].getTangentForOffset(4.0), isNotNull); - expect(() => metrics[1].extractPath(4.0, 10.0), isNotNull); + expect(metrics[1].getTangentForOffset(4.0).vector, const Offset(1.0, 0.0)); + expect(metrics[1].extractPath(4.0, 6.0).computeMetrics().first.length, 2.0); + }); + + test('PathMetrics on a mutated path', () { + final Path path = Path()..lineTo(0, 10); + final PathMetrics metrics = path.computeMetrics(); + final PathMetric firstMetric = metrics.first; + // We've consumed the iterator. + expect(metrics, isEmpty); + expect(firstMetric.length, 10); + expect(firstMetric.isClosed, false); + expect(firstMetric.getTangentForOffset(4.0).vector, const Offset(0.0, 1.0)); + expect(firstMetric.extractPath(4.0, 10.0).computeMetrics().first.length, 6.0); + + path..lineTo(10, 10)..lineTo(10, 0)..close(); + // mutating the path shouldn't have added anything to the iterator. + expect(metrics, isEmpty); + expect(firstMetric.length, 10); + expect(firstMetric.isClosed, false); + expect(firstMetric.getTangentForOffset(4.0).vector, const Offset(0.0, 1.0)); + expect(firstMetric.extractPath(4.0, 10.0).computeMetrics().first.length, 6.0); + + // getting a new iterator should update us. + final PathMetrics newMetrics = path.computeMetrics(); + final PathMetric newFirstMetric = newMetrics.first; + expect(newMetrics, isEmpty); + expect(newFirstMetric.length, 40); + expect(newFirstMetric.isClosed, true); + expect(newFirstMetric.getTangentForOffset(4.0).vector, const Offset(0.0, 1.0)); + expect(newFirstMetric.extractPath(4.0, 10.0).computeMetrics().first.length, 6.0); }); }