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
5import 'package:flutter/material.dart';
6import 'package:flutter_api_samples/material/snack_bar/snack_bar.2.dart' as example;
7import 'package:flutter_test/flutter_test.dart';
8
9void main() {
10 testWidgets('Shows correct static elements', (WidgetTester tester) async {
11 await tester.pumpWidget(const example.SnackBarExampleApp());
12
13 expect(find.byType(SnackBar), findsNothing);
14 expect(find.widgetWithText(AppBar, 'SnackBar Sample'), findsOneWidget);
15 expect(find.widgetWithText(FloatingActionButton, 'Show Snackbar'), findsOneWidget);
16 expect(find.text('Behavior'), findsOneWidget);
17 expect(find.text('Fixed'), findsOneWidget);
18 expect(find.text('Floating'), findsOneWidget);
19 expect(find.text('Content'), findsOneWidget);
20 expect(find.widgetWithText(SwitchListTile, 'Include close Icon'), findsOneWidget);
21 expect(find.widgetWithText(SwitchListTile, 'Multi Line Text'), findsOneWidget);
22 expect(find.widgetWithText(SwitchListTile, 'Include Action'), findsOneWidget);
23 expect(find.widgetWithText(SwitchListTile, 'Long Action Label'), findsOneWidget);
24
25 await tester.scrollUntilVisible(find.byType(Slider), 30);
26 expect(find.text('Action new-line overflow threshold'), findsOneWidget);
27 expect(find.byType(Slider), findsOneWidget);
28 });
29
30 testWidgets('Applies default configuration to snackbar', (WidgetTester tester) async {
31 await tester.pumpWidget(const example.SnackBarExampleApp());
32
33 expect(find.byType(SnackBar), findsNothing);
34 expect(find.text('Single Line Snack Bar'), findsNothing);
35 expect(find.textContaining('spans across multiple lines'), findsNothing);
36 expect(find.text('Long Action Text'), findsNothing);
37 expect(find.text('Action'), findsNothing);
38 expect(find.byIcon(Icons.close), findsNothing);
39
40 await tester.tap(find.text('Show Snackbar'));
41 await tester.pumpAndSettle();
42
43 expect(find.byType(SnackBar), findsOneWidget);
44 expect(find.text('Single Line Snack Bar'), findsOneWidget);
45 expect(find.text('Action'), findsOneWidget);
46 expect(find.byIcon(Icons.close), findsOneWidget);
47 expect(tester.widget<SnackBar>(find.byType(SnackBar)).behavior, SnackBarBehavior.floating);
48
49 await tester.tap(find.byIcon(Icons.close));
50 await tester.pumpAndSettle();
51 expect(find.byType(SnackBar), findsNothing);
52 });
53
54 testWidgets('Can configure fixed snack bar with long text', (WidgetTester tester) async {
55 await tester.pumpWidget(const example.SnackBarExampleApp());
56
57 await tester.tap(find.text('Fixed'));
58 await tester.tap(find.text('Multi Line Text'));
59 await tester.tap(find.text('Long Action Label'));
60 await tester.tap(find.text('Show Snackbar'));
61 await tester.pumpAndSettle();
62
63 expect(find.byType(SnackBar), findsOneWidget);
64 expect(find.textContaining('spans across multiple lines'), findsOneWidget);
65 expect(find.text('Long Action Text'), findsOneWidget);
66 expect(find.byIcon(Icons.close), findsOneWidget);
67 expect(tester.widget<SnackBar>(find.byType(SnackBar)).behavior, SnackBarBehavior.fixed);
68
69 await tester.tap(find.byIcon(Icons.close));
70 await tester.pumpAndSettle();
71 expect(find.byType(SnackBar), findsNothing);
72 });
73
74 testWidgets('Can configure to remove action and close icon', (WidgetTester tester) async {
75 await tester.pumpWidget(const example.SnackBarExampleApp());
76
77 await tester.tap(find.text('Include close Icon'));
78 await tester.tap(find.text('Include Action'));
79 await tester.tap(find.text('Show Snackbar'));
80 await tester.pumpAndSettle();
81
82 expect(find.byType(SnackBar), findsOneWidget);
83 expect(find.byType(SnackBarAction), findsNothing);
84 expect(find.byIcon(Icons.close), findsNothing);
85 });
86
87 testWidgets('Higher overflow threshold leads to smaller snack bars', (WidgetTester tester) async {
88 await tester.pumpWidget(const example.SnackBarExampleApp());
89
90 await tester.tap(find.text('Fixed'));
91 await tester.tap(find.text('Multi Line Text'));
92 await tester.tap(find.text('Long Action Label'));
93
94 // Establish max size with low threshold (causes overflow)
95 await tester.scrollUntilVisible(find.byType(Slider), 30);
96 TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
97 await gesture.moveTo(tester.getBottomLeft(find.byType(Slider)));
98 await gesture.up();
99 await tester.tapAt(tester.getBottomLeft(find.byType(Slider)));
100 await tester.tap(find.text('Show Snackbar'));
101 await tester.pumpAndSettle();
102
103 final double highSnackBar = tester.getSize(find.byType(SnackBar)).height;
104 await tester.tap(find.byIcon(Icons.close));
105 await tester.pumpAndSettle();
106
107 // Configure high threshold (everything is in one row)
108 gesture = await tester.startGesture(tester.getCenter(find.byType(Slider)));
109 await gesture.moveTo(tester.getTopRight(find.byType(Slider)));
110 await gesture.up();
111 await tester.tapAt(tester.getTopRight(find.byType(Slider)));
112 await tester.tap(find.text('Show Snackbar'));
113 await tester.pumpAndSettle();
114
115 expect(tester.getSize(find.byType(SnackBar)).height, lessThan(highSnackBar));
116 });
117
118 testWidgets('Disable unusable elements', (WidgetTester tester) async {
119 await tester.pumpWidget(const example.SnackBarExampleApp());
120
121 expect(find.text('Long Action Label'), findsOneWidget);
122 expect(
123 tester
124 .widget<SwitchListTile>(
125 find.ancestor(
126 of: find.text('Long Action Label'),
127 matching: find.byType(SwitchListTile),
128 ),
129 )
130 .onChanged,
131 isNotNull,
132 );
133
134 await tester.tap(find.text('Include Action'));
135 await tester.pumpAndSettle();
136
137 expect(
138 tester
139 .widget<SwitchListTile>(
140 find.ancestor(
141 of: find.text('Long Action Label'),
142 matching: find.byType(SwitchListTile),
143 ),
144 )
145 .onChanged,
146 isNull,
147 );
148 });
149}
150