Skip to content

Commit

Permalink
taking screenshots works just fine with new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Nov 15, 2022
1 parent c493cde commit 7d330a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
21 changes: 15 additions & 6 deletions packages/patrol/lib/patrol_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,24 @@ Future<void> _initCommunication({
}

final method = event.extensionData!.data['method'] as String;
final requestId = event.extensionData!.data['request_id'] as int;
switch (method) {
case 'take_screenshot':
_takeScreenshot(
event.extensionData!.data['args'] as Map<String, dynamic>,
bool status;
try {
_takeScreenshot(
event.extensionData!.data['args'] as Map<String, dynamic>,
);
status = true;
} catch (err) {
status = false;
}

vmService.callServiceExtension(
'ext.flutter.patrol',
isolateId: isolateId,
args: <String, dynamic>{'request_id': requestId, 'status': status},
);

vmService.callServiceExtension('patrol', isolateId: isolateId);
break;
default:
throw StateError('unknown method $method');
Expand All @@ -73,14 +84,12 @@ void _takeScreenshot(Map<String, dynamic> args) {
print('Error: DRIVER_DEVICE_ID is not set');
io.exit(1);
}
print('DRIVER_DEVICE_ID: $deviceId');

final deviceOs = io.Platform.environment['DRIVER_DEVICE_OS'];
if (deviceOs == null || deviceOs.isEmpty) {
print('Error: DRIVER_DEVICE_OS is not set');
io.exit(1);
}
print('DRIVER_DEVICE_OS: $deviceOs');

final screenshotName = args['name'] as String?;
if (screenshotName == null) {
Expand Down
49 changes: 41 additions & 8 deletions packages/patrol/lib/src/native/binding.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import 'dart:async';
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:vm_service/vm_service.dart' as vm;

class _Response {
factory _Response.fromJson(Map<String, dynamic> json) {
return _Response._(
int.parse(json['request_id'] as String),
json['status'] == 'true',
);
}

const _Response._(this.id, this.ok);

final int id;
final bool ok;
}

// ignore: avoid_print
void _defaultPrintLogger(String message) => print('PatrolBinding: $message');
Expand All @@ -25,14 +39,9 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {

final _logger = _defaultPrintLogger;

/// ID of the Isolate which the host driver script is running in.
///
/// Has the form of e.g "isolates/1566121372315359".
late String driverIsolateId;
int _latestEventId = 0;

/// Dart VM service (aka Dart Observatory server) running in the main isolate
/// of the Dart VM which is running the test driver script.
late vm.VmService vmService;
final _controller = StreamController<_Response>.broadcast();

// TODO: Remove once https://github.com/flutter/flutter/pull/108430 is
// available on the stable channel
Expand All @@ -49,17 +58,41 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding {
}
}

@override
void initServiceExtensions() {
super.initServiceExtensions();

if (!kReleaseMode) {
registerServiceExtension(
name: 'patrol',
callback: (args) async {
_controller.add(_Response.fromJson(args));
return <String, String>{};
},
);
_logger('registered service extension ext.flutter.patrol');
}
}

/// Takes a screenshot using the `flutter screenshot` command.
///
/// The screenshot is placed in [path], named [name], and has .png extension.
Future<void> takeFlutterScreenshot({
required String name,
required String path,
}) async {
final eventId = ++_latestEventId;

postEvent('patrol', <String, dynamic>{
'method': 'take_screenshot',
'request_id': eventId,
'args': {'name': name, 'path': path}
});

final resp = await _controller.stream.firstWhere((r) => r.id == eventId);
if (!resp.ok) {
throw StateError('event with request_id $eventId failed');
}
}

/// The singleton instance of this object.
Expand Down

0 comments on commit 7d330a7

Please sign in to comment.