Improve path metrics tests and docs (flutter/engine#7851)

* Improve path metrics tests and docs
This commit is contained in:
Dan Field
2019-04-05 13:58:46 -07:00
committed by GitHub
parent 19d5a39d24
commit 812955c39d
2 changed files with 36 additions and 9 deletions

View File

@@ -2257,7 +2257,9 @@ class PathMetricIterator implements Iterator<PathMetric> {
/// 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) {

View File

@@ -207,14 +207,42 @@ void main() {
..lineTo(10, 15);
final List<PathMetric> 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);
});
}