Fix how tests count open SemanticsHandles (#121571)

Fix how tests count open SemanticsHandles
This commit is contained in:
Michael Goderbauer
2023-02-28 15:55:58 -08:00
committed by GitHub
parent f97a51533b
commit 6de42a70f1
27 changed files with 219 additions and 85 deletions

View File

@@ -155,10 +155,10 @@ void testWidgets(
() {
tester._testDescription = combinedDescription;
SemanticsHandle? semanticsHandle;
tester._recordNumberOfSemanticsHandles();
if (semanticsEnabled == true) {
semanticsHandle = tester.ensureSemantics();
}
tester._recordNumberOfSemanticsHandles();
test_package.addTearDown(binding.postTest);
return binding.runTest(
() async {
@@ -1044,18 +1044,16 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
void _verifySemanticsHandlesWereDisposed() {
assert(_lastRecordedSemanticsHandles != null);
if (binding.pipelineOwner.debugOutstandingSemanticsHandles > _lastRecordedSemanticsHandles!) {
// TODO(goderbauer): Fix known leak in web engine when running integration tests and remove this "correction", https://github.com/flutter/flutter/issues/121640.
final int knownWebEngineLeakForLiveTestsCorrection = kIsWeb && binding is LiveTestWidgetsFlutterBinding ? 2 : 0;
if (_currentSemanticsHandles - knownWebEngineLeakForLiveTestsCorrection > _lastRecordedSemanticsHandles!) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('A SemanticsHandle was active at the end of the test.'),
ErrorDescription(
'All SemanticsHandle instances must be disposed by calling dispose() on '
'the SemanticsHandle.'
),
ErrorHint(
'If your test uses SemanticsTester, it is '
'sufficient to call dispose() on SemanticsTester. Otherwise, the '
'existing handle will leak into another test and alter its behavior.'
),
]);
}
_lastRecordedSemanticsHandles = null;
@@ -1063,8 +1061,10 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
int? _lastRecordedSemanticsHandles;
int get _currentSemanticsHandles => binding.debugOutstandingSemanticsHandles + binding.pipelineOwner.debugOutstandingSemanticsHandles;
void _recordNumberOfSemanticsHandles() {
_lastRecordedSemanticsHandles = binding.pipelineOwner.debugOutstandingSemanticsHandles;
_lastRecordedSemanticsHandles = _currentSemanticsHandles;
}
/// Returns the TestTextInput singleton.

View File

@@ -737,7 +737,6 @@ void main() {
testWidgets('failure does not throw unexpected errors', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(() => handle.dispose());
const Key key = Key('semantics');
await tester.pumpWidget(Semantics(
@@ -789,13 +788,13 @@ void main() {
);
expect(failedExpectation, throwsA(isA<TestFailure>()));
handle.dispose();
});
});
group('containsSemantics', () {
testWidgets('matches SemanticsData', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(() => handle.dispose());
const Key key = Key('semantics');
await tester.pumpWidget(Semantics(
@@ -889,6 +888,7 @@ void main() {
)),
reason: 'onTapHint "scans" should not have matched "scan".',
);
handle.dispose();
});
testWidgets('can match all semantics flags and actions enabled', (WidgetTester tester) async {
@@ -1233,7 +1233,6 @@ void main() {
testWidgets('failure does not throw unexpected errors', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
addTearDown(() => handle.dispose());
const Key key = Key('semantics');
await tester.pumpWidget(Semantics(
@@ -1283,6 +1282,7 @@ void main() {
);
expect(failedExpectation, throwsA(isA<TestFailure>()));
handle.dispose();
});
});

View File

@@ -0,0 +1,71 @@
// 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 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
reportTestException = (FlutterErrorDetails details, String testDescription) {
errors.add(details);
};
// The error that the test throws in their run methods below will be forwarded
// to our exception handler above and do not cause the test to fail. The
// tearDown method then checks that the test threw the expected exception.
await testMain();
}
void pipelineOwnerTestRun() {
testWidgets('open SemanticsHandle from PipelineOwner fails test', (WidgetTester tester) async {
final int outstandingHandles = tester.binding.pipelineOwner.debugOutstandingSemanticsHandles;
tester.binding.pipelineOwner.ensureSemantics();
expect(tester.binding.pipelineOwner.debugOutstandingSemanticsHandles, outstandingHandles + 1);
// SemanticsHandle is not disposed on purpose to verify in tearDown that
// the test failed due to an active SemanticsHandle.
});
tearDown(() {
expect(errors, hasLength(1));
expect(errors.single.toString(), contains('SemanticsHandle was active at the end of the test'));
});
}
void semanticsBindingTestRun() {
testWidgets('open SemanticsHandle from SemanticsBinding fails test', (WidgetTester tester) async {
final int outstandingHandles = tester.binding.debugOutstandingSemanticsHandles;
tester.binding.ensureSemantics();
expect(tester.binding.debugOutstandingSemanticsHandles, outstandingHandles + 1);
// SemanticsHandle is not disposed on purpose to verify in tearDown that
// the test failed due to an active SemanticsHandle.
});
tearDown(() {
expect(errors, hasLength(1));
expect(errors.single.toString(), contains('SemanticsHandle was active at the end of the test'));
});
}
void failingTestTestRun() {
testWidgets('open SemanticsHandle from SemanticsBinding fails test', (WidgetTester tester) async {
final int outstandingHandles = tester.binding.debugOutstandingSemanticsHandles;
tester.binding.ensureSemantics();
expect(tester.binding.debugOutstandingSemanticsHandles, outstandingHandles + 1);
// Failing expectation to verify that an open semantics handle doesn't
// cause any cascading failures and only the failing expectation is
// reported.
expect(1, equals(2));
fail('The test should never have gotten this far.');
});
tearDown(() {
expect(errors, hasLength(1));
expect(errors.single.toString(), contains('Expected: <2>'));
expect(errors.single.toString(), contains('Actual: <1>'));
});
}

View File

@@ -0,0 +1,7 @@
// 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 'flutter_test_config.dart' as config;
void main() => config.failingTestTestRun();

View File

@@ -0,0 +1,7 @@
// 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 'flutter_test_config.dart' as config;
void main() => config.pipelineOwnerTestRun();

View File

@@ -0,0 +1,7 @@
// 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 'flutter_test_config.dart' as config;
void main() => config.semanticsBindingTestRun();