PatrolBinding constructor

PatrolBinding(
  1. NativeAutomatorConfig config
)

Creates a new PatrolBinding.

You most likely don't want to call it yourself.

Implementation

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) {
    final currentDartTest = _currentDartTest;
    if (currentDartTest != null) {
      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[currentDartTest] = Failure(
        testDescription,
        detailsAsString,
      );
    }
    oldTestExceptionReporter(details, testDescription);
  };

  setUp(() {
    if (constants.hotRestartEnabled) {
      return;
    }

    if (global_state.currentTestIndividualName == 'patrol_test_explorer') {
      return;
    }

    _currentDartTest = global_state.currentTestFullName;
  });

  tearDown(() async {
    if (constants.hotRestartEnabled) {
      // Sending results ends the test, which we don't want for Hot Restart
      return;
    }

    final testName = global_state.currentTestIndividualName;
    if (testName == 'patrol_test_explorer') {
      return;
    } else {
      logger(
        'tearDown(): count: ${_testResults.length}, results: $_testResults',
      );
    }

    final nameOfRequestedTest = await patrolAppService.testExecutionRequested;

    if (nameOfRequestedTest == _currentDartTest) {
      logger(
        'finished test $_currentDartTest. Will report its status back to the native side',
      );

      final passed = global_state.isCurrentTestPassing;
      logger(
        'tearDown(): test "$testName" in group "$_currentDartTest", passed: $passed',
      );
      await patrolAppService.markDartTestAsCompleted(
        dartFileName: _currentDartTest!,
        passed: passed,
        details: _testResults[_currentDartTest!] is Failure
            ? (_testResults[_currentDartTest!] as Failure?)?.details
            : null,
      );
    } else {
      logger(
        'finished test $_currentDartTest, but it was not requested, so its status will not be reported back to the native side',
      );
    }
  });
}