Add tests for SingleChildScrollView examples (#153548)

Contributes to https://github.com/flutter/flutter/issues/130459

It adds a test for
- `examples/api/lib/widgets/single_child_scroll_view/single_child_scroll_view.0.dart`
- `examples/api/lib/widgets/single_child_scroll_view/single_child_scroll_view.1.dart`

I also fixed a mistake in the documentation
This commit is contained in:
Valentin Vignal 2024-08-21 17:45:32 +08:00 committed by GitHub
parent 23883b13d4
commit 36a391f689
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 102 additions and 2 deletions

View File

@ -355,8 +355,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart',
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart',
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.1_test.dart',
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.0_test.dart',
'examples/api/test/widgets/restoration/restoration_mixin.0_test.dart',
'examples/api/test/widgets/actions/focusable_action_detector.0_test.dart',
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',

View File

@ -0,0 +1,46 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'package:flutter_api_samples/widgets/single_child_scroll_view/single_child_scroll_view.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('The children should be spaced out equally when the screen is big enough', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SingleChildScrollViewExampleApp(),
);
expect(find.text('Fixed Height Content'), findsExactly(2));
expect(tester.getTopLeft(find.byType(Container).first), const Offset(0, 90));
expect(tester.getTopLeft(find.byType(Container).last), const Offset(0, 390));
await tester.fling(find.byType(SingleChildScrollView).last, const Offset(0, -100), 10.0);
// The view should not scroll when the screen is big enough.
expect(tester.getTopLeft(find.byType(Container).first), const Offset(0, 90));
expect(tester.getTopLeft(find.byType(Container).last), const Offset(0, 390));
});
testWidgets('The view should be scrollable when the screen is not big enough', (WidgetTester tester) async {
tester.view
..physicalSize = const Size(400, 200)
..devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
const example.SingleChildScrollViewExampleApp(),
);
expect(find.text('Fixed Height Content'), findsExactly(2));
expect(tester.getTopLeft(find.byType(Container).first), Offset.zero);
expect(tester.getTopLeft(find.byType(Container).last), const Offset(0, 120));
await tester.fling(find.byType(SingleChildScrollView).last, const Offset(0, -40), 10.0);
// The view should scroll when the screen is not big enough.
expect(tester.getTopLeft(find.byType(Container).first), const Offset(0, -40));
expect(tester.getTopLeft(find.byType(Container).last), const Offset(0, 80));
});
}

View File

@ -0,0 +1,56 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'package:flutter_api_samples/widgets/single_child_scroll_view/single_child_scroll_view.1.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('The flexible child should fill the space if the screen is big enough', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SingleChildScrollViewExampleApp(),
);
final Finder fixedHeightFinder = find.widgetWithText(Container, 'Fixed Height Content');
final Finder flexibleHeightFinder = find.widgetWithText(Container, 'Flexible Content');
expect(tester.getTopLeft(fixedHeightFinder), Offset.zero);
expect(tester.getSize(fixedHeightFinder), const Size(800, 120));
expect(tester.getTopLeft(flexibleHeightFinder), const Offset(0, 120));
expect(tester.getSize(flexibleHeightFinder), const Size(800, 480));
await tester.fling(find.byType(SingleChildScrollView).last, const Offset(0, -100), 10.0);
// The view should not scroll when the screen is big enough.
expect(tester.getTopLeft(fixedHeightFinder), Offset.zero);
expect(tester.getSize(fixedHeightFinder), const Size(800, 120));
expect(tester.getTopLeft(flexibleHeightFinder), const Offset(0, 120));
expect(tester.getSize(flexibleHeightFinder), const Size(800, 480));
});
testWidgets('The view should be scrollable when the screen is not big enough', (WidgetTester tester) async {
tester.view
..physicalSize = const Size(400, 200)
..devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
const example.SingleChildScrollViewExampleApp(),
);
final Finder fixedHeightFinder = find.widgetWithText(Container, 'Fixed Height Content');
final Finder flexibleHeightFinder = find.widgetWithText(Container, 'Flexible Content');
expect(tester.getTopLeft(fixedHeightFinder), Offset.zero);
expect(tester.getSize(fixedHeightFinder), const Size(400, 120));
expect(tester.getTopLeft(flexibleHeightFinder), const Offset(0, 120));
expect(tester.getSize(flexibleHeightFinder), const Size(400, 120));
await tester.fling(find.byType(SingleChildScrollView).last, const Offset(0, -40), 10.0);
// The view should scroll when the screen is not big enough.
expect(tester.getTopLeft(fixedHeightFinder), const Offset(0, -40));
expect(tester.getSize(fixedHeightFinder), const Size(400, 120));
expect(tester.getTopLeft(flexibleHeightFinder), const Offset(0, 80));
expect(tester.getSize(flexibleHeightFinder), const Size(400, 120));
});
}