From eddb6120feb7c09faf3d4141b4aebd9ca63ad8f5 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 18 Jul 2024 11:48:27 -0400 Subject: [PATCH 1/3] Adds an option to run drive-examples with the web-wasm platform --- script/tool/lib/src/common/core.dart | 4 ++++ .../tool/lib/src/drive_examples_command.dart | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/script/tool/lib/src/common/core.dart b/script/tool/lib/src/common/core.dart index c75432f98c2..fa43ffe1bbc 100644 --- a/script/tool/lib/src/common/core.dart +++ b/script/tool/lib/src/common/core.dart @@ -27,6 +27,9 @@ const String platformMacOS = 'macos'; /// Key for Web platform. const String platformWeb = 'web'; +/// Key for Web WASM platform. +const String platformWebWasm = 'web-wasm'; + /// Key for windows platform. const String platformWindows = 'windows'; @@ -47,6 +50,7 @@ const Map _platformByName = { platformLinux: FlutterPlatform.linux, platformMacOS: FlutterPlatform.macos, platformWeb: FlutterPlatform.web, + platformWebWasm: FlutterPlatform.web, platformWindows: FlutterPlatform.windows, }; diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 984bff7c8bf..8a3ee07c28f 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -38,6 +38,8 @@ class DriveExamplesCommand extends PackageLoopingCommand { help: 'Runs the macOS implementation of the examples'); argParser.addFlag(platformWeb, help: 'Runs the web implementation of the examples'); + argParser.addFlag(platformWebWasm, + help: 'Runs the web implementation of the examples compiled to WASM'); argParser.addFlag(platformWindows, help: 'Runs the Windows implementation of the examples'); argParser.addOption( @@ -72,6 +74,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { platformLinux, platformMacOS, platformWeb, + platformWebWasm, platformWindows, ]; final int platformCount = platformSwitches @@ -107,22 +110,26 @@ class DriveExamplesCommand extends PackageLoopingCommand { iOSDevice = devices.first; } + final bool isPlatformWebWasm = getBoolArg(platformWebWasm); + _targetDeviceFlags = >{ if (getBoolArg(platformAndroid)) platformAndroid: ['-d', androidDevice!], if (getBoolArg(platformIOS)) platformIOS: ['-d', iOSDevice!], if (getBoolArg(platformLinux)) platformLinux: ['-d', 'linux'], if (getBoolArg(platformMacOS)) platformMacOS: ['-d', 'macos'], - if (getBoolArg(platformWeb)) + if (getBoolArg(platformWeb) || isPlatformWebWasm) platformWeb: [ '-d', 'web-server', '--web-port=7357', '--browser-name=chrome', + if (isPlatformWebWasm) + '--wasm' // TODO(dit): Clean this up, https://github.com/flutter/flutter/issues/151869 - if (platform.environment['CHANNEL']?.toLowerCase() == 'master') - '--web-renderer=canvaskit', - if (platform.environment['CHANNEL']?.toLowerCase() != 'master') + else if (platform.environment['CHANNEL']?.toLowerCase() == 'master') + '--web-renderer=canvaskit' + else '--web-renderer=html', if (platform.environment.containsKey('CHROME_EXECUTABLE')) '--chrome-binary=${platform.environment['CHROME_EXECUTABLE']}', @@ -194,7 +201,8 @@ class DriveExamplesCommand extends PackageLoopingCommand { // `flutter test` doesn't yet support web integration tests, so fall back // to `flutter drive`. - final bool useFlutterDrive = getBoolArg(platformWeb); + final bool useFlutterDrive = + getBoolArg(platformWeb) || getBoolArg(platformWebWasm); final List drivers; if (useFlutterDrive) { From 5a19522d8dc09cef74a326f6b176b1d8dd4c1e72 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 18 Jul 2024 11:51:37 -0400 Subject: [PATCH 2/3] Add a test --- .../test/drive_examples_command_test.dart | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/script/tool/test/drive_examples_command_test.dart b/script/tool/test/drive_examples_command_test.dart index 4484b7099ae..d68f68279ea 100644 --- a/script/tool/test/drive_examples_command_test.dart +++ b/script/tool/test/drive_examples_command_test.dart @@ -691,6 +691,56 @@ void main() { ])); }); + test('drives a web plugin compiled to WASM', () async { + final RepositoryPackage plugin = createFakePlugin( + 'plugin', + packagesDir, + extraFiles: [ + 'example/integration_test/plugin_test.dart', + 'example/test_driver/integration_test.dart', + 'example/web/index.html', + ], + platformSupport: { + platformWeb: const PlatformDetails(PlatformSupport.inline), + }, + ); + + final Directory pluginExampleDirectory = getExampleDir(plugin); + + final List output = await runCapturingPrint(runner, [ + 'drive-examples', + '--web-wasm', + ]); + + expect( + output, + containsAllInOrder([ + contains('Running for plugin'), + contains('No issues found!'), + ]), + ); + + expect( + processRunner.recordedCalls, + orderedEquals([ + ProcessCall( + getFlutterCommand(mockPlatform), + const [ + 'drive', + '-d', + 'web-server', + '--web-port=7357', + '--browser-name=chrome', + '--wasm', + '--driver', + 'test_driver/integration_test.dart', + '--target', + 'integration_test/plugin_test.dart', + ], + pluginExampleDirectory.path), + ])); + }); + // TODO(dit): Clean this up, https://github.com/flutter/flutter/issues/151869 test('drives a web plugin (html renderer in stable)', () async { // Override the platform to simulate CHANNEL: stable From 1127bb891d873653edc606254b67e75e2cd6a5a6 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 18 Jul 2024 16:46:14 -0400 Subject: [PATCH 3/3] Switch to using `--wasm` flag --- script/tool/lib/src/common/core.dart | 7 +++--- .../tool/lib/src/drive_examples_command.dart | 23 +++++++++++-------- .../test/drive_examples_command_test.dart | 21 ++++++++++++++++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/script/tool/lib/src/common/core.dart b/script/tool/lib/src/common/core.dart index fa43ffe1bbc..520685f31fc 100644 --- a/script/tool/lib/src/common/core.dart +++ b/script/tool/lib/src/common/core.dart @@ -27,9 +27,6 @@ const String platformMacOS = 'macos'; /// Key for Web platform. const String platformWeb = 'web'; -/// Key for Web WASM platform. -const String platformWebWasm = 'web-wasm'; - /// Key for windows platform. const String platformWindows = 'windows'; @@ -40,6 +37,9 @@ const String kEnableExperiment = 'enable-experiment'; /// land (e.g., dependency overrides in federated plugin combination PRs). const String kDoNotLandWarning = 'DO NOT MERGE'; +/// Key for enabling web WASM compilation +const String kWebWasmFlag = 'wasm'; + /// Target platforms supported by Flutter. // ignore: public_member_api_docs enum FlutterPlatform { android, ios, linux, macos, web, windows } @@ -50,7 +50,6 @@ const Map _platformByName = { platformLinux: FlutterPlatform.linux, platformMacOS: FlutterPlatform.macos, platformWeb: FlutterPlatform.web, - platformWebWasm: FlutterPlatform.web, platformWindows: FlutterPlatform.windows, }; diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 8a3ee07c28f..a5ddd3bd4a9 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -13,7 +13,7 @@ import 'common/package_looping_command.dart'; import 'common/plugin_utils.dart'; import 'common/repository_package.dart'; -const int _exitNoPlatformFlags = 2; +const int _exitInvalidArgs = 2; const int _exitNoAvailableDevice = 3; // From https://flutter.dev/to/integration-test-on-web @@ -38,10 +38,10 @@ class DriveExamplesCommand extends PackageLoopingCommand { help: 'Runs the macOS implementation of the examples'); argParser.addFlag(platformWeb, help: 'Runs the web implementation of the examples'); - argParser.addFlag(platformWebWasm, - help: 'Runs the web implementation of the examples compiled to WASM'); argParser.addFlag(platformWindows, help: 'Runs the Windows implementation of the examples'); + argParser.addFlag(kWebWasmFlag, + help: 'Compile to WebAssembly rather than JavaScript'); argParser.addOption( kEnableExperiment, defaultsTo: '', @@ -74,7 +74,6 @@ class DriveExamplesCommand extends PackageLoopingCommand { platformLinux, platformMacOS, platformWeb, - platformWebWasm, platformWindows, ]; final int platformCount = platformSwitches @@ -87,7 +86,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { printError( 'Exactly one of ${platformSwitches.map((String platform) => '--$platform').join(', ')} ' 'must be specified.'); - throw ToolExit(_exitNoPlatformFlags); + throw ToolExit(_exitInvalidArgs); } String? androidDevice; @@ -110,7 +109,12 @@ class DriveExamplesCommand extends PackageLoopingCommand { iOSDevice = devices.first; } - final bool isPlatformWebWasm = getBoolArg(platformWebWasm); + final bool useWasm = getBoolArg(kWebWasmFlag); + final bool hasPlatformWeb = getBoolArg(platformWeb); + if (useWasm && !hasPlatformWeb) { + printError('--wasm is only supported on the web platform'); + throw ToolExit(_exitInvalidArgs); + } _targetDeviceFlags = >{ if (getBoolArg(platformAndroid)) @@ -118,13 +122,13 @@ class DriveExamplesCommand extends PackageLoopingCommand { if (getBoolArg(platformIOS)) platformIOS: ['-d', iOSDevice!], if (getBoolArg(platformLinux)) platformLinux: ['-d', 'linux'], if (getBoolArg(platformMacOS)) platformMacOS: ['-d', 'macos'], - if (getBoolArg(platformWeb) || isPlatformWebWasm) + if (hasPlatformWeb) platformWeb: [ '-d', 'web-server', '--web-port=7357', '--browser-name=chrome', - if (isPlatformWebWasm) + if (useWasm) '--wasm' // TODO(dit): Clean this up, https://github.com/flutter/flutter/issues/151869 else if (platform.environment['CHANNEL']?.toLowerCase() == 'master') @@ -201,8 +205,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { // `flutter test` doesn't yet support web integration tests, so fall back // to `flutter drive`. - final bool useFlutterDrive = - getBoolArg(platformWeb) || getBoolArg(platformWebWasm); + final bool useFlutterDrive = getBoolArg(platformWeb); final List drivers; if (useFlutterDrive) { diff --git a/script/tool/test/drive_examples_command_test.dart b/script/tool/test/drive_examples_command_test.dart index d68f68279ea..6bbd5e301ad 100644 --- a/script/tool/test/drive_examples_command_test.dart +++ b/script/tool/test/drive_examples_command_test.dart @@ -89,6 +89,24 @@ void main() { ); }); + test('fails if wasm flag is present but not web platform', () async { + setMockFlutterDevicesOutput(); + Error? commandError; + final List output = await runCapturingPrint( + runner, ['drive-examples', '--android', '--wasm'], + errorHandler: (Error e) { + commandError = e; + }); + + expect(commandError, isA()); + expect( + output, + containsAllInOrder([ + contains('--wasm is only supported on the web platform'), + ]), + ); + }); + test('fails if multiple platforms are provided', () async { setMockFlutterDevicesOutput(); Error? commandError; @@ -709,7 +727,8 @@ void main() { final List output = await runCapturingPrint(runner, [ 'drive-examples', - '--web-wasm', + '--web', + '--wasm', ]); expect(