Skip to content

Commit eef9f71

Browse files
Add test for navigator_state.restorable_push_replacement.0.dart (#157668)
Contributes to flutter/flutter#130459 It adds a test for - `examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart`
1 parent 5879a5c commit eef9f71

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ class SampleChecker {
310310
// See https://github.com/flutter/flutter/issues/130459
311311
final Set<String> _knownMissingTests = <String>{
312312
'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart',
313-
'examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.0_test.dart',
314313
'examples/api/test/widgets/navigator/navigator.restorable_push_replacement.0_test.dart',
315314
'examples/api/test/widgets/navigator/restorable_route_future.0_test.dart',
316315
'examples/api/test/widgets/navigator/navigator_state.restorable_push.0_test.dart',

examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ class RestorablePushReplacementExampleApp extends StatelessWidget {
1313

1414
@override
1515
Widget build(BuildContext context) {
16-
return const MaterialApp(
17-
home: RestorablePushReplacementExample(),
16+
return const RootRestorationScope(
17+
restorationId: 'app',
18+
child: MaterialApp(
19+
restorationScopeId: 'app',
20+
home: RestorablePushReplacementExample(),
21+
),
1822
);
1923
}
2024
}
2125

2226
class RestorablePushReplacementExample extends StatefulWidget {
23-
const RestorablePushReplacementExample({super.key});
27+
const RestorablePushReplacementExample({
28+
this.wasPushed = false,
29+
super.key,
30+
});
31+
32+
final bool wasPushed;
2433

2534
@override
2635
State<RestorablePushReplacementExample> createState() => _RestorablePushReplacementExampleState();
@@ -30,7 +39,9 @@ class _RestorablePushReplacementExampleState extends State<RestorablePushReplace
3039
@pragma('vm:entry-point')
3140
static Route<void> _myRouteBuilder(BuildContext context, Object? arguments) {
3241
return MaterialPageRoute<void>(
33-
builder: (BuildContext context) => const RestorablePushReplacementExample(),
42+
builder: (BuildContext context) => const RestorablePushReplacementExample(
43+
wasPushed: true,
44+
),
3445
);
3546
}
3647

@@ -40,6 +51,11 @@ class _RestorablePushReplacementExampleState extends State<RestorablePushReplace
4051
appBar: AppBar(
4152
title: const Text('Sample Code'),
4253
),
54+
body: Center(
55+
child: widget.wasPushed
56+
? const Text('This is a new route.')
57+
: const Text('This is the initial route.'),
58+
),
4359
floatingActionButton: FloatingActionButton(
4460
onPressed: () => Navigator.of(context).restorablePushReplacement(
4561
_myRouteBuilder,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/widgets/navigator/navigator_state.restorable_push_replacement.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('It pushes a restorable route and restores it', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.RestorablePushReplacementExampleApp(),
13+
);
14+
15+
expect(find.widgetWithText(AppBar, 'Sample Code'), findsOne);
16+
expect(find.text('This is the initial route.'), findsOne);
17+
18+
final TestRestorationData initialData = await tester.getRestorationData();
19+
20+
await tester.tap(find.widgetWithIcon(FloatingActionButton, Icons.add));
21+
await tester.pumpAndSettle();
22+
23+
expect(find.text('This is a new route.'), findsOne);
24+
25+
await tester.restartAndRestore();
26+
27+
expect(find.text('This is a new route.'), findsOne);
28+
29+
final TestRestorationData pushedData = await tester.getRestorationData();
30+
31+
await tester.restoreFrom(initialData);
32+
33+
await tester.pumpAndSettle();
34+
expect(find.text('This is the initial route.'), findsOne);
35+
36+
await tester.restoreFrom(pushedData);
37+
expect(find.text('This is a new route.'), findsOne);
38+
});
39+
}

0 commit comments

Comments
 (0)