Skip to content

Commit

Permalink
Add tests for restorable_value.0.dart API example. (#148676)
Browse files Browse the repository at this point in the history
This PR contributes to flutter/flutter#130459

### Description
- Updates `examples/api/lib/widgets/restoration_properties/restorable_value.0.dart` to meet the latest API examples structure
- Adds tests for `examples/api/lib/widgets/restoration_properties/restorable_value.0.dart`
  • Loading branch information
ksokolovskyi authored May 23, 2024
1 parent fe0932f commit 3beeed4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
1 change: 0 additions & 1 deletion dev/bots/check_code_samples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/scroll_position/scroll_metrics_notification.0_test.dart',
'examples/api/test/widgets/media_query/media_query_data.system_gesture_insets.0_test.dart',
'examples/api/test/widgets/async/future_builder.0_test.dart',
'examples/api/test/widgets/restoration_properties/restorable_value.0_test.dart',
'examples/api/test/widgets/animated_switcher/animated_switcher.0_test.dart',
'examples/api/test/widgets/transitions/relative_positioned_transition.0_test.dart',
'examples/api/test/widgets/transitions/positioned_transition.0_test.dart',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class RestorableValueExampleApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return WidgetsApp(
title: 'RestorableValue Sample',
color: const Color(0xffffffff),
builder: (BuildContext context, Widget? child) {
return const Center(
child: RestorableValueExample(restorationId: 'main'),
);
},
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('RestorableValue Sample'),
),
body: const RestorableValueExample(restorationId: 'main'),
),
);
}
}
Expand Down Expand Up @@ -72,9 +71,11 @@ class _RestorableValueExampleState extends State<RestorableValueExample> with Re

@override
Widget build(BuildContext context) {
return OutlinedButton(
onPressed: _incrementAnswer,
child: Text('${_answer.value}'),
return Center(
child: OutlinedButton(
onPressed: _incrementAnswer,
child: Text('${_answer.value}'),
),
);
}
}
2 changes: 1 addition & 1 deletion examples/api/test/widgets/actions/actions.0_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
return button.style?.foregroundColor?.resolve(<WidgetState>{});
}

testWidgets('increments and decrements value', (WidgetTester tester) async {
testWidgets('Increments and decrements value', (WidgetTester tester) async {
await tester.pumpWidget(
const example.ActionsExampleApp(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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/material.dart';
import 'package:flutter_api_samples/widgets/restoration_properties/restorable_value.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('Increments answer on OutlinedButton tap', (WidgetTester tester) async {
await tester.pumpWidget(
const example.RestorableValueExampleApp(),
);

// Verify that the initial answer value in the example equals 42.
expect(find.text('42'), findsOneWidget);

// Tap the button to increment the answer value by 1.
await tester.tap(find.byType(OutlinedButton));
await tester.pump();

// Verify that the answer value increased by 1.
expect(find.text('43'), findsOneWidget);
});

testWidgets('Restores answer value after restart', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: RootRestorationScope(
restorationId: 'root',
child: example.RestorableValueExample(restorationId: 'child'),
),
),
);

// The initial answer value in the example equals 42.
expect(find.text('42'), findsOneWidget);

// Tap the button 10 times to change the answer value.
for (int i = 0; i < 10; i++) {
await tester.tap(find.byType(OutlinedButton));
await tester.pump();
}

// Verify that the answer value increased by 10.
expect(find.text('52'), findsOneWidget);

// Simulate restoring the state of the widget tree after the application
// is restarted.
await tester.restartAndRestore();

// Verify that the answer value is restored correctly.
expect(find.text('52'), findsOneWidget);
});
}

0 comments on commit 3beeed4

Please sign in to comment.