Skip to content

Commit c37c8c9

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 51f6855 commit c37c8c9

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
@@ -7018,6 +7018,58 @@ void main() {
70187018
expect(find.text('Top page'), findsOneWidget);
70197019
},
70207020
);
7021+
7022+
testWidgets('MultiSelectableSelectionContainerDelegate._compareScreenOrder catches '
7023+
'StateError from unlaid-out selectables', (WidgetTester tester) async {
7024+
// Regression test for https://github.com/flutter/flutter/issues/151536
7025+
//
7026+
// Red/green test that directly exercises the `on StateError` catch
7027+
// branch of MultiSelectableSelectionContainerDelegate._compareScreenOrder
7028+
// by registering mock Selectables whose `boundingBoxes` getter throws,
7029+
// simulating the production scenario where _RenderTheater skipped layout
7030+
// for an obscured OverlayEntry. Without the guard, _flushAdditions would
7031+
// propagate the StateError out of its post-frame callback and
7032+
// tester.takeException() would report it.
7033+
await tester.pumpWidget(
7034+
TestWidgetsApp(
7035+
home: SelectableRegion(
7036+
selectionControls: emptyTextSelectionControls,
7037+
child: const Column(
7038+
children: <Widget>[
7039+
_ThrowingSelectionSpy(throwKind: _ThrowKind.stateError),
7040+
_ThrowingSelectionSpy(throwKind: _ThrowKind.stateError),
7041+
],
7042+
),
7043+
),
7044+
),
7045+
);
7046+
7047+
expect(tester.takeException(), isNull);
7048+
});
7049+
7050+
testWidgets('MultiSelectableSelectionContainerDelegate._compareScreenOrder catches '
7051+
'AssertionError from unlaid-out selectables', (WidgetTester tester) async {
7052+
// Regression test for https://github.com/flutter/flutter/issues/151536
7053+
//
7054+
// Red/green test for the `on AssertionError` catch, which covers
7055+
// debug-mode builds where `assert(hasSize)` fires before the
7056+
// `_size ?? throw StateError(...)` expression in RenderBox.size.
7057+
await tester.pumpWidget(
7058+
TestWidgetsApp(
7059+
home: SelectableRegion(
7060+
selectionControls: emptyTextSelectionControls,
7061+
child: const Column(
7062+
children: <Widget>[
7063+
_ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError),
7064+
_ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError),
7065+
],
7066+
),
7067+
),
7068+
),
7069+
);
7070+
7071+
expect(tester.takeException(), isNull);
7072+
});
70217073
}
70227074

70237075
void _noopRemovePage(Page<Object?> page) {}
@@ -7237,3 +7289,84 @@ class _TextSelectionControlsSpy extends TextSelectionControls with TextSelection
72377289
return Offset.zero;
72387290
}
72397291
}
7292+
7293+
// Mock Selectable whose [boundingBoxes] getter throws — used by the
7294+
// `_compareScreenOrder` regression tests for
7295+
// https://github.com/flutter/flutter/issues/151536 to directly exercise the
7296+
// `on StateError` / `on AssertionError` catch branches.
7297+
enum _ThrowKind { stateError, assertionError }
7298+
7299+
class _ThrowingSelectionSpy extends LeafRenderObjectWidget {
7300+
const _ThrowingSelectionSpy({required this.throwKind});
7301+
7302+
final _ThrowKind throwKind;
7303+
7304+
@override
7305+
RenderObject createRenderObject(BuildContext context) {
7306+
return _RenderThrowingSelectionSpy(SelectionContainer.maybeOf(context), throwKind);
7307+
}
7308+
7309+
@override
7310+
void updateRenderObject(BuildContext context, covariant RenderObject renderObject) {}
7311+
}
7312+
7313+
class _RenderThrowingSelectionSpy extends RenderProxyBox with Selectable, SelectionRegistrant {
7314+
_RenderThrowingSelectionSpy(SelectionRegistrar? registrar, this._throwKind) {
7315+
this.registrar = registrar;
7316+
}
7317+
7318+
final _ThrowKind _throwKind;
7319+
7320+
@override
7321+
List<Rect> get boundingBoxes {
7322+
switch (_throwKind) {
7323+
case _ThrowKind.stateError:
7324+
throw StateError('Bad state: RenderBox was not laid out (test)');
7325+
case _ThrowKind.assertionError:
7326+
throw AssertionError('RenderBox was not laid out (test)');
7327+
}
7328+
}
7329+
7330+
@override
7331+
Size computeDryLayout(BoxConstraints constraints) => constraints.smallest;
7332+
7333+
@override
7334+
void performLayout() => size = computeDryLayout(constraints);
7335+
7336+
@override
7337+
void addListener(VoidCallback listener) {}
7338+
7339+
@override
7340+
void removeListener(VoidCallback listener) {}
7341+
7342+
@override
7343+
SelectionResult dispatchSelectionEvent(SelectionEvent event) => SelectionResult.end;
7344+
7345+
@override
7346+
SelectedContent? getSelectedContent() => null;
7347+
7348+
@override
7349+
SelectedContentRange? getSelection() => null;
7350+
7351+
@override
7352+
int get contentLength => 0;
7353+
7354+
@override
7355+
final SelectionGeometry value = const SelectionGeometry(
7356+
hasContent: true,
7357+
status: SelectionStatus.uncollapsed,
7358+
startSelectionPoint: SelectionPoint(
7359+
localPosition: Offset.zero,
7360+
lineHeight: 0.0,
7361+
handleType: TextSelectionHandleType.left,
7362+
),
7363+
endSelectionPoint: SelectionPoint(
7364+
localPosition: Offset.zero,
7365+
lineHeight: 0.0,
7366+
handleType: TextSelectionHandleType.left,
7367+
),
7368+
);
7369+
7370+
@override
7371+
void pushHandleLayers(LayerLink? startHandle, LayerLink? endHandle) {}
7372+
}

0 commit comments

Comments
 (0)