simulators_test.dart 14.2 KB
Newer Older
1
import 'dart:async';
2
import 'dart:io' show ProcessResult, Process;
3

4
import 'package:file/file.dart';
5 6
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/application_package.dart';
7
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/ios/mac.dart';
9
import 'package:flutter_tools/src/ios/simulators.dart';
10
import 'package:mockito/mockito.dart';
11
import 'package:platform/platform.dart';
12 13
import 'package:process/process.dart';

14
import '../src/common.dart';
15
import '../src/context.dart';
16
import '../src/mocks.dart';
17 18

class MockFile extends Mock implements File {}
19
class MockIMobileDevice extends Mock implements IMobileDevice {}
20
class MockProcess extends Mock implements Process {}
21 22
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcode extends Mock implements Xcode {}
23

24
void main() {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  FakePlatform osx;

  setUp(() {
    osx = new FakePlatform.fromPlatform(const LocalPlatform());
    osx.operatingSystem = 'macos';
  });

  group('logFilePath', () {
    testUsingContext('defaults to rooted from HOME', () {
      osx.environment['HOME'] = '/foo/bar';
      expect(new IOSSimulator('123').logFilePath, '/foo/bar/Library/Logs/CoreSimulator/123/system.log');
    }, overrides: <Type, Generator>{
      Platform: () => osx,
    }, testOn: 'posix');

    testUsingContext('respects IOS_SIMULATOR_LOG_FILE_PATH', () {
      osx.environment['HOME'] = '/foo/bar';
      osx.environment['IOS_SIMULATOR_LOG_FILE_PATH'] = '/baz/qux/%{id}/system.log';
      expect(new IOSSimulator('456').logFilePath, '/baz/qux/456/system.log');
    }, overrides: <Type, Generator>{
      Platform: () => osx,
    });
  });
48

49 50 51
  group('compareIosVersions', () {
    test('compares correctly', () {
      // This list must be sorted in ascending preference order
52
      final List<String> testList = <String>[
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        '8', '8.0', '8.1', '8.2',
        '9', '9.0', '9.1', '9.2',
        '10', '10.0', '10.1',
      ];

      for (int i = 0; i < testList.length; i++) {
        expect(compareIosVersions(testList[i], testList[i]), 0);
      }

      for (int i = 0; i < testList.length - 1; i++) {
        for (int j = i + 1; j < testList.length; j++) {
          expect(compareIosVersions(testList[i], testList[j]), lessThan(0));
          expect(compareIosVersions(testList[j], testList[i]), greaterThan(0));
        }
      }
    });
  });

  group('compareIphoneVersions', () {
    test('compares correctly', () {
      // This list must be sorted in ascending preference order
74
      final List<String> testList = <String>[
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        'com.apple.CoreSimulator.SimDeviceType.iPhone-4s',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-5',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-5s',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-6strange',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-6',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus',
        'com.apple.CoreSimulator.SimDeviceType.iPhone-6s',
      ];

      for (int i = 0; i < testList.length; i++) {
        expect(compareIphoneVersions(testList[i], testList[i]), 0);
      }

      for (int i = 0; i < testList.length - 1; i++) {
        for (int j = i + 1; j < testList.length; j++) {
          expect(compareIphoneVersions(testList[i], testList[j]), lessThan(0));
          expect(compareIphoneVersions(testList[j], testList[i]), greaterThan(0));
        }
      }
    });
  });
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  group('sdkMajorVersion', () {
    // This new version string appears in SimulatorApp-850 CoreSimulator-518.16 beta.
    test('can be parsed from iOS-11-3', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'com.apple.CoreSimulator.SimRuntime.iOS-11-3');

      expect(await device.sdkMajorVersion, 11);
    });

    test('can be parsed from iOS 11.2', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'iOS 11.2');

      expect(await device.sdkMajorVersion, 11);
    });
  });

113
  group('IOSSimulator.isSupported', () {
114
    testUsingContext('Apple TV is unsupported', () {
115
      expect(new IOSSimulator('x', name: 'Apple TV').isSupported(), false);
116 117
    }, overrides: <Type, Generator>{
      Platform: () => osx,
118 119
    });

120
    testUsingContext('Apple Watch is unsupported', () {
121
      expect(new IOSSimulator('x', name: 'Apple Watch').isSupported(), false);
122 123
    }, overrides: <Type, Generator>{
      Platform: () => osx,
124 125
    });

126 127
    testUsingContext('iPad 2 is supported', () {
      expect(new IOSSimulator('x', name: 'iPad 2').isSupported(), true);
128 129
    }, overrides: <Type, Generator>{
      Platform: () => osx,
130 131
    });

132 133
    testUsingContext('iPad Retina is supported', () {
      expect(new IOSSimulator('x', name: 'iPad Retina').isSupported(), true);
134 135
    }, overrides: <Type, Generator>{
      Platform: () => osx,
136 137
    });

138 139
    testUsingContext('iPhone 5 is supported', () {
      expect(new IOSSimulator('x', name: 'iPhone 5').isSupported(), true);
140 141
    }, overrides: <Type, Generator>{
      Platform: () => osx,
142 143
    });

144
    testUsingContext('iPhone 5s is supported', () {
145
      expect(new IOSSimulator('x', name: 'iPhone 5s').isSupported(), true);
146 147
    }, overrides: <Type, Generator>{
      Platform: () => osx,
148 149
    });

150
    testUsingContext('iPhone SE is supported', () {
151
      expect(new IOSSimulator('x', name: 'iPhone SE').isSupported(), true);
152 153
    }, overrides: <Type, Generator>{
      Platform: () => osx,
154 155
    });

156
    testUsingContext('iPhone 7 Plus is supported', () {
157
      expect(new IOSSimulator('x', name: 'iPhone 7 Plus').isSupported(), true);
158 159
    }, overrides: <Type, Generator>{
      Platform: () => osx,
160
    });
161 162 163 164 165 166

    testUsingContext('iPhone X is supported', () {
      expect(new IOSSimulator('x', name: 'iPhone X').isSupported(), true);
    }, overrides: <Type, Generator>{
      Platform: () => osx,
    });
167
  });
168 169 170 171 172 173 174 175 176 177 178

  group('Simulator screenshot', () {
    MockXcode mockXcode;
    MockProcessManager mockProcessManager;
    IOSSimulator deviceUnderTest;

    setUp(() {
      mockXcode = new MockXcode();
      mockProcessManager = new MockProcessManager();
      // Let everything else return exit code 0 so process.dart doesn't crash.
      when(
179
        mockProcessManager.run(any, environment: null, workingDirectory: null)
180
      ).thenAnswer((Invocation invocation) =>
181
        new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
182 183 184 185 186 187 188 189
      );
      // Doesn't matter what the device is.
      deviceUnderTest = new IOSSimulator('x', name: 'iPhone SE');
    });

    testUsingContext(
      'old Xcode doesn\'t support screenshot',
      () {
190 191
        when(mockXcode.majorVersion).thenReturn(7);
        when(mockXcode.minorVersion).thenReturn(1);
192 193
        expect(deviceUnderTest.supportsScreenshot, false);
      },
194
      overrides: <Type, Generator>{Xcode: () => mockXcode}
195 196 197 198
    );

    testUsingContext(
      'Xcode 8.2+ supports screenshots',
199
      () async {
200 201
        when(mockXcode.majorVersion).thenReturn(8);
        when(mockXcode.minorVersion).thenReturn(2);
202
        expect(deviceUnderTest.supportsScreenshot, true);
203
        final MockFile mockFile = new MockFile();
204
        when(mockFile.path).thenReturn(fs.path.join('some', 'path', 'to', 'screenshot.png'));
205 206
        await deviceUnderTest.takeScreenshot(mockFile);
        verify(mockProcessManager.run(
207 208 209 210
          <String>[
              '/usr/bin/xcrun',
              'simctl',
              'io',
211
              'x',
212
              'screenshot',
213
              fs.path.join('some', 'path', 'to', 'screenshot.png'),
214 215 216 217 218 219 220 221 222 223 224 225 226
          ],
          environment: null,
          workingDirectory: null
        ));
      },
      overrides: <Type, Generator>{
        ProcessManager: () => mockProcessManager,
        // Test a real one. Screenshot doesn't require instance states.
        SimControl: () => new SimControl(),
        Xcode: () => mockXcode,
      }
    );
  });
227 228 229 230 231 232

  group('launchDeviceLogTool', () {
    MockProcessManager mockProcessManager;

    setUp(() {
      mockProcessManager = new MockProcessManager();
233
      when(mockProcessManager.start(any, environment: null, workingDirectory: null))
234
        .thenAnswer((Invocation invocation) => new Future<Process>.value(new MockProcess()));
235 236 237 238 239 240
    });

    testUsingContext('uses tail on iOS versions prior to iOS 11', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'iOS 9.3');
      await launchDeviceLogTool(device);
      expect(
241
        verify(mockProcessManager.start(captureAny, environment: null, workingDirectory: null)).captured.single,
242 243 244 245 246 247 248 249 250 251 252
        contains('tail'),
      );
    },
    overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });

    testUsingContext('uses /usr/bin/log on iOS 11 and above', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'iOS 11.0');
      await launchDeviceLogTool(device);
      expect(
253
        verify(mockProcessManager.start(captureAny, environment: null, workingDirectory: null)).captured.single,
254 255 256 257 258 259 260 261 262 263 264 265 266 267
        contains('/usr/bin/log'),
      );
    },
    overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });
  });

  group('launchSystemLogTool', () {
    MockProcessManager mockProcessManager;

    setUp(() {
      mockProcessManager = new MockProcessManager();
      when(mockProcessManager.start(any, environment: null, workingDirectory: null))
268
        .thenAnswer((Invocation invocation) => new Future<Process>.value(new MockProcess()));
269 270 271 272 273 274
    });

    testUsingContext('uses tail on iOS versions prior to iOS 11', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'iOS 9.3');
      await launchSystemLogTool(device);
      expect(
275
        verify(mockProcessManager.start(captureAny, environment: null, workingDirectory: null)).captured.single,
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        contains('tail'),
      );
    },
    overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });

    testUsingContext('uses /usr/bin/log on iOS 11 and above', () async {
      final IOSSimulator device = new IOSSimulator('x', name: 'iPhone SE', category: 'iOS 11.0');
      await launchSystemLogTool(device);
      verifyNever(mockProcessManager.start(any, environment: null, workingDirectory: null));
    },
    overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });
  });
292 293 294

  group('log reader', () {
    MockProcessManager mockProcessManager;
295
    MockIosProject mockIosProject;
296 297 298

    setUp(() {
      mockProcessManager = new MockProcessManager();
299
      mockIosProject = new MockIosProject();
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    });

    testUsingContext('simulator can output `)`', () async {
      when(mockProcessManager.start(any, environment: null, workingDirectory: null))
          .thenAnswer((Invocation invocation) {
        final Process mockProcess = new MockProcess();
        when(mockProcess.stdout).thenAnswer((Invocation invocation) =>
            new Stream<List<int>>.fromIterable(<List<int>>['''
2017-09-13 15:26:57.228948-0700  localhost Runner[37195]: (Flutter) Observatory listening on http://127.0.0.1:57701/
2017-09-13 15:26:57.228948-0700  localhost Runner[37195]: (Flutter) ))))))))))
2017-09-13 15:26:57.228948-0700  localhost Runner[37195]: (Flutter) #0      Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)'''
                .codeUnits]));
        when(mockProcess.stderr)
            .thenAnswer((Invocation invocation) => const Stream<List<int>>.empty());
        // Delay return of exitCode until after stdout stream data, since it terminates the logger.
        when(mockProcess.exitCode)
316
            .thenAnswer((Invocation invocation) => new Future<int>.delayed(Duration.zero, () => 0));
317
        return new Future<Process>.value(mockProcess);
318
      });
319 320 321

      final IOSSimulator device = new IOSSimulator('123456', category: 'iOS 11.0');
      final DeviceLogReader logReader = device.getLogReader(
322
        app: new BuildableIOSApp(mockIosProject),
323 324 325 326 327 328 329 330 331 332 333 334
      );

      final List<String> lines = await logReader.logLines.toList();
      expect(lines, <String>[
        'Observatory listening on http://127.0.0.1:57701/',
        '))))))))))',
        '#0      Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)',
      ]);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });
  });
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

  group('SimControl', () {
    const int mockPid = 123;
    const String validSimControlOutput = '''
{
  "devices" : {
    "watchOS 4.3" : [
      {
        "state" : "Shutdown",
        "availability" : "(available)",
        "name" : "Apple Watch - 38mm",
        "udid" : "TEST-WATCH-UDID"
      }
    ],
    "iOS 11.4" : [
      {
        "state" : "Booted",
        "availability" : "(available)",
        "name" : "iPhone 5s",
        "udid" : "TEST-PHONE-UDID"
      }
    ],
    "tvOS 11.4" : [
      {
        "state" : "Shutdown",
        "availability" : "(available)",
        "name" : "Apple TV",
        "udid" : "TEST-TV-UDID"
      }
    ]
  }
}
    ''';

    MockProcessManager mockProcessManager;
    SimControl simControl;

    setUp(() {
      mockProcessManager = new MockProcessManager();
      when(mockProcessManager.runSync(any))
          .thenReturn(new ProcessResult(mockPid, 0, validSimControlOutput, ''));

      simControl = new SimControl();
    });

    testUsingContext('getDevices succeeds', () {
      final List<SimDevice> devices = simControl.getDevices();

      final SimDevice watch = devices[0];
      expect(watch.category, 'watchOS 4.3');
      expect(watch.state, 'Shutdown');
      expect(watch.availability, '(available)');
      expect(watch.name, 'Apple Watch - 38mm');
      expect(watch.udid, 'TEST-WATCH-UDID');
      expect(watch.isBooted, isFalse);

      final SimDevice phone = devices[1];
      expect(phone.category, 'iOS 11.4');
      expect(phone.state, 'Booted');
      expect(phone.availability, '(available)');
      expect(phone.name, 'iPhone 5s');
      expect(phone.udid, 'TEST-PHONE-UDID');
      expect(phone.isBooted, isTrue);

      final SimDevice tv = devices[2];
      expect(tv.category, 'tvOS 11.4');
      expect(tv.state, 'Shutdown');
      expect(tv.availability, '(available)');
      expect(tv.name, 'Apple TV');
      expect(tv.udid, 'TEST-TV-UDID');
      expect(tv.isBooted, isFalse);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
      SimControl: () => simControl,
    });
  });
411
}