From eef9f7129e7e722a9ffc3494b9d8020fdb3a6705 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:56:39 +0800 Subject: [PATCH] Add test for `navigator_state.restorable_push_replacement.0.dart` (#157668) Contributes to https://github.com/flutter/flutter/issues/130459 It adds a test for - `examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart` --- dev/bots/check_code_samples.dart | 1 - ...r_state.restorable_push_replacement.0.dart | 24 ++++++++++-- ...te.restorable_push_replacement.0_test.dart | 39 +++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index ce181f6858..ae636580f2 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -310,7 +310,6 @@ class SampleChecker { // See https://github.com/flutter/flutter/issues/130459 final Set _knownMissingTests = { 'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart', - 'examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.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/navigator/navigator_state.restorable_push.0_test.dart', diff --git a/examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart b/examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart index b22315a2f5..75a191e030 100644 --- a/examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart +++ b/examples/api/lib/widgets/navigator/navigator_state.restorable_push_replacement.0.dart @@ -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 createState() => _RestorablePushReplacementExampleState(); @@ -30,7 +39,9 @@ class _RestorablePushReplacementExampleState extends State _myRouteBuilder(BuildContext context, Object? arguments) { return MaterialPageRoute( - builder: (BuildContext context) => const RestorablePushReplacementExample(), + builder: (BuildContext context) => const RestorablePushReplacementExample( + wasPushed: true, + ), ); } @@ -40,6 +51,11 @@ class _RestorablePushReplacementExampleState extends State Navigator.of(context).restorablePushReplacement( _myRouteBuilder, diff --git a/examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.0_test.dart b/examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.0_test.dart new file mode 100644 index 0000000000..8ad4a18fdd --- /dev/null +++ b/examples/api/test/widgets/navigator/navigator_state.restorable_push_replacement.0_test.dart @@ -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_state.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); + }); +}