flutter_platform_test.dart 5.03 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
// @dart = 2.8

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

16 17
import '../src/common.dart';
import '../src/context.dart';
18 19

void main() {
20 21 22 23
  FileSystem fileSystem;

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

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

Dan Field's avatar
Dan Field committed
43
      expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit());
44 45 46
    }, overrides: <Type, Generator>{
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.any(),
47 48
    });

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

Dan Field's avatar
Dan Field committed
59
      expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit());
60 61 62
    }, overrides: <Type, Generator>{
      FileSystem: () => fileSystem,
      ProcessManager: () => FakeProcessManager.any(),
63 64
    });

65 66 67
    testUsingContext('installHook creates a FlutterPlatform', () {
      expect(() => installHook(
        shellPath: 'abc',
68 69 70 71
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          startPaused: true,
        ),
72
        enableObservatory: false,
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,
        ),
82
        enableObservatory: false,
Dan Field's avatar
Dan Field committed
83
      ), throwsAssertionError);
84 85 86 87 88

      FlutterPlatform capturedPlatform;
      final Map<String, String> expectedPrecompiledDillFiles = <String, String>{'Key': 'Value'};
      final FlutterPlatform flutterPlatform = installHook(
        shellPath: 'abc',
89 90 91 92 93 94
        debuggingOptions: DebuggingOptions.enabled(
          BuildInfo.debug,
          startPaused: true,
          disableServiceAuthCodes: true,
          hostVmServicePort: 200,
        ),
95 96 97 98 99 100 101 102 103 104 105 106 107 108
        enableObservatory: true,
        machine: true,
        precompiledDillPath: 'def',
        precompiledDillFiles: expectedPrecompiledDillFiles,
        updateGoldens: true,
        buildTestAssets: true,
        serverType: InternetAddressType.IPv6,
        icudtlPath: 'ghi',
        platformPluginRegistration: (FlutterPlatform platform) {
          capturedPlatform = platform;
        });

      expect(identical(capturedPlatform, flutterPlatform), equals(true));
      expect(flutterPlatform.shellPath, equals('abc'));
109 110 111 112
      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));
113 114 115 116 117 118 119 120 121
      expect(flutterPlatform.enableObservatory, equals(true));
      expect(flutterPlatform.machine, equals(true));
      expect(flutterPlatform.host, InternetAddress.loopbackIPv6);
      expect(flutterPlatform.precompiledDillPath, equals('def'));
      expect(flutterPlatform.precompiledDillFiles, expectedPrecompiledDillFiles);
      expect(flutterPlatform.updateGoldens, equals(true));
      expect(flutterPlatform.buildTestAssets, equals(true));
      expect(flutterPlatform.icudtlPath, equals('ghi'));
    });
122 123 124
  });
}

125 126 127 128
class MockSuitePlatform extends Mock implements SuitePlatform {}

// A FlutterPlatform with enough fields set to load and start a test.
class TestFlutterPlatform extends FlutterPlatform {
129
  TestFlutterPlatform() : super(
130
    shellPath: '/',
131
    debuggingOptions: DebuggingOptions.enabled(
132 133 134 135 136
      const BuildInfo(
        BuildMode.debug,
        '',
        treeShakeIcons: false,
      ),
137
    ),
138
  );
139
}