2020-11-06 01:28:47 +00:00
|
|
|
// 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:async';
|
|
|
|
import 'dart:developer' as developer;
|
|
|
|
import 'dart:ui';
|
|
|
|
|
2020-11-14 03:39:05 +00:00
|
|
|
import 'package:flutter/rendering.dart';
|
2021-01-16 04:05:10 +00:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
2020-11-06 01:28:47 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:vm_service/vm_service.dart' as vm;
|
|
|
|
import 'package:vm_service/vm_service_io.dart' as vm_io;
|
|
|
|
|
2021-01-16 04:05:10 +00:00
|
|
|
import '_callback_io.dart' if (dart.library.html) '_callback_web.dart' as driver_actions;
|
2020-11-06 01:28:47 +00:00
|
|
|
import '_extension_io.dart' if (dart.library.html) '_extension_web.dart';
|
|
|
|
import 'common.dart';
|
2020-11-14 03:39:05 +00:00
|
|
|
|
2021-01-16 04:05:10 +00:00
|
|
|
const String _success = 'success';
|
2020-11-06 01:28:47 +00:00
|
|
|
|
|
|
|
/// A subclass of [LiveTestWidgetsFlutterBinding] that reports tests results
|
|
|
|
/// on a channel to adapt them to native instrumentation test format.
|
2021-01-16 04:05:10 +00:00
|
|
|
class IntegrationTestWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding implements IntegrationTestResults {
|
|
|
|
/// Sets up a listener to report that the tests are finished when everything is
|
|
|
|
/// torn down.
|
2020-11-06 01:28:47 +00:00
|
|
|
IntegrationTestWidgetsFlutterBinding() {
|
2021-01-16 04:05:10 +00:00
|
|
|
// TODO(jackson): Report test results as they arrive
|
2020-11-06 01:28:47 +00:00
|
|
|
tearDownAll(() async {
|
2021-01-16 04:05:10 +00:00
|
|
|
try {
|
|
|
|
// For web integration tests we are not using the
|
|
|
|
// `plugins.flutter.io/integration_test`. Mark the tests as complete
|
|
|
|
// before invoking the channel.
|
|
|
|
if (kIsWeb) {
|
|
|
|
if (!_allTestsPassed.isCompleted) {
|
|
|
|
_allTestsPassed.complete(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callbackManager.cleanup();
|
|
|
|
await _channel.invokeMethod<void>(
|
|
|
|
'allTestsFinished',
|
|
|
|
<String, dynamic>{
|
2021-01-29 23:46:21 +00:00
|
|
|
'results': results.map((String name, Object result) {
|
2021-01-16 04:05:10 +00:00
|
|
|
if (result is Failure) {
|
|
|
|
return MapEntry<String, dynamic>(name, result.details);
|
|
|
|
}
|
|
|
|
return MapEntry<String, Object>(name, result);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} on MissingPluginException {
|
|
|
|
print('Warning: integration_test test plugin was not detected.');
|
|
|
|
}
|
|
|
|
if (!_allTestsPassed.isCompleted) {
|
|
|
|
_allTestsPassed.complete(true);
|
|
|
|
}
|
2020-11-06 01:28:47 +00:00
|
|
|
});
|
|
|
|
|
2021-01-16 04:05:10 +00:00
|
|
|
// TODO(jackson): Report the results individually instead of all at once
|
|
|
|
// See https://github.com/flutter/flutter/issues/38985
|
2020-11-06 01:28:47 +00:00
|
|
|
final TestExceptionReporter oldTestExceptionReporter = reportTestException;
|
2021-01-16 04:05:10 +00:00
|
|
|
reportTestException =
|
|
|
|
(FlutterErrorDetails details, String testDescription) {
|
|
|
|
results[testDescription] = Failure(testDescription, details.toString());
|
|
|
|
if (!_allTestsPassed.isCompleted) {
|
|
|
|
_allTestsPassed.complete(false);
|
|
|
|
}
|
2020-11-06 01:28:47 +00:00
|
|
|
oldTestExceptionReporter(details, testDescription);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get overrideHttpClient => false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get registerTestTextInput => false;
|
|
|
|
|
2021-01-29 23:46:21 +00:00
|
|
|
Size _surfaceSize;
|
2020-11-06 01:28:47 +00:00
|
|
|
|
|
|
|
// This flag is used to print warning messages when tracking performance
|
|
|
|
// under debug mode.
|
|
|
|
static bool _firstRun = false;
|
|
|
|
|
|
|
|
/// Artificially changes the surface size to `size` on the Widget binding,
|
|
|
|
/// then flushes microtasks.
|
|
|
|
///
|
|
|
|
/// Set to null to use the default surface size.
|
|
|
|
@override
|
2021-01-29 23:46:21 +00:00
|
|
|
Future<void> setSurfaceSize(Size size) {
|
2020-11-06 01:28:47 +00:00
|
|
|
return TestAsyncUtils.guard<void>(() async {
|
|
|
|
assert(inTest);
|
|
|
|
if (_surfaceSize == size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_surfaceSize = size;
|
|
|
|
handleMetricsChanged();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ViewConfiguration createViewConfiguration() {
|
|
|
|
final double devicePixelRatio = window.devicePixelRatio;
|
|
|
|
final Size size = _surfaceSize ?? window.physicalSize / devicePixelRatio;
|
|
|
|
return TestViewConfiguration(
|
|
|
|
size: size,
|
|
|
|
window: window,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Completer<bool> get allTestsPassed => _allTestsPassed;
|
|
|
|
final Completer<bool> _allTestsPassed = Completer<bool>();
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Failure> get failureMethodsDetails => _failures;
|
|
|
|
|
|
|
|
/// Similar to [WidgetsFlutterBinding.ensureInitialized].
|
|
|
|
///
|
|
|
|
/// Returns an instance of the [IntegrationTestWidgetsFlutterBinding], creating and
|
|
|
|
/// initializing it if necessary.
|
|
|
|
static WidgetsBinding ensureInitialized() {
|
|
|
|
if (WidgetsBinding.instance == null) {
|
|
|
|
IntegrationTestWidgetsFlutterBinding();
|
|
|
|
}
|
|
|
|
assert(WidgetsBinding.instance is IntegrationTestWidgetsFlutterBinding);
|
2021-01-29 23:46:21 +00:00
|
|
|
return WidgetsBinding.instance;
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MethodChannel _channel =
|
|
|
|
MethodChannel('plugins.flutter.io/integration_test');
|
|
|
|
|
|
|
|
/// Test results that will be populated after the tests have completed.
|
2021-01-16 04:05:10 +00:00
|
|
|
///
|
|
|
|
/// Keys are the test descriptions, and values are either [_success] or
|
|
|
|
/// a [Failure].
|
2020-11-06 01:28:47 +00:00
|
|
|
@visibleForTesting
|
2021-01-16 04:05:10 +00:00
|
|
|
Map<String, Object> results = <String, Object>{};
|
2020-11-06 01:28:47 +00:00
|
|
|
|
|
|
|
List<Failure> get _failures => results.values.whereType<Failure>().toList();
|
|
|
|
|
|
|
|
/// The extra data for the reported result.
|
|
|
|
///
|
|
|
|
/// The values in `reportData` must be json-serializable objects or `null`.
|
|
|
|
/// If it's `null`, no extra data is attached to the result.
|
|
|
|
///
|
|
|
|
/// The default value is `null`.
|
|
|
|
@override
|
2021-01-29 23:46:21 +00:00
|
|
|
Map<String, dynamic> reportData;
|
2020-11-06 01:28:47 +00:00
|
|
|
|
|
|
|
/// Manages callbacks received from driver side and commands send to driver
|
|
|
|
/// side.
|
|
|
|
final CallbackManager callbackManager = driver_actions.callbackManager;
|
|
|
|
|
|
|
|
/// Taking a screenshot.
|
|
|
|
///
|
|
|
|
/// Called by test methods. Implementation differs for each platform.
|
|
|
|
Future<void> takeScreenshot(String screenshotName) async {
|
|
|
|
await callbackManager.takeScreenshot(screenshotName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The callback function to response the driver side input.
|
|
|
|
@visibleForTesting
|
|
|
|
Future<Map<String, dynamic>> callback(Map<String, String> params) async {
|
|
|
|
return await callbackManager.callback(
|
|
|
|
params, this /* as IntegrationTestResults */);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emulates the Flutter driver extension, returning 'pass' or 'fail'.
|
|
|
|
@override
|
|
|
|
void initServiceExtensions() {
|
|
|
|
super.initServiceExtensions();
|
|
|
|
|
|
|
|
if (kIsWeb) {
|
|
|
|
registerWebServiceExtension(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerServiceExtension(name: 'driver', callback: callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> runTest(
|
|
|
|
Future<void> testBody(),
|
|
|
|
VoidCallback invariantTester, {
|
|
|
|
String description = '',
|
2021-01-29 23:46:21 +00:00
|
|
|
Duration timeout,
|
2020-11-06 01:28:47 +00:00
|
|
|
}) async {
|
|
|
|
await super.runTest(
|
|
|
|
testBody,
|
|
|
|
invariantTester,
|
|
|
|
description: description,
|
|
|
|
timeout: timeout,
|
|
|
|
);
|
2021-01-16 04:05:10 +00:00
|
|
|
results[description] ??= _success;
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:46:21 +00:00
|
|
|
vm.VmService _vmService;
|
2020-11-06 01:28:47 +00:00
|
|
|
|
|
|
|
/// Initialize the [vm.VmService] settings for the timeline.
|
|
|
|
@visibleForTesting
|
|
|
|
Future<void> enableTimeline({
|
|
|
|
List<String> streams = const <String>['all'],
|
2021-01-29 23:46:21 +00:00
|
|
|
@visibleForTesting vm.VmService vmService,
|
2020-11-06 01:28:47 +00:00
|
|
|
}) async {
|
2021-01-29 23:46:21 +00:00
|
|
|
assert(streams != null);
|
2020-11-06 01:28:47 +00:00
|
|
|
assert(streams.isNotEmpty);
|
|
|
|
if (vmService != null) {
|
|
|
|
_vmService = vmService;
|
|
|
|
}
|
|
|
|
if (_vmService == null) {
|
|
|
|
final developer.ServiceProtocolInfo info =
|
|
|
|
await developer.Service.getInfo();
|
|
|
|
assert(info.serverUri != null);
|
|
|
|
_vmService = await vm_io.vmServiceConnectUri(
|
2021-01-29 23:46:21 +00:00
|
|
|
'ws://localhost:${info.serverUri.port}${info.serverUri.path}ws',
|
2020-11-06 01:28:47 +00:00
|
|
|
);
|
|
|
|
}
|
2021-01-29 23:46:21 +00:00
|
|
|
await _vmService.setVMTimelineFlags(streams);
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs [action] and returns a [vm.Timeline] trace for it.
|
|
|
|
///
|
|
|
|
/// Waits for the `Future` returned by [action] to complete prior to stopping
|
|
|
|
/// the trace.
|
|
|
|
///
|
|
|
|
/// The `streams` parameter limits the recorded timeline event streams to only
|
|
|
|
/// the ones listed. By default, all streams are recorded.
|
|
|
|
/// See `timeline_streams` in
|
|
|
|
/// [Dart-SDK/runtime/vm/timeline.cc](https://github.com/dart-lang/sdk/blob/master/runtime/vm/timeline.cc)
|
|
|
|
///
|
|
|
|
/// If [retainPriorEvents] is true, retains events recorded prior to calling
|
|
|
|
/// [action]. Otherwise, prior events are cleared before calling [action]. By
|
|
|
|
/// default, prior events are cleared.
|
|
|
|
Future<vm.Timeline> traceTimeline(
|
|
|
|
Future<dynamic> action(), {
|
|
|
|
List<String> streams = const <String>['all'],
|
|
|
|
bool retainPriorEvents = false,
|
|
|
|
}) async {
|
|
|
|
await enableTimeline(streams: streams);
|
|
|
|
if (retainPriorEvents) {
|
|
|
|
await action();
|
2021-01-29 23:46:21 +00:00
|
|
|
return await _vmService.getVMTimeline();
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:46:21 +00:00
|
|
|
await _vmService.clearVMTimeline();
|
|
|
|
final vm.Timestamp startTime = await _vmService.getVMTimelineMicros();
|
2020-11-06 01:28:47 +00:00
|
|
|
await action();
|
2021-01-29 23:46:21 +00:00
|
|
|
final vm.Timestamp endTime = await _vmService.getVMTimelineMicros();
|
|
|
|
return await _vmService.getVMTimeline(
|
2020-11-06 01:28:47 +00:00
|
|
|
timeOriginMicros: startTime.timestamp,
|
|
|
|
timeExtentMicros: endTime.timestamp,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a convenience wrap of [traceTimeline] and send the result back to
|
|
|
|
/// the host for the [flutter_driver] style tests.
|
|
|
|
///
|
|
|
|
/// This records the timeline during `action` and adds the result to
|
|
|
|
/// [reportData] with `reportKey`. The [reportData] contains extra information
|
|
|
|
/// from the test other than test success/fail. It will be passed back to the
|
|
|
|
/// host and be processed by the [ResponseDataCallback] defined in
|
|
|
|
/// [integration_test_driver.integrationDriver]. By default it will be written
|
|
|
|
/// to `build/integration_response_data.json` with the key `timeline`.
|
|
|
|
///
|
|
|
|
/// For tests with multiple calls of this method, `reportKey` needs to be a
|
|
|
|
/// unique key, otherwise the later result will override earlier one.
|
|
|
|
///
|
|
|
|
/// The `streams` and `retainPriorEvents` parameters are passed as-is to
|
|
|
|
/// [traceTimeline].
|
|
|
|
Future<void> traceAction(
|
|
|
|
Future<dynamic> action(), {
|
|
|
|
List<String> streams = const <String>['all'],
|
|
|
|
bool retainPriorEvents = false,
|
|
|
|
String reportKey = 'timeline',
|
|
|
|
}) async {
|
|
|
|
final vm.Timeline timeline = await traceTimeline(
|
|
|
|
action,
|
|
|
|
streams: streams,
|
|
|
|
retainPriorEvents: retainPriorEvents,
|
|
|
|
);
|
|
|
|
reportData ??= <String, dynamic>{};
|
2021-01-29 23:46:21 +00:00
|
|
|
reportData[reportKey] = timeline.toJson();
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Watches the [FrameTiming] during `action` and report it to the binding
|
|
|
|
/// with key `reportKey`.
|
|
|
|
///
|
|
|
|
/// This can be used to implement performance tests previously using
|
|
|
|
/// [traceAction] and [TimelineSummary] from [flutter_driver]
|
|
|
|
Future<void> watchPerformance(
|
|
|
|
Future<void> action(), {
|
|
|
|
String reportKey = 'performance',
|
|
|
|
}) async {
|
|
|
|
assert(() {
|
|
|
|
if (_firstRun) {
|
|
|
|
debugPrint(kDebugWarning);
|
|
|
|
_firstRun = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}());
|
|
|
|
|
|
|
|
// The engine could batch FrameTimings and send them only once per second.
|
|
|
|
// Delay for a sufficient time so either old FrameTimings are flushed and not
|
|
|
|
// interfering our measurements here, or new FrameTimings are all reported.
|
|
|
|
// TODO(CareF): remove this when flush FrameTiming is readly in engine.
|
|
|
|
// See https://github.com/flutter/flutter/issues/64808
|
|
|
|
// and https://github.com/flutter/flutter/issues/67593
|
|
|
|
Future<void> delayForFrameTimings() => Future<void>.delayed(const Duration(seconds: 2));
|
|
|
|
|
|
|
|
await delayForFrameTimings(); // flush old FrameTimings
|
|
|
|
final List<FrameTiming> frameTimings = <FrameTiming>[];
|
|
|
|
final TimingsCallback watcher = frameTimings.addAll;
|
|
|
|
addTimingsCallback(watcher);
|
|
|
|
await action();
|
|
|
|
await delayForFrameTimings(); // make sure all FrameTimings are reported
|
|
|
|
removeTimingsCallback(watcher);
|
|
|
|
final FrameTimingSummarizer frameTimes =
|
|
|
|
FrameTimingSummarizer(frameTimings);
|
|
|
|
reportData ??= <String, dynamic>{};
|
2021-01-29 23:46:21 +00:00
|
|
|
reportData[reportKey] = frameTimes.summary;
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|
2020-11-09 16:39:05 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Timeout get defaultTestTimeout => _defaultTestTimeout ?? super.defaultTestTimeout;
|
|
|
|
|
|
|
|
/// Configures the default timeout for [testWidgets].
|
|
|
|
///
|
|
|
|
/// See [TestWidgetsFlutterBinding.defaultTestTimeout] for more details.
|
|
|
|
set defaultTestTimeout(Timeout timeout) => _defaultTestTimeout = timeout;
|
2021-01-29 23:46:21 +00:00
|
|
|
Timeout _defaultTestTimeout;
|
2020-12-10 00:33:04 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void attachRootWidget(Widget rootWidget) {
|
|
|
|
// This is a workaround where screenshots of root widgets have incorrect
|
|
|
|
// bounds.
|
|
|
|
// TODO(jiahaog): Remove when https://github.com/flutter/flutter/issues/66006 is fixed.
|
|
|
|
super.attachRootWidget(RepaintBoundary(child: rootWidget));
|
|
|
|
}
|
2020-11-06 01:28:47 +00:00
|
|
|
}
|