Add test for navigator.restorable_push_replacement.0.dart (#157704)

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

It adds a test for
- `examples/api/lib/widgets/navigator/navigator.restorable_push_replacement.0.dart`
This commit is contained in:
Valentin Vignal 2024-10-28 19:23:30 +08:00 committed by GitHub
parent d9cb4b8c17
commit b9f62f6f9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 6 deletions

View File

@ -310,7 +310,6 @@ class SampleChecker {
// See https://github.com/flutter/flutter/issues/130459
final Set<String> _knownMissingTests = <String>{
'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart',
'examples/api/test/widgets/navigator/navigator.restorable_push_replacement.0_test.dart',
'examples/api/test/widgets/navigator/restorable_route_future.0_test.dart',
'examples/api/test/widgets/nested_scroll_view/nested_scroll_view_state.0_test.dart',
'examples/api/test/widgets/scroll_position/scroll_metrics_notification.0_test.dart',

View File

@ -4,7 +4,7 @@
import 'package:flutter/material.dart';
/// Flutter code sample for [Navigator.restorablePushReplacement].
/// Flutter code sample for [NavigatorState.restorablePushReplacement].
void main() => runApp(const RestorablePushReplacementExampleApp());
@ -13,14 +13,23 @@ class RestorablePushReplacementExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: RestorablePushReplacementExample(),
return const RootRestorationScope(
restorationId: 'app',
child: MaterialApp(
restorationScopeId: 'app',
home: RestorablePushReplacementExample(),
),
);
}
}
class RestorablePushReplacementExample extends StatefulWidget {
const RestorablePushReplacementExample({super.key});
const RestorablePushReplacementExample({
this.wasPushed = false,
super.key,
});
final bool wasPushed;
@override
State<RestorablePushReplacementExample> createState() => _RestorablePushReplacementExampleState();
@ -30,7 +39,9 @@ class _RestorablePushReplacementExampleState extends State<RestorablePushReplace
@pragma('vm:entry-point')
static Route<void> _myRouteBuilder(BuildContext context, Object? arguments) {
return MaterialPageRoute<void>(
builder: (BuildContext context) => const RestorablePushReplacementExample(),
builder: (BuildContext context) => const RestorablePushReplacementExample(
wasPushed: true,
),
);
}
@ -40,6 +51,11 @@ class _RestorablePushReplacementExampleState extends State<RestorablePushReplace
appBar: AppBar(
title: const Text('Sample Code'),
),
body: Center(
child: widget.wasPushed
? const Text('This is a new route.')
: const Text('This is the initial route.'),
),
floatingActionButton: FloatingActionButton(
onPressed: () => Navigator.restorablePushReplacement(context, _myRouteBuilder),
tooltip: 'Increment Counter',

View File

@ -0,0 +1,39 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/navigator/navigator.restorable_push_replacement.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('It pushes a restorable route and restores it', (WidgetTester tester) async {
await tester.pumpWidget(
const example.RestorablePushReplacementExampleApp(),
);
expect(find.widgetWithText(AppBar, 'Sample Code'), findsOne);
expect(find.text('This is the initial route.'), findsOne);
final TestRestorationData initialData = await tester.getRestorationData();
await tester.tap(find.widgetWithIcon(FloatingActionButton, Icons.add));
await tester.pumpAndSettle();
expect(find.text('This is a new route.'), findsOne);
await tester.restartAndRestore();
expect(find.text('This is a new route.'), findsOne);
final TestRestorationData pushedData = await tester.getRestorationData();
await tester.restoreFrom(initialData);
await tester.pumpAndSettle();
expect(find.text('This is the initial route.'), findsOne);
await tester.restoreFrom(pushedData);
expect(find.text('This is a new route.'), findsOne);
});
}