test_utils.dart 4.35 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
import 'package:file/file.dart';
import 'package:file/local.dart';
7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/base/platform.dart';
9
import 'package:process/process.dart';
10
import 'package:vm_service/vm_service.dart';
11 12

import '../src/common.dart';
13
import 'test_driver.dart';
14

15 16 17 18 19 20 21
/// The [FileSystem] for the integration test environment.
const FileSystem fileSystem = LocalFileSystem();

/// The [Platform] for the integration test environment.
const Platform platform = LocalPlatform();

/// The [ProcessManager] for the integration test environment.
22
const ProcessManager processManager = LocalProcessManager();
23

24 25 26
/// Creates a temporary directory but resolves any symlinks to return the real
/// underlying path to avoid issues with breakpoints/hot reload.
/// https://github.com/flutter/flutter/pull/21741
27
Directory createResolvedTempDirectorySync(String prefix) {
28
  assert(prefix.endsWith('.'));
29 30
  final Directory tempDirectory = fileSystem.systemTempDirectory.createTempSync('flutter_$prefix');
  return fileSystem.directory(tempDirectory.resolveSymbolicLinksSync());
31 32
}

33 34
void writeFile(String path, String content, {bool writeFutureModifiedDate = false}) {
  final File file = fileSystem.file(path)
35
    ..createSync(recursive: true)
36
    ..writeAsStringSync(content, flush: true);
37 38 39 40 41 42
    // Some integration tests on Windows to not see this file as being modified
    // recently enough for the hot reload to pick this change up unless the
    // modified time is written in the future.
    if (writeFutureModifiedDate) {
      file.setLastModifiedSync(DateTime.now().add(const Duration(seconds: 5)));
    }
43 44
}

45 46 47
void writeBytesFile(String path, List<int> content) {
  fileSystem.file(path)
    ..createSync(recursive: true)
48
    ..writeAsBytesSync(content);
49 50
}

51
void writePackages(String folder) {
52 53
  writeFile(fileSystem.path.join(folder, '.packages'), '''
test:${fileSystem.path.join(fileSystem.currentDirectory.path, 'lib')}/
54 55 56 57 58
''');
}

Future<void> getPackages(String folder) async {
  final List<String> command = <String>[
59
    fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'),
60
    'pub',
61
    'get',
62
  ];
63
  final ProcessResult result = await processManager.run(command, workingDirectory: folder);
64 65
  if (result.exitCode != 0) {
    throw Exception('flutter pub get failed: ${result.stderr}\n${result.stdout}');
66
  }
67
}
68 69 70 71 72 73 74 75 76 77 78 79

const String kLocalEngineEnvironment = 'FLUTTER_LOCAL_ENGINE';
const String kLocalEngineLocation = 'FLUTTER_LOCAL_ENGINE_SRC_PATH';

List<String> getLocalEngineArguments() {
  return <String>[
    if (platform.environment.containsKey(kLocalEngineEnvironment))
      '--local-engine=${platform.environment[kLocalEngineEnvironment]}',
    if (platform.environment.containsKey(kLocalEngineLocation))
      '--local-engine-src-path=${platform.environment[kLocalEngineLocation]}',
  ];
}
80 81

Future<void> pollForServiceExtensionValue<T>({
82 83 84 85
  required FlutterTestDriver testDriver,
  required String extension,
  required T continuePollingValue,
  required Matcher matches,
86 87
  String valueKey = 'value',
}) async {
88
  for (int i = 0; i < 10; i++) {
89
    final Response response = await testDriver.callServiceExtension(extension);
90
    if (response.json?[valueKey] as T == continuePollingValue) {
91 92
      await Future<void>.delayed(const Duration(seconds: 1));
    } else {
93
      expect(response.json?[valueKey] as T, matches);
94
      return;
95 96
    }
  }
97
  fail(
98 99
    "Did not find expected value for service extension '$extension'. All call"
    " attempts responded with '$continuePollingValue'.",
100
  );
101
}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

class AppleTestUtils {
  // static only
  AppleTestUtils._();

  static const List<String> requiredSymbols = <String>[
    '_kDartIsolateSnapshotData',
    '_kDartIsolateSnapshotInstructions',
    '_kDartVmSnapshotData',
    '_kDartVmSnapshotInstructions'
  ];

  static List<String> getExportedSymbols(String dwarfPath) {
    final ProcessResult nm = processManager.runSync(
      <String>[
        'nm',
        '--debug-syms',  // nm docs: 'Show all symbols, even debugger only'
        '--defined-only',
        '--just-symbol-name',
        dwarfPath,
        '-arch',
        'arm64',
      ],
    );
    final String nmOutput = (nm.stdout as String).trim();
    return nmOutput.isEmpty ? const <String>[] : nmOutput.split('\n');
  }
}