diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 9f6f61e54110..39a1ebf24f3e 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'package:flutter_tools/src/android/android_workflow.dart'; @@ -36,6 +34,7 @@ import 'package:flutter_tools/src/reporting/crash_reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/version.dart'; import 'package:meta/meta.dart'; +import 'package:test/fake.dart'; import 'common.dart'; import 'fake_http_client.dart'; @@ -48,9 +47,9 @@ export 'package:flutter_tools/src/base/context.dart' show Generator; export 'fake_process_manager.dart' show ProcessManager, FakeProcessManager, FakeCommand; /// Return the test logger. This assumes that the current Logger is a BufferLogger. -BufferLogger get testLogger => context.get() as BufferLogger; +BufferLogger get testLogger => context.get()! as BufferLogger; -FakeDeviceManager get testDeviceManager => context.get() as FakeDeviceManager; +FakeDeviceManager get testDeviceManager => context.get()! as FakeDeviceManager; @isTest void testUsingContext( @@ -58,8 +57,8 @@ void testUsingContext( dynamic Function() testMethod, { Map overrides = const {}, bool initializeFlutterRoot = true, - String testOn, - bool skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this + String? testOn, + bool? skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this }) { if (overrides[FileSystem] != null && overrides[ProcessManager] == null) { throw StateError( @@ -74,10 +73,10 @@ void testUsingContext( // Ensure we don't rely on the default [Config] constructor which will // leak a sticky $HOME/.flutter_settings behind! - Directory configDir; + Directory? configDir; tearDown(() { if (configDir != null) { - tryToDelete(configDir); + tryToDelete(configDir!); configDir = null; } }); @@ -92,7 +91,7 @@ void testUsingContext( PersistentToolState buildPersistentToolState(FileSystem fs) { configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.'); return PersistentToolState.test( - directory: configDir, + directory: configDir!, logger: globals.logger, ); } @@ -172,7 +171,7 @@ void testUsingContext( void _printBufferedErrors(AppContext testContext) { if (testContext.get() is BufferLogger) { - final BufferLogger bufferLogger = testContext.get() as BufferLogger; + final BufferLogger bufferLogger = testContext.get()! as BufferLogger; if (bufferLogger.errorText.isNotEmpty) { // This is where the logger outputting errors is implemented, so it has // to use `print`. @@ -185,10 +184,10 @@ void _printBufferedErrors(AppContext testContext) { class FakeDeviceManager implements DeviceManager { List devices = []; - String _specifiedDeviceId; + String? _specifiedDeviceId; @override - String get specifiedDeviceId { + String? get specifiedDeviceId { if (_specifiedDeviceId == null || _specifiedDeviceId == 'all') { return null; } @@ -196,7 +195,7 @@ class FakeDeviceManager implements DeviceManager { } @override - set specifiedDeviceId(String id) { + set specifiedDeviceId(String? id) { _specifiedDeviceId = id; } @@ -212,7 +211,7 @@ class FakeDeviceManager implements DeviceManager { Future> getAllConnectedDevices() async => devices; @override - Future> refreshAllConnectedDevices({ Duration timeout }) async => devices; + Future> refreshAllConnectedDevices({ Duration? timeout }) async => devices; @override Future> getDevicesById(String deviceId) async { @@ -222,7 +221,7 @@ class FakeDeviceManager implements DeviceManager { @override Future> getDevices() { return hasSpecifiedDeviceId - ? getDevicesById(specifiedDeviceId) + ? getDevicesById(specifiedDeviceId!) : getAllConnectedDevices(); } @@ -238,17 +237,17 @@ class FakeDeviceManager implements DeviceManager { List get deviceDiscoverers => []; @override - bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) { - return device.isSupportedForProject(flutterProject); + bool isDeviceSupportedForProject(Device device, FlutterProject? flutterProject) { + return device.isSupportedForProject(flutterProject!); } @override - Future> findTargetDevices(FlutterProject flutterProject, { Duration timeout }) async { + Future> findTargetDevices(FlutterProject? flutterProject, { Duration? timeout }) async { return devices; } } -class FakeAndroidLicenseValidator extends AndroidLicenseValidator { +class FakeAndroidLicenseValidator extends Fake implements AndroidLicenseValidator { @override Future get licensesAccepted async => LicensesAccepted.all; } @@ -302,7 +301,7 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter { @override Future> getBuildSettings( String projectPath, { - XcodeProjectBuildContext buildContext, + XcodeProjectBuildContext? buildContext, Duration timeout = const Duration(minutes: 1), }) async { return {}; @@ -313,14 +312,14 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter { Directory podXcodeProject, { Duration timeout = const Duration(minutes: 1), }) async { - return null; + return ''; } @override Future cleanWorkspace(String workspacePath, String scheme, { bool verbose = false }) async { } @override - Future getInfo(String projectPath, {String projectFilename}) async { + Future getInfo(String projectPath, {String? projectFilename}) async { return XcodeProjectInfo( ['Runner'], ['Debug', 'Release'], @@ -358,7 +357,7 @@ class LocalFileSystemBlockingSetCurrentDirectory extends LocalFileSystem { class FakeSignals implements Signals { @override Object addHandler(ProcessSignal signal, SignalHandler handler) { - return null; + return Object(); } @override diff --git a/packages/flutter_tools/test/src/testbed.dart b/packages/flutter_tools/test/src/testbed.dart index c47037803b78..ba8451212462 100644 --- a/packages/flutter_tools/test/src/testbed.dart +++ b/packages/flutter_tools/test/src/testbed.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'dart:io'; @@ -81,18 +79,18 @@ class Testbed { /// `overrides` provides more overrides in addition to the test defaults. /// `setup` may be provided to apply mocks within the tool managed zone, /// including any specified overrides. - Testbed({FutureOr Function() setup, Map overrides}) + Testbed({FutureOr Function()? setup, Map? overrides}) : _setup = setup, _overrides = overrides; - final FutureOr Function() _setup; - final Map _overrides; + final FutureOr Function()? _setup; + final Map? _overrides; /// Runs `test` within a tool zone. /// /// `overrides` may be used to provide new context values for the single test /// case or override any context values from the setup. - Future run(FutureOr Function() test, {Map overrides}) { + Future run(FutureOr Function() test, {Map? overrides}) { final Map testOverrides = { ..._testbedDefaults, // Add the initial setUp overrides @@ -104,13 +102,13 @@ class Testbed { throw StateError('Do not inject ProcessUtils for testing, use ProcessManager instead.'); } // Cache the original flutter root to restore after the test case. - final String originalFlutterRoot = Cache.flutterRoot; + final String? originalFlutterRoot = Cache.flutterRoot; // Track pending timers to verify that they were correctly cleaned up. final Map timers = {}; return HttpOverrides.runZoned(() { - return runInContext(() { - return context.run( + return runInContext(() { + return context.run( name: 'testbed', overrides: testOverrides, zoneSpecification: ZoneSpecification( @@ -128,7 +126,7 @@ class Testbed { body: () async { Cache.flutterRoot = ''; if (_setup != null) { - await _setup(); + await _setup?.call(); } await test(); Cache.flutterRoot = originalFlutterRoot; @@ -140,6 +138,6 @@ class Testbed { return null; }); }); - }, createHttpClient: (SecurityContext c) => FakeHttpClient.any()); + }, createHttpClient: (SecurityContext? c) => FakeHttpClient.any()); } }