xcode_test.dart 32 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:async';

9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/io.dart' show ProcessException, ProcessResult;
11
import 'package:flutter_tools/src/base/logger.dart';
12
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/cache.dart';
15
import 'package:flutter_tools/src/ios/devices.dart';
16
import 'package:flutter_tools/src/ios/iproxy.dart';
17 18 19 20 21
import 'package:flutter_tools/src/ios/xcodeproj.dart';
import 'package:flutter_tools/src/macos/xcode.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

22 23
import '../../src/common.dart';
import '../../src/context.dart';
24 25

void main() {
26
  BufferLogger logger;
27 28

  setUp(() {
29
    logger = BufferLogger.test();
30 31
  });

32 33 34 35
  // Group exists to work around https://github.com/flutter/flutter/issues/56415.
  // Do not add more `MockProcessManager` tests.
  group('MockProcessManager', () {
    ProcessManager processManager;
36 37

    setUp(() {
38
      processManager = MockProcessManager();
39 40
    });

41 42
    group('Xcode', () {
      Xcode xcode;
43
      MockXcodeProjectInterpreter mockXcodeProjectInterpreter;
44 45

      setUp(() {
46
        mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
47
        xcode = Xcode.test(
48
          processManager: processManager,
49
          xcodeProjectInterpreter: mockXcodeProjectInterpreter,
50 51 52 53 54
        );
      });

      testWithoutContext('xcodeSelectPath returns null when xcode-select is not installed', () {
        when(processManager.runSync(<String>['/usr/bin/xcode-select', '--print-path']))
55
          .thenThrow(const ProcessException('/usr/bin/xcode-select', <String>['--print-path']));
56 57
        expect(xcode.xcodeSelectPath, isNull);
        when(processManager.runSync(<String>['/usr/bin/xcode-select', '--print-path']))
58
          .thenThrow(ArgumentError('Invalid argument(s): Cannot find executable for /usr/bin/xcode-select'));
59

60 61
        expect(xcode.xcodeSelectPath, isNull);
      });
62

63
      testWithoutContext('eulaSigned is false when clang is not installed', () {
64 65
        when(mockXcodeProjectInterpreter.xcrunCommand()).thenReturn(<String>['xcrun']);

66 67
        when(processManager.runSync(<String>['which', 'sysctl']))
            .thenReturn(ProcessResult(1, 0, '', ''));
68 69 70 71
        when(processManager.runSync(<String>['sysctl', 'hw.optional.arm64']))
            .thenReturn(ProcessResult(123, 1, '', ''));
        when(processManager.runSync(<String>['xcrun', 'clang']))
          .thenThrow(const ProcessException('xcrun', <String>['clang']));
72

73 74
        expect(xcode.eulaSigned, isFalse);
      });
75 76
    });

77 78
    group('xcdevice', () {
      XCDevice xcdevice;
79
      Xcode xcode;
80 81

      setUp(() {
82 83 84 85 86 87
        xcode = Xcode.test(
          processManager: FakeProcessManager.any(),
          xcodeProjectInterpreter: XcodeProjectInterpreter.test(
            processManager: FakeProcessManager.any(),
          ),
        );
88 89 90
        xcdevice = XCDevice(
          processManager: processManager,
          logger: logger,
91
          xcode: xcode,
92
          platform: null,
93 94
          artifacts: Artifacts.test(),
          cache: Cache.test(),
95
          iproxy: IProxy.test(logger: logger, processManager: processManager),
96 97
        );
      });
98

99 100 101
      testWithoutContext('available devices xcdevice fails', () async {
        when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
          .thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
102

103
        expect(await xcdevice.getAvailableIOSDevices(), isEmpty);
104
      });
105

106 107 108
      testWithoutContext('diagnostics xcdevice fails', () async {
        when(processManager.run(<String>['xcrun', 'xcdevice', 'list', '--timeout', '2']))
          .thenThrow(const ProcessException('xcrun', <String>['xcdevice', 'list', '--timeout', '2']));
109

110 111
        expect(await xcdevice.getDiagnostics(), isEmpty);
      });
112
    });
113
  });
114

115 116
  group('FakeProcessManager', () {
    FakeProcessManager fakeProcessManager;
117

118 119
    setUp(() {
      fakeProcessManager = FakeProcessManager.list(<FakeCommand>[]);
120 121
    });

122 123 124 125 126
    group('Xcode', () {
      MockXcodeProjectInterpreter mockXcodeProjectInterpreter;

      setUp(() {
        mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
127 128 129
      });

      testWithoutContext('isInstalledAndMeetsVersionCheck is false when not macOS', () {
130
        final Xcode xcode = Xcode.test(
131
          platform: FakePlatform(operatingSystem: 'windows'),
132 133 134
          processManager: fakeProcessManager,
          xcodeProjectInterpreter: mockXcodeProjectInterpreter,
        );
135

136
        expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
137 138
      });

139 140 141 142 143 144 145 146 147 148 149
      testWithoutContext('isSimctlInstalled is true when simctl list succeeds', () {
        when(mockXcodeProjectInterpreter.xcrunCommand()).thenReturn(<String>['xcrun']);
        fakeProcessManager.addCommand(
          const FakeCommand(
            command: <String>[
              'xcrun',
              'simctl',
              'list',
            ],
          ),
        );
150
        final Xcode xcode = Xcode.test(
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
          processManager: fakeProcessManager,
          xcodeProjectInterpreter: mockXcodeProjectInterpreter,
        );

        expect(xcode.isSimctlInstalled, isTrue);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      });

      testWithoutContext('isSimctlInstalled is true when simctl list fails', () {
        when(mockXcodeProjectInterpreter.xcrunCommand()).thenReturn(<String>['xcrun']);
        fakeProcessManager.addCommand(
          const FakeCommand(
            command: <String>[
              'xcrun',
              'simctl',
              'list',
            ],
            exitCode: 1,
          ),
        );
171
        final Xcode xcode = Xcode.test(
172 173 174 175 176 177 178 179
          processManager: fakeProcessManager,
          xcodeProjectInterpreter: mockXcodeProjectInterpreter,
        );

        expect(xcode.isSimctlInstalled, isFalse);
        expect(fakeProcessManager.hasRemainingExpectations, isFalse);
      });

180 181 182 183 184
      group('macOS', () {
        Xcode xcode;

        setUp(() {
          mockXcodeProjectInterpreter = MockXcodeProjectInterpreter();
185
          when(mockXcodeProjectInterpreter.xcrunCommand()).thenReturn(<String>['xcrun']);
186
          xcode = Xcode.test(
187 188 189 190
            processManager: fakeProcessManager,
            xcodeProjectInterpreter: mockXcodeProjectInterpreter,
          );
        });
191

192 193 194 195 196 197
        testWithoutContext('xcodeSelectPath returns path when xcode-select is installed', () {
          const String xcodePath = '/Applications/Xcode8.0.app/Contents/Developer';
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['/usr/bin/xcode-select', '--print-path'],
            stdout: xcodePath,
          ));
198

199 200 201
          expect(xcode.xcodeSelectPath, xcodePath);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
202

203 204 205 206 207
        testWithoutContext('xcodeVersionSatisfactory is false when version is less than minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(9);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
208

209
          expect(xcode.isRequiredVersionSatisfactory, isFalse);
210
        });
211

212 213
        testWithoutContext('xcodeVersionSatisfactory is false when xcodebuild tools are not installed', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);
214

215
          expect(xcode.isRequiredVersionSatisfactory, isFalse);
216
        });
217

218 219 220 221 222
        testWithoutContext('xcodeVersionSatisfactory is true when version meets minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
223

224
          expect(xcode.isRequiredVersionSatisfactory, isTrue);
225
        });
226

227 228 229 230 231
        testWithoutContext('xcodeVersionSatisfactory is true when major version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(12);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
232

233
          expect(xcode.isRequiredVersionSatisfactory, isTrue);
234
        });
235

236 237 238 239 240
        testWithoutContext('xcodeVersionSatisfactory is true when minor version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(3);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
241

242
          expect(xcode.isRequiredVersionSatisfactory, isTrue);
243
        });
244

245 246 247 248 249
        testWithoutContext('xcodeVersionSatisfactory is true when patch version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(1);
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          expect(xcode.isRequiredVersionSatisfactory, isTrue);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is false when version is less than minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(9);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);

          expect(xcode.isRecommendedVersionSatisfactory, isFalse);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is false when xcodebuild tools are not installed', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);

          expect(xcode.isRecommendedVersionSatisfactory, isFalse);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is true when version meets minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(12);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(1);

          expect(xcode.isRecommendedVersionSatisfactory, isTrue);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is true when major version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(13);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);

          expect(xcode.isRecommendedVersionSatisfactory, isTrue);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is true when minor version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(12);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(3);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);

          expect(xcode.isRecommendedVersionSatisfactory, isTrue);
        });

        testWithoutContext('isRecommendedVersionSatisfactory is true when patch version exceeds minimum', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(12);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(2);

          expect(xcode.isRecommendedVersionSatisfactory, isTrue);
303
        });
304

305 306
        testWithoutContext('isInstalledAndMeetsVersionCheck is false when not installed', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(false);
307

308 309 310
          expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
311

312 313 314 315 316
        testWithoutContext('isInstalledAndMeetsVersionCheck is false when version not satisfied', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(10);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(2);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);
317

318
          expect(xcode.isInstalledAndMeetsVersionCheck, isFalse);
319 320 321
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

322 323 324 325 326 327 328 329 330
        testWithoutContext('isInstalledAndMeetsVersionCheck is true when macOS and installed and version is satisfied', () {
          when(mockXcodeProjectInterpreter.isInstalled).thenReturn(true);
          when(mockXcodeProjectInterpreter.majorVersion).thenReturn(11);
          when(mockXcodeProjectInterpreter.minorVersion).thenReturn(0);
          when(mockXcodeProjectInterpreter.patchVersion).thenReturn(0);

          expect(xcode.isInstalledAndMeetsVersionCheck, isTrue);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
331

332 333 334 335 336 337 338 339 340 341 342
        testWithoutContext('eulaSigned is false when clang output indicates EULA not yet accepted', () {
          fakeProcessManager.addCommands(const <FakeCommand>[
            FakeCommand(
              command: <String>['xcrun', 'clang'],
              exitCode: 1,
              stderr:
                  'Xcode EULA has not been accepted.\nLaunch Xcode and accept the license.',
            ),
          ]);

          expect(xcode.eulaSigned, isFalse);
343 344
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

        testWithoutContext('eulaSigned is true when clang output indicates EULA has been accepted', () {
          fakeProcessManager.addCommands(
            const <FakeCommand>[
              FakeCommand(
                command: <String>['xcrun', 'clang'],
                exitCode: 1,
                stderr: 'clang: error: no input files',
              ),
            ],
          );
          expect(xcode.eulaSigned, isTrue);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

        testWithoutContext('SDK name', () {
361 362
          expect(getSDKNameForIOSEnvironmentType(EnvironmentType.physical), 'iphoneos');
          expect(getSDKNameForIOSEnvironmentType(EnvironmentType.simulator), 'iphonesimulator');
363 364 365 366 367 368 369 370 371 372 373
        });

        group('SDK location', () {
          const String sdkroot = 'Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk';

          testWithoutContext('--show-sdk-path iphoneos', () async {
            fakeProcessManager.addCommand(const FakeCommand(
              command: <String>['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'],
              stdout: sdkroot,
            ));

374
            expect(await xcode.sdkLocation(EnvironmentType.physical), sdkroot);
375 376 377 378 379 380 381 382 383 384
            expect(fakeProcessManager.hasRemainingExpectations, isFalse);
          });

          testWithoutContext('--show-sdk-path fails', () async {
            fakeProcessManager.addCommand(const FakeCommand(
              command: <String>['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'],
              exitCode: 1,
              stderr: 'xcrun: error:',
            ));

385
            expect(() async => await xcode.sdkLocation(EnvironmentType.physical),
386 387 388 389
              throwsToolExit(message: 'Could not find SDK location'));
            expect(fakeProcessManager.hasRemainingExpectations, isFalse);
          });
        });
390 391 392
      });
    });

393
    group('xcdevice not installed', () {
394
      XCDevice xcdevice;
395
      Xcode xcode;
396 397

      setUp(() {
398 399 400 401 402 403 404
        xcode = Xcode.test(
          processManager: FakeProcessManager.any(),
          xcodeProjectInterpreter: XcodeProjectInterpreter.test(
            processManager: FakeProcessManager.any(),
            majorVersion: null, // Not installed.
          ),
        );
405 406 407
        xcdevice = XCDevice(
          processManager: fakeProcessManager,
          logger: logger,
408
          xcode: xcode,
409
          platform: null,
410 411
          artifacts: Artifacts.test(),
          cache: Cache.test(),
412
          iproxy: IProxy.test(logger: logger, processManager: fakeProcessManager),
413
        );
414 415
      });

416 417 418 419 420 421 422 423
      testWithoutContext('Xcode not installed', () async {
        expect(xcode.isInstalled, false);

        expect(xcdevice.isInstalled, false);
        expect(xcdevice.observedDeviceEvents(), isNull);
        expect(logger.traceText, contains("Xcode not found. Run 'flutter doctor' for more information."));
        expect(await xcdevice.getAvailableIOSDevices(), isEmpty);
        expect(await xcdevice.getDiagnostics(), isEmpty);
424
      });
425
    });
426

427 428 429
    group('xcdevice', () {
      XCDevice xcdevice;
      Xcode xcode;
430

431 432 433 434 435 436 437 438 439 440 441 442
      setUp(() {
        xcode = Xcode.test(processManager: FakeProcessManager.any());
        xcdevice = XCDevice(
          processManager: fakeProcessManager,
          logger: logger,
          xcode: xcode,
          platform: null,
          artifacts: Artifacts.test(),
          cache: Cache.test(),
          iproxy: IProxy.test(logger: logger, processManager: fakeProcessManager),
        );
      });
443

444
      group('observe device events', () {
445 446 447 448 449 450 451 452 453 454 455 456
        testUsingContext('relays events', () async {
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>[
              'script',
              '-t',
              '0',
              '/dev/null',
              'xcrun',
              'xcdevice',
              'observe',
              '--both',
            ], stdout: 'Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418\n'
457 458
              'Attach: 00008027-00192736010F802E\n'
              'Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418',
459 460 461
            stderr: 'Some error',
          ));

462 463 464
          final Completer<void> attach1 = Completer<void>();
          final Completer<void> attach2 = Completer<void>();
          final Completer<void> detach1 = Completer<void>();
465 466

          // Attach: d83d5bc53967baa0ee18626ba87b6254b2ab5418
467
          // Attach: 00008027-00192736010F802E
468 469 470 471
          // Detach: d83d5bc53967baa0ee18626ba87b6254b2ab5418
          xcdevice.observedDeviceEvents().listen((Map<XCDeviceEvent, String> event) {
            expect(event.length, 1);
            if (event.containsKey(XCDeviceEvent.attach)) {
472 473 474 475 476 477
              if (event[XCDeviceEvent.attach] == 'd83d5bc53967baa0ee18626ba87b6254b2ab5418') {
                attach1.complete();
              } else
              if (event[XCDeviceEvent.attach] == '00008027-00192736010F802E') {
                attach2.complete();
              }
478 479
            } else if (event.containsKey(XCDeviceEvent.detach)) {
              expect(event[XCDeviceEvent.detach], 'd83d5bc53967baa0ee18626ba87b6254b2ab5418');
480
              detach1.complete();
481 482 483 484
            } else {
              fail('Unexpected event');
            }
          });
485 486 487
          await attach1.future;
          await attach2.future;
          await detach1.future;
488 489 490 491
          expect(logger.traceText, contains('xcdevice observe error: Some error'));
        });
      });

492 493 494 495
      group('available devices', () {
        final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
        testUsingContext('returns devices', () async {
          const String devicesOutput = '''
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
[
  {
    "simulator" : true,
    "operatingSystemVersion" : "13.3 (17K446)",
    "available" : true,
    "platform" : "com.apple.platform.appletvsimulator",
    "modelCode" : "AppleTV5,3",
    "identifier" : "CBB5E1ED-2172-446E-B4E7-F2B5823DBBA6",
    "architecture" : "x86_64",
    "modelName" : "Apple TV",
    "name" : "Apple TV"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
515
    "identifier" : "00008027-00192736010F802E",
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "An iPhone (Space Gray)"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44",
    "architecture" : "armv7",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "iPad 1"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "network",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "234234234234234234345445687594e089dede3c44",
    "architecture" : "arm64",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "A networked iPad"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "10.1 (14C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPad11,4",
    "identifier" : "f577a7903cc54959be2e34bc4f7f80b7009efcf4",
    "architecture" : "BOGUS",
    "modelName" : "iPad Air 3rd Gen",
    "name" : "iPad 2"
  },
  {
    "simulator" : true,
    "operatingSystemVersion" : "6.1.1 (17S445)",
    "available" : true,
    "platform" : "com.apple.platform.watchsimulator",
    "modelCode" : "Watch5,4",
    "identifier" : "2D74FB11-88A0-44D0-B81E-C0C142B1C94A",
    "architecture" : "i386",
    "modelName" : "Apple Watch Series 5 - 44mm",
    "name" : "Apple Watch Series 5 - 44mm"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "c4ca6f7a53027d1b7e4972e28478e7a28e2faee2",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -9,
      "failureReason" : "",
      "description" : "iPhone is not paired with your computer.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

588 589 590 591
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
592
          final List<IOSDevice> devices = await xcdevice.getAvailableIOSDevices();
593
          expect(devices, hasLength(3));
594
          expect(devices[0].id, '00008027-00192736010F802E');
595 596 597 598 599 600 601 602 603 604 605 606 607 608
          expect(devices[0].name, 'An iPhone (Space Gray)');
          expect(await devices[0].sdkNameAndVersion, 'iOS 13.3');
          expect(devices[0].cpuArchitecture, DarwinArch.arm64);
          expect(devices[1].id, '98206e7a4afd4aedaff06e687594e089dede3c44');
          expect(devices[1].name, 'iPad 1');
          expect(await devices[1].sdkNameAndVersion, 'iOS 10.1');
          expect(devices[1].cpuArchitecture, DarwinArch.armv7);
          expect(devices[2].id, 'f577a7903cc54959be2e34bc4f7f80b7009efcf4');
          expect(devices[2].name, 'iPad 2');
          expect(await devices[2].sdkNameAndVersion, 'iOS 10.1');
          expect(devices[2].cpuArchitecture, DarwinArch.arm64); // Defaults to arm64 for unknown architecture.
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
609
          Artifacts: () => Artifacts.test(),
610 611 612 613 614 615 616
        });

        testWithoutContext('uses timeout', () async {
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '20'],
            stdout: '[]',
          ));
617
          await xcdevice.getAvailableIOSDevices(timeout: const Duration(seconds: 20));
618 619 620 621 622
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        });

        testUsingContext('ignores "Preparing debugger support for iPhone" error', () async {
          const String devicesOutput = '''
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -10,
      "failureReason" : "",
      "description" : "iPhone is busy: Preparing debugger support for iPhone",
      "recoverySuggestion" : "Xcode will continue when iPhone is finished.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

646 647 648 649
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
650
          final List<IOSDevice> devices = await xcdevice.getAvailableIOSDevices();
651 652 653 654 655
          expect(devices, hasLength(1));
          expect(devices[0].id, '43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7');
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
656
          Artifacts: () => Artifacts.test(),
657 658 659 660
        });

        testUsingContext('handles unknown architectures', () async {
          const String devicesOutput = '''
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "armv7x",
    "modelName" : "iPad 3 BOGUS",
    "name" : "iPad"
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : true,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "BOGUS",
    "modelName" : "Future iPad",
    "name" : "iPad"
  }
]
''';

689 690 691 692
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));
693
          final List<IOSDevice> devices = await xcdevice.getAvailableIOSDevices();
694 695 696 697 698
          expect(devices[0].cpuArchitecture, DarwinArch.armv7);
          expect(devices[1].cpuArchitecture, DarwinArch.arm64);
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
699
          Artifacts: () => Artifacts.test(),
700
        });
701
      });
702

703 704 705 706
      group('diagnostics', () {
        final FakePlatform macPlatform = FakePlatform(operatingSystem: 'macos');
        testUsingContext('uses cache', () async {
          const String devicesOutput = '''
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
[
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "network",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "error" : {
      "code" : -13,
      "failureReason" : "",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

727 728 729 730 731
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));

732
          await xcdevice.getAvailableIOSDevices();
733 734 735 736 737 738 739 740 741
          final List<String> errors = await xcdevice.getDiagnostics();
          expect(errors, hasLength(1));
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });

        testUsingContext('returns error message', () async {
          const String devicesOutput = '''
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
[
   {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "98206e7a4afd4aedaff06e687594e089dede3c44",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "An iPhone (Space Gray)",
    "error" : {
      "code" : -9,
      "failureReason" : "",
      "underlyingErrors" : [
        {
          "code" : 5,
760
          "failureReason" : "allowsSecureServices: 1. isConnected: 0. Platform: <DVTPlatform:0x7f804ce32880:'com.apple.platform.iphoneos':<DVTFilePath:0x7f804ce32800:'/Users/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform'>>. DTDKDeviceIdentifierIsIDID: 0",
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
          "description" : "📱<DVTiOSDevice (0x7f801f190450), iPhone, iPhone, 13.3 (17C54), d83d5bc53967baa0ee18626ba87b6254b2ab5418> -- Failed _shouldMakeReadyForDevelopment check even though device is not locked by passcode.",
          "recoverySuggestion" : "",
          "domain" : "com.apple.platform.iphoneos"
        }
      ],
      "description" : "iPhone is not paired with your computer.",
      "recoverySuggestion" : "To use iPhone with Xcode, unlock it and choose to trust this computer when prompted.",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "failureReason" : "",
      "description" : "iPhone is not paired with your computer",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "network",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "d83d5bc53967baa0ee18626ba87b6254b2ab5418",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "error" : {
      "code" : -13,
      "failureReason" : "",
      "domain" : "com.apple.platform.iphoneos"
    }
  },
  {
    "simulator" : false,
    "operatingSystemVersion" : "13.3 (17C54)",
    "interface" : "usb",
    "available" : false,
    "platform" : "com.apple.platform.iphoneos",
    "modelCode" : "iPhone8,1",
    "identifier" : "43ad2fda7991b34fe1acbda82f9e2fd3d6ddc9f7",
    "architecture" : "arm64",
    "modelName" : "iPhone 6s",
    "name" : "iPhone",
    "error" : {
      "code" : -10,
      "failureReason" : "",
      "description" : "iPhone is busy: Preparing debugger support for iPhone",
      "recoverySuggestion" : "Xcode will continue when iPhone is finished.",
      "domain" : "com.apple.platform.iphoneos"
    }
  }
]
''';

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
          fakeProcessManager.addCommand(const FakeCommand(
            command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
            stdout: devicesOutput,
          ));

          final List<String> errors = await xcdevice.getDiagnostics();
          expect(errors, hasLength(4));
          expect(errors[0], 'Error: iPhone is not paired with your computer. To use iPhone with Xcode, unlock it and choose to trust this computer when prompted. (code -9)');
          expect(errors[1], 'Error: iPhone is not paired with your computer.');
          expect(errors[2], 'Error: Xcode pairing error. (code -13)');
          expect(errors[3], 'Error: iPhone is busy: Preparing debugger support for iPhone. Xcode will continue when iPhone is finished. (code -10)');
          expect(fakeProcessManager.hasRemainingExpectations, isFalse);
        }, overrides: <Type, Generator>{
          Platform: () => macPlatform,
        });
841 842
      });
    });
843 844
  });
}
845

846 847
class MockProcessManager extends Mock implements ProcessManager {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}