Skip to content

Commit 3beeed4

Browse files
authored
Add tests for restorable_value.0.dart API example. (flutter#148676)
This PR contributes to 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`
1 parent fe0932f commit 3beeed4

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ final Set<String> _knownMissingTests = <String>{
388388
'examples/api/test/widgets/scroll_position/scroll_metrics_notification.0_test.dart',
389389
'examples/api/test/widgets/media_query/media_query_data.system_gesture_insets.0_test.dart',
390390
'examples/api/test/widgets/async/future_builder.0_test.dart',
391-
'examples/api/test/widgets/restoration_properties/restorable_value.0_test.dart',
392391
'examples/api/test/widgets/animated_switcher/animated_switcher.0_test.dart',
393392
'examples/api/test/widgets/transitions/relative_positioned_transition.0_test.dart',
394393
'examples/api/test/widgets/transitions/positioned_transition.0_test.dart',

examples/api/lib/widgets/restoration_properties/restorable_value.0.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ class RestorableValueExampleApp extends StatelessWidget {
1313

1414
@override
1515
Widget build(BuildContext context) {
16-
return WidgetsApp(
17-
title: 'RestorableValue Sample',
18-
color: const Color(0xffffffff),
19-
builder: (BuildContext context, Widget? child) {
20-
return const Center(
21-
child: RestorableValueExample(restorationId: 'main'),
22-
);
23-
},
16+
return MaterialApp(
17+
home: Scaffold(
18+
appBar: AppBar(
19+
title: const Text('RestorableValue Sample'),
20+
),
21+
body: const RestorableValueExample(restorationId: 'main'),
22+
),
2423
);
2524
}
2625
}
@@ -72,9 +71,11 @@ class _RestorableValueExampleState extends State<RestorableValueExample> with Re
7271

7372
@override
7473
Widget build(BuildContext context) {
75-
return OutlinedButton(
76-
onPressed: _incrementAnswer,
77-
child: Text('${_answer.value}'),
74+
return Center(
75+
child: OutlinedButton(
76+
onPressed: _incrementAnswer,
77+
child: Text('${_answer.value}'),
78+
),
7879
);
7980
}
8081
}

examples/api/test/widgets/actions/actions.0_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void main() {
2121
return button.style?.foregroundColor?.resolve(<WidgetState>{});
2222
}
2323

24-
testWidgets('increments and decrements value', (WidgetTester tester) async {
24+
testWidgets('Increments and decrements value', (WidgetTester tester) async {
2525
await tester.pumpWidget(
2626
const example.ActionsExampleApp(),
2727
);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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/restoration_properties/restorable_value.0.dart'
7+
as example;
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
void main() {
11+
testWidgets('Increments answer on OutlinedButton tap', (WidgetTester tester) async {
12+
await tester.pumpWidget(
13+
const example.RestorableValueExampleApp(),
14+
);
15+
16+
// Verify that the initial answer value in the example equals 42.
17+
expect(find.text('42'), findsOneWidget);
18+
19+
// Tap the button to increment the answer value by 1.
20+
await tester.tap(find.byType(OutlinedButton));
21+
await tester.pump();
22+
23+
// Verify that the answer value increased by 1.
24+
expect(find.text('43'), findsOneWidget);
25+
});
26+
27+
testWidgets('Restores answer value after restart', (WidgetTester tester) async {
28+
await tester.pumpWidget(
29+
const MaterialApp(
30+
home: RootRestorationScope(
31+
restorationId: 'root',
32+
child: example.RestorableValueExample(restorationId: 'child'),
33+
),
34+
),
35+
);
36+
37+
// The initial answer value in the example equals 42.
38+
expect(find.text('42'), findsOneWidget);
39+
40+
// Tap the button 10 times to change the answer value.
41+
for (int i = 0; i < 10; i++) {
42+
await tester.tap(find.byType(OutlinedButton));
43+
await tester.pump();
44+
}
45+
46+
// Verify that the answer value increased by 10.
47+
expect(find.text('52'), findsOneWidget);
48+
49+
// Simulate restoring the state of the widget tree after the application
50+
// is restarted.
51+
await tester.restartAndRestore();
52+
53+
// Verify that the answer value is restored correctly.
54+
expect(find.text('52'), findsOneWidget);
55+
});
56+
}

0 commit comments

Comments
 (0)