Test snack bar examples (#147774)

Adds tests to the last two Snack Bar examples as part of #130459. Makes the [last example](https://api.flutter.dev/flutter/material/SnackBar-class.html#material.SnackBar.3) more usable through the use of standard widgets and visual hierarchy. Constraints on options that are not required by the SnackBar contract have been removed (Overflow threshold works on fixed SnackBars).
This commit is contained in:
derdilla
2024-05-23 20:54:11 +02:00
committed by GitHub
parent 1c1516c35e
commit aaa4d336f6
5 changed files with 283 additions and 80 deletions

View File

@@ -15,7 +15,7 @@ void main() {
expect(find.widgetWithText(AppBar, 'SnackBar Sample'), findsOneWidget);
expect(find.widgetWithText(ElevatedButton, 'Show Snackbar'), findsOneWidget);
await tester.tap(find.widgetWithText(ElevatedButton, 'Show Snackbar'));
await tester.pumpAndSettle();
await tester.pump();
expect(find.text('Awesome Snackbar!'), findsOneWidget);
expect(find.text('Action'), findsOneWidget);
});

View File

@@ -0,0 +1,60 @@
// 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/snack_bar/snack_bar.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Tapping on button shows snackbar', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
expect(find.byType(SnackBar), findsNothing);
expect(find.widgetWithText(AppBar, 'SnackBar Sample'), findsOneWidget);
await tester.tap(find.widgetWithText(ElevatedButton, 'Show Snackbar'));
await tester.pump();
expect(find.text('Awesome SnackBar!'), findsOneWidget);
expect(find.widgetWithText(SnackBarAction, 'Action'), findsOneWidget);
final SnackBar bar = tester.widget<SnackBar>(find.ancestor(
of: find.text('Awesome SnackBar!'),
matching: find.byType(SnackBar)));
expect(bar.behavior, SnackBarBehavior.floating);
});
testWidgets('Snackbar is styled correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
await tester.tap(find.byType(ElevatedButton));
await tester.pump();
expect(find.byType(SnackBar), findsOneWidget);
final SnackBar bar = tester.widget<SnackBar>(find.byType(SnackBar));
expect(bar.behavior, SnackBarBehavior.floating);
expect(bar.width, 280.0);
expect(bar.shape, isA<RoundedRectangleBorder>()
.having((RoundedRectangleBorder b) => b.borderRadius, 'radius', BorderRadius.circular(10.0)));
});
testWidgets('Snackbar should disappear after timeout', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
expect(find.byType(SnackBar), findsNothing);
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsOneWidget);
await tester.pump(const Duration(milliseconds: 1500));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsNothing);
});
}

View File

@@ -0,0 +1,154 @@
// 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/snack_bar/snack_bar.2.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Shows correct static elements', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
expect(find.byType(SnackBar), findsNothing);
expect(find.widgetWithText(AppBar, 'SnackBar Sample'), findsOneWidget);
expect(find.widgetWithText(FloatingActionButton, 'Show Snackbar'), findsOneWidget);
expect(find.text('Behavior'), findsOneWidget);
expect(find.text('Fixed'), findsOneWidget);
expect(find.text('Floating'), findsOneWidget);
expect(find.text('Content'), findsOneWidget);
expect(find.widgetWithText(SwitchListTile, 'Include close Icon'), findsOneWidget);
expect(find.widgetWithText(SwitchListTile, 'Multi Line Text'), findsOneWidget);
expect(find.widgetWithText(SwitchListTile, 'Include Action'), findsOneWidget);
expect(find.widgetWithText(SwitchListTile, 'Long Action Label'), findsOneWidget);
await tester.scrollUntilVisible(find.byType(Slider), 30);
expect(find.text('Action new-line overflow threshold'), findsOneWidget);
expect(find.byType(Slider), findsOneWidget);
});
testWidgets('Applies default configuration to snackbar', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
expect(find.byType(SnackBar), findsNothing);
expect(find.text('Single Line Snack Bar'), findsNothing);
expect(find.textContaining('spans across multiple lines'), findsNothing);
expect(find.text('Long Action Text'), findsNothing);
expect(find.text('Action'), findsNothing);
expect(find.byIcon(Icons.close), findsNothing);
await tester.tap(find.text('Show Snackbar'));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsOneWidget);
expect(find.text('Single Line Snack Bar'), findsOneWidget);
expect(find.text('Action'), findsOneWidget);
expect(find.byIcon(Icons.close), findsOneWidget);
expect(tester
.widget<SnackBar>(find.byType(SnackBar))
.behavior,
SnackBarBehavior.floating);
await tester.tap(find.byIcon(Icons.close));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsNothing);
});
testWidgets('Can configure fixed snack bar with long text', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
await tester.tap(find.text('Fixed'));
await tester.tap(find.text('Multi Line Text'));
await tester.tap(find.text('Long Action Label'));
await tester.tap(find.text('Show Snackbar'));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsOneWidget);
expect(find.textContaining('spans across multiple lines'), findsOneWidget);
expect(find.text('Long Action Text'), findsOneWidget);
expect(find.byIcon(Icons.close), findsOneWidget);
expect(tester
.widget<SnackBar>(find.byType(SnackBar))
.behavior,
SnackBarBehavior.fixed);
await tester.tap(find.byIcon(Icons.close));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsNothing);
});
testWidgets('Can configure to remove action and close icon', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
await tester.tap(find.text('Include close Icon'));
await tester.tap(find.text('Include Action'));
await tester.tap(find.text('Show Snackbar'));
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsOneWidget);
expect(find.byType(SnackBarAction), findsNothing);
expect(find.byIcon(Icons.close), findsNothing);
});
testWidgets('Higher overflow threshold leads to smaller snack bars', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
await tester.tap(find.text('Fixed'));
await tester.tap(find.text('Multi Line Text'));
await tester.tap(find.text('Long Action Label'));
// Establish max size with low threshold (causes overflow)
await tester.scrollUntilVisible(find.byType(Slider), 30);
TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
await gesture.moveTo(tester.getBottomLeft(find.byType(Slider)));
await gesture.up();
await tester.tapAt(tester.getBottomLeft(find.byType(Slider)));
await tester.tap(find.text('Show Snackbar'));
await tester.pumpAndSettle();
final double highSnackBar = tester.getSize(find.byType(SnackBar)).height;
await tester.tap(find.byIcon(Icons.close));
await tester.pumpAndSettle();
// Configure high threshold (everything is in one row)
gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
await gesture.moveTo(tester.getTopRight(find.byType(Slider)));
await gesture.up();
await tester.tapAt(tester.getTopRight(find.byType(Slider)));
await tester.tap(find.text('Show Snackbar'));
await tester.pumpAndSettle();
expect(tester.getSize(find.byType(SnackBar)).height,
lessThan(highSnackBar));
});
testWidgets('Disable unusable elements', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SnackBarExampleApp(),
);
expect(find.text('Long Action Label'), findsOneWidget);
expect(tester.widget<SwitchListTile>(find.ancestor(
of: find.text('Long Action Label'),
matching: find.byType(SwitchListTile),
)).onChanged, isNotNull);
await tester.tap(find.text('Include Action'));
await tester.pumpAndSettle();
expect(tester.widget<SwitchListTile>(find.ancestor(
of: find.text('Long Action Label'),
matching: find.byType(SwitchListTile),
)).onChanged, isNull);
});
}