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

import 'dart:async';

import 'package:file/file.dart';
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/build_info.dart';
11
import 'package:flutter_tools/src/build_system/build_system.dart';
12 13
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/tester/flutter_tester.dart';
14
import 'package:mockito/mockito.dart';
15
import 'package:process/process.dart';
16
import 'package:platform/platform.dart';
17

18 19 20
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/mocks.dart';
21 22

void main() {
23
  MemoryFileSystem fileSystem;
24 25

  setUp(() {
26
    fileSystem = MemoryFileSystem();
27 28 29 30 31
  });

  group('FlutterTesterApp', () {
    testUsingContext('fromCurrentDirectory', () async {
      const String projectPath = '/home/my/projects/my_project';
32 33
      await fileSystem.directory(projectPath).create(recursive: true);
      fileSystem.currentDirectory = projectPath;
34

35
      final FlutterTesterApp app = FlutterTesterApp.fromCurrentDirectory();
36
      expect(app.name, 'my_project');
37
      expect(app.packagesFile.path, fileSystem.path.join(projectPath, '.packages'));
38
    }, overrides: <Type, Generator>{
39
      FileSystem: () => fileSystem,
40
      ProcessManager: () => FakeProcessManager.any(),
41 42 43 44 45 46 47 48 49
    });
  });

  group('FlutterTesterDevices', () {
    tearDown(() {
      FlutterTesterDevices.showFlutterTesterDevice = false;
    });

    testUsingContext('no device', () async {
50
      final FlutterTesterDevices discoverer = FlutterTesterDevices();
51 52 53 54 55 56 57

      final List<Device> devices = await discoverer.devices;
      expect(devices, isEmpty);
    });

    testUsingContext('has device', () async {
      FlutterTesterDevices.showFlutterTesterDevice = true;
58
      final FlutterTesterDevices discoverer = FlutterTesterDevices();
59 60 61 62 63

      final List<Device> devices = await discoverer.devices;
      expect(devices, hasLength(1));

      final Device device = devices.single;
Dan Field's avatar
Dan Field committed
64
      expect(device, isA<FlutterTesterDevice>());
65 66
      expect(device.id, 'flutter-tester');
    });
67 68 69 70 71 72 73 74 75

    testUsingContext('discoverDevices', () async {
      FlutterTesterDevices.showFlutterTesterDevice = true;
      final FlutterTesterDevices discoverer = FlutterTesterDevices();

      // Timeout ignored.
      final List<Device> devices = await discoverer.discoverDevices(timeout: const Duration(seconds: 10));
      expect(devices, hasLength(1));
    });
76 77 78 79 80 81 82
  });

  group('FlutterTesterDevice', () {
    FlutterTesterDevice device;
    List<String> logLines;

    setUp(() {
83
      device = FlutterTesterDevice('flutter-tester');
84 85 86 87 88 89 90 91 92

      logLines = <String>[];
      device.getLogReader().logLines.listen(logLines.add);
    });

    testUsingContext('getters', () async {
      expect(device.id, 'flutter-tester');
      expect(await device.isLocalEmulator, isFalse);
      expect(device.name, 'Flutter test device');
93
      expect(device.portForwarder, isNot(isNull));
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      expect(await device.targetPlatform, TargetPlatform.tester);

      expect(await device.installApp(null), isTrue);
      expect(await device.isAppInstalled(null), isFalse);
      expect(await device.isLatestBuildInstalled(null), isFalse);
      expect(await device.uninstallApp(null), isTrue);

      expect(device.isSupported(), isTrue);
    });

    group('startApp', () {
      String flutterRoot;
      String flutterTesterPath;

      String projectPath;
      String mainPath;

111
      MockArtifacts mockArtifacts;
112 113
      MockProcessManager mockProcessManager;
      MockProcess mockProcess;
114
      MockBuildSystem mockBuildSystem;
115 116

      final Map<Type, Generator> startOverrides = <Type, Generator>{
117
        Platform: () => FakePlatform(operatingSystem: 'linux'),
118
        FileSystem: () => fileSystem,
119
        ProcessManager: () => mockProcessManager,
120
        Artifacts: () => mockArtifacts,
121
        BuildSystem: () => mockBuildSystem,
122 123 124
      };

      setUp(() {
125
        mockBuildSystem = MockBuildSystem();
126 127
        flutterRoot = fileSystem.path.join('home', 'me', 'flutter');
        flutterTesterPath = fileSystem.path.join(flutterRoot, 'bin', 'cache',
128
            'artifacts', 'engine', 'linux-x64', 'flutter_tester');
129
        final File flutterTesterFile = fileSystem.file(flutterTesterPath);
130 131 132
        flutterTesterFile.parent.createSync(recursive: true);
        flutterTesterFile.writeAsBytesSync(const <int>[]);

133 134
        projectPath = fileSystem.path.join('home', 'me', 'hello');
        mainPath = fileSystem.path.join(projectPath, 'lin', 'main.dart');
135

136
        mockProcessManager = MockProcessManager();
137 138
        mockProcessManager.processFactory =
            (List<String> commands) => mockProcess;
139

140
        mockArtifacts = MockArtifacts();
141 142
        final String artifactPath = fileSystem.path.join(flutterRoot, 'artifact');
        fileSystem.file(artifactPath).createSync(recursive: true);
143 144 145 146
        when(mockArtifacts.getArtifactPath(
          any,
          mode: anyNamed('mode')
        )).thenReturn(artifactPath);
147

148 149 150 151
        when(mockBuildSystem.build(
          any,
          any,
        )).thenAnswer((_) async {
152
          fileSystem.file('$mainPath.dill').createSync(recursive: true);
153 154
          return BuildResult(success: true);
        });
155 156 157 158 159
      });

      testUsingContext('not debug', () async {
        final LaunchResult result = await device.startApp(null,
            mainPath: mainPath,
160
            debuggingOptions: DebuggingOptions.disabled(const BuildInfo(BuildMode.release, null, treeShakeIcons: false)));
161

162 163 164 165 166 167
        expect(result.started, isFalse);
      }, overrides: startOverrides);


      testUsingContext('start', () async {
        final Uri observatoryUri = Uri.parse('http://127.0.0.1:6666/');
168
        mockProcess = MockProcess(stdout: Stream<List<int>>.fromIterable(<List<int>>[
169 170 171 172
          '''
Observatory listening on $observatoryUri
Hello!
'''
173
              .codeUnits,
174 175 176 177
        ]));

        final LaunchResult result = await device.startApp(null,
            mainPath: mainPath,
178
            debuggingOptions: DebuggingOptions.enabled(const BuildInfo(BuildMode.debug, null, treeShakeIcons: false)));
179 180 181 182 183 184 185
        expect(result.started, isTrue);
        expect(result.observatoryUri, observatoryUri);
        expect(logLines.last, 'Hello!');
      }, overrides: startOverrides);
    });
  });
}
186

187
class MockBuildSystem extends Mock implements BuildSystem {}
188
class MockArtifacts extends Mock implements Artifacts {}