Support flutter_test_config for flutter test on web platforms (#72488)

This commit is contained in:
Jia Hao
2021-01-06 07:49:02 +08:00
committed by GitHub
parent ba91a69f22
commit 47bad77287
6 changed files with 46 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
// 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.
export 'package:flutter_goldens/flutter_goldens.dart' show testExecutable;

View File

@@ -0,0 +1,8 @@
// 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';
// package:flutter_goldens is not used as part of the test process for web.
Future<void> testExecutable(FutureOr<void> testMain()) async => testMain();

View File

@@ -2,4 +2,5 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export 'package:flutter_goldens/flutter_goldens.dart' show testExecutable;
export '_goldens_io.dart'
if (dart.library.html) '_goldens_web.dart' show testExecutable;

View File

@@ -18,6 +18,7 @@ import '../compile.dart';
import '../dart/language_version.dart';
import '../web/bootstrap.dart';
import '../web/memory_fs.dart';
import 'test_config.dart';
/// A web compiler for the test runner.
class WebTestCompiler {
@@ -79,6 +80,7 @@ class WebTestCompiler {
..writeAsStringSync(generateTestEntrypoint(
relativeTestPath: relativeTestSegments.join('/'),
absolutePath: testFilePath,
testConfigPath: findTestConfigFile(_fileSystem.file(testFilePath))?.path,
languageVersion: languageVersion,
));
generatedFiles.add(generatedFile);

View File

@@ -102,6 +102,7 @@ define("$bootstrapModule", ["$entrypoint", "dart_sdk"], function(app, dart_sdk)
String generateTestEntrypoint({
@required String relativeTestPath,
@required String absolutePath,
@required String testConfigPath,
@required LanguageVersion languageVersion,
}) {
return '''
@@ -110,6 +111,7 @@ String generateTestEntrypoint({
import 'dart:ui' as ui;
import 'dart:html';
import 'dart:js';
${testConfigPath != null ? "import '${Uri.file(testConfigPath)}' as test_config;" : ""}
import 'package:stream_channel/stream_channel.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_api/src/backend/stack_trace_formatter.dart'; // ignore: implementation_imports
@@ -122,7 +124,10 @@ String generateTestEntrypoint({
webGoldenComparator = DefaultWebGoldenComparator(Uri.parse('$absolutePath'));
(ui.window as dynamic).debugOverrideDevicePixelRatio(3.0);
(ui.window as dynamic).webOnlyDebugPhysicalSizeOverride = const ui.Size(2400, 1800);
internalBootstrapBrowserTest(() => test.main);
internalBootstrapBrowserTest(() {
return ${testConfigPath != null ? "() => test_config.testExecutable(test.main)" : "test.main"};
});
}
void internalBootstrapBrowserTest(Function getMain()) {

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:flutter_tools/src/web/bootstrap.dart';
import 'package:package_config/package_config.dart';
import '../../src/common.dart';
@@ -70,4 +71,26 @@ void main() {
expect(result, contains('el.setAttribute("data-main", \'foo.dart.js\');'));
});
test('generateTestEntrypoint does not generate test config wrappers when testConfigPath is not passed', () {
final String result = generateTestEntrypoint(
relativeTestPath: 'relative_path.dart',
absolutePath: 'absolute_path.dart',
testConfigPath: null,
languageVersion: LanguageVersion(2, 8),
);
expect(result, isNot(contains('test_config.testExecutable')));
});
test('generateTestEntrypoint generates test config wrappers when testConfigPath is passed', () {
final String result = generateTestEntrypoint(
relativeTestPath: 'relative_path.dart',
absolutePath: 'absolute_path.dart',
testConfigPath: 'test_config_path.dart',
languageVersion: LanguageVersion(2, 8),
);
expect(result, contains('test_config.testExecutable'));
});
}