drive_test.dart 7.74 KB
Newer Older
yjbanov's avatar
yjbanov committed
1 2 3 4 5 6 7
// Copyright 2016 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';
8
import 'package:flutter_tools/src/android/android_device.dart';
yjbanov's avatar
yjbanov committed
9 10
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
11 12 13
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/commands/drive.dart';
import 'package:flutter_tools/src/device.dart';
yjbanov's avatar
yjbanov committed
14
import 'package:flutter_tools/src/globals.dart';
15 16
import 'package:flutter_tools/src/ios/simulators.dart';
import 'package:mockito/mockito.dart';
yjbanov's avatar
yjbanov committed
17 18 19 20 21 22
import 'package:test/test.dart';

import 'src/common.dart';
import 'src/context.dart';
import 'src/mocks.dart';

23
void main() {
yjbanov's avatar
yjbanov committed
24
  group('drive', () {
25 26 27 28 29 30 31 32 33
    DriveCommand command;
    Device mockDevice;

    void withMockDevice([Device mock]) {
      mockDevice = mock ?? new MockDevice();
      targetDeviceFinder = () async => mockDevice;
      testDeviceManager.addDevice(mockDevice);
    }

yjbanov's avatar
yjbanov committed
34
    setUp(() {
35 36
      command = new DriveCommand();
      applyMocksToCommand(command);
37
      useInMemoryFileSystem(cwd: '/some/app');
38 39 40 41 42 43 44 45 46 47 48 49 50
      toolchainDownloader = (_) async { };
      targetDeviceFinder = () {
        throw 'Unexpected call to targetDeviceFinder';
      };
      appStarter = (_) {
        throw 'Unexpected call to appStarter';
      };
      testRunner = (_) {
        throw 'Unexpected call to testRunner';
      };
      appStopper = (_) {
        throw 'Unexpected call to appStopper';
      };
yjbanov's avatar
yjbanov committed
51 52 53
    });

    tearDown(() {
54
      command = null;
yjbanov's avatar
yjbanov committed
55
      restoreFileSystem();
56 57 58 59
      restoreAppStarter();
      restoreAppStopper();
      restoreTestRunner();
      restoreTargetDeviceFinder();
yjbanov's avatar
yjbanov committed
60 61 62
    });

    testUsingContext('returns 1 when test file is not found', () {
63
      withMockDevice();
yjbanov's avatar
yjbanov committed
64 65 66 67 68 69 70
      List<String> args = [
        'drive',
        '--target=/some/app/test/e2e.dart',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
        expect(code, equals(1));
        BufferLogger buffer = logger;
Hixie's avatar
Hixie committed
71 72 73
        expect(buffer.errorText, contains(
          'Test file not found: /some/app/test_driver/e2e_test.dart'
        ));
yjbanov's avatar
yjbanov committed
74 75 76 77
      });
    });

    testUsingContext('returns 1 when app fails to run', () async {
78 79
      withMockDevice();
      appStarter = expectAsync((_) async => 1);
yjbanov's avatar
yjbanov committed
80

81 82
      String testApp = '/some/app/test_driver/e2e.dart';
      String testFile = '/some/app/test_driver/e2e_test.dart';
yjbanov's avatar
yjbanov committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

      MemoryFileSystem memFs = fs;
      await memFs.file(testApp).writeAsString('main() {}');
      await memFs.file(testFile).writeAsString('main() {}');

      List<String> args = [
        'drive',
        '--target=$testApp',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
        expect(code, equals(1));
        BufferLogger buffer = logger;
        expect(buffer.errorText, contains(
          'Application failed to start. Will not run test. Quitting.'
        ));
      });
    });

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    testUsingContext('returns 1 when app file is outside package', () async {
      String packageDir = '/my/app';
      useInMemoryFileSystem(cwd: packageDir);

      String appFile = '/not/in/my/app.dart';
      List<String> args = [
        'drive',
        '--target=$appFile',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
        expect(code, equals(1));
        BufferLogger buffer = logger;
        expect(buffer.errorText, contains(
          'Application file $appFile is outside the package directory $packageDir'
        ));
      });
    });

    testUsingContext('returns 1 when app file is in the root dir', () async {
      String packageDir = '/my/app';
      useInMemoryFileSystem(cwd: packageDir);

      String appFile = '/my/app/main.dart';
      List<String> args = [
        'drive',
        '--target=$appFile',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
        expect(code, equals(1));
        BufferLogger buffer = logger;
        expect(buffer.errorText, contains(
          'Application file main.dart must reside in one of the '
          'sub-directories of the package structure, not in the root directory.'
        ));
      });
    });

yjbanov's avatar
yjbanov committed
138
    testUsingContext('returns 0 when test ends successfully', () async {
139 140
      withMockDevice();

yjbanov's avatar
yjbanov committed
141
      String testApp = '/some/app/test/e2e.dart';
142
      String testFile = '/some/app/test_driver/e2e_test.dart';
yjbanov's avatar
yjbanov committed
143

144 145 146 147 148 149 150 151 152 153
      appStarter = expectAsync((_) {
        return new Future<int>.value(0);
      });
      testRunner = expectAsync((List<String> testArgs) {
        expect(testArgs, [testFile]);
        return new Future<Null>.value();
      });
      appStopper = expectAsync((_) {
        return new Future<int>.value(0);
      });
yjbanov's avatar
yjbanov committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

      MemoryFileSystem memFs = fs;
      await memFs.file(testApp).writeAsString('main() {}');
      await memFs.file(testFile).writeAsString('main() {}');

      List<String> args = [
        'drive',
        '--target=$testApp',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
        expect(code, equals(0));
        BufferLogger buffer = logger;
        expect(buffer.errorText, isEmpty);
      });
    });
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183


    group('findTargetDevice', () {
      testUsingContext('uses specified device', () async {
        testDeviceManager.specifiedDeviceId = '123';
        withMockDevice();
        when(mockDevice.name).thenReturn('specified-device');
        when(mockDevice.id).thenReturn('123');

        Device device = await findTargetDevice();
        expect(device.name, 'specified-device');
      });
    });

    group('findTargetDevice on iOS', () {
184
      void setOs() {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        when(os.isMacOS).thenReturn(true);
        when(os.isLinux).thenReturn(false);
      }

      testUsingContext('uses existing emulator', () async {
        setOs();
        withMockDevice();
        when(mockDevice.name).thenReturn('mock-simulator');
        when(mockDevice.isLocalEmulator).thenReturn(true);

        Device device = await findTargetDevice();
        expect(device.name, 'mock-simulator');
      });

      testUsingContext('uses existing Android device if and there are no simulators', () async {
        setOs();
        mockDevice = new MockAndroidDevice();
        when(mockDevice.name).thenReturn('mock-android-device');
        when(mockDevice.isLocalEmulator).thenReturn(false);
        withMockDevice(mockDevice);

        Device device = await findTargetDevice();
        expect(device.name, 'mock-android-device');
      });

      testUsingContext('launches emulator', () async {
        setOs();
        when(SimControl.instance.boot()).thenReturn(true);
        Device emulator = new MockDevice();
        when(emulator.name).thenReturn('new-simulator');
        when(IOSSimulatorUtils.instance.getAttachedDevices())
            .thenReturn([emulator]);

        Device device = await findTargetDevice();
        expect(device.name, 'new-simulator');
      });
    });

    group('findTargetDevice on Linux', () {
224
      void setOs() {
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        when(os.isMacOS).thenReturn(false);
        when(os.isLinux).thenReturn(true);
      }

      testUsingContext('returns null if no devices found', () async {
        setOs();
        expect(await findTargetDevice(), isNull);
      });

      testUsingContext('uses existing Android device', () async {
        setOs();
        mockDevice = new MockAndroidDevice();
        when(mockDevice.name).thenReturn('mock-android-device');
        withMockDevice(mockDevice);

        Device device = await findTargetDevice();
        expect(device.name, 'mock-android-device');
      });
    });
yjbanov's avatar
yjbanov committed
244 245
  });
}
246 247 248 249 250 251 252 253

class MockDevice extends Mock implements Device {
  MockDevice() {
    when(this.isSupported()).thenReturn(true);
  }
}

class MockAndroidDevice extends Mock implements AndroidDevice { }