@@ -1917,10 +1917,160 @@ void main() {
19171917 expect (find.text ('Top page' ), findsOneWidget);
19181918 },
19191919 );
1920+
1921+ testWidgets ('_SelectableTextContainerDelegate._compareScreenOrder catches StateError '
1922+ 'from unlaid-out selectables' , (WidgetTester tester) async {
1923+ // Regression test for https://github.com/flutter/flutter/issues/151536
1924+ //
1925+ // Red/green test that exercises the `on StateError` catch branch of
1926+ // _SelectableTextContainerDelegate._compareScreenOrder (text.dart).
1927+ // Registers mock throwing Selectables inside a Text.rich via WidgetSpan
1928+ // children; the enclosing _SelectableTextContainer's delegate picks them
1929+ // up through SelectionContainer.maybeOf(context), and the post-frame
1930+ // sort routes them through text.dart's comparator override.
1931+ final focusNode = FocusNode ();
1932+ addTearDown (focusNode.dispose);
1933+
1934+ await tester.pumpWidget (
1935+ TestWidgetsApp (
1936+ home: SelectableRegion (
1937+ focusNode: focusNode,
1938+ selectionControls: EmptyTextSelectionControls (),
1939+ child: const Text .rich (
1940+ TextSpan (
1941+ children: < InlineSpan > [
1942+ TextSpan (text: 'before ' ),
1943+ WidgetSpan (child: _ThrowingSelectionSpy (throwKind: _ThrowKind .stateError)),
1944+ TextSpan (text: ' middle ' ),
1945+ WidgetSpan (child: _ThrowingSelectionSpy (throwKind: _ThrowKind .stateError)),
1946+ TextSpan (text: ' after' ),
1947+ ],
1948+ ),
1949+ ),
1950+ ),
1951+ ),
1952+ );
1953+
1954+ expect (tester.takeException (), isNull);
1955+ });
1956+
1957+ testWidgets ('_SelectableTextContainerDelegate._compareScreenOrder catches '
1958+ 'AssertionError from unlaid-out selectables' , (WidgetTester tester) async {
1959+ // Regression test for https://github.com/flutter/flutter/issues/151536
1960+ //
1961+ // Red/green test for the `on AssertionError` catch in text.dart, which
1962+ // covers debug-mode builds where `assert(hasSize)` fires before the
1963+ // `_size ?? throw StateError(...)` expression in RenderBox.size.
1964+ final focusNode = FocusNode ();
1965+ addTearDown (focusNode.dispose);
1966+
1967+ await tester.pumpWidget (
1968+ TestWidgetsApp (
1969+ home: SelectableRegion (
1970+ focusNode: focusNode,
1971+ selectionControls: EmptyTextSelectionControls (),
1972+ child: const Text .rich (
1973+ TextSpan (
1974+ children: < InlineSpan > [
1975+ TextSpan (text: 'before ' ),
1976+ WidgetSpan (child: _ThrowingSelectionSpy (throwKind: _ThrowKind .assertionError)),
1977+ TextSpan (text: ' middle ' ),
1978+ WidgetSpan (child: _ThrowingSelectionSpy (throwKind: _ThrowKind .assertionError)),
1979+ TextSpan (text: ' after' ),
1980+ ],
1981+ ),
1982+ ),
1983+ ),
1984+ ),
1985+ );
1986+
1987+ expect (tester.takeException (), isNull);
1988+ });
19201989}
19211990
19221991void _noopRemovePage (Page <Object ?> page) {}
19231992
1993+ // Mock Selectable whose [boundingBoxes] getter throws — used by the text.dart
1994+ // `_SelectableTextContainerDelegate._compareScreenOrder` regression tests for
1995+ // https://github.com/flutter/flutter/issues/151536 to directly exercise the
1996+ // `on StateError` / `on AssertionError` catch branches.
1997+ enum _ThrowKind { stateError, assertionError }
1998+
1999+ class _ThrowingSelectionSpy extends LeafRenderObjectWidget {
2000+ const _ThrowingSelectionSpy ({required this .throwKind});
2001+
2002+ final _ThrowKind throwKind;
2003+
2004+ @override
2005+ RenderObject createRenderObject (BuildContext context) {
2006+ return _RenderThrowingSelectionSpy (SelectionContainer .maybeOf (context), throwKind);
2007+ }
2008+
2009+ @override
2010+ void updateRenderObject (BuildContext context, covariant RenderObject renderObject) {}
2011+ }
2012+
2013+ class _RenderThrowingSelectionSpy extends RenderProxyBox with Selectable , SelectionRegistrant {
2014+ _RenderThrowingSelectionSpy (SelectionRegistrar ? registrar, this ._throwKind) {
2015+ this .registrar = registrar;
2016+ }
2017+
2018+ final _ThrowKind _throwKind;
2019+
2020+ @override
2021+ List <Rect > get boundingBoxes {
2022+ switch (_throwKind) {
2023+ case _ThrowKind .stateError:
2024+ throw StateError ('Bad state: RenderBox was not laid out (test)' );
2025+ case _ThrowKind .assertionError:
2026+ throw AssertionError ('RenderBox was not laid out (test)' );
2027+ }
2028+ }
2029+
2030+ @override
2031+ Size computeDryLayout (BoxConstraints constraints) => constraints.smallest;
2032+
2033+ @override
2034+ void performLayout () => size = computeDryLayout (constraints);
2035+
2036+ @override
2037+ void addListener (VoidCallback listener) {}
2038+
2039+ @override
2040+ void removeListener (VoidCallback listener) {}
2041+
2042+ @override
2043+ SelectionResult dispatchSelectionEvent (SelectionEvent event) => SelectionResult .end;
2044+
2045+ @override
2046+ SelectedContent ? getSelectedContent () => null ;
2047+
2048+ @override
2049+ SelectedContentRange ? getSelection () => null ;
2050+
2051+ @override
2052+ int get contentLength => 0 ;
2053+
2054+ @override
2055+ final SelectionGeometry value = const SelectionGeometry (
2056+ hasContent: true ,
2057+ status: SelectionStatus .uncollapsed,
2058+ startSelectionPoint: SelectionPoint (
2059+ localPosition: Offset .zero,
2060+ lineHeight: 0.0 ,
2061+ handleType: TextSelectionHandleType .left,
2062+ ),
2063+ endSelectionPoint: SelectionPoint (
2064+ localPosition: Offset .zero,
2065+ lineHeight: 0.0 ,
2066+ handleType: TextSelectionHandleType .left,
2067+ ),
2068+ );
2069+
2070+ @override
2071+ void pushHandleLayers (LayerLink ? startHandle, LayerLink ? endHandle) {}
2072+ }
2073+
19242074Future <void > _pumpTextWidget ({
19252075 required WidgetTester tester,
19262076 required String text,
0 commit comments