From 83b04f9440b018d9ea69f775897bc66c8c43907f Mon Sep 17 00:00:00 2001 From: p-mazhnik Date: Fri, 1 Mar 2024 01:32:57 +0300 Subject: [PATCH 1/2] addView/removeView test --- .../test/engine/app_bootstrap_test.dart | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/web_ui/test/engine/app_bootstrap_test.dart b/lib/web_ui/test/engine/app_bootstrap_test.dart index ddd7218d04546..5a1fcea3e5054 100644 --- a/lib/web_ui/test/engine/app_bootstrap_test.dart +++ b/lib/web_ui/test/engine/app_bootstrap_test.dart @@ -28,6 +28,8 @@ void testMain() { }); Future mockInit ([JsFlutterConfiguration? configuration]) async { + debugOverrideJsConfiguration(configuration); + addTearDown(() => debugOverrideJsConfiguration(null)); initCalled = callOrder++; await Future.delayed(const Duration(milliseconds: 1)); } @@ -128,5 +130,41 @@ void testMain() { expect(getJsProperty(maybeApp, 'addView'), isA()); expect(getJsProperty(maybeApp, 'removeView'), isA()); }); + test('addView/removeView respectively adds/removes view', () async { + final AppBootstrap bootstrap = AppBootstrap( + initializeEngine: mockInit, + runApp: mockRunApp, + ); + + final FlutterEngineInitializer engineInitializer = bootstrap.prepareEngineInitializer(); + + final Object appInitializer = await promiseToFuture(callMethod( + engineInitializer, + 'initializeEngine', + [jsify({ + 'multiViewEnabled': true, + })] + )); + final Object maybeApp = await promiseToFuture(callMethod( + appInitializer, + 'runApp', + [] + )); + final Object viewId = await promiseToFuture(callMethod( + maybeApp, + 'addView', + [jsify({ + 'hostElement': createDomElement('div'), + })] + )); + expect(bootstrap.viewManager[viewId as int], isNotNull); + + await promiseToFuture(callMethod( + maybeApp, + 'removeView', + [viewId] + )); + expect(bootstrap.viewManager[viewId as int], isNull); + }); }); } From b405524ca7d8b238c6a5ef5b5f23eb6b9ffa33d9 Mon Sep 17 00:00:00 2001 From: p-mazhnik Date: Fri, 1 Mar 2024 01:59:12 +0300 Subject: [PATCH 2/2] [web] make addView/removeView functions sync --- lib/web_ui/lib/src/engine/app_bootstrap.dart | 4 +- .../lib/src/engine/js_interop/js_app.dart | 10 ++-- .../test/engine/app_bootstrap_test.dart | 46 +++++++++---------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/web_ui/lib/src/engine/app_bootstrap.dart b/lib/web_ui/lib/src/engine/app_bootstrap.dart index ac09fc0c39bfe..686811314775f 100644 --- a/lib/web_ui/lib/src/engine/app_bootstrap.dart +++ b/lib/web_ui/lib/src/engine/app_bootstrap.dart @@ -68,11 +68,11 @@ class AppBootstrap { /// Represents the App that was just started, and its JS API. FlutterApp _prepareFlutterApp() { return FlutterApp( - addView: (JsFlutterViewOptions options) async { + addView: (JsFlutterViewOptions options) { assert(configuration.multiViewEnabled, 'Cannot addView when multiView is not enabled'); return viewManager.createAndRegisterView(options).viewId; }, - removeView: (int viewId) async { + removeView: (int viewId) { assert(configuration.multiViewEnabled, 'Cannot removeView when multiView is not enabled'); return viewManager.disposeAndUnregisterView(viewId); } diff --git a/lib/web_ui/lib/src/engine/js_interop/js_app.dart b/lib/web_ui/lib/src/engine/js_interop/js_app.dart index 49feafaa27c58..959abb059624d 100644 --- a/lib/web_ui/lib/src/engine/js_interop/js_app.dart +++ b/lib/web_ui/lib/src/engine/js_interop/js_app.dart @@ -67,10 +67,8 @@ abstract class FlutterApp { required RemoveFlutterViewFn removeView, }) => FlutterApp._( - addView: ((JsFlutterViewOptions options) => - futureToPromise(addView(options) as Future)).toJS, - removeView: ((int id) => - futureToPromise(removeView(id) as Future)).toJS, + addView: addView.toJS, + removeView: ((JSNumber id) => removeView(id.toDartInt)).toJS, ); external factory FlutterApp._({ required JSFunction addView, @@ -81,9 +79,9 @@ abstract class FlutterApp { /// Typedef for the function that adds a new view to the app. /// /// Returns the ID of the newly created view. -typedef AddFlutterViewFn = Future Function(JsFlutterViewOptions); +typedef AddFlutterViewFn = int Function(JsFlutterViewOptions); /// Typedef for the function that removes a view from the app. /// /// Returns the configuration used to create the view. -typedef RemoveFlutterViewFn = Future Function(int); +typedef RemoveFlutterViewFn = JsFlutterViewOptions? Function(int); diff --git a/lib/web_ui/test/engine/app_bootstrap_test.dart b/lib/web_ui/test/engine/app_bootstrap_test.dart index 5a1fcea3e5054..683073ea02c18 100644 --- a/lib/web_ui/test/engine/app_bootstrap_test.dart +++ b/lib/web_ui/test/engine/app_bootstrap_test.dart @@ -139,32 +139,32 @@ void testMain() { final FlutterEngineInitializer engineInitializer = bootstrap.prepareEngineInitializer(); final Object appInitializer = await promiseToFuture(callMethod( - engineInitializer, - 'initializeEngine', - [jsify({ - 'multiViewEnabled': true, - })] + engineInitializer, + 'initializeEngine', + [jsify({ + 'multiViewEnabled': true, + })] )); final Object maybeApp = await promiseToFuture(callMethod( - appInitializer, - 'runApp', - [] - )); - final Object viewId = await promiseToFuture(callMethod( - maybeApp, - 'addView', - [jsify({ - 'hostElement': createDomElement('div'), - })] - )); - expect(bootstrap.viewManager[viewId as int], isNotNull); - - await promiseToFuture(callMethod( - maybeApp, - 'removeView', - [viewId] + appInitializer, + 'runApp', + [] )); - expect(bootstrap.viewManager[viewId as int], isNull); + final int viewId = callMethod( + maybeApp, + 'addView', + [jsify({ + 'hostElement': createDomElement('div'), + })] + ).toInt(); + expect(bootstrap.viewManager[viewId], isNotNull); + + callMethod( + maybeApp, + 'removeView', + [viewId] + ); + expect(bootstrap.viewManager[viewId], isNull); }); }); }