flutter_tester_test.dart 5.92 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2018 The Chromium 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 'dart:async';

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

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

void main() {
  MemoryFileSystem fs;

  setUp(() {
26
    fs = MemoryFileSystem();
27 28 29 30 31 32 33 34
  });

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

35
      final FlutterTesterApp app = FlutterTesterApp.fromCurrentDirectory();
36
      expect(app.name, 'my_project');
37
      expect(app.packagesFile.path, fs.path.join(projectPath, '.packages'));
38 39
    }, overrides: <Type, Generator>{
      FileSystem: () => fs,
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;
64
      expect(device, isInstanceOf<FlutterTesterDevice>());
65 66 67 68 69 70 71 72 73
      expect(device.id, 'flutter-tester');
    });
  });

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

    setUp(() {
74
      device = FlutterTesterDevice('flutter-tester');
75 76 77 78 79 80 81 82 83

      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');
84
      expect(device.portForwarder, isNot(isNull));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      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;

102
      MockArtifacts mockArtifacts;
103 104
      MockProcessManager mockProcessManager;
      MockProcess mockProcess;
105
      MockBuildSystem mockBuildSystem;
106 107

      final Map<Type, Generator> startOverrides = <Type, Generator>{
108
        Platform: () => FakePlatform(operatingSystem: 'linux'),
109 110
        FileSystem: () => fs,
        ProcessManager: () => mockProcessManager,
111
        Artifacts: () => mockArtifacts,
112
        BuildSystem: () => mockBuildSystem,
113 114 115
      };

      setUp(() {
116
        mockBuildSystem = MockBuildSystem();
117 118 119 120 121 122 123 124 125 126
        flutterRoot = fs.path.join('home', 'me', 'flutter');
        flutterTesterPath = fs.path.join(flutterRoot, 'bin', 'cache',
            'artifacts', 'engine', 'linux-x64', 'flutter_tester');
        final File flutterTesterFile = fs.file(flutterTesterPath);
        flutterTesterFile.parent.createSync(recursive: true);
        flutterTesterFile.writeAsBytesSync(const <int>[]);

        projectPath = fs.path.join('home', 'me', 'hello');
        mainPath = fs.path.join(projectPath, 'lin', 'main.dart');

127
        mockProcessManager = MockProcessManager();
128 129
        mockProcessManager.processFactory =
            (List<String> commands) => mockProcess;
130

131
        mockArtifacts = MockArtifacts();
132 133
        final String artifactPath = fs.path.join(flutterRoot, 'artifact');
        fs.file(artifactPath).createSync(recursive: true);
134 135 136 137
        when(mockArtifacts.getArtifactPath(
          any,
          mode: anyNamed('mode')
        )).thenReturn(artifactPath);
138

139 140 141 142 143 144 145
        when(mockBuildSystem.build(
          any,
          any,
        )).thenAnswer((_) async {
          fs.file('$mainPath.dill').createSync(recursive: true);
          return BuildResult(success: true);
        });
146 147 148 149 150
      });

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

153 154 155 156 157 158
        expect(result.started, isFalse);
      }, overrides: startOverrides);


      testUsingContext('start', () async {
        final Uri observatoryUri = Uri.parse('http://127.0.0.1:6666/');
159
        mockProcess = MockProcess(stdout: Stream<List<int>>.fromIterable(<List<int>>[
160 161 162 163
          '''
Observatory listening on $observatoryUri
Hello!
'''
164
              .codeUnits,
165 166 167 168
        ]));

        final LaunchResult result = await device.startApp(null,
            mainPath: mainPath,
169
            debuggingOptions: DebuggingOptions.enabled(const BuildInfo(BuildMode.debug, null)));
170 171 172 173 174 175 176
        expect(result.started, isTrue);
        expect(result.observatoryUri, observatoryUri);
        expect(logLines.last, 'Hello!');
      }, overrides: startOverrides);
    });
  });
}
177

178
class MockBuildSystem extends Mock implements BuildSystem {}
179
class MockArtifacts extends Mock implements Artifacts {}