flutter_platform_test.dart 4.76 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/build_info.dart';
9
import 'package:flutter_tools/src/device.dart';
10
import 'package:flutter_tools/src/test/flutter_platform.dart';
11
import 'package:test_core/backend.dart'; // ignore: deprecated_member_use
12

13 14
import '../src/common.dart';
import '../src/context.dart';
15 16

void main() {
17
  late FileSystem fileSystem;
18 19 20

  setUp(() {
    fileSystem = MemoryFileSystem.test();
21
    fileSystem.file('.dart_tool/package_config.json')
22 23
      ..createSync(recursive: true)
      ..writeAsStringSync('{"configVersion":2,"packages":[]}');
24 25
  });

26
  group('FlutterPlatform', () {
27 28 29 30 31
    late SuitePlatform fakeSuitePlatform;
    setUp(() {
      fakeSuitePlatform = SuitePlatform(Runtime.vm);
    });

32
    testUsingContext('ensureConfiguration throws an error if an '
33
      'explicitVmServicePort is specified and more than one test file', () async {
34 35
      final FlutterPlatform flutterPlatform = FlutterPlatform(
        shellPath: '/',
36 37 38 39
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          hostVmServicePort: 1234,
        ),
40
        enableVmService: false,
41
      );
42
      flutterPlatform.loadChannel('test1.dart', fakeSuitePlatform);
43

44
      expect(() => flutterPlatform.loadChannel('test2.dart', fakeSuitePlatform), throwsToolExit());
45 46 47
    }, overrides: <Type, Generator>{
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.any(),
48 49
    });

50 51 52
    testUsingContext('ensureConfiguration throws an error if a precompiled '
      'entrypoint is specified and more that one test file', () {
      final FlutterPlatform flutterPlatform = FlutterPlatform(
53
        debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
54 55
        shellPath: '/',
        precompiledDillPath: 'example.dill',
56
        enableVmService: false,
57
      );
58
      flutterPlatform.loadChannel('test1.dart', fakeSuitePlatform);
59

60
      expect(() => flutterPlatform.loadChannel('test2.dart', fakeSuitePlatform), throwsToolExit());
61 62 63
    }, overrides: <Type, Generator>{
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.any(),
64 65
    });

66 67 68
    testUsingContext('installHook creates a FlutterPlatform', () {
      expect(() => installHook(
        shellPath: 'abc',
69 70 71 72
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          startPaused: true,
        ),
Dan Field's avatar
Dan Field committed
73
      ), throwsAssertionError);
74 75 76

      expect(() => installHook(
        shellPath: 'abc',
77 78 79 80 81
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          startPaused: true,
          hostVmServicePort: 123,
        ),
Dan Field's avatar
Dan Field committed
82
      ), throwsAssertionError);
83

84
      FlutterPlatform? capturedPlatform;
85 86 87
      final Map<String, String> expectedPrecompiledDillFiles = <String, String>{'Key': 'Value'};
      final FlutterPlatform flutterPlatform = installHook(
        shellPath: 'abc',
88 89 90 91 92 93
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          startPaused: true,
          disableServiceAuthCodes: true,
          hostVmServicePort: 200,
        ),
94
        enableVmService: true,
95 96 97 98
        machine: true,
        precompiledDillPath: 'def',
        precompiledDillFiles: expectedPrecompiledDillFiles,
        updateGoldens: true,
99
        testAssetDirectory: '/build/test',
100 101 102 103
        serverType: InternetAddressType.IPv6,
        icudtlPath: 'ghi',
        platformPluginRegistration: (FlutterPlatform platform) {
          capturedPlatform = platform;
104 105 106
        },
        uriConverter: (String input) => '$input/test',
      );
107 108 109

      expect(identical(capturedPlatform, flutterPlatform), equals(true));
      expect(flutterPlatform.shellPath, equals('abc'));
110 111 112 113
      expect(flutterPlatform.debuggingOptions.buildInfo, equals(BuildInfo.debug));
      expect(flutterPlatform.debuggingOptions.startPaused, equals(true));
      expect(flutterPlatform.debuggingOptions.disableServiceAuthCodes, equals(true));
      expect(flutterPlatform.debuggingOptions.hostVmServicePort, equals(200));
114
      expect(flutterPlatform.enableVmService, equals(true));
115 116 117 118 119
      expect(flutterPlatform.machine, equals(true));
      expect(flutterPlatform.host, InternetAddress.loopbackIPv6);
      expect(flutterPlatform.precompiledDillPath, equals('def'));
      expect(flutterPlatform.precompiledDillFiles, expectedPrecompiledDillFiles);
      expect(flutterPlatform.updateGoldens, equals(true));
120
      expect(flutterPlatform.testAssetDirectory, '/build/test');
121
      expect(flutterPlatform.icudtlPath, equals('ghi'));
122
      expect(flutterPlatform.uriConverter?.call('hello'), 'hello/test');
123
    });
124 125
  });
}