postTest method

  1. @mustCallSuper
void postTest()

Called by the testWidgets function after a test is executed.

Subclasses that override this method must call super.postTest() and must not throw exceptions. This method will not throw errors; any invariant failures detected during verification are reported via reportTestException.

Implementation

@mustCallSuper
void postTest() {
  assert(inTest);
  FlutterErrorDetails? invariantError;
  if (_shouldVerifyInvariants) {
    _shouldVerifyInvariants = false;
    try {
      _verifyAutoUpdateGoldensUnset(_beforeTestAutoUpdateGoldens && !isBrowser);
      _verifyReportTestExceptionUnset(_beforeTestReportTestException);
      _verifyErrorWidgetBuilderUnset(_beforeTestErrorWidgetBuilder);
      _verifyShouldPropagateDevicePointerEventsUnset(
        _beforeTestShouldPropagateDevicePointerEvents,
      );
      _verifyInvariants();
    } catch (error, stack) {
      invariantError = FlutterErrorDetails(
        exception: error,
        stack: stack,
        context: ErrorDescription('running invariant verifications after a test'),
        library: 'Flutter test framework',
      );
    }
    // Some invariant checks (such as debugAssertNoTransientCallbacks and the
    // _verify*Unset helpers above) report failures through
    // FlutterError.reportError instead of throwing. During the test,
    // FlutterError.onError stores them in _pendingExceptionDetails, which is
    // normally reported by the test completion handler. That handler has
    // already run by the time postTest is called, so these errors must be
    // picked up here or they would be silently dropped.
    final FlutterErrorDetails? reportedError = _pendingExceptionDetails;
    if (reportedError != null) {
      if (invariantError == null) {
        invariantError = reportedError;
      } else {
        debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
        FlutterError.dumpErrorToConsole(reportedError, forceReport: true);
      }
    }
  }
  final String testDescription = _currentTestDescription;
  _currentTestDescription = '';
  FlutterError.onError = _oldExceptionHandler;
  FlutterError.demangleStackTrace = _oldStackTraceDemangler;
  _pendingExceptionDetails = null;
  _parentZone = null;
  _testZone = null;
  buildOwner!.focusManager.dispose();

  if (TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.checkMockMessageHandler(
    SystemChannels.accessibility.name,
    _announcementHandler,
  )) {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
        .setMockDecodedMessageHandler(SystemChannels.accessibility, null);
    _announcementHandler = null;
  }
  _announcements = <CapturedAccessibilityAnnouncement>[];

  ServicesBinding.instance.keyEventManager.keyMessageHandler = null;
  buildOwner!.focusManager = FocusManager()..registerGlobalHandlers();

  // Disabling the warning because @visibleForTesting doesn't take the testing
  // framework itself into account, but we don't want it visible outside of
  // tests.
  // ignore: invalid_use_of_visible_for_testing_member
  RawKeyboard.instance.clearKeysPressed();
  // ignore: invalid_use_of_visible_for_testing_member
  HardwareKeyboard.instance.clearState();
  // ignore: invalid_use_of_visible_for_testing_member
  keyEventManager.clearState();
  // ignore: invalid_use_of_visible_for_testing_member
  RendererBinding.instance.initMouseTracker();

  assert(ServicesBinding.instance == WidgetsBinding.instance);
  // ignore: invalid_use_of_visible_for_testing_member
  ServicesBinding.instance.resetInternalState();

  platformDispatcher.resetInternalState();

  if (invariantError != null) {
    debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
    reportTestException(invariantError, testDescription);
  }
}