diff --git a/engine/src/flutter/lib/web_ui/dev/clean.dart b/engine/src/flutter/lib/web_ui/dev/clean.dart index b31567c15c..5e47309733 100644 --- a/engine/src/flutter/lib/web_ui/dev/clean.dart +++ b/engine/src/flutter/lib/web_ui/dev/clean.dart @@ -38,20 +38,11 @@ class CleanCommand extends Command with ArgUtils { @override FutureOr run() async { - final io.Directory assetsDir = io.Directory(path.join( - environment.webUiRootDir.path, 'lib', 'assets' - )); - final Iterable fontFiles = assetsDir - .listSync() - .whereType() - .where((io.File file) => file.path.endsWith('.ttf')); - final List thingsToBeCleaned = [ environment.webUiDartToolDir, environment.webUiBuildDir, io.File(path.join(environment.webUiRootDir.path, '.packages')), io.File(path.join(environment.webUiRootDir.path, 'pubspec.lock')), - ...fontFiles, if (_alsoCleanNinja) environment.outDir, if(_alsoCleanFlutterRepo) diff --git a/engine/src/flutter/lib/web_ui/dev/environment.dart b/engine/src/flutter/lib/web_ui/dev/environment.dart index 0ed67f8a35..1dc2d06e05 100644 --- a/engine/src/flutter/lib/web_ui/dev/environment.dart +++ b/engine/src/flutter/lib/web_ui/dev/environment.dart @@ -151,7 +151,7 @@ class Environment { /// Path to the clone of the flutter/goldens repository. io.Directory get webUiGoldensRepositoryDirectory => io.Directory(pathlib.join( - webUiDartToolDir.path, + webUiBuildDir.path, 'goldens', )); diff --git a/engine/src/flutter/lib/web_ui/dev/steps/compile_tests_step.dart b/engine/src/flutter/lib/web_ui/dev/steps/compile_tests_step.dart index 1a65be97b5..86088aa725 100644 --- a/engine/src/flutter/lib/web_ui/dev/steps/compile_tests_step.dart +++ b/engine/src/flutter/lib/web_ui/dev/steps/compile_tests_step.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:convert' show JsonEncoder; import 'dart:io' as io; import 'package:path/path.dart' as pathlib; @@ -13,12 +14,16 @@ import '../exceptions.dart'; import '../pipeline.dart'; import '../utils.dart'; -/// Compiles web tests and their dependencies. +/// Compiles web tests and their dependencies into web_ui/build/. /// -/// Includes: -/// * compile the test code itself -/// * compile the page that hosts the tests -/// * fetch the golden repo for screenshot comparison +/// Outputs of this step: +/// +/// * canvaskit/ - CanvasKit artifacts +/// * assets/ - test fonts +/// * goldens/ - the goldens fetched from flutter/goldens +/// * host/ - compiled test host page and static artifacts +/// * test/ - compiled test code +/// * test_images/ - test images copied from Skis sources. class CompileTestsStep implements PipelineStep { CompileTestsStep({ this.skipGoldensRepoFetch = false, @@ -41,14 +46,118 @@ class CompileTestsStep implements PipelineStep { @override Future run() async { + await environment.webUiBuildDir.create(); if (!skipGoldensRepoFetch) { await fetchGoldensRepo(); } + await copyCanvasKitFiles(); await buildHostPage(); + await copyTestFonts(); + await copySkiaTestImages(); await compileTests(testFiles ?? findAllTests()); } } +const Map _kTestFonts = { + 'Ahem': 'ahem.ttf', + 'Roboto': 'Roboto-Regular.ttf', + 'Noto Naskh Arabic UI': 'NotoNaskhArabic-Regular.ttf', + 'Noto Color Emoji': 'NotoColorEmoji.ttf', +}; + +Future copyTestFonts() async { + final String fontsPath = pathlib.join( + environment.flutterDirectory.path, + 'third_party', + 'txt', + 'third_party', + 'fonts', + ); + + final List fontManifest = []; + for (final MapEntry fontEntry in _kTestFonts.entries) { + final String family = fontEntry.key; + final String fontFile = fontEntry.value; + + fontManifest.add({ + 'family': family, + 'fonts': [ + { + 'asset': 'fonts/$fontFile', + }, + ], + }); + + final io.File sourceTtf = io.File(pathlib.join(fontsPath, fontFile)); + final io.File destinationTtf = io.File(pathlib.join( + environment.webUiBuildDir.path, + 'assets', + 'fonts', + fontFile, + )); + await destinationTtf.create(recursive: true); + await sourceTtf.copy(destinationTtf.path); + } + + final io.File fontManifestFile = io.File(pathlib.join( + environment.webUiBuildDir.path, + 'assets', + 'FontManifest.json', + )); + await fontManifestFile.create(recursive: true); + await fontManifestFile.writeAsString( + const JsonEncoder.withIndent(' ').convert(fontManifest), + ); +} + +Future copySkiaTestImages() async { + final io.Directory testImagesDir = io.Directory(pathlib.join( + environment.engineSrcDir.path, + 'third_party', + 'skia', + 'resources', + 'images', + )); + + for (final io.File imageFile in testImagesDir.listSync(recursive: true).whereType()) { + final io.File destination = io.File(pathlib.join( + environment.webUiBuildDir.path, + 'test_images', + pathlib.relative(imageFile.path, from: testImagesDir.path), + )); + destination.createSync(recursive: true); + await imageFile.copy(destination.path); + } +} + +Future copyCanvasKitFiles() async { + final io.Directory canvasKitDir = io.Directory(pathlib.join( + environment.engineSrcDir.path, + 'third_party', + 'web_dependencies', + 'canvaskit', + )); + + final Iterable canvasKitFiles = canvasKitDir + .listSync(recursive: true, followLinks: true) + .whereType(); + + final io.Directory targetDir = io.Directory(pathlib.join( + environment.webUiBuildDir.path, + 'canvaskit', + )); + + for (final io.File file in canvasKitFiles) { + final String relativePath = pathlib.relative(file.path, from: canvasKitDir.path); + final io.File targetFile = io.File(pathlib.join( + targetDir.path, + relativePath, + )); + await targetFile.create(recursive: true); + await file.copy(targetFile.path); + } +} + /// Compiles the specified unit tests. Future compileTests(List testFiles) async { final Stopwatch stopwatch = Stopwatch()..start(); @@ -176,9 +285,38 @@ Future buildHostPage() async { environment.webEngineTesterRootDir.path, hostDartPath, )); + final String targetDirectoryPath = pathlib.join( + environment.webUiBuildDir.path, + 'host', + ); + io.Directory(targetDirectoryPath).createSync(recursive: true); + final String targetFilePath = pathlib.join( + targetDirectoryPath, + 'host.dart', + ); + + const List staticFiles = [ + 'favicon.ico', + 'host.css', + 'index.html', + ]; + for (final String staticFilePath in staticFiles) { + final io.File source = io.File(pathlib.join( + environment.webEngineTesterRootDir.path, + 'lib', + 'static', + staticFilePath, + )); + final io.File destination = io.File(pathlib.join( + targetDirectoryPath, + staticFilePath, + )); + await source.copy(destination.path); + } + final io.File timestampFile = io.File(pathlib.join( environment.webEngineTesterRootDir.path, - '$hostDartPath.js.timestamp', + '$targetFilePath.js.timestamp', )); final String timestamp = @@ -203,7 +341,7 @@ Future buildHostPage() async { 'js', hostDartPath, '-o', - '$hostDartPath.js', + '$targetFilePath.js', ], workingDirectory: environment.webEngineTesterRootDir.path, ); diff --git a/engine/src/flutter/lib/web_ui/dev/steps/run_tests_step.dart b/engine/src/flutter/lib/web_ui/dev/steps/run_tests_step.dart index ce47068050..e7382a869e 100644 --- a/engine/src/flutter/lib/web_ui/dev/steps/run_tests_step.dart +++ b/engine/src/flutter/lib/web_ui/dev/steps/run_tests_step.dart @@ -66,7 +66,6 @@ class RunTestsStep implements PipelineStep { @override Future run() async { - _copyTestFontsIntoWebUi(); await _prepareTestResultsDirectory(); await browserEnvironment.prepareEnvironment(); @@ -173,30 +172,6 @@ Future _prepareTestResultsDirectory() async { environment.webUiTestResultsDirectory.createSync(recursive: true); } -const List _kTestFonts = [ - 'ahem.ttf', - 'Roboto-Regular.ttf', - 'NotoNaskhArabic-Regular.ttf', - 'NotoColorEmoji.ttf', -]; - -void _copyTestFontsIntoWebUi() { - final String fontsPath = pathlib.join( - environment.flutterDirectory.path, - 'third_party', - 'txt', - 'third_party', - 'fonts', - ); - - for (final String fontFile in _kTestFonts) { - final io.File sourceTtf = io.File(pathlib.join(fontsPath, fontFile)); - final String destinationTtfPath = - pathlib.join(environment.webUiRootDir.path, 'lib', 'assets', fontFile); - sourceTtf.copySync(destinationTtfPath); - } -} - /// Runs a batch of tests. /// /// Unless [expectFailure] is set to false, sets [io.exitCode] to a non-zero diff --git a/engine/src/flutter/lib/web_ui/dev/test_platform.dart b/engine/src/flutter/lib/web_ui/dev/test_platform.dart index f5092a27c4..1a2a145e2b 100644 --- a/engine/src/flutter/lib/web_ui/dev/test_platform.dart +++ b/engine/src/flutter/lib/web_ui/dev/test_platform.dart @@ -57,8 +57,6 @@ class BrowserPlatform extends PlatformPlugin { browserEnvironment: browserEnvironment, server: server, isDebug: Configuration.current.pauseAfterLoad, - faviconPath: p.fromUri(await Isolate.resolvePackageUri( - Uri.parse('package:test/src/runner/browser/static/favicon.ico'))), doUpdateScreenshotGoldens: doUpdateScreenshotGoldens, packageConfig: await loadPackageConfigUri((await Isolate.packageConfig)!), ); @@ -104,7 +102,6 @@ class BrowserPlatform extends PlatformPlugin { required this.browserEnvironment, required this.server, required this.isDebug, - required String faviconPath, required this.doUpdateScreenshotGoldens, required this.packageConfig, }) : _screenshotManager = browserEnvironment.getScreenshotManager() { @@ -115,9 +112,6 @@ class BrowserPlatform extends PlatformPlugin { // for details on how the channels are established. .add(_webSocketHandler.handler) - // Serves /favicon.ico - .add(createFileHandler(faviconPath)) - // Serves /packages/* requests; fetches files and sources from // pubspec dependencies. // @@ -126,15 +120,8 @@ class BrowserPlatform extends PlatformPlugin { // * Assets that are part of the engine sources, such as Ahem.ttf .add(_packageUrlHandler) - // Serves CanvasKit assets. - .add(_canvasKitHandler) - // Serves files from the web_ui/build/ directory at the root (/) URL path. - // - // Includes: - // * Precompiles .js files for tests - // * Sourcemaps - .add(createStaticHandler(env.environment.webUiBuildDir.path)) + .add(buildDirectoryHandler) // Serves the initial HTML for the test. .add(_testBootstrapHandler) @@ -152,11 +139,17 @@ class BrowserPlatform extends PlatformPlugin { // Serves absolute package URLs (i.e. not /packages/* but /Users/user/*/hosted/pub.dartlang.org/*). // This handler goes last, after all more specific handlers failed to handle the request. .add(_createAbsolutePackageUrlHandler()) - .add(_screeshotHandler); + .add(_screeshotHandler) + .add(_fileNotFoundCatcher); server.mount(cascade.handler); } + Future _fileNotFoundCatcher(shelf.Request request) async { + print('HTTP 404: ${request.url}'); + return shelf.Response.notFound('File not found'); + } + /// Handles URLs pointing to Dart sources using absolute URI paths. /// /// Dart source paths that dart2js puts in source maps for pub packages are @@ -313,44 +306,51 @@ class BrowserPlatform extends PlatformPlugin { write: write); } - /// Serves /canvaskit/* requests. - /// - /// The requested path is rewritten to look for CanvasKit assets under: - /// - /// ENGINE_ROOT/src/third_party/web_dependencies/canvaskit - shelf.Response _canvasKitHandler(shelf.Request request) { - if (!request.url.path.startsWith('canvaskit/')) { - return shelf.Response.notFound('Not found.'); - } + static const Map contentTypes = { + '.js': 'text/javascript', + '.wasm': 'application/wasm', + '.html': 'text/html', + '.htm': 'text/html', + '.css': 'text/css', + '.ico': 'image/icon-x', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.gif': 'image/gif', + '.webp': 'image/webp', + '.bmp': 'image/bmp', + '.svg': 'image/svg+xml', + '.json': 'application/json', + '.ttf': 'font/ttf', + '.woff': 'font/woff', + '.woff2': 'font/woff2', + }; - final String filePath = p.join( - env.environment.engineSrcDir.path, - 'third_party', - 'web_dependencies', + /// A simple file handler that serves files whose URLs and paths are + /// statically known. + /// + /// This is used for trivial use-cases, such as `favicon.ico`, host pages, etc. + shelf.Response buildDirectoryHandler(shelf.Request request) { + final File fileInBuild = File(p.join( + env.environment.webUiBuildDir.path, request.url.path, - ); - final File fileInPackage = File(filePath); - if (!fileInPackage.existsSync()) { + )); + + if (!fileInBuild.existsSync()) { return shelf.Response.notFound('File not found: ${request.url.path}'); } - final String extension = p.extension(filePath); - final String contentType; - switch (extension) { - case '.js': - contentType = 'text/javascript'; - break; - case '.wasm': - contentType = 'application/wasm'; - break; - default: - final String error = 'Failed to determine Content-Type for "${request.url.path}".'; - stderr.writeln(error); - return shelf.Response.internalServerError(body: error); + final String extension = p.extension(fileInBuild.path); + final String? contentType = contentTypes[extension]; + + if (contentType == null) { + final String error = 'Failed to determine Content-Type for "${request.url.path}".'; + stderr.writeln(error); + return shelf.Response.internalServerError(body: error); } return shelf.Response.ok( - fileInPackage.readAsBytesSync(), + fileInBuild.readAsBytesSync(), headers: { HttpHeaders.contentTypeHeader: contentType, }, @@ -373,6 +373,7 @@ class BrowserPlatform extends PlatformPlugin { ${htmlEscape.convert(test)} Test +