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

@@ -2,12 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html';
import 'package:flutter_test/flutter_test.dart';
import 'package:web/web.dart' as web;
/// Whether the current browser is Firefox.
bool get isFirefox => window.navigator.userAgent.toLowerCase().contains('firefox');
bool get isFirefox => web.window.navigator.userAgent.toLowerCase().contains('firefox');
/// Finds elements in the DOM tree rendered by the Flutter Web engine.
///
@@ -15,8 +14,8 @@ bool get isFirefox => window.navigator.userAgent.toLowerCase().contains('firefox
/// `<flt-glass-pane>` element. Otherwise, looks under `<flt-glass-pane>`
/// without penetrating the shadow DOM. In the latter case, if the application
/// creates platform views, this will also find platform view elements.
List<Node> findElements(String selector) {
final Element? flutterView = document.querySelector('flutter-view');
web.NodeList findElements(String selector) {
final web.Element? flutterView = web.document.querySelector('flutter-view');
if (flutterView == null) {
fail(

View File

@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html' as html;
import 'package:flutter/material.dart';
import 'package:web/web.dart' as web;
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
@@ -79,29 +79,25 @@ abstract class DeltaMode {
void dispatchMouseWheelEvent(int mouseX, int mouseY,
int deltaMode, double deltaX, double deltaY) {
final html.EventTarget target = html.document.elementFromPoint(mouseX, mouseY)!;
final web.EventTarget target = web.document.elementFromPoint(mouseX, mouseY)!;
target.dispatchEvent(html.MouseEvent('mouseover',
target.dispatchEvent(web.MouseEvent('mouseover', web.MouseEventInit(
screenX: mouseX,
screenY: mouseY,
clientX: mouseX,
clientY: mouseY,
));
)));
target.dispatchEvent(html.MouseEvent('mousemove',
target.dispatchEvent(web.MouseEvent('mousemove', web.MouseEventInit(
screenX: mouseX,
screenY: mouseY,
clientX: mouseX,
clientY: mouseY,
));
)));
target.dispatchEvent(html.WheelEvent('wheel',
screenX: mouseX,
screenY: mouseY,
clientX: mouseX,
clientY: mouseY,
target.dispatchEvent(web.WheelEvent('wheel', web.WheelEventInit(
deltaMode: deltaMode,
deltaX : deltaX,
deltaY : deltaY,
));
)));
}

View File

@@ -21,6 +21,7 @@ dependencies:
integration_test:
sdk: flutter
flutter_gallery_assets: 1.0.2
web: 0.5.1
async: 2.11.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
boolean_selector: 2.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
@@ -82,7 +83,6 @@ dev_dependencies:
test_core: 0.6.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
typed_data: 1.3.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
watcher: 1.1.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
web: 0.5.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
web_socket_channel: 2.4.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
webkit_inspection_protocol: 1.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
yaml: 3.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"

View File

@@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html' as html;
import 'dart:ui_web' as ui_web;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web/web.dart' as web;
import 'package:web_e2e_tests/platform_messages_main.dart' as app;
void main() {
@@ -27,7 +27,7 @@ void main() {
await tester.tap(find.byKey(const Key('input')));
// Focus in input, otherwise clipboard will fail with
// 'document is not focused' platform exception.
html.document.querySelector('input')?.focus();
(web.document.querySelector('input') as web.HTMLElement?)?.focus();
await Clipboard.setData(const ClipboardData(text: 'sample text'));
}, skip: true); // https://github.com/flutter/flutter/issues/54296
@@ -38,7 +38,7 @@ void main() {
platformViewsRegistry.getNextPlatformViewId();
ui_web.platformViewRegistry.registerViewFactory('MyView', (int viewId) {
viewInstanceCount += 1;
return html.DivElement();
return web.HTMLDivElement();
});
app.main();

View File

@@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html';
import 'dart:js_util' as js_util;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web/web.dart' as web;
import 'package:web_e2e_tests/common.dart';
import 'package:web_e2e_tests/text_editing_main.dart' as app;
@@ -26,9 +26,9 @@ void main() {
await tester.tap(find.byKey(const Key('input')));
// A native input element will be appended to the DOM.
final List<Node> nodeList = findElements('input');
final web.NodeList nodeList = findElements('input');
expect(nodeList.length, equals(1));
final InputElement input = nodeList[0] as InputElement;
final web.HTMLInputElement input = nodeList.item(0)! as web.HTMLInputElement;
// The element's value will be the same as the textFormField's value.
expect(input.value, 'Text1');
@@ -50,9 +50,9 @@ void main() {
await tester.tap(find.byKey(const Key('empty-input')));
// A native input element will be appended to the DOM.
final List<Node> nodeList = findElements('input');
final web.NodeList nodeList = findElements('input');
expect(nodeList.length, equals(1));
final InputElement input = nodeList[0] as InputElement;
final web.HTMLInputElement input = nodeList.item(0)! as web.HTMLInputElement;
// The element's value will be empty.
expect(input.value, '');
@@ -81,7 +81,7 @@ void main() {
await tester.tap(find.byKey(const Key('input2')));
// Press Tab. This should trigger `onFieldSubmitted` of TextField.
final InputElement input = findElements('input')[0] as InputElement;
final web.HTMLInputElement input = findElements('input').item(0)! as web.HTMLInputElement;
dispatchKeyboardEvent(input, 'keydown', <String, dynamic>{
'keyCode': 13, // Enter.
'cancelable': true,
@@ -111,9 +111,9 @@ void main() {
await tester.tap(find.byKey(const Key('input')));
// A native input element will be appended to the DOM.
final List<Node> nodeList = findElements('input');
final web.NodeList nodeList = findElements('input');
expect(nodeList.length, equals(1));
final InputElement input = nodeList[0] as InputElement;
final web.HTMLInputElement input = nodeList.item(0)! as web.HTMLInputElement;
// Press Tab. The focus should move to the next TextFormField.
dispatchKeyboardEvent(input, 'keydown', <String, dynamic>{
@@ -136,7 +136,7 @@ void main() {
// A native input element for the next TextField should be attached to the
// DOM.
final InputElement input2 = findElements('input')[0] as InputElement;
final web.HTMLInputElement input2 = findElements('input').item(0)! as web.HTMLInputElement;
expect(input2.value, 'Text2');
}, semanticsEnabled: false);
@@ -150,9 +150,9 @@ void main() {
await tester.tap(find.byKey(const Key('input')));
// A native input element will be appended to the DOM.
final List<Node> nodeList = findElements('input');
final web.NodeList nodeList = findElements('input');
expect(nodeList.length, equals(1));
final InputElement input = nodeList[0] as InputElement;
final web.HTMLInputElement input = nodeList.item(0)! as web.HTMLInputElement;
// Press and release CapsLock.
dispatchKeyboardEvent(input, 'keydown', <String, dynamic>{
@@ -191,7 +191,7 @@ void main() {
// A native input element for the next TextField should be attached to the
// DOM.
final InputElement input2 = findElements('input')[0] as InputElement;
final web.HTMLInputElement input2 = findElements('input').item(0)! as web.HTMLInputElement;
expect(input2.value, 'Text2');
}, semanticsEnabled: false);
@@ -215,16 +215,16 @@ void main() {
await gesture.up();
// A native input element will be appended to the DOM.
final List<Node> nodeList = findElements('textarea');
final web.NodeList nodeList = findElements('textarea');
expect(nodeList.length, equals(1));
final TextAreaElement input = nodeList[0] as TextAreaElement;
final web.HTMLTextAreaElement input = nodeList.item(0)! as web.HTMLTextAreaElement;
// The element's value should contain the selectable text.
expect(input.value, text);
expect(input.hasAttribute('readonly'), isTrue);
// Make sure the entire text is selected.
TextRange? range =
TextRange(start: input.selectionStart!, end: input.selectionEnd!);
TextRange(start: input.selectionStart, end: input.selectionEnd);
expect(range.textInside(text), text);
// Double tap to select the first word.
@@ -236,7 +236,7 @@ void main() {
await gesture.up();
await gesture.down(firstWordOffset);
await gesture.up();
range = TextRange(start: input.selectionStart!, end: input.selectionEnd!);
range = TextRange(start: input.selectionStart, end: input.selectionEnd);
expect(range.textInside(text), 'Lorem');
// Double tap to select the last word.
@@ -248,21 +248,21 @@ void main() {
await gesture.up();
await gesture.down(lastWordOffset);
await gesture.up();
range = TextRange(start: input.selectionStart!, end: input.selectionEnd!);
range = TextRange(start: input.selectionStart, end: input.selectionEnd);
expect(range.textInside(text), 'amet');
}, semanticsEnabled: false);
}
KeyboardEvent dispatchKeyboardEvent(
EventTarget target, String type, Map<String, dynamic> args) {
final Object jsKeyboardEvent = js_util.getProperty(window, 'KeyboardEvent') as Object;
web.KeyboardEvent dispatchKeyboardEvent(
web.EventTarget target, String type, Map<String, dynamic> args) {
final Object jsKeyboardEvent = js_util.getProperty(web.window, 'KeyboardEvent') as Object;
final List<dynamic> eventArgs = <dynamic>[
type,
args,
];
final KeyboardEvent event = js_util.callConstructor(
final web.KeyboardEvent event = js_util.callConstructor(
jsKeyboardEvent, js_util.jsify(eventArgs) as List<dynamic>)
as KeyboardEvent;
as web.KeyboardEvent;
target.dispatchEvent(event);
return event;

View File

@@ -3,13 +3,16 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:html' as html;
import 'dart:js_interop';
import 'dart:ui' as ui;
import 'dart:ui_web';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web/helpers.dart';
import 'package:web/web.dart' as web;
import 'package:web_e2e_tests/url_strategy_main.dart' as app;
void main() {
@@ -56,7 +59,7 @@ class TestUrlStrategy extends UrlStrategy {
String getPath() => currentEntry.url;
@override
dynamic getState() => currentEntry.state;
Object? getState() => currentEntry.state;
int _currentEntryIndex;
final List<TestHistoryEntry> history;
@@ -111,10 +114,10 @@ class TestUrlStrategy extends UrlStrategy {
});
}
final List<html.EventListener> listeners = <html.EventListener>[];
final List<PopStateListener> listeners = <PopStateListener>[];
@override
ui.VoidCallback addPopStateListener(html.EventListener fn) {
ui.VoidCallback addPopStateListener(PopStateListener fn) {
listeners.add(fn);
return () {
// Schedule a micro task here to avoid removing the listener during
@@ -134,9 +137,9 @@ class TestUrlStrategy extends UrlStrategy {
/// like a real browser.
void _firePopStateEvent() {
assert(withinAppHistory);
final html.PopStateEvent event = html.PopStateEvent(
final web.PopStateEvent event = web.PopStateEvent(
'popstate',
<String, dynamic>{'state': currentEntry.state},
PopStateEventInit(state: currentEntry.state?.toJSBox),
);
for (int i = 0; i < listeners.length; i++) {
listeners[i](event);
@@ -160,7 +163,7 @@ class TestUrlStrategy extends UrlStrategy {
class TestHistoryEntry {
const TestHistoryEntry(this.state, this.title, this.url);
final dynamic state;
final Object? state;
final String? title;
final String url;