devices_test.dart 3.81 KB
Newer Older
1 2 3 4 5
// Copyright 2017 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:io' show ProcessResult;
6

7 8 9
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/ios/devices.dart';
10
import 'package:mockito/mockito.dart';
11
import 'package:platform/platform.dart';
12 13 14 15
import 'package:process/process.dart';
import 'package:test/test.dart';

import '../context.dart';
16

17 18 19 20
class MockProcessManager extends Mock implements ProcessManager {}
class MockFile extends Mock implements File {}

void main() {
21 22 23
  final FakePlatform osx = new FakePlatform.fromPlatform(const LocalPlatform());
  osx.operatingSystem = 'macos';

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  group('test screenshot', () {
    MockProcessManager mockProcessManager;
    MockFile mockOutputFile;
    IOSDevice iosDeviceUnderTest;

    setUp(() {
      mockProcessManager = new MockProcessManager();
      mockOutputFile = new MockFile();
    });

    testUsingContext(
      'screenshot without ideviceinstaller error',
      () async {
        when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png'));
        // Let everything else return exit code 0 so process.dart doesn't crash.
        // The matcher order is important.
        when(
          mockProcessManager.runSync(any, environment: null, workingDirectory:  null)
        ).thenReturn(
          new ProcessResult(2, 0, '', null)
        );
        // Let `which idevicescreenshot` fail with exit code 1.
        when(
          mockProcessManager.runSync(
            <String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
        ).thenReturn(
          new ProcessResult(1, 1, '', null)
        );

        iosDeviceUnderTest = new IOSDevice('1234');
        iosDeviceUnderTest.takeScreenshot(mockOutputFile);
        verify(mockProcessManager.runSync(
          <String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
        verifyNever(mockProcessManager.runSync(
          <String>['idevicescreenshot', fs.path.join('some', 'test', 'path', 'image.png')],
          environment: null,
          workingDirectory: null
        ));
        expect(testLogger.errorText, contains('brew install ideviceinstaller'));
      },
64 65 66 67
      overrides: <Type, Generator>{
        ProcessManager: () => mockProcessManager,
        Platform: () => osx,
      }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    );

    testUsingContext(
      'screenshot with ideviceinstaller gets command',
      () async {
        when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png'));
        // Let everything else return exit code 0.
        // The matcher order is important.
        when(
          mockProcessManager.runSync(any, environment: null, workingDirectory:  null)
        ).thenReturn(
          new ProcessResult(4, 0, '', null)
        );
        // Let there be idevicescreenshot in the PATH.
        when(
          mockProcessManager.runSync(
            <String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
        ).thenReturn(
          new ProcessResult(3, 0, fs.path.join('some', 'path', 'to', 'iscreenshot'), null)
        );

        iosDeviceUnderTest = new IOSDevice('1234');
        iosDeviceUnderTest.takeScreenshot(mockOutputFile);
        verify(mockProcessManager.runSync(
          <String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
        verify(mockProcessManager.runSync(
          <String>[
            fs.path.join('some', 'path', 'to', 'iscreenshot'),
            fs.path.join('some', 'test', 'path', 'image.png')
          ],
          environment: null,
          workingDirectory: null
        ));
      },
      overrides: <Type, Generator>{ProcessManager: () => mockProcessManager}
    );
  });
}