Skip to content

Commit

Permalink
Fix onError when using patrol finders
Browse files Browse the repository at this point in the history
  • Loading branch information
karolinaoparczyk committed Oct 11, 2024
1 parent 5a57113 commit b233a24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
6 changes: 6 additions & 0 deletions dev/e2e_app/integration_test/overflow_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Uncomment to test `Multiple exceptions were thrown` issue
// import 'package:flutter/widgets.dart';

import 'common.dart';

void main() {
patrol('opens the overflow screen', ($) async {
await createApp($);

await $('Open overflow screen').scrollTo().tap();

// Uncomment to test `Multiple exceptions were thrown` issue
// return $(ValueKey('key')).scrollTo().tap();
});
}
10 changes: 9 additions & 1 deletion dev/e2e_app/lib/overflow_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
// Uncomment to test `Multiple exceptions were thrown` issue
// import 'package:flutter/services.dart';

class OverflowScreen extends StatelessWidget {
const OverflowScreen({super.key});
Expand All @@ -12,12 +14,18 @@ class OverflowScreen extends StatelessWidget {
// }
//
// Future<void> throwPlatformException() async {
// await Future<void>.delayed(const Duration(milliseconds: 150));
// await Future<void>.delayed(const Duration(milliseconds: 110));
// throw PlatformException(code: 'code');
// }
//
// Future<void> throwFormatException() async {
// await Future<void>.delayed(const Duration(milliseconds: 120));
// throw FormatException();
// }
//
// throwStackOverflowException();
// throwPlatformException();
// throwFormatException();

return Scaffold(
appBar: AppBar(
Expand Down
23 changes: 0 additions & 23 deletions packages/patrol/lib/src/binding.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io' as io;
import 'dart:isolate';

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -44,28 +43,6 @@ class PatrolBinding extends LiveTestWidgetsFlutterBinding {
/// You most likely don't want to call it yourself.
PatrolBinding(NativeAutomatorConfig config)
: _serviceExtensions = DevtoolsServiceExtensions(config) {
final oldTestExceptionReporter = reportTestException;

/// Wraps the default test exception reporter to report the test results to
/// the native side of Patrol.
reportTestException = (details, testDescription) {
if (_currentDartTest case final testName?) {
assert(!constants.hotRestartEnabled);
// On iOS in release mode, diagnostics are compacted or truncated.
// We use the exceptionAsString() and stack to get the information
// about the exception. See [DiagnosticLevel].
final detailsAsString = (kReleaseMode && io.Platform.isIOS)
? '${details.exceptionAsString()} \n ${details.stack}'
: details.toString();

testResults[testName] = Failure(
testDescription,
detailsAsString,
);
}
oldTestExceptionReporter(details, testDescription);
};

setUp(() {
if (constants.hotRestartEnabled) {
return;
Expand Down
11 changes: 8 additions & 3 deletions packages/patrol/lib/src/native/patrol_app_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,22 @@ class PatrolAppService extends PatrolAppServiceServer {

final previousOnError = FlutterError.onError;
FlutterError.onError = (details) {
final previousDetails =
switch (patrolBinding.testResults[request.name] as Failure?) {
final previousDetails = switch (patrolBinding.testResults[request.name]) {
Failure(:final details?) => FlutterErrorDetails(exception: details),
_ => null,
};
final detailsAsString = (kReleaseMode && Platform.isIOS)
? '${details.exceptionAsString()} \n ${details.stack}'
: details.toString();

patrolBinding.testResults[request.name] = Failure(
request.name,
'$details\n$previousDetails',
'$detailsAsString\n$previousDetails',
);

previousOnError?.call(details);
};

final testExecutionResult = await testExecutionCompleted;
FlutterError.onError = previousOnError;

Expand Down

0 comments on commit b233a24

Please sign in to comment.