From c4c0d976f129afda66ace5d85ea6c077e50206a0 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Wed, 31 Jul 2024 22:59:13 +0800 Subject: [PATCH] Add tests for scaffold messenger examples (#152536) Contributes to https://github.com/flutter/flutter/issues/130459 It adds a test for - `examples/api/lib/material/scaffold/scaffold_messenger.0.dart` - `examples/api/lib/material/scaffold/scaffold_messenger.of.0.dart` - `examples/api/lib/material/scaffold/scaffold_messenger.of.1.dart` --- dev/bots/check_code_samples.dart | 3 -- .../scaffold/scaffold_messenger.0_test.dart | 22 +++++++++++++ .../scaffold_messenger.of.0_test.dart | 22 +++++++++++++ .../scaffold_messenger.of.1_test.dart | 31 +++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 examples/api/test/material/scaffold/scaffold_messenger.0_test.dart create mode 100644 examples/api/test/material/scaffold/scaffold_messenger.of.0_test.dart create mode 100644 examples/api/test/material/scaffold/scaffold_messenger.of.1_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 9bea7e56740c7..c9e1f7b65a25a 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -322,11 +322,8 @@ final Set _knownMissingTests = { 'examples/api/test/material/search_anchor/search_anchor.1_test.dart', 'examples/api/test/material/search_anchor/search_anchor.2_test.dart', 'examples/api/test/material/selection_area/selection_area.0_test.dart', - 'examples/api/test/material/scaffold/scaffold_messenger.of.0_test.dart', - 'examples/api/test/material/scaffold/scaffold_messenger.0_test.dart', 'examples/api/test/material/scaffold/scaffold_state.show_bottom_sheet.0_test.dart', 'examples/api/test/material/scaffold/scaffold_messenger_state.show_material_banner.0_test.dart', - 'examples/api/test/material/scaffold/scaffold_messenger.of.1_test.dart', 'examples/api/test/material/scaffold/scaffold_messenger_state.show_snack_bar.0_test.dart', 'examples/api/test/material/app_bar/sliver_app_bar.2_test.dart', 'examples/api/test/material/app_bar/sliver_app_bar.3_test.dart', diff --git a/examples/api/test/material/scaffold/scaffold_messenger.0_test.dart b/examples/api/test/material/scaffold/scaffold_messenger.0_test.dart new file mode 100644 index 0000000000000..ca09f57fc3822 --- /dev/null +++ b/examples/api/test/material/scaffold/scaffold_messenger.0_test.dart @@ -0,0 +1,22 @@ +// 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/material/scaffold/scaffold_messenger.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('The snack bar should be visible after tapping the button', (WidgetTester tester) async { + await tester.pumpWidget( + const example.ScaffoldMessengerExampleApp(), + ); + + expect(find.widgetWithText(AppBar, 'ScaffoldMessenger Sample'), findsOne); + + await tester.tap(find.widgetWithText(OutlinedButton, 'Show SnackBar')); + await tester.pumpAndSettle(); + + expect(find.widgetWithText(SnackBar, 'A SnackBar has been shown.'), findsOne); + }); +} diff --git a/examples/api/test/material/scaffold/scaffold_messenger.of.0_test.dart b/examples/api/test/material/scaffold/scaffold_messenger.of.0_test.dart new file mode 100644 index 0000000000000..77e8f4e0380b0 --- /dev/null +++ b/examples/api/test/material/scaffold/scaffold_messenger.of.0_test.dart @@ -0,0 +1,22 @@ +// 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/material/scaffold/scaffold_messenger.of.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('The snack bar should be visible after tapping the button', (WidgetTester tester) async { + await tester.pumpWidget( + const example.OfExampleApp(), + ); + + expect(find.widgetWithText(AppBar, 'ScaffoldMessenger.of Sample'), findsOne); + + await tester.tap(find.widgetWithText(ElevatedButton, 'SHOW A SNACKBAR')); + await tester.pumpAndSettle(); + + expect(find.widgetWithText(SnackBar, 'Have a snack!'), findsOne); + }); +} diff --git a/examples/api/test/material/scaffold/scaffold_messenger.of.1_test.dart b/examples/api/test/material/scaffold/scaffold_messenger.of.1_test.dart new file mode 100644 index 0000000000000..b48dc0ca8964b --- /dev/null +++ b/examples/api/test/material/scaffold/scaffold_messenger.of.1_test.dart @@ -0,0 +1,31 @@ +// 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/material/scaffold/scaffold_messenger.of.1.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('A snack bar is displayed after 10 taps', (WidgetTester tester) async { + await tester.pumpWidget( + const example.OfExampleApp(), + ); + + expect(find.widgetWithText(AppBar, 'ScaffoldMessenger Demo'), findsOne); + expect(find.text('You have pushed the button this many times:'), findsOne); + + for (int i = 0; i < 9; i++) { + await tester.tap(find.byType(FloatingActionButton)); + await tester.pump(); + expect(find.text('${i + 1}'), findsOne); + } + expect(find.byType(SnackBar), findsNothing); + + await tester.tap(find.byType(FloatingActionButton)); + await tester.pumpAndSettle(); + expect(find.text('10'), findsOne); + + expect(find.widgetWithText(SnackBar, 'A multiple of ten!'), findsOne); + }); +}