From 25a9d88894e216405e2281f60e97c5f9905682b2 Mon Sep 17 00:00:00 2001 From: Yegor Date: Mon, 1 Jun 2020 09:43:15 -0700 Subject: [PATCH] implement SkPath.computeMetrics (flutter/engine#18667) * implement SkPath.computeMetrics --- .../flutter/lib/web_ui/build.canvaskit.yaml | 17 ++ .../web_ui/{build.yaml => build.html.yaml} | 5 +- .../flutter/lib/web_ui/dev/environment.dart | 108 +++++++------ .../flutter/lib/web_ui/dev/test_runner.dart | 128 ++++++++++++---- .../lib/src/engine/compositor/path.dart | 13 +- .../src/engine/compositor/path_metrics.dart | 145 ++++++++---------- engine/src/flutter/lib/web_ui/pubspec.yaml | 2 + .../test/canvaskit/path_metrics_test.dart | 67 ++++++++ .../compositor => canvaskit}/util_test.dart | 0 9 files changed, 326 insertions(+), 159 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/build.canvaskit.yaml rename engine/src/flutter/lib/web_ui/{build.yaml => build.html.yaml} (70%) create mode 100644 engine/src/flutter/lib/web_ui/test/canvaskit/path_metrics_test.dart rename engine/src/flutter/lib/web_ui/test/{engine/compositor => canvaskit}/util_test.dart (100%) diff --git a/engine/src/flutter/lib/web_ui/build.canvaskit.yaml b/engine/src/flutter/lib/web_ui/build.canvaskit.yaml new file mode 100644 index 0000000000..e628a26122 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/build.canvaskit.yaml @@ -0,0 +1,17 @@ +# Build configuration used by CanvasKit tests. When building for CanvasKit +# we must pass FLUTTER_WEB_USE_SKIA=true to dart2js. +# +# See also `build.html.yaml`. +targets: + $default: + builders: + build_web_compilers|entrypoint: + options: + compiler: dart2js + dart2js_args: + - --no-minify + - --enable-asserts + - -DFLUTTER_WEB_USE_SKIA=true + generate_for: + include: + - test/canvaskit/**.dart diff --git a/engine/src/flutter/lib/web_ui/build.yaml b/engine/src/flutter/lib/web_ui/build.html.yaml similarity index 70% rename from engine/src/flutter/lib/web_ui/build.yaml rename to engine/src/flutter/lib/web_ui/build.html.yaml index af02fd0fb3..3a208b2045 100644 --- a/engine/src/flutter/lib/web_ui/build.yaml +++ b/engine/src/flutter/lib/web_ui/build.html.yaml @@ -1,3 +1,6 @@ +# Build configuration used by HTML (non-CanvasKit) tests. +# +# See also `build.canvaskit.yaml`. targets: $default: builders: @@ -12,4 +15,4 @@ targets: - test/**.dart exclude: - test/**vm_test.dart - + - test/canvaskit/**.dart diff --git a/engine/src/flutter/lib/web_ui/dev/environment.dart b/engine/src/flutter/lib/web_ui/dev/environment.dart index 824bded595..8147007f9f 100644 --- a/engine/src/flutter/lib/web_ui/dev/environment.dart +++ b/engine/src/flutter/lib/web_ui/dev/environment.dart @@ -13,6 +13,7 @@ Environment get environment { _environment ??= Environment(); return _environment; } + Environment _environment; /// Contains various environment variables, such as common file paths and command-line options. @@ -20,14 +21,26 @@ class Environment { factory Environment() { final io.File self = io.File.fromUri(io.Platform.script); final io.Directory engineSrcDir = self.parent.parent.parent.parent.parent; - final io.Directory engineToolsDir = io.Directory(pathlib.join(engineSrcDir.path, 'flutter', 'tools')); - final io.Directory outDir = io.Directory(pathlib.join(engineSrcDir.path, 'out')); - final io.Directory hostDebugUnoptDir = io.Directory(pathlib.join(outDir.path, 'host_debug_unopt')); - final io.Directory dartSdkDir = io.Directory(pathlib.join(hostDebugUnoptDir.path, 'dart-sdk')); - final io.Directory webUiRootDir = io.Directory(pathlib.join(engineSrcDir.path, 'flutter', 'lib', 'web_ui')); - final io.Directory integrationTestsDir = io.Directory(pathlib.join(engineSrcDir.path, 'flutter', 'e2etests', 'web')); + final io.Directory engineToolsDir = + io.Directory(pathlib.join(engineSrcDir.path, 'flutter', 'tools')); + final io.Directory outDir = + io.Directory(pathlib.join(engineSrcDir.path, 'out')); + final io.Directory hostDebugUnoptDir = + io.Directory(pathlib.join(outDir.path, 'host_debug_unopt')); + final io.Directory dartSdkDir = + io.Directory(pathlib.join(hostDebugUnoptDir.path, 'dart-sdk')); + final io.Directory webUiRootDir = io.Directory( + pathlib.join(engineSrcDir.path, 'flutter', 'lib', 'web_ui')); + final io.Directory integrationTestsDir = io.Directory( + pathlib.join(engineSrcDir.path, 'flutter', 'e2etests', 'web')); - for (io.Directory expectedDirectory in [engineSrcDir, outDir, hostDebugUnoptDir, dartSdkDir, webUiRootDir]) { + for (io.Directory expectedDirectory in [ + engineSrcDir, + outDir, + hostDebugUnoptDir, + dartSdkDir, + webUiRootDir + ]) { if (!expectedDirectory.existsSync()) { throw ToolException('$expectedDirectory does not exist.'); } @@ -89,63 +102,71 @@ class Environment { String get pubExecutable => pathlib.join(dartSdkDir.path, 'bin', 'pub'); /// The "dart2js" executable file. - String get dart2jsExecutable => pathlib.join(dartSdkDir.path, 'bin', 'dart2js'); + String get dart2jsExecutable => + pathlib.join(dartSdkDir.path, 'bin', 'dart2js'); /// Path to where github.com/flutter/engine is checked out inside the engine workspace. - io.Directory get flutterDirectory => io.Directory(pathlib.join(engineSrcDir.path, 'flutter')); + io.Directory get flutterDirectory => + io.Directory(pathlib.join(engineSrcDir.path, 'flutter')); io.Directory get webSdkRootDir => io.Directory(pathlib.join( - flutterDirectory.path, - 'web_sdk', - )); + flutterDirectory.path, + 'web_sdk', + )); /// Path to the "web_engine_tester" package. io.Directory get webEngineTesterRootDir => io.Directory(pathlib.join( - webSdkRootDir.path, - 'web_engine_tester', - )); + webSdkRootDir.path, + 'web_engine_tester', + )); /// Path to the "build" directory, generated by "package:build_runner". /// /// This is where compiled output goes. io.Directory get webUiBuildDir => io.Directory(pathlib.join( - webUiRootDir.path, - 'build', - )); + webUiRootDir.path, + 'build', + )); /// Path to the ".dart_tool" directory, generated by various Dart tools. io.Directory get webUiDartToolDir => io.Directory(pathlib.join( - webUiRootDir.path, - '.dart_tool', - )); + webUiRootDir.path, + '.dart_tool', + )); - /// Path to the ".dart_tool" directory living under `engine/src/flutter`. - /// - /// This is a designated area for tool downloads which can be used by - /// multiple platforms. For exampe: Flutter repo for e2e tests. + /// Path to the ".dart_tool" directory living under `engine/src/flutter`. + /// + /// This is a designated area for tool downloads which can be used by + /// multiple platforms. For exampe: Flutter repo for e2e tests. io.Directory get engineDartToolDir => io.Directory(pathlib.join( - engineSrcDir.path, - 'flutter', - '.dart_tool', - )); + engineSrcDir.path, + 'flutter', + '.dart_tool', + )); /// Path to the "dev" directory containing engine developer tools and /// configuration files. io.Directory get webUiDevDir => io.Directory(pathlib.join( - webUiRootDir.path, - 'dev', - )); + webUiRootDir.path, + 'dev', + )); + + /// Path to the "test" directory containing web engine tests. + io.Directory get webUiTestDir => io.Directory(pathlib.join( + webUiRootDir.path, + 'test', + )); /// Path to the clone of the flutter/goldens repository. io.Directory get webUiGoldensRepositoryDirectory => io.Directory(pathlib.join( - webUiDartToolDir.path, - 'goldens', - )); + webUiDartToolDir.path, + 'goldens', + )); /// Path to the script that clones the Flutter repo. io.File get cloneFlutterScript => io.File(pathlib.join( - engineToolsDir.path, - 'clone_flutter.sh', - )); + engineToolsDir.path, + 'clone_flutter.sh', + )); /// Path to flutter. /// @@ -153,10 +174,9 @@ class Environment { /// /// Only use [cloneFlutterScript] to clone flutter to the engine build. io.File get flutterCommand => io.File(pathlib.join( - engineDartToolDir.path, - 'flutter', - 'bin', - 'flutter', - )); - + engineDartToolDir.path, + 'flutter', + 'bin', + 'flutter', + )); } diff --git a/engine/src/flutter/lib/web_ui/dev/test_runner.dart b/engine/src/flutter/lib/web_ui/dev/test_runner.dart index 41f23d75c9..948b5f6953 100644 --- a/engine/src/flutter/lib/web_ui/dev/test_runner.dart +++ b/engine/src/flutter/lib/web_ui/dev/test_runner.dart @@ -57,17 +57,17 @@ class TestCommand extends Command with ArgUtils { 'tests.', ) ..addFlag('use-system-flutter', - defaultsTo: false, - help: 'integration tests are using flutter repository for various tasks' - ', such as flutter drive, flutter pub get. If this flag is set, felt ' - 'will use flutter command without cloning the repository. This flag ' - 'can save internet bandwidth. However use with caution. Note that ' - 'since flutter repo is always synced to youngest commit older than ' - 'the engine commit for the tests running in CI, the tests results ' - 'won\'t be consistent with CIs when this flag is set. flutter ' - 'command should be set in the PATH for this flag to be useful.' - 'This flag can also be used to test local Flutter changes.' - ) + defaultsTo: false, + help: + 'integration tests are using flutter repository for various tasks' + ', such as flutter drive, flutter pub get. If this flag is set, felt ' + 'will use flutter command without cloning the repository. This flag ' + 'can save internet bandwidth. However use with caution. Note that ' + 'since flutter repo is always synced to youngest commit older than ' + 'the engine commit for the tests running in CI, the tests results ' + 'won\'t be consistent with CIs when this flag is set. flutter ' + 'command should be set in the PATH for this flag to be useful.' + 'This flag can also be used to test local Flutter changes.') ..addFlag( 'update-screenshot-goldens', defaultsTo: false, @@ -159,8 +159,6 @@ class TestCommand extends Command with ArgUtils { await _runPubGet(); } - await _buildTests(targets: targetFiles); - // Many tabs will be left open after Safari runs, quit Safari during // cleanup. if (browser == 'safari') { @@ -180,14 +178,57 @@ class TestCommand extends Command with ArgUtils { }); } + await _buildTargets(); + if (runAllTests) { - await _runAllTests(); + await _runAllTestsForCurrentPlatform(); } else { - await _runTargetTests(targetFiles); + await _runSpecificTests(targetFiles); } return true; } + /// Builds all test targets that will be run. + Future _buildTargets() async { + final Stopwatch stopwatch = Stopwatch()..start(); + List allTargets; + if (runAllTests) { + allTargets = environment.webUiTestDir + .listSync(recursive: true) + .whereType() + .where((io.File f) => f.path.endsWith('_test.dart')) + .map((io.File f) => FilePath.fromWebUi( + path.relative(f.path, from: environment.webUiRootDir.path))) + .toList(); + } else { + allTargets = targetFiles; + } + + // Separate HTML targets from CanvasKit targets because the two use + // different dart2js options (and different build.*.yaml files). + final List htmlTargets = []; + final List canvasKitTargets = []; + final String canvasKitTestDirectory = + path.join(environment.webUiTestDir.path, 'canvaskit'); + for (FilePath target in allTargets) { + if (path.isWithin(canvasKitTestDirectory, target.absolute)) { + canvasKitTargets.add(target); + } else { + htmlTargets.add(target); + } + } + + if (htmlTargets.isNotEmpty) { + await _buildTests(targets: htmlTargets, forCanvasKit: false); + } + + if (canvasKitTargets.isNotEmpty) { + await _buildTests(targets: canvasKitTargets, forCanvasKit: true); + } + stopwatch.stop(); + print('The build took ${stopwatch.elapsedMilliseconds ~/ 1000} seconds.'); + } + /// Whether to start the browser in debug mode. /// /// In this mode the browser pauses before running the test to allow @@ -222,12 +263,18 @@ class TestCommand extends Command with ArgUtils { /// ".dart_tool/goldens". bool get doUpdateScreenshotGoldens => boolArg('update-screenshot-goldens'); - Future _runTargetTests(List targets) async { + /// Runs all tests specified in [targets]. + /// + /// Unlike [_runAllTestsForCurrentPlatform], this does not filter targets + /// by platform/browser capabilites, and instead attempts to run all of + /// them. + Future _runSpecificTests(List targets) async { await _runTestBatch(targets, concurrency: 1, expectFailure: false); _checkExitCode(); } - Future _runAllTests() async { + /// Runs as many tests as possible on the current OS/browser combination. + Future _runAllTestsForCurrentPlatform() async { final io.Directory testDir = io.Directory(path.join( environment.webUiRootDir.path, 'test', @@ -373,21 +420,34 @@ class TestCommand extends Command with ArgUtils { timestampFile.writeAsStringSync(timestamp); } - Future _buildTests({List targets}) async { + /// Builds the specific test [targets]. + /// + /// [targets] must not be null. + /// + /// When building for CanvasKit we have to use a separate `build.canvaskit.yaml` + /// config file. Otherwise, `build.html.yaml` is used. Because `build_runner` + /// overwrites the output directories, we redirect the CanvasKit output to a + /// separate directory, then copy the files back to `build/test`. + Future _buildTests({List targets, bool forCanvasKit}) async { + print( + 'Building ${targets.length} targets for ${forCanvasKit ? 'CanvasKit' : 'HTML'}'); + final String canvasKitOutputRelativePath = + path.join('.dart_tool', 'canvaskit_tests'); List arguments = [ 'run', 'build_runner', 'build', 'test', '-o', - 'build', - if (targets != null) - for (FilePath path in targets) ...[ - '--build-filter=${path.relativeToWebUi}.js', - '--build-filter=${path.relativeToWebUi}.browser_test.dart.js', - ], + forCanvasKit ? canvasKitOutputRelativePath : 'build', + '--config', + // CanvasKit uses `build.canvaskit.yaml`, which HTML Uses `build.html.yaml`. + forCanvasKit ? 'canvaskit' : 'html', + for (FilePath path in targets) ...[ + '--build-filter=${path.relativeToWebUi}.js', + '--build-filter=${path.relativeToWebUi}.browser_test.dart.js', + ], ]; - final Stopwatch stopwatch = Stopwatch()..start(); final int exitCode = await runProcess( environment.pubExecutable, @@ -401,16 +461,28 @@ class TestCommand extends Command with ArgUtils { // In a testing on a 32-core 132GB workstation increasing this number to // 32 sped up the build from ~4min to ~1.5min. if (io.Platform.environment.containsKey('BUILD_MAX_WORKERS_PER_TASK')) - 'BUILD_MAX_WORKERS_PER_TASK': io.Platform.environment['BUILD_MAX_WORKERS_PER_TASK'], + 'BUILD_MAX_WORKERS_PER_TASK': + io.Platform.environment['BUILD_MAX_WORKERS_PER_TASK'], }, ); - stopwatch.stop(); - print('The build took ${stopwatch.elapsedMilliseconds ~/ 1000} seconds.'); if (exitCode != 0) { throw ToolException( 'Failed to compile tests. Compiler exited with exit code $exitCode'); } + + if (forCanvasKit) { + final io.Directory canvasKitTemporaryOutputDirectory = io.Directory( + path.join(environment.webUiRootDir.path, canvasKitOutputRelativePath, + 'test', 'canvaskit')); + final io.Directory canvasKitOutputDirectory = io.Directory( + path.join(environment.webUiBuildDir.path, 'test', 'canvaskit')); + if (await canvasKitOutputDirectory.exists()) { + await canvasKitOutputDirectory.delete(recursive: true); + } + await canvasKitTemporaryOutputDirectory + .rename(canvasKitOutputDirectory.path); + } } /// Runs a batch of tests. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart index c66fac4044..045ff331c9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart @@ -11,13 +11,17 @@ part of engine; class SkPath implements ui.Path { js.JsObject _skPath; + /// Cached constructor function for `SkPath`, so we don't have to look it up + /// every time we construct a new path. + static final js.JsFunction _skPathConstructor = canvasKit['SkPath']; + SkPath() { - _skPath = js.JsObject(canvasKit['SkPath']); + _skPath = js.JsObject(_skPathConstructor); fillType = ui.PathFillType.nonZero; } SkPath.from(SkPath other) { - _skPath = js.JsObject(canvasKit['SkPath'], [other._skPath]); + _skPath = js.JsObject(_skPathConstructor, [other._skPath]); fillType = other.fillType; } @@ -323,4 +327,9 @@ class SkPath implements ui.Path { String toSvgString() { return _skPath.callMethod('toSVGString'); } + + /// Return `true` if this path contains no segments. + bool get isEmpty { + return _skPath.callMethod('isEmpty'); + } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart index c7e7a01d13..0913544bdb 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart @@ -7,120 +7,97 @@ part of engine; class SkPathMetrics extends IterableBase implements ui.PathMetrics { - SkPathMetrics(SkPath path, bool forceClosed) - : _iterator = SkPathMetricIterator._(_SkPathMeasure(path, forceClosed)); + SkPathMetrics(this._path, this._forceClosed); - final Iterator _iterator; + final SkPath _path; + final bool _forceClosed; + /// The [SkPath.isEmpty] case is special-cased to avoid booting the WASM machinery just to find out there are no contours. @override - Iterator get iterator => _iterator; + Iterator get iterator => _path.isEmpty ? const SkPathMetricIteratorEmpty._() : SkContourMeasureIter(_path, _forceClosed); } -class SkPathMetricIterator implements Iterator { - SkPathMetricIterator._(this._pathMeasure) : assert(_pathMeasure != null); +class SkContourMeasureIter implements Iterator { + /// Cached constructor function for `SkContourMeasureIter`, so we don't have to look it + /// up every time we're constructing a new instance. + static final js.JsFunction _skContourMeasureIterConstructor = canvasKit['SkContourMeasureIter']; - _SkPathMetric _pathMetric; - _SkPathMeasure _pathMeasure; + SkContourMeasureIter(SkPath path, bool forceClosed) + : _skObject = js.JsObject(_skContourMeasureIterConstructor, [ + path._skPath, + forceClosed, + 1, + ]); + + /// The JavaScript `SkContourMeasureIter` object. + final js.JsObject _skObject; + + /// A monotonically increasing counter used to generate [ui.PathMetric.contourIndex]. + /// + /// CanvasKit does not supply the contour index. We have to add it ourselves. + int _contourIndexCounter = 0; @override - ui.PathMetric get current => _pathMetric; + ui.PathMetric get current => _current; + SkContourMeasure _current; @override bool moveNext() { - if (_pathMeasure._nextContour()) { - _pathMetric = _SkPathMetric._(_pathMeasure); - return true; + final js.JsObject skContourMeasure = _skObject.callMethod('next'); + if (skContourMeasure == null) { + _current = null; + return false; } - _pathMetric = null; - return false; + + _current = SkContourMeasure(_contourIndexCounter, skContourMeasure); + _contourIndexCounter += 1; + return true; } } -class _SkPathMetric implements ui.PathMetric { - _SkPathMetric._(this._measure) - : assert(_measure != null), - length = _measure.length(_measure.currentContourIndex), - isClosed = _measure.isClosed(_measure.currentContourIndex), - contourIndex = _measure.currentContourIndex; +class SkContourMeasure implements ui.PathMetric { + SkContourMeasure(this.contourIndex, this._skObject); - @override - final double length; - - @override - final bool isClosed; + final js.JsObject _skObject; @override final int contourIndex; - final _SkPathMeasure _measure; + @override + ui.Path extractPath(double start, double end, {bool startWithMoveTo = true}) { + final js.JsObject skPath = _skObject + .callMethod('getSegment', [start, end, startWithMoveTo]); + return SkPath._fromSkPath(skPath); + } @override ui.Tangent getTangentForOffset(double distance) { - return _measure.getTangentForOffset(contourIndex, distance); - } - - @override - ui.Path extractPath(double start, double end, {bool startWithMoveTo = true}) { - return _measure.extractPath(contourIndex, start, end, - startWithMoveTo: startWithMoveTo); - } - - @override - String toString() => 'PathMetric{length: $length, isClosed: $isClosed, ' - 'contourIndex: $contourIndex}'; -} - -class _SkPathMeasure { - _SkPathMeasure(SkPath path, bool forceClosed) { - currentContourIndex = -1; - pathMeasure = js.JsObject(canvasKit['SkPathMeasure'], [ - path._skPath, - forceClosed, - 1, - ]); - } - - js.JsObject pathMeasure; - - double length(int contourIndex) { - assert(contourIndex == currentContourIndex, - 'PathMetrics are invalid if it is not the current contour.'); - return pathMeasure.callMethod('getLength'); - } - - ui.Tangent getTangentForOffset(int contourIndex, double distance) { - assert(contourIndex == currentContourIndex, - 'PathMetrics are invalid if it is not the current contour.'); - final js.JsObject posTan = - pathMeasure.callMethod('getPosTan', [distance]); + final js.JsObject posTan = _skObject.callMethod('getPosTan', [distance]); return ui.Tangent( ui.Offset(posTan[0], posTan[1]), ui.Offset(posTan[2], posTan[3]), ); } - ui.Path extractPath(int contourIndex, double start, double end, - {bool startWithMoveTo = true}) { - assert(contourIndex == currentContourIndex, - 'PathMetrics are invalid if it is not the current contour.'); - final js.JsObject skPath = pathMeasure - .callMethod('getSegment', [start, end, startWithMoveTo]); - return SkPath._fromSkPath(skPath); + @override + bool get isClosed { + return _skObject.callMethod('isClosed'); } - bool isClosed(int contourIndex) { - assert(contourIndex == currentContourIndex, - 'PathMetrics are invalid if it is not the current contour.'); - return pathMeasure.callMethod('isClosed'); + @override + double get length { + return _skObject.callMethod('length'); + } +} + +class SkPathMetricIteratorEmpty implements Iterator { + const SkPathMetricIteratorEmpty._(); + + @override + ui.PathMetric get current => null; + + @override + bool moveNext() { + return false; } - - bool _nextContour() { - final bool next = pathMeasure.callMethod('nextContour'); - if (next) { - currentContourIndex++; - } - return next; - } - - int currentContourIndex; } diff --git a/engine/src/flutter/lib/web_ui/pubspec.yaml b/engine/src/flutter/lib/web_ui/pubspec.yaml index a800bccff4..856c6cf2a6 100644 --- a/engine/src/flutter/lib/web_ui/pubspec.yaml +++ b/engine/src/flutter/lib/web_ui/pubspec.yaml @@ -17,6 +17,8 @@ dev_dependencies: build_runner: 1.7.2 build_test: 1.0.0 build_web_compilers: 2.7.1 + # TODO(yjbanov): the Dart SDK complains about nonVirtual for some reason. + petitparser: 3.0.2 yaml: 2.2.0 watcher: 0.9.7+12 web_engine_tester: diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/path_metrics_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/path_metrics_test.dart new file mode 100644 index 0000000000..18bab1ced5 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/path_metrics_test.dart @@ -0,0 +1,67 @@ +// 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. + +// @dart = 2.6 +import 'package:test/test.dart'; + +import 'package:ui/src/engine.dart'; +import 'package:ui/ui.dart' as ui; + +void main() { + setUpAll(() async { + await ui.webOnlyInitializePlatform(); + }); + + test('Using CanvasKit', () { + expect(experimentalUseSkia, true); + }); + + test(SkPathMetrics, () { + final ui.Path path = ui.Path(); + expect(path, isA()); + expect(path.computeMetrics().length, 0); + + path.addRect(ui.Rect.fromLTRB(0, 0, 10, 10)); + final ui.PathMetric metric = path.computeMetrics().single; + expect(metric.contourIndex, 0); + expect(metric.extractPath(0, 0.5).computeMetrics().length, 1); + + final ui.Tangent tangent1 = metric.getTangentForOffset(5); + expect(tangent1.position, ui.Offset(5, 0)); + expect(tangent1.vector, ui.Offset(1, 0)); + + final ui.Tangent tangent2 = metric.getTangentForOffset(15); + expect(tangent2.position, ui.Offset(10, 5)); + expect(tangent2.vector, ui.Offset(0, 1)); + + expect(metric.isClosed, true); + + path.addOval(ui.Rect.fromLTRB(10, 10, 100, 100)); + expect(path.computeMetrics().length, 2); + + // Path metrics can be iterated over multiple times. + final ui.PathMetrics metrics = path.computeMetrics(); + expect(metrics.toList().length, 2); + expect(metrics.toList().length, 2); + expect(metrics.toList().length, 2); + + // Can simultaneously iterate over multiple metrics from the same path. + final ui.PathMetrics metrics1 = path.computeMetrics(); + final ui.PathMetrics metrics2 = path.computeMetrics(); + final Iterator iter1 = metrics1.iterator; + final Iterator iter2 = metrics2.iterator; + expect(iter1.moveNext(), true); + expect(iter2.moveNext(), true); + expect(iter1.current, isNotNull); + expect(iter2.current, isNotNull); + expect(iter1.moveNext(), true); + expect(iter2.moveNext(), true); + expect(iter1.current, isNotNull); + expect(iter2.current, isNotNull); + expect(iter1.moveNext(), false); + expect(iter2.moveNext(), false); + expect(iter1.current, isNull); + expect(iter2.current, isNull); + }); +} diff --git a/engine/src/flutter/lib/web_ui/test/engine/compositor/util_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/util_test.dart similarity index 100% rename from engine/src/flutter/lib/web_ui/test/engine/compositor/util_test.dart rename to engine/src/flutter/lib/web_ui/test/canvaskit/util_test.dart