Skip to content

Commit

Permalink
Cover more test/widgets tests with leak tracking #10 (flutter#135143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksokolovskyi committed Sep 20, 2023
1 parent 9b767e6 commit 96a4ae9
Show file tree
Hide file tree
Showing 33 changed files with 506 additions and 294 deletions.
5 changes: 3 additions & 2 deletions packages/flutter/test/widgets/sliver_fill_viewport_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

void main() {
testWidgets('SliverFillViewport control test', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverFillViewport control test', (WidgetTester tester) async {
final List<Widget> children = List<Widget>.generate(20, (int i) {
return ColoredBox(color: Colors.green, child: Text('$i', textDirection: TextDirection.ltr));
});
Expand Down Expand Up @@ -158,7 +159,7 @@ void main() {
);
});

testWidgets('SliverFillViewport padding test', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverFillViewport padding test', (WidgetTester tester) async {
final SliverChildListDelegate delegate = SliverChildListDelegate(
<Widget>[
const Text('0'),
Expand Down
37 changes: 25 additions & 12 deletions packages/flutter/test/widgets/sliver_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

void main() {
testWidgets('SliverList reverse children (with keys)', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList reverse children (with keys)', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
const double itemHeight = 300.0;
const double viewportHeight = 500.0;

const double scrollPosition = 18 * itemHeight;
final ScrollController controller = ScrollController(initialScrollOffset: scrollPosition);
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverList(
items: items,
Expand Down Expand Up @@ -53,13 +55,14 @@ void main() {
expect(find.text('Tile 0'), findsNothing);
});

testWidgets('SliverList replace children (with keys)', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList replace children (with keys)', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
const double itemHeight = 300.0;
const double viewportHeight = 500.0;

const double scrollPosition = 18 * itemHeight;
final ScrollController controller = ScrollController(initialScrollOffset: scrollPosition);
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverList(
items: items,
Expand Down Expand Up @@ -105,13 +108,14 @@ void main() {
expect(find.text('Tile 119'), findsNothing);
});

testWidgets('SliverList replace with shorter children list (with keys)', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList replace with shorter children list (with keys)', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
const double itemHeight = 300.0;
const double viewportHeight = 500.0;

final double scrollPosition = items.length * itemHeight - viewportHeight;
final ScrollController controller = ScrollController(initialScrollOffset: scrollPosition);
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverList(
items: items,
Expand Down Expand Up @@ -145,28 +149,33 @@ void main() {
expect(find.text('Tile 19'), findsNothing);
});

testWidgets('SliverList should layout first child in case of child reordering', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList should layout first child in case of child reordering', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/35904.
List<String> items = <String>['1', '2'];

await tester.pumpWidget(_buildSliverListRenderWidgetChild(items));
final ScrollController controller1 = ScrollController();
addTearDown(controller1.dispose);
await tester.pumpWidget(_buildSliverListRenderWidgetChild(items, controller1));
await tester.pumpAndSettle();

expect(find.text('Tile 1'), findsOneWidget);
expect(find.text('Tile 2'), findsOneWidget);

items = items.reversed.toList();
await tester.pumpWidget(_buildSliverListRenderWidgetChild(items));
final ScrollController controller2 = ScrollController();
addTearDown(controller2.dispose);
await tester.pumpWidget(_buildSliverListRenderWidgetChild(items, controller2));
await tester.pumpAndSettle();

expect(find.text('Tile 1'), findsOneWidget);
expect(find.text('Tile 2'), findsOneWidget);
});

testWidgets('SliverList should recalculate inaccurate layout offset case 1', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList should recalculate inaccurate layout offset case 1', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/42142.
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverList(
items: List<int>.from(items),
Expand Down Expand Up @@ -223,10 +232,12 @@ void main() {

});

testWidgets('SliverList should recalculate inaccurate layout offset case 2', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList should recalculate inaccurate layout offset case 2', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/42142.
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverList(
items: List<int>.from(items),
Expand Down Expand Up @@ -275,10 +286,12 @@ void main() {
expect(find.text('Tile 3'), findsOneWidget);
});

testWidgets('SliverList should start to perform layout from the initial child when there is no valid offset', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverList should start to perform layout from the initial child when there is no valid offset', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/66198.
bool isShow = true;
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

Widget buildSliverList(ScrollController controller) {
return Directionality(
textDirection: TextDirection.ltr,
Expand Down Expand Up @@ -335,15 +348,15 @@ void main() {
});
}

Widget _buildSliverListRenderWidgetChild(List<String> items) {
Widget _buildSliverListRenderWidgetChild(List<String> items, ScrollController controller) {
return MaterialApp(
home: Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: SizedBox(
height: 500,
child: CustomScrollView(
controller: ScrollController(),
controller: controller,
slivers: <Widget>[
SliverList(
delegate: SliverChildListDelegate(
Expand Down
57 changes: 41 additions & 16 deletions packages/flutter/test/widgets/sliver_main_axis_group_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import '../rendering/sliver_utils.dart';

Expand All @@ -13,9 +14,10 @@ const double VIEWPORT_HEIGHT = 600;
const double VIEWPORT_WIDTH = 300;

void main() {
testWidgets('SliverMainAxisGroup is laid out properly', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverMainAxisGroup is laid out properly', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverMainAxisGroup(
Expand Down Expand Up @@ -64,9 +66,10 @@ void main() {
expect(renderGroup.geometry!.hasVisualOverflow, isTrue);
});

testWidgets('SliverMainAxisGroup is laid out properly when reversed', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverMainAxisGroup is laid out properly when reversed', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverMainAxisGroup(
Expand Down Expand Up @@ -116,9 +119,10 @@ void main() {
expect(renderGroup.geometry!.hasVisualOverflow, isTrue);
});

testWidgets('SliverMainAxisGroup is laid out properly when horizontal', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverMainAxisGroup is laid out properly when horizontal', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverMainAxisGroup(
Expand Down Expand Up @@ -173,9 +177,10 @@ void main() {
expect(renderGroup.geometry!.hasVisualOverflow, isTrue);
});

testWidgets('SliverMainAxisGroup is laid out properly when horizontal, reversed', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverMainAxisGroup is laid out properly when horizontal, reversed', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(
_buildSliverMainAxisGroup(
Expand Down Expand Up @@ -231,9 +236,10 @@ void main() {
expect(renderGroup.geometry!.hasVisualOverflow, isTrue);
});

testWidgets('Hit test works properly on various parts of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('Hit test works properly on various parts of SliverMainAxisGroup', (WidgetTester tester) async {
final List<int> items = List<int>.generate(20, (int i) => i);
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

String? clickedTile;

Expand Down Expand Up @@ -303,7 +309,7 @@ void main() {
expect(clickedTile, equals('Group 1 Tile 2'));
});

testWidgets('applyPaintTransform is implemented properly', (WidgetTester tester) async {
testWidgetsWithLeakTracking('applyPaintTransform is implemented properly', (WidgetTester tester) async {
await tester.pumpWidget(_buildSliverMainAxisGroup(
slivers: <Widget>[
const SliverToBoxAdapter(child: Text('first box')),
Expand All @@ -319,8 +325,10 @@ void main() {
expect(second.localToGlobal(Offset.zero), Offset(0, first.size.height));
});

testWidgets('visitChildrenForSemantics visits children in the correct order', (WidgetTester tester) async {
testWidgetsWithLeakTracking('visitChildrenForSemantics visits children in the correct order', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: const <Widget>[
Expand All @@ -344,8 +352,10 @@ void main() {
expect(visitedChildren[1].geometry!.scrollExtent, equals(500));
});

testWidgets('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand All @@ -371,8 +381,10 @@ void main() {
});


testWidgets('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -400,8 +412,10 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
});

testWidgets('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverPinnedPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -429,8 +443,10 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
});

testWidgets('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -465,8 +481,10 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
});

testWidgets('SliverPinnedFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverPinnedFloatingPersistentHeader is painted within bounds of SliverMainAxisGroup with different minExtent/maxExtent', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -502,8 +520,10 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(0.0));
});

testWidgets('SliverAppBar with floating: false, pinned: false, snap: false is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverAppBar with floating: false, pinned: false, snap: false is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -532,8 +552,10 @@ void main() {
expect(renderHeader.geometry!.layoutExtent, equals(0.0));
});

testWidgets('SliverAppBar with floating: true, pinned: false, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverAppBar with floating: true, pinned: false, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -572,8 +594,10 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-50.0));
});

testWidgets('SliverAppBar with floating: true, pinned: true, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverAppBar with floating: true, pinned: true, snap: true is painted within bounds of SliverMainAxisGroup', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

await tester.pumpWidget(_buildSliverMainAxisGroup(
controller: controller,
slivers: <Widget>[
Expand Down Expand Up @@ -612,8 +636,9 @@ void main() {
expect((renderHeader.parentData! as SliverPhysicalParentData).paintOffset.dy, equals(-50.0));
});

testWidgets('SliverMainAxisGroup skips painting invisible children', (WidgetTester tester) async {
testWidgetsWithLeakTracking('SliverMainAxisGroup skips painting invisible children', (WidgetTester tester) async {
final ScrollController controller = ScrollController();
addTearDown(controller.dispose);

int counter = 0;
void incrementCounter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import 'package:flutter/material.dart';
import 'package:flutter/src/rendering/sliver_persistent_header.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

void main() {
testWidgets(
testWidgetsWithLeakTracking(
'_SliverScrollingPersistentHeader should update stretchConfiguration',
(WidgetTester tester) async {
for (final double stretchTriggerOffset in <double>[10.0, 20.0]) {
Expand Down Expand Up @@ -37,7 +38,7 @@ void main() {
expect(render.stretchConfiguration?.stretchTriggerOffset, 20);
});

testWidgets(
testWidgetsWithLeakTracking(
'_SliverPinnedPersistentHeader should update stretchConfiguration',
(WidgetTester tester) async {
for (final double stretchTriggerOffset in <double>[10.0, 20.0]) {
Expand Down Expand Up @@ -68,7 +69,7 @@ void main() {
expect(render.stretchConfiguration?.stretchTriggerOffset, 20);
});

testWidgets(
testWidgetsWithLeakTracking(
'_SliverPinnedPersistentHeader should update showOnScreenConfiguration',
(WidgetTester tester) async {
for (final double maxShowOnScreenExtent in <double>[1000, 2000]) {
Expand Down
Loading

0 comments on commit 96a4ae9

Please sign in to comment.