flutter test --wasm support (#145347)

* Adds support for `flutter test --wasm`.
  * The test compilation flow is a bit different now, so that it supports compilers other than DDC. Specifically, when we run a set of unit tests, we generate a "switchboard" main function that imports each unit test and runs the main function for a specific one based off of a value set by the JS bootstrapping code. This way, there is one compile step and the same compile output is invoked for each unit test file.
* Also, removes all references to `dart:html` from flutter/flutter.
* Adds CI steps for running the framework unit tests with dart2wasm+skwasm
  * These steps are marked as `bringup: true`, so we don't know what kind of failures they will result in. Any failures they have will not block the tree at all yet while we're still in `bringup: true`. Once this PR is merged, I plan on looking at any failures and either fixing them or disabling them so we can get these CI steps running on presubmit.

This fixes https://github.com/flutter/flutter/issues/126692
This commit is contained in:
Jackson Gardner
2024-03-21 13:08:07 -07:00
committed by GitHub
parent 98d10b6211
commit 31209d04ff
62 changed files with 953 additions and 365 deletions

View File

@@ -55,8 +55,9 @@ library flutter_test;
export 'dart:async' show Future;
export 'src/_goldens_io.dart' if (dart.library.html) 'src/_goldens_web.dart';
export 'src/_matchers_io.dart' if (dart.library.html) 'src/_matchers_web.dart';
export 'src/_goldens_io.dart' if (dart.library.js_interop) 'src/_goldens_web.dart';
export 'src/_matchers_io.dart' if (dart.library.js_interop) 'src/_matchers_web.dart';
export 'src/_test_selector_io.dart' if (dart.library.js_interop) 'src/_test_selector_web.dart';
export 'src/accessibility.dart';
export 'src/animation_sheet.dart';
export 'src/binding.dart';

View File

@@ -292,6 +292,11 @@ ByteData _invert(ByteData imageBytes) {
/// An unsupported [WebGoldenComparator] that exists for API compatibility.
class DefaultWebGoldenComparator extends WebGoldenComparator {
/// This is provided to prevent warnings from the analyzer.
DefaultWebGoldenComparator(Uri _) {
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');
}
@override
Future<bool> compare(double width, double height, Uri golden) {
throw UnsupportedError('DefaultWebGoldenComparator is only supported on the web.');

View File

@@ -3,12 +3,13 @@
// found in the LICENSE file.
import 'dart:convert';
import 'dart:html' as html;
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:matcher/expect.dart' show fail;
import 'goldens.dart';
import 'web.dart' as web;
/// An unsupported [GoldenFileComparator] that exists for API compatibility.
class LocalFileComparator extends GoldenFileComparator {
@@ -58,21 +59,23 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
@override
Future<bool> compare(double width, double height, Uri golden) async {
final String key = golden.toString();
final html.HttpRequest request = await html.HttpRequest.request(
'flutter_goldens',
method: 'POST',
sendData: json.encode(<String, Object>{
'testUri': testUri.toString(),
'key': key,
'width': width.round(),
'height': height.round(),
}),
);
final String response = request.response as String;
if (response == 'true') {
final web.Response response = await web.window.fetch(
'flutter_goldens'.toJS,
web.RequestInit(
method: 'POST',
body: json.encode(<String, Object>{
'testUri': testUri.toString(),
'key': key,
'width': width.round(),
'height': height.round(),
}).toJS,
)
).toDart;
final String responseText = (await response.text().toDart).toDart;
if (responseText == 'true') {
return true;
}
fail(response);
fail(responseText);
}
@override
@@ -85,20 +88,22 @@ class DefaultWebGoldenComparator extends WebGoldenComparator {
Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
final String key = golden.toString();
final String bytesEncoded = base64.encode(bytes);
final html.HttpRequest request = await html.HttpRequest.request(
'flutter_goldens',
method: 'POST',
sendData: json.encode(<String, Object>{
'testUri': testUri.toString(),
'key': key,
'bytes': bytesEncoded,
}),
);
final String response = request.response as String;
if (response == 'true') {
final web.Response response = await web.window.fetch(
'flutter_goldens'.toJS,
web.RequestInit(
method: 'POST',
body: json.encode(<String, Object>{
'testUri': testUri.toString(),
'key': key,
'bytes': bytesEncoded,
}).toJS,
)
).toDart;
final String responseText = (await response.text().toDart).toDart;
if (responseText == 'true') {
return true;
}
fail(response);
fail(responseText);
}
@override

View File

@@ -0,0 +1,6 @@
// Copyright 2014 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.
// This is an empty file that is imported instead of web_test_selector.dart on
// non-web platforms.

View File

@@ -0,0 +1,85 @@
// Copyright 2014 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.
import 'dart:async';
import 'dart:js_interop';
import 'dart:ui' as ui;
import 'dart:ui_web' as ui_web;
import 'package:stream_channel/stream_channel.dart';
import 'package:test_api/backend.dart';
import '_goldens_web.dart';
import 'goldens.dart';
import 'web.dart' as web;
// This file contains APIs that are used by the generated test harness for
// running flutter unit tests.
/// A `main` entry point for a test.
typedef EntryPoint = FutureOr<void> Function();
/// An entry point runner provided by a test config file
typedef EntryPointRunner = Future<void> Function(EntryPoint);
/// Metadata about a web test to run
typedef WebTest = ({
EntryPoint entryPoint,
EntryPointRunner? entryPointRunner,
Uri goldensUri,
});
/// Gets the test selector set by the test bootstrapping logic
String get testSelector {
final JSString? jsTestSelector = web.window.testSelector;
if (jsTestSelector == null) {
throw Exception('Test selector not set');
}
return jsTestSelector.toDart;
}
/// Runs a specific web test
Future<void> runWebTest(WebTest test) async {
ui_web.debugEmulateFlutterTesterEnvironment = true;
final Completer<void> completer = Completer<void>();
await ui_web.bootstrapEngine(runApp: () => completer.complete());
await completer.future;
webGoldenComparator = DefaultWebGoldenComparator(test.goldensUri);
/// This hard-codes the device pixel ratio to 3.0 and a 2400 x 1800 window
/// size for the purposes of testing.
ui_web.debugOverrideDevicePixelRatio(3.0);
ui.window.debugPhysicalSizeOverride = const ui.Size(2400, 1800);
final EntryPointRunner? entryPointRunner = test.entryPointRunner;
final EntryPoint entryPoint = test.entryPoint;
_internalBootstrapBrowserTest(() {
return entryPointRunner != null ? () => entryPointRunner(entryPoint) : entryPoint;
});
}
void _internalBootstrapBrowserTest(EntryPoint Function() getMain) {
final StreamChannel<Object?> channel = _serializeSuite(getMain, hidePrints: false);
_postMessageChannel().pipe(channel);
}
StreamChannel<Object?> _serializeSuite(EntryPoint Function() getMain, {bool hidePrints = true}) => RemoteListener.start(getMain, hidePrints: hidePrints);
StreamChannel<Object?> _postMessageChannel() {
final StreamChannelController<Object?> controller = StreamChannelController<Object?>(sync: true);
final web.MessageChannel channel = web.MessageChannel();
web.window.parent!.postMessage('port'.toJS, web.window.location.origin, <JSObject>[channel.port2].toJS);
final JSFunction eventCallback = (web.Event event) {
controller.local.sink.add(event.data.dartify());
}.toJS;
channel.port1.addEventListener('message'.toJS, eventCallback);
channel.port1.start();
controller.local.stream.listen(
(Object? message) => channel.port1.postMessage(message.jsify()),
onDone: () => channel.port1.removeEventListener('message'.toJS, eventCallback),
);
return controller.foreign;
}

View File

@@ -18,7 +18,7 @@ import 'package:stack_trace/stack_trace.dart' as stack_trace;
import 'package:test_api/scaffolding.dart' as test_package show Timeout;
import 'package:vector_math/vector_math_64.dart';
import '_binding_io.dart' if (dart.library.html) '_binding_web.dart' as binding;
import '_binding_io.dart' if (dart.library.js_interop) '_binding_web.dart' as binding;
import 'goldens.dart';
import 'platform.dart';
import 'restoration.dart';

View File

@@ -8,7 +8,7 @@ import 'dart:ui';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import '_goldens_io.dart' if (dart.library.html) '_goldens_web.dart' as goldens;
import '_goldens_io.dart' if (dart.library.js_interop) '_goldens_web.dart' as goldens;
/// Compares image pixels against a golden image file.
///

View File

@@ -14,7 +14,7 @@ import 'package:matcher/expect.dart';
import 'package:matcher/src/expect/async_matcher.dart'; // ignore: implementation_imports
import 'package:vector_math/vector_math_64.dart' show Matrix3;
import '_matchers_io.dart' if (dart.library.html) '_matchers_web.dart' show MatchesGoldenFile, captureImage;
import '_matchers_io.dart' if (dart.library.js_interop) '_matchers_web.dart' show MatchesGoldenFile, captureImage;
import 'accessibility.dart';
import 'binding.dart';
import 'controller.dart';

View File

@@ -0,0 +1,74 @@
// Copyright 2014 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.
// This code is copied from `package:web` which still needs its own
// documentation for public members. Since this is a shim that users should not
// use, we ignore this lint for this file.
// ignore_for_file: public_member_api_docs
/// A stripped down version of `package:web` to avoid pinning that repo in
/// Flutter as a dependency.
///
/// These are manually copied over from `package:web` as needed, and should stay
/// in sync with the latest package version as much as possible.
///
/// If missing members are needed, copy them over into the corresponding
/// extension or interface. If missing interfaces/types are needed, copy them
/// over while excluding unnecessary inheritance to make the copy minimal. These
/// types are erased at runtime, so excluding supertypes is safe. If a member is
/// needed that belongs to a supertype, then add the necessary `implements`
/// clause to the subtype when you add that supertype. Keep extensions next to
/// the interface they extend.
library;
import 'dart:js_interop';
@JS()
external Window get window;
extension type Window._(JSObject _) implements JSObject {
external JSPromise<Response> fetch(
JSAny input, [
RequestInit init,
]);
external Location get location;
external Window? get parent;
external void postMessage(JSAny message, JSString targetOrigin, JSArray<JSAny> transfers);
external JSString? get testSelector;
}
extension type Response._(JSObject _) implements JSObject {
external JSPromise<JSString> text();
}
extension type RequestInit._(JSObject _) implements JSObject {
external factory RequestInit({
String method,
JSAny? body,
});
}
extension type Location._(JSObject _) implements JSObject {
external JSString get origin;
}
extension type MessageChannel._(JSObject _) implements JSObject {
external factory MessageChannel();
external MessagePort port1;
external MessagePort port2;
}
extension type MessagePort._(JSObject _) implements JSObject {
external void addEventListener(JSString eventName, JSFunction callback);
external void removeEventListener(JSString eventName, JSFunction callback);
external void postMessage(JSAny? message);
external void start();
}
extension type Event._(JSObject _) implements JSObject {
external JSObject? get data;
}

View File

@@ -1,7 +1,7 @@
name: flutter_test
environment:
sdk: '>=3.2.0-0 <4.0.0'
sdk: '>=3.3.0-0 <4.0.0'
dependencies:
# To update these, use "flutter update-packages --force-upgrade".