drive_test.dart 9.53 KB
Newer Older
yjbanov's avatar
yjbanov committed
1 2 3 4
// 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.

5 6
import 'dart:async';

7
import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/android/android_device.dart';
9
import 'package:flutter_tools/src/base/common.dart';
yjbanov's avatar
yjbanov committed
10
import 'package:flutter_tools/src/base/file_system.dart';
11
import 'package:flutter_tools/src/base/io.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/cache.dart';
14 15
import 'package:flutter_tools/src/commands/drive.dart';
import 'package:flutter_tools/src/device.dart';
16
import 'package:mockito/mockito.dart';
yjbanov's avatar
yjbanov committed
17 18
import 'package:test/test.dart';

19 20 21
import '../src/common.dart';
import '../src/context.dart';
import '../src/mocks.dart';
yjbanov's avatar
yjbanov committed
22

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

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

36 37 38 39
    setUpAll(() {
      Cache.disableLocking();
    });

yjbanov's avatar
yjbanov committed
40
    setUp(() {
41 42
      command = new DriveCommand();
      applyMocksToCommand(command);
43 44 45 46 47 48 49
      fs = new MemoryFileSystem();
      cwd = fs.systemTempDirectory.createTempSync('some_app_');
      fs.currentDirectory = cwd;
      fs.directory('test').createSync();
      fs.directory('test_driver').createSync();
      fs.file('pubspec.yaml')..createSync();
      fs.file('.packages').createSync();
50
      setExitFunctionForTests();
51 52 53
      targetDeviceFinder = () {
        throw 'Unexpected call to targetDeviceFinder';
      };
54
      appStarter = (DriveCommand command) {
55 56
        throw 'Unexpected call to appStarter';
      };
57
      testRunner = (List<String> testArgs, String observatoryUri) {
58 59
        throw 'Unexpected call to testRunner';
      };
60
      appStopper = (DriveCommand command) {
61 62
        throw 'Unexpected call to appStopper';
      };
yjbanov's avatar
yjbanov committed
63 64 65
    });

    tearDown(() {
66
      command = null;
67
      restoreExitFunction();
68 69 70 71
      restoreAppStarter();
      restoreAppStopper();
      restoreTestRunner();
      restoreTargetDeviceFinder();
yjbanov's avatar
yjbanov committed
72 73
    });

74
    testUsingContext('returns 1 when test file is not found', () async {
75
      withMockDevice();
76

77 78
      final String testApp = fs.path.join(cwd.path, 'test', 'e2e.dart');
      final String testFile = fs.path.join(cwd.path, 'test_driver', 'e2e_test.dart');
79

80
      final List<String> args = <String>[
yjbanov's avatar
yjbanov committed
81
        'drive',
82
        '--target=$testApp}',
yjbanov's avatar
yjbanov committed
83
      ];
84 85 86 87 88
      try {
        await createTestCommandRunner(command).run(args);
        fail('Expect exception');
      } on ToolExit catch (e) {
        expect(e.exitCode ?? 1, 1);
89
        expect(e.message, contains('Test file not found: $testFile'));
90
      }
91
    }, overrides: <Type, Generator>{
92
      FileSystem: () => fs,
yjbanov's avatar
yjbanov committed
93 94 95
    });

    testUsingContext('returns 1 when app fails to run', () async {
96
      withMockDevice();
97
      appStarter = expectAsync1((DriveCommand command) async => null);
yjbanov's avatar
yjbanov committed
98

99 100
      final String testApp = fs.path.join(cwd.path, 'test_driver', 'e2e.dart');
      final String testFile = fs.path.join(cwd.path, 'test_driver', 'e2e_test.dart');
yjbanov's avatar
yjbanov committed
101

102
      final MemoryFileSystem memFs = fs;
103 104
      await memFs.file(testApp).writeAsString('main() { }');
      await memFs.file(testFile).writeAsString('main() { }');
yjbanov's avatar
yjbanov committed
105

106
      final List<String> args = <String>[
yjbanov's avatar
yjbanov committed
107 108 109
        'drive',
        '--target=$testApp',
      ];
110 111 112 113 114
      try {
        await createTestCommandRunner(command).run(args);
        fail('Expect exception');
      } on ToolExit catch (e) {
        expect(e.exitCode, 1);
115
        expect(e.message, contains('Application failed to start. Will not run test. Quitting.'));
116
      }
117
    }, overrides: <Type, Generator>{
118
      FileSystem: () => fs,
yjbanov's avatar
yjbanov committed
119 120
    });

121
    testUsingContext('returns 1 when app file is outside package', () async {
122 123
      final String appFile = fs.path.join(cwd.dirname, 'other_app', 'app.dart');
      final List<String> args = <String>[
124 125 126
        'drive',
        '--target=$appFile',
      ];
127 128 129 130 131 132
      try {
        await createTestCommandRunner(command).run(args);
        fail('Expect exception');
      } on ToolExit catch (e) {
        expect(e.exitCode ?? 1, 1);
        expect(testLogger.errorText, contains(
133
            'Application file $appFile is outside the package directory ${cwd.path}',
134
        ));
135
      }
136
    }, overrides: <Type, Generator>{
137
      FileSystem: () => fs,
138 139 140
    });

    testUsingContext('returns 1 when app file is in the root dir', () async {
141 142
      final String appFile = fs.path.join(cwd.path, 'main.dart');
      final List<String> args = <String>[
143 144 145
        'drive',
        '--target=$appFile',
      ];
146 147 148 149 150 151
      try {
        await createTestCommandRunner(command).run(args);
        fail('Expect exception');
      } on ToolExit catch (e) {
        expect(e.exitCode ?? 1, 1);
        expect(testLogger.errorText, contains(
152 153
            'Application file main.dart must reside in one of the '
            'sub-directories of the package structure, not in the root directory.',
154
        ));
155
      }
156
    }, overrides: <Type, Generator>{
157
      FileSystem: () => fs,
158 159
    });

yjbanov's avatar
yjbanov committed
160
    testUsingContext('returns 0 when test ends successfully', () async {
161 162
      withMockDevice();

163 164
      final String testApp = fs.path.join(cwd.path, 'test', 'e2e.dart');
      final String testFile = fs.path.join(cwd.path, 'test_driver', 'e2e_test.dart');
yjbanov's avatar
yjbanov committed
165

166
      appStarter = expectAsync1((DriveCommand command) async {
167
        return new LaunchResult.succeeded();
168
      });
169
      testRunner = expectAsync2((List<String> testArgs, String observatoryUri) async {
170
        expect(testArgs, <String>[testFile]);
171
        return null;
172
      });
173
      appStopper = expectAsync1((DriveCommand command) async {
174
        return true;
175
      });
yjbanov's avatar
yjbanov committed
176

177
      final MemoryFileSystem memFs = fs;
yjbanov's avatar
yjbanov committed
178 179 180
      await memFs.file(testApp).writeAsString('main() {}');
      await memFs.file(testFile).writeAsString('main() {}');

181
      final List<String> args = <String>[
yjbanov's avatar
yjbanov committed
182 183 184
        'drive',
        '--target=$testApp',
      ];
185 186
      await createTestCommandRunner(command).run(args);
      expect(testLogger.errorText, isEmpty);
187
    }, overrides: <Type, Generator>{
188
      FileSystem: () => fs,
yjbanov's avatar
yjbanov committed
189
    });
190

191 192 193
    testUsingContext('returns exitCode set by test runner', () async {
      withMockDevice();

194 195
      final String testApp = fs.path.join(cwd.path, 'test', 'e2e.dart');
      final String testFile = fs.path.join(cwd.path, 'test_driver', 'e2e_test.dart');
196

197
      appStarter = expectAsync1((DriveCommand command) async {
198
        return new LaunchResult.succeeded();
199
      });
200
      testRunner = (List<String> testArgs, String observatoryUri) async {
201 202
        throwToolExit(null, exitCode: 123);
      };
203
      appStopper = expectAsync1((DriveCommand command) async {
204
        return true;
205 206
      });

207
      final MemoryFileSystem memFs = fs;
208 209 210
      await memFs.file(testApp).writeAsString('main() {}');
      await memFs.file(testFile).writeAsString('main() {}');

211
      final List<String> args = <String>[
212 213 214
        'drive',
        '--target=$testApp',
      ];
215 216 217 218 219 220 221
      try {
        await createTestCommandRunner(command).run(args);
        fail('Expect exception');
      } on ToolExit catch (e) {
        expect(e.exitCode ?? 1, 123);
        expect(e.message, isNull);
      }
222
    }, overrides: <Type, Generator>{
223
      FileSystem: () => fs,
224 225
    });

226 227 228 229 230 231 232
    group('findTargetDevice', () {
      testUsingContext('uses specified device', () async {
        testDeviceManager.specifiedDeviceId = '123';
        withMockDevice();
        when(mockDevice.name).thenReturn('specified-device');
        when(mockDevice.id).thenReturn('123');

233
        final Device device = await findTargetDevice();
234
        expect(device.name, 'specified-device');
235
      }, overrides: <Type, Generator>{
236
        FileSystem: () => fs,
237 238 239
      });
    });

240 241
    void findTargetDeviceOnOperatingSystem(String operatingSystem) {
      Platform platform() => new FakePlatform(operatingSystem: operatingSystem);
242 243 244

      testUsingContext('returns null if no devices found', () async {
        expect(await findTargetDevice(), isNull);
245
      }, overrides: <Type, Generator>{
246
        FileSystem: () => fs,
247
        Platform: platform,
248 249 250 251 252 253 254
      });

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

255
        final Device device = await findTargetDevice();
256
        expect(device.name, 'mock-android-device');
257
      }, overrides: <Type, Generator>{
258
        FileSystem: () => fs,
259
        Platform: platform,
260
      });
261 262 263
    }

    group('findTargetDevice on Linux', () {
264
      findTargetDeviceOnOperatingSystem('linux');
265 266 267
    });

    group('findTargetDevice on Windows', () {
268
      findTargetDeviceOnOperatingSystem('windows');
269
    });
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

    group('findTargetDevice on macOS', () {
      findTargetDeviceOnOperatingSystem('macos');

      Platform macOsPlatform() => new FakePlatform(operatingSystem: 'macos');

      testUsingContext('uses existing simulator', () async {
        withMockDevice();
        when(mockDevice.name).thenReturn('mock-simulator');
        when(mockDevice.isLocalEmulator).thenReturn(new Future<bool>.value(true));

        final Device device = await findTargetDevice();
        expect(device.name, 'mock-simulator');
      }, overrides: <Type, Generator>{
        FileSystem: () => fs,
        Platform: macOsPlatform,
      });
    });
288
  });
yjbanov's avatar
yjbanov committed
289
}
290 291 292

class MockDevice extends Mock implements Device {
  MockDevice() {
293
    when(isSupported()).thenReturn(true);
294 295 296 297
  }
}

class MockAndroidDevice extends Mock implements AndroidDevice { }