flavors_test_ios.dart 2.95 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 7 8
import 'dart:io';
import 'dart:typed_data';

import 'package:collection/collection.dart';
9
import 'package:flutter_devicelab/framework/devices.dart';
10
import 'package:flutter_devicelab/framework/framework.dart';
11
import 'package:flutter_devicelab/framework/task_result.dart';
12
import 'package:flutter_devicelab/framework/utils.dart';
13
import 'package:flutter_devicelab/tasks/integration_tests.dart';
14 15
import 'package:path/path.dart' as path;
import 'package:standard_message_codec/standard_message_codec.dart';
16

17
Future<void> main() async {
18
  deviceOperatingSystem = DeviceOperatingSystem.ios;
19 20 21
  await task(() async {
    await createFlavorsTest().call();
    await createIntegrationTestFlavorsTest().call();
22
    // test install and uninstall of flavors app
23
    final String projectDir = '${flutterDirectory.path}/dev/integration_tests/flavors';
24
    final TaskResult installTestsResult = await inDirectory(
25
      projectDir,
26
      () async {
27 28 29 30 31 32 33 34 35 36
        final List<TaskResult> testResults = <TaskResult>[
            await _testInstallDebugPaidFlavor(projectDir),
            await _testInstallBogusFlavor(),
        ];

        final TaskResult? firstInstallFailure = testResults
          .firstWhereOrNull((TaskResult element) => element.failed);

        if (firstInstallFailure != null) {
          return firstInstallFailure;
37
        }
38

39 40 41 42 43
        return TaskResult.success(null);
      },
    );

    return installTestsResult;
44
  });
45
}
46 47 48 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 86 87 88 89 90 91 92 93 94 95 96 97 98

Future<TaskResult> _testInstallDebugPaidFlavor(String projectDir) async {
  await evalFlutter(
    'install',
    options: <String>['--flavor', 'paid'],
  );
  final Uint8List assetManifestFileData = File(
    path.join(
      projectDir,
      'build',
      'ios',
      'iphoneos',
      'Paid App.app',
      'Frameworks',
      'App.framework',
      'flutter_assets',
      'AssetManifest.bin',
    ),
  ).readAsBytesSync();

  final Map<Object?, Object?> assetManifest = const StandardMessageCodec()
    .decodeMessage(ByteData.sublistView(assetManifestFileData)) as Map<Object?, Object?>;

  if (assetManifest.containsKey('assets/free/free.txt')) {
    return TaskResult.failure('Assets declared with a flavor not equal to the '
      'argued --flavor value should not be bundled.');
  }

  await flutter(
    'install',
    options: <String>['--flavor', 'paid', '--uninstall-only'],
  );

  return TaskResult.success(null);
}

Future<TaskResult> _testInstallBogusFlavor() async {
  final StringBuffer stderr = StringBuffer();
  await evalFlutter(
    'install',
    canFail: true,
    stderr: stderr,
    options: <String>['--flavor', 'bogus'],
  );

  final String stderrString = stderr.toString();
  if (!stderrString.contains('The Xcode project defines schemes: free, paid')) {
    print(stderrString);
    return TaskResult.failure('Should not succeed with bogus flavor');
  }

  return TaskResult.success(null);
}