Skip to content

Commit 36f0728

Browse files
committed
Also add red/green tests for text.dart _compareScreenOrder
The previous commit's red/green tests only exercised the MultiSelectableSelectionContainerDelegate._compareScreenOrder override in selectable_region.dart. Add the equivalent red/green tests for _SelectableTextContainerDelegate._compareScreenOrder in text.dart by registering throwing mock Selectables via WidgetSpan children of a Text.rich wrapped in SelectableRegion. The enclosing _SelectableTextContainer's delegate picks the mocks up through SelectionContainer.maybeOf(context), so the post-frame sort routes them through text.dart's comparator override specifically. Verified red/green: both tests fail without the text.dart guard (the thrown StateError / AssertionError propagates out of _flushAdditions' post-frame callback and tester.takeException() reports it) and pass with the guard. Related to flutter#151536
1 parent 10ba24b commit 36f0728

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

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

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,10 +1871,160 @@ void main() {
18711871
expect(find.text('Top page'), findsOneWidget);
18721872
},
18731873
);
1874+
1875+
testWidgets('_SelectableTextContainerDelegate._compareScreenOrder catches StateError '
1876+
'from unlaid-out selectables', (WidgetTester tester) async {
1877+
// Regression test for https://github.com/flutter/flutter/issues/151536
1878+
//
1879+
// Red/green test that exercises the `on StateError` catch branch of
1880+
// _SelectableTextContainerDelegate._compareScreenOrder (text.dart).
1881+
// Registers mock throwing Selectables inside a Text.rich via WidgetSpan
1882+
// children; the enclosing _SelectableTextContainer's delegate picks them
1883+
// up through SelectionContainer.maybeOf(context), and the post-frame
1884+
// sort routes them through text.dart's comparator override.
1885+
final focusNode = FocusNode();
1886+
addTearDown(focusNode.dispose);
1887+
1888+
await tester.pumpWidget(
1889+
TestWidgetsApp(
1890+
home: SelectableRegion(
1891+
focusNode: focusNode,
1892+
selectionControls: EmptyTextSelectionControls(),
1893+
child: const Text.rich(
1894+
TextSpan(
1895+
children: <InlineSpan>[
1896+
TextSpan(text: 'before '),
1897+
WidgetSpan(child: _ThrowingSelectionSpy(throwKind: _ThrowKind.stateError)),
1898+
TextSpan(text: ' middle '),
1899+
WidgetSpan(child: _ThrowingSelectionSpy(throwKind: _ThrowKind.stateError)),
1900+
TextSpan(text: ' after'),
1901+
],
1902+
),
1903+
),
1904+
),
1905+
),
1906+
);
1907+
1908+
expect(tester.takeException(), isNull);
1909+
});
1910+
1911+
testWidgets('_SelectableTextContainerDelegate._compareScreenOrder catches '
1912+
'AssertionError from unlaid-out selectables', (WidgetTester tester) async {
1913+
// Regression test for https://github.com/flutter/flutter/issues/151536
1914+
//
1915+
// Red/green test for the `on AssertionError` catch in text.dart, which
1916+
// covers debug-mode builds where `assert(hasSize)` fires before the
1917+
// `_size ?? throw StateError(...)` expression in RenderBox.size.
1918+
final focusNode = FocusNode();
1919+
addTearDown(focusNode.dispose);
1920+
1921+
await tester.pumpWidget(
1922+
TestWidgetsApp(
1923+
home: SelectableRegion(
1924+
focusNode: focusNode,
1925+
selectionControls: EmptyTextSelectionControls(),
1926+
child: const Text.rich(
1927+
TextSpan(
1928+
children: <InlineSpan>[
1929+
TextSpan(text: 'before '),
1930+
WidgetSpan(child: _ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError)),
1931+
TextSpan(text: ' middle '),
1932+
WidgetSpan(child: _ThrowingSelectionSpy(throwKind: _ThrowKind.assertionError)),
1933+
TextSpan(text: ' after'),
1934+
],
1935+
),
1936+
),
1937+
),
1938+
),
1939+
);
1940+
1941+
expect(tester.takeException(), isNull);
1942+
});
18741943
}
18751944

18761945
void _noopRemovePage(Page<Object?> page) {}
18771946

1947+
// Mock Selectable whose [boundingBoxes] getter throws — used by the text.dart
1948+
// `_SelectableTextContainerDelegate._compareScreenOrder` regression tests for
1949+
// https://github.com/flutter/flutter/issues/151536 to directly exercise the
1950+
// `on StateError` / `on AssertionError` catch branches.
1951+
enum _ThrowKind { stateError, assertionError }
1952+
1953+
class _ThrowingSelectionSpy extends LeafRenderObjectWidget {
1954+
const _ThrowingSelectionSpy({required this.throwKind});
1955+
1956+
final _ThrowKind throwKind;
1957+
1958+
@override
1959+
RenderObject createRenderObject(BuildContext context) {
1960+
return _RenderThrowingSelectionSpy(SelectionContainer.maybeOf(context), throwKind);
1961+
}
1962+
1963+
@override
1964+
void updateRenderObject(BuildContext context, covariant RenderObject renderObject) {}
1965+
}
1966+
1967+
class _RenderThrowingSelectionSpy extends RenderProxyBox with Selectable, SelectionRegistrant {
1968+
_RenderThrowingSelectionSpy(SelectionRegistrar? registrar, this._throwKind) {
1969+
this.registrar = registrar;
1970+
}
1971+
1972+
final _ThrowKind _throwKind;
1973+
1974+
@override
1975+
List<Rect> get boundingBoxes {
1976+
switch (_throwKind) {
1977+
case _ThrowKind.stateError:
1978+
throw StateError('Bad state: RenderBox was not laid out (test)');
1979+
case _ThrowKind.assertionError:
1980+
throw AssertionError('RenderBox was not laid out (test)');
1981+
}
1982+
}
1983+
1984+
@override
1985+
Size computeDryLayout(BoxConstraints constraints) => constraints.smallest;
1986+
1987+
@override
1988+
void performLayout() => size = computeDryLayout(constraints);
1989+
1990+
@override
1991+
void addListener(VoidCallback listener) {}
1992+
1993+
@override
1994+
void removeListener(VoidCallback listener) {}
1995+
1996+
@override
1997+
SelectionResult dispatchSelectionEvent(SelectionEvent event) => SelectionResult.end;
1998+
1999+
@override
2000+
SelectedContent? getSelectedContent() => null;
2001+
2002+
@override
2003+
SelectedContentRange? getSelection() => null;
2004+
2005+
@override
2006+
int get contentLength => 0;
2007+
2008+
@override
2009+
final SelectionGeometry value = const SelectionGeometry(
2010+
hasContent: true,
2011+
status: SelectionStatus.uncollapsed,
2012+
startSelectionPoint: SelectionPoint(
2013+
localPosition: Offset.zero,
2014+
lineHeight: 0.0,
2015+
handleType: TextSelectionHandleType.left,
2016+
),
2017+
endSelectionPoint: SelectionPoint(
2018+
localPosition: Offset.zero,
2019+
lineHeight: 0.0,
2020+
handleType: TextSelectionHandleType.left,
2021+
),
2022+
);
2023+
2024+
@override
2025+
void pushHandleLayers(LayerLink? startHandle, LayerLink? endHandle) {}
2026+
}
2027+
18782028
Future<void> _pumpTextWidget({
18792029
required WidgetTester tester,
18802030
required String text,

0 commit comments

Comments
 (0)