drive_test.dart 8.62 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
      targetDeviceFinder = () {
        throw 'Unexpected call to targetDeviceFinder';
      };
41
      appStarter = (_) {
42 43 44 45 46 47 48 49
        throw 'Unexpected call to appStarter';
      };
      testRunner = (_) {
        throw 'Unexpected call to testRunner';
      };
      appStopper = (_) {
        throw 'Unexpected call to appStopper';
      };
yjbanov's avatar
yjbanov committed
50 51 52
    });

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

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

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

80 81
      String testApp = '/some/app/test_driver/e2e.dart';
      String testFile = '/some/app/test_driver/e2e_test.dart';
yjbanov's avatar
yjbanov committed
82 83 84 85 86

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

87
      List<String> args = <String>[
yjbanov's avatar
yjbanov committed
88 89 90 91
        'drive',
        '--target=$testApp',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
92
        expect(code, 1);
yjbanov's avatar
yjbanov committed
93 94 95 96 97 98 99
        BufferLogger buffer = logger;
        expect(buffer.errorText, contains(
          'Application failed to start. Will not run test. Quitting.'
        ));
      });
    });

100 101 102 103 104
    testUsingContext('returns 1 when app file is outside package', () async {
      String packageDir = '/my/app';
      useInMemoryFileSystem(cwd: packageDir);

      String appFile = '/not/in/my/app.dart';
105
      List<String> args = <String>[
106 107 108 109
        'drive',
        '--target=$appFile',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
110
        expect(code, 1);
111 112 113 114 115 116 117 118 119 120 121 122
        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';
123
      List<String> args = <String>[
124 125 126 127
        'drive',
        '--target=$appFile',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
128
        expect(code, 1);
129 130 131 132 133 134 135 136
        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
137
    testUsingContext('returns 0 when test ends successfully', () async {
138 139
      withMockDevice();

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

143
      appStarter = expectAsync((_) {
144 145 146
        return new Future<int>.value(0);
      });
      testRunner = expectAsync((List<String> testArgs) {
147
        expect(testArgs, <String>[testFile]);
148
        return new Future<int>.value(0);
149 150 151 152
      });
      appStopper = expectAsync((_) {
        return new Future<int>.value(0);
      });
yjbanov's avatar
yjbanov committed
153 154 155 156 157

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

158
      List<String> args = <String>[
yjbanov's avatar
yjbanov committed
159 160 161 162
        'drive',
        '--target=$testApp',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
163
        expect(code, 0);
yjbanov's avatar
yjbanov committed
164 165 166 167
        BufferLogger buffer = logger;
        expect(buffer.errorText, isEmpty);
      });
    });
168

169 170 171 172 173 174
    testUsingContext('returns exitCode set by test runner', () async {
      withMockDevice();

      String testApp = '/some/app/test/e2e.dart';
      String testFile = '/some/app/test_driver/e2e_test.dart';

175
      appStarter = expectAsync((_) {
176 177 178 179 180 181 182 183 184 185 186 187 188
        return new Future<int>.value(0);
      });
      testRunner = expectAsync((_) {
        return new Future<int>.value(123);
      });
      appStopper = expectAsync((_) {
        return new Future<int>.value(0);
      });

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

189
      List<String> args = <String>[
190 191 192 193
        'drive',
        '--target=$testApp',
      ];
      return createTestCommandRunner(command).run(args).then((int code) {
194
        expect(code, 123);
195 196 197 198 199
        BufferLogger buffer = logger;
        expect(buffer.errorText, isEmpty);
      });
    });

200 201 202 203 204 205 206 207 208 209 210 211 212 213

    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', () {
214
      void setOs() {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        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())
246
            .thenReturn(<Device>[emulator]);
247 248 249 250 251 252 253

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

    group('findTargetDevice on Linux', () {
254
      void setOs() {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        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
274 275
  });
}
276 277 278 279 280 281 282 283

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

class MockAndroidDevice extends Mock implements AndroidDevice { }