Skip to content

Commit c563a0c

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 a70227d commit c563a0c

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

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

0 commit comments

Comments
 (0)