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

import '../src/common.dart';
16
import 'test_driver.dart';
17

18 19 20 21 22 23 24 25 26
/// 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.
const ProcessManager processManager = LocalProcessManager();

27 28 29
/// 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
30
Directory createResolvedTempDirectorySync(String prefix) {
31
  assert(prefix.endsWith('.'));
32 33
  final Directory tempDirectory = fileSystem.systemTempDirectory.createTempSync('flutter_$prefix');
  return fileSystem.directory(tempDirectory.resolveSymbolicLinksSync());
34 35
}

36
void writeFile(String path, String content) {
37
  fileSystem.file(path)
38
    ..createSync(recursive: true)
39 40
    ..writeAsStringSync(content)
    ..setLastModifiedSync(DateTime.now().add(const Duration(seconds: 10)));
41 42 43
}

void writePackages(String folder) {
44 45
  writeFile(fileSystem.path.join(folder, '.packages'), '''
test:${fileSystem.path.join(fileSystem.currentDirectory.path, 'lib')}/
46 47 48 49
''');
}

void writePubspec(String folder) {
50
  writeFile(fileSystem.path.join(folder, 'pubspec.yaml'), '''
51 52 53 54 55 56 57 58 59
name: test
dependencies:
  flutter:
    sdk: flutter
''');
}

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

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]}',
  ];
}
81 82 83 84 85

Future<void> pollForServiceExtensionValue<T>({
  @required FlutterTestDriver testDriver,
  @required String extension,
  @required T continuePollingValue,
86
  @required Matcher matches,
87 88
  String valueKey = 'value',
}) async {
89
  for (int i = 0; i < 10; i++) {
90
    final Response response = await testDriver.callServiceExtension(extension);
91
    if (response.json[valueKey] as T == continuePollingValue) {
92 93
      await Future<void>.delayed(const Duration(seconds: 1));
    } else {
94 95
      expect(response.json[valueKey] as T, matches);
      return;
96 97
    }
  }
98 99 100 101
  fail(
    'Did not find expected value for service extension \'$extension\'. All call'
    ' attempts responded with \'$continuePollingValue\'.',
  );
102
}