// 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 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/test/flutter_platform.dart'; import 'package:meta/meta.dart'; import 'package:platform/platform.dart'; import 'package:mockito/mockito.dart'; import 'package:process/process.dart'; import 'package:test_core/backend.dart'; // ignore: deprecated_member_use import '../src/common.dart'; import '../src/context.dart'; void main() { group('FlutterPlatform', () { testUsingContext('ensureConfiguration throws an error if an explicitObservatoryPort is specified and more than one test file', () async { final FlutterPlatform flutterPlatform = FlutterPlatform(buildMode: BuildMode.debug, shellPath: '/', explicitObservatoryPort: 1234); flutterPlatform.loadChannel('test1.dart', MockSuitePlatform()); expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit()); }); testUsingContext('ensureConfiguration throws an error if a precompiled entrypoint is specified and more that one test file', () { final FlutterPlatform flutterPlatform = FlutterPlatform(buildMode: BuildMode.debug, shellPath: '/', precompiledDillPath: 'example.dill'); flutterPlatform.loadChannel('test1.dart', MockSuitePlatform()); expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit()); }); group('The FLUTTER_TEST environment variable is passed to the test process', () { MockPlatform mockPlatform; MockProcessManager mockProcessManager; FlutterPlatform flutterPlatform; final Map contextOverrides = { Platform: () => mockPlatform, ProcessManager: () => mockProcessManager, }; setUp(() { mockPlatform = MockPlatform(); mockProcessManager = MockProcessManager(); flutterPlatform = TestFlutterPlatform(); }); Future> captureEnvironment() async { flutterPlatform.loadChannel('test1.dart', MockSuitePlatform()); when(mockProcessManager.start( any, environment: anyNamed('environment')), ).thenAnswer((_) { return Future.value(MockProcess()); }); await untilCalled(mockProcessManager.start(any, environment: anyNamed('environment'))); final VerificationResult toVerify = verify(mockProcessManager.start( any, environment: captureAnyNamed('environment'), )); expect(toVerify.captured, hasLength(1)); expect(toVerify.captured.first, isA>()); return toVerify.captured.first as Map; } testUsingContext('as true when not originally set', () async { when(mockPlatform.environment).thenReturn({}); final Map capturedEnvironment = await captureEnvironment(); expect(capturedEnvironment['FLUTTER_TEST'], 'true'); }, overrides: contextOverrides); testUsingContext('as true when set to true', () async { when(mockPlatform.environment).thenReturn({'FLUTTER_TEST': 'true'}); final Map capturedEnvironment = await captureEnvironment(); expect(capturedEnvironment['FLUTTER_TEST'], 'true'); }, overrides: contextOverrides); testUsingContext('as false when set to false', () async { when(mockPlatform.environment).thenReturn({'FLUTTER_TEST': 'false'}); final Map capturedEnvironment = await captureEnvironment(); expect(capturedEnvironment['FLUTTER_TEST'], 'false'); }, overrides: contextOverrides); testUsingContext('unchanged when set', () async { when(mockPlatform.environment).thenReturn({'FLUTTER_TEST': 'neither true nor false'}); final Map capturedEnvironment = await captureEnvironment(); expect(capturedEnvironment['FLUTTER_TEST'], 'neither true nor false'); }, overrides: contextOverrides); testUsingContext('as null when set to null', () async { when(mockPlatform.environment).thenReturn({'FLUTTER_TEST': null}); final Map capturedEnvironment = await captureEnvironment(); expect(capturedEnvironment['FLUTTER_TEST'], null); }, overrides: contextOverrides); }); testUsingContext('installHook creates a FlutterPlatform', () { expect(() => installHook( buildMode: BuildMode.debug, shellPath: 'abc', enableObservatory: false, startPaused: true, ), throwsAssertionError); expect(() => installHook( buildMode: BuildMode.debug, shellPath: 'abc', enableObservatory: false, startPaused: false, observatoryPort: 123, ), throwsAssertionError); FlutterPlatform capturedPlatform; final Map expectedPrecompiledDillFiles = {'Key': 'Value'}; final FlutterPlatform flutterPlatform = installHook( shellPath: 'abc', enableObservatory: true, machine: true, startPaused: true, disableServiceAuthCodes: true, port: 100, precompiledDillPath: 'def', precompiledDillFiles: expectedPrecompiledDillFiles, buildMode: BuildMode.debug, trackWidgetCreation: true, updateGoldens: true, buildTestAssets: true, observatoryPort: 200, serverType: InternetAddressType.IPv6, icudtlPath: 'ghi', platformPluginRegistration: (FlutterPlatform platform) { capturedPlatform = platform; }); expect(identical(capturedPlatform, flutterPlatform), equals(true)); expect(flutterPlatform.shellPath, equals('abc')); expect(flutterPlatform.enableObservatory, equals(true)); expect(flutterPlatform.machine, equals(true)); expect(flutterPlatform.startPaused, equals(true)); expect(flutterPlatform.disableServiceAuthCodes, equals(true)); expect(flutterPlatform.port, equals(100)); expect(flutterPlatform.host, InternetAddress.loopbackIPv6); expect(flutterPlatform.explicitObservatoryPort, equals(200)); expect(flutterPlatform.precompiledDillPath, equals('def')); expect(flutterPlatform.precompiledDillFiles, expectedPrecompiledDillFiles); expect(flutterPlatform.buildMode, equals(BuildMode.debug)); expect(flutterPlatform.trackWidgetCreation, equals(true)); expect(flutterPlatform.updateGoldens, equals(true)); expect(flutterPlatform.buildTestAssets, equals(true)); expect(flutterPlatform.icudtlPath, equals('ghi')); }); }); } class MockSuitePlatform extends Mock implements SuitePlatform {} class MockProcessManager extends Mock implements ProcessManager {} class MockProcess extends Mock implements Process {} class MockPlatform extends Mock implements Platform {} class MockHttpServer extends Mock implements HttpServer {} // A FlutterPlatform with enough fields set to load and start a test. // // Uses a mock HttpServer. We don't want to bind random ports in our CI hosts. class TestFlutterPlatform extends FlutterPlatform { TestFlutterPlatform() : super( buildMode: BuildMode.debug, shellPath: '/', precompiledDillPath: 'example.dill', host: InternetAddress.loopbackIPv6, port: 0, updateGoldens: false, startPaused: false, enableObservatory: false, buildTestAssets: false, ); @override @protected Future bind(InternetAddress host, int port) async => MockHttpServer(); }