Skip to content

Commit 10ba24b

Browse files
committed
Add red/green tests for _compareScreenOrder catch branches
The regression tests added in the previous commit only exercise the happy path of _compareScreenOrder (the try block). Add mock-based red/green tests that directly exercise the on StateError and on AssertionError catch branches by registering Selectables whose boundingBoxes getter throws. Verified that both tests fail without the guard (the thrown errors propagate out of _flushAdditions' post-frame callback and tester.takeException() reports them) and pass with the guard. Related to flutter#151536
1 parent c0ffe68 commit 10ba24b

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

‎packages/flutter/test/widgets/selectable_region_test.dart‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6727,6 +6727,58 @@ void main() {
67276727
expect(find.text('Top page'), findsOneWidget);
67286728
},
67296729
);
6730+
6731+
testWidgets('MultiSelectableSelectionContainerDelegate._compareScreenOrder catches '
6732+
'StateError from unlaid-out selectables', (WidgetTester tester) async {
6733+
// Regression test for https://github.com/flutter/flutter/issues/151536
6734+
//
6735+
// Red/green test that directly exercises the `on StateError` catch
6736+
// branch of MultiSelectableSelectionContainerDelegate._compareScreenOrder
6737+
// by registering mock Selectables whose `boundingBoxes` getter throws,
6738+
// simulating the production scenario where _RenderTheater skipped layout
6739+
// for an obscured OverlayEntry. Without the guard, _flushAdditions would
6740+
// propagate the StateError out of its post-frame callback and
6741+
// tester.takeException() would report it.
6742+
await tester.pumpWidget(
6743+
MaterialApp(
6744+
home: SelectableRegion(
6745+
selectionControls: materialTextSelectionControls,
6746+
child: const Column(
6747+
children: <Widget>[
6748+
_ThrowingSelectionSpy(throwKind: _ThrowKind.stateError),
6749+
_ThrowingSelectionSpy(throwKind: _ThrowKind.stateError),
6750+
],
6751+
),
6752+
),
6753+
),
6754+
);
6755+
6756+
expect(tester.takeException(), isNull);
6757+
});
6758+
6759+
testWidgets('MultiSelectableSelectionContainerDelegate._compareScreenOrder catches '
6760+
'AssertionError from unlaid-out selectables', (WidgetTester tester) async {
6761+
// Regression test for https://github.com/flutter/flutter/issues/151536
6762+
//
6763+
// Red/green test for the `on AssertionError` catch, which covers
6764+
// debug-mode builds where `assert(hasSize)` fires before the
6765+
// `_size ?? throw StateError(...)` expression in RenderBox.size.
6766+
await tester.pumpWidget(
6767+
MaterialApp(
6768+
home: SelectableRegion(
6769+
selectionControls: materialTextSelectionControls,
6770+
child: const Column(
6771+
children: <Widget>[
6772+
_ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError),
6773+
_ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError),
6774+
],
6775+
),
6776+
),
6777+
),
6778+
);
6779+
6780+
expect(tester.takeException(), isNull);
6781+
});
67306782
}
67316783

67326784
void _noopRemovePage(Page<Object?> page) {}
@@ -6919,3 +6971,84 @@ class RenderSelectAll extends RenderProxyBox with Selectable, SelectionRegistran
69196971
this.endHandle = endHandle;
69206972
}
69216973
}
6974+
6975+
// Mock Selectable whose [boundingBoxes] getter throws — used by the
6976+
// `_compareScreenOrder` regression tests for
6977+
// https://github.com/flutter/flutter/issues/151536 to directly exercise the
6978+
// `on StateError` / `on AssertionError` catch branches.
6979+
enum _ThrowKind { stateError, assertionError }
6980+
6981+
class _ThrowingSelectionSpy extends LeafRenderObjectWidget {
6982+
const _ThrowingSelectionSpy({required this.throwKind});
6983+
6984+
final _ThrowKind throwKind;
6985+
6986+
@override
6987+
RenderObject createRenderObject(BuildContext context) {
6988+
return _RenderThrowingSelectionSpy(SelectionContainer.maybeOf(context), throwKind);
6989+
}
6990+
6991+
@override
6992+
void updateRenderObject(BuildContext context, covariant RenderObject renderObject) {}
6993+
}
6994+
6995+
class _RenderThrowingSelectionSpy extends RenderProxyBox with Selectable, SelectionRegistrant {
6996+
_RenderThrowingSelectionSpy(SelectionRegistrar? registrar, this._throwKind) {
6997+
this.registrar = registrar;
6998+
}
6999+
7000+
final _ThrowKind _throwKind;
7001+
7002+
@override
7003+
List<Rect> get boundingBoxes {
7004+
switch (_throwKind) {
7005+
case _ThrowKind.stateError:
7006+
throw StateError('Bad state: RenderBox was not laid out (test)');
7007+
case _ThrowKind.assertionError:
7008+
throw AssertionError('RenderBox was not laid out (test)');
7009+
}
7010+
}
7011+
7012+
@override
7013+
Size computeDryLayout(BoxConstraints constraints) => constraints.smallest;
7014+
7015+
@override
7016+
void performLayout() => size = computeDryLayout(constraints);
7017+
7018+
@override
7019+
void addListener(VoidCallback listener) {}
7020+
7021+
@override
7022+
void removeListener(VoidCallback listener) {}
7023+
7024+
@override
7025+
SelectionResult dispatchSelectionEvent(SelectionEvent event) => SelectionResult.end;
7026+
7027+
@override
7028+
SelectedContent? getSelectedContent() => null;
7029+
7030+
@override
7031+
SelectedContentRange? getSelection() => null;
7032+
7033+
@override
7034+
int get contentLength => 0;
7035+
7036+
@override
7037+
final SelectionGeometry value = const SelectionGeometry(
7038+
hasContent: true,
7039+
status: SelectionStatus.uncollapsed,
7040+
startSelectionPoint: SelectionPoint(
7041+
localPosition: Offset.zero,
7042+
lineHeight: 0.0,
7043+
handleType: TextSelectionHandleType.left,
7044+
),
7045+
endSelectionPoint: SelectionPoint(
7046+
localPosition: Offset.zero,
7047+
lineHeight: 0.0,
7048+
handleType: TextSelectionHandleType.left,
7049+
),
7050+
);
7051+
7052+
@override
7053+
void pushHandleLayers(LayerLink? startHandle, LayerLink? endHandle) {}
7054+
}

0 commit comments

Comments
 (0)