ios_app_with_extensions_test.dart 10.3 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter 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:convert';
import 'dart:io';

import 'package:flutter_devicelab/framework/framework.dart';
9
import 'package:flutter_devicelab/framework/ios.dart';
10
import 'package:flutter_devicelab/framework/task_result.dart';
11 12
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;
13
import 'package:meta/meta.dart';
14 15 16

Future<void> main() async {
  await task(() async {
17
    section('Copy test Flutter App with watchOS Companion');
18 19 20 21

    String watchDeviceID;
    String phoneDeviceID;
    final Directory tempDir = Directory.systemTemp
22
        .createTempSync('ios_app_with_extensions_test');
23
    final Directory projectDir =
24
        Directory(path.join(tempDir.path, 'app_with_extensions'));
25 26 27 28
    try {
      mkdir(projectDir);
      recursiveCopy(
        Directory(path.join(flutterDirectory.path, 'dev', 'integration_tests',
29
            'ios_app_with_extensions')),
30 31 32 33 34 35 36 37
        projectDir,
      );

      section('Create release build');

      await inDirectory(projectDir, () async {
        await flutter(
          'build',
38
          options: <String>['ios', '--no-codesign', '--release'],
39 40 41
        );
      });

42
      final String appBundle = Directory(path.join(
43 44 45 46 47
        projectDir.path,
        'build',
        'ios',
        'iphoneos',
        'Runner.app',
48
      )).path;
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      final String appFrameworkPath = path.join(
        appBundle,
        'Frameworks',
        'App.framework',
        'App',
      );
      final String flutterFrameworkPath = path.join(
        appBundle,
        'Frameworks',
        'Flutter.framework',
        'Flutter',
      );

      checkDirectoryExists(appBundle);
      await _checkFlutterFrameworkArchs(appFrameworkPath, isSimulator: false);
      await _checkFlutterFrameworkArchs(flutterFrameworkPath, isSimulator: false);

      // Check the watch extension framework added in the Podfile
      // is in place with the expected watch archs.
      final String watchExtensionFrameworkPath = path.join(
        appBundle,
        'Watch',
        'watch.app',
        'PlugIns',
        'watch Extension.appex',
        'Frameworks',
        'EFQRCode.framework',
        'EFQRCode',
      );
      _checkWatchExtensionFrameworkArchs(watchExtensionFrameworkPath);

      section('Clean build');

      await inDirectory(projectDir, () async {
        await flutter('clean');
      });
86 87 88 89 90 91 92 93 94 95

      section('Create debug build');

      await inDirectory(projectDir, () async {
        await flutter(
          'build',
          options: <String>['ios', '--debug', '--no-codesign'],
        );
      });

96 97 98 99
      checkDirectoryExists(appBundle);
      await _checkFlutterFrameworkArchs(appFrameworkPath, isSimulator: false);
      await _checkFlutterFrameworkArchs(flutterFrameworkPath, isSimulator: false);
      _checkWatchExtensionFrameworkArchs(watchExtensionFrameworkPath);
100

101
      section('Clean build');
102

103 104 105 106 107
      await inDirectory(projectDir, () async {
        await flutter('clean');
      });

      section('Run app on simulator device');
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      // Xcode 11.4 simctl create makes the runtime argument optional, and defaults to latest.
      // TODO(jmagman): Remove runtime parsing when devicelab upgrades to Xcode 11.4 https://github.com/flutter/flutter/issues/54889
      final String availableRuntimes = await eval(
        'xcrun',
        <String>[
          'simctl',
          'list',
          'runtimes',
        ],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );

      // Example simctl list:
      //    == Runtimes ==
      //    iOS 10.3 (10.3.1 - 14E8301) - com.apple.CoreSimulator.SimRuntime.iOS-10-3
      //    iOS 13.4 (13.4 - 17E255) - com.apple.CoreSimulator.SimRuntime.iOS-13-4
      //    tvOS 13.4 (13.4 - 17L255) - com.apple.CoreSimulator.SimRuntime.tvOS-13-4
      //    watchOS 6.2 (6.2 - 17T256) - com.apple.CoreSimulator.SimRuntime.watchOS-6-2
      String iOSSimRuntime;
      String watchSimRuntime;

      final RegExp iOSRuntimePattern = RegExp(r'iOS .*\) - (.*)');
      final RegExp watchOSRuntimePattern = RegExp(r'watchOS .*\) - (.*)');

      for (final String runtime in LineSplitter.split(availableRuntimes)) {
        // These seem to be in order, so allow matching multiple lines so it grabs
        // the last (hopefully latest) one.
        final RegExpMatch iOSRuntimeMatch = iOSRuntimePattern.firstMatch(runtime);
        if (iOSRuntimeMatch != null) {
          iOSSimRuntime = iOSRuntimeMatch.group(1).trim();
          continue;
        }
        final RegExpMatch watchOSRuntimeMatch = watchOSRuntimePattern.firstMatch(runtime);
        if (watchOSRuntimeMatch != null) {
          watchSimRuntime = watchOSRuntimeMatch.group(1).trim();
        }
      }
      if (iOSSimRuntime == null || watchSimRuntime == null) {
        String message;
        if (iOSSimRuntime != null) {
          message = 'Found "$iOSSimRuntime", but no watchOS simulator runtime found.';
        } else if (watchSimRuntime != null) {
          message = 'Found "$watchSimRuntime", but no iOS simulator runtime found.';
        } else {
          message = 'watchOS and iOS simulator runtimes not found.';
        }
        return TaskResult.failure('$message Available runtimes:\n$availableRuntimes');
      }

      // Create iOS simulator.
160 161 162 163 164 165 166
      phoneDeviceID = await eval(
        'xcrun',
        <String>[
          'simctl',
          'create',
          'TestFlutteriPhoneWithWatch',
          'com.apple.CoreSimulator.SimDeviceType.iPhone-11',
167
          iOSSimRuntime,
168 169 170 171 172
        ],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );

173
      // Create watchOS simulator.
174 175 176 177 178 179 180
      watchDeviceID = await eval(
        'xcrun',
        <String>[
          'simctl',
          'create',
          'TestFlutterWatch',
          'com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-5-44mm',
181
          watchSimRuntime,
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        ],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );

      // Pair watch with phone.
      await eval(
        'xcrun',
        <String>['simctl', 'pair', watchDeviceID, phoneDeviceID],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );

      // Boot simulator devices.
      await eval(
        'xcrun',
        <String>['simctl', 'bootstatus', phoneDeviceID, '-b'],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );
      await eval(
        'xcrun',
        <String>['simctl', 'bootstatus', watchDeviceID, '-b'],
        canFail: false,
        workingDirectory: flutterDirectory.path,
      );

      // Start app on simulated device.
      final Process process = await startProcess(
          path.join(flutterDirectory.path, 'bin', 'flutter'),
          <String>['run', '-d', phoneDeviceID],
          workingDirectory: projectDir.path);

      process.stdout
          .transform<String>(utf8.decoder)
          .transform<String>(const LineSplitter())
          .listen((String line) {
        print('stdout: $line');
        // Wait for app startup to complete and quit immediately afterwards.
        if (line.startsWith('An Observatory debugger')) {
          process.stdin.write('q');
        }
      });
      process.stderr
          .transform<String>(utf8.decoder)
          .transform<String>(const LineSplitter())
          .listen((String line) {
        print('stderr: $line');
      });

      final int exitCode = await process.exitCode;

234
      if (exitCode != 0) {
235
        return TaskResult.failure(
236
            'Failed to start flutter iOS app with WatchOS companion on simulated device.');
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      }

      final String simulatorAppBundle = Directory(path.join(
        projectDir.path,
        'build',
        'ios',
        'iphonesimulator',
        'Runner.app',
      )).path;

      checkDirectoryExists(simulatorAppBundle);

      final String simulatorAppFrameworkPath = path.join(
        simulatorAppBundle,
        'Frameworks',
        'App.framework',
        'App',
      );
      final String simulatorFlutterFrameworkPath = path.join(
        simulatorAppBundle,
        'Frameworks',
        'Flutter.framework',
        'Flutter',
      );

      await _checkFlutterFrameworkArchs(simulatorAppFrameworkPath, isSimulator: true);
      await _checkFlutterFrameworkArchs(simulatorFlutterFrameworkPath, isSimulator: true);
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

      return TaskResult.success(null);
    } catch (e) {
      return TaskResult.failure(e.toString());
    } finally {
      rmTree(tempDir);
      // Delete simulator devices
      if (watchDeviceID != null && watchDeviceID != '') {
        await eval(
          'xcrun',
          <String>['simctl', 'shutdown', watchDeviceID],
          canFail: true,
          workingDirectory: flutterDirectory.path,
        );
        await eval(
          'xcrun',
          <String>['simctl', 'delete', watchDeviceID],
          canFail: true,
          workingDirectory: flutterDirectory.path,
        );
      }
      if (phoneDeviceID != null && phoneDeviceID != '') {
        await eval(
          'xcrun',
          <String>['simctl', 'shutdown', phoneDeviceID],
          canFail: true,
          workingDirectory: flutterDirectory.path,
        );
        await eval(
          'xcrun',
          <String>['simctl', 'delete', phoneDeviceID],
          canFail: true,
          workingDirectory: flutterDirectory.path,
        );
      }
    }
  });
}
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

Future<void> _checkFlutterFrameworkArchs(String frameworkPath, {
  @required bool isSimulator
}) async {
  checkFileExists(frameworkPath);

  final String archs = await fileType(frameworkPath);
  if (isSimulator == archs.contains('armv7')) {
    throw TaskResult.failure('$frameworkPath armv7 architecture unexpected');
  }

  if (isSimulator == archs.contains('arm64')) {
    throw TaskResult.failure('$frameworkPath arm64 architecture unexpected');
  }

  if (isSimulator != archs.contains('x86_64')) {
    throw TaskResult.failure(
        '$frameworkPath x86_64 architecture unexpected');
  }
}

Future<void> _checkWatchExtensionFrameworkArchs(String frameworkPath) async {
  checkFileExists(frameworkPath);
  final String archs = await fileType(frameworkPath);
  if (!archs.contains('armv7k')) {
    throw TaskResult.failure('$frameworkPath armv7k architecture missing');
  }

  if (!archs.contains('arm64_32')) {
    throw TaskResult.failure('$frameworkPath arm64_32 architecture missing');
  }
}