snack_bar_test.dart 51.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/material.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
8 9

void main() {
10
  testWidgets('SnackBar control test', (WidgetTester tester) async {
11
    const String helloSnackBar = 'Hello SnackBar';
12
    const Key tapTarget = Key('tap-target');
13 14 15
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
16
          builder: (BuildContext context) {
17
            return GestureDetector(
18
              onTap: () {
19
                Scaffold.of(context).showSnackBar(const SnackBar(
20
                  content: Text(helloSnackBar),
21
                  duration: Duration(seconds: 2),
22 23 24
                ));
              },
              behavior: HitTestBehavior.opaque,
25
              child: Container(
26 27
                height: 100.0,
                width: 100.0,
28 29
                key: tapTarget,
              ),
30 31
            );
          }
32 33
        ),
      ),
34 35
    ));
    expect(find.text(helloSnackBar), findsNothing);
36
    await tester.tap(find.byKey(tapTarget));
37
    expect(find.text(helloSnackBar), findsNothing);
38
    await tester.pump(); // schedule animation
39
    expect(find.text(helloSnackBar), findsOneWidget);
40
    await tester.pump(); // begin animation
41
    expect(find.text(helloSnackBar), findsOneWidget);
42
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
43
    expect(find.text(helloSnackBar), findsOneWidget);
44
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
45
    expect(find.text(helloSnackBar), findsOneWidget);
46
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
47
    expect(find.text(helloSnackBar), findsOneWidget);
48
    await tester.pump(const Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
49
    await tester.pump(); // begin animation
50
    expect(find.text(helloSnackBar), findsOneWidget); // frame 0 of dismiss animation
51
    await tester.pump(const Duration(milliseconds: 750)); // 3.75s // last frame of animation, snackbar removed from build
52
    expect(find.text(helloSnackBar), findsNothing);
Hixie's avatar
Hixie committed
53 54
  });

55
  testWidgets('SnackBar twice test', (WidgetTester tester) async {
56
    int snackBarCount = 0;
57
    const Key tapTarget = Key('tap-target');
58 59 60
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
61
          builder: (BuildContext context) {
62
            return GestureDetector(
63 64
              onTap: () {
                snackBarCount += 1;
65 66
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text('bar$snackBarCount'),
67
                  duration: const Duration(seconds: 2),
68 69 70
                ));
              },
              behavior: HitTestBehavior.opaque,
71
              child: Container(
72 73
                height: 100.0,
                width: 100.0,
74 75
                key: tapTarget,
              ),
76 77
            );
          }
78 79
        ),
      ),
80 81 82
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
83 84
    await tester.tap(find.byKey(tapTarget)); // queue bar1
    await tester.tap(find.byKey(tapTarget)); // queue bar2
85 86
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
87
    await tester.pump(); // schedule animation for bar1
88 89
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
90
    await tester.pump(); // begin animation
91 92
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
93
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
94 95
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
96
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
97 98
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
99
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
100 101
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
102
    await tester.pump(const Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
103
    await tester.pump(); // begin animation
104 105
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
106
    await tester.pump(const Duration(milliseconds: 750)); // 3.75s // last frame of animation, snackbar removed from build, new snack bar put in its place
107 108
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
109
    await tester.pump(); // begin animation
110 111
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
112
    await tester.pump(const Duration(milliseconds: 750)); // 4.50s // animation last frame; two second timer starts here
113 114
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
115
    await tester.pump(const Duration(milliseconds: 750)); // 5.25s
116 117
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
118
    await tester.pump(const Duration(milliseconds: 750)); // 6.00s
119 120
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
121
    await tester.pump(const Duration(milliseconds: 750)); // 6.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
122
    await tester.pump(); // begin animation
123 124
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
125
    await tester.pump(const Duration(milliseconds: 750)); // 7.50s // last frame of animation, snackbar removed from build, new snack bar put in its place
126 127
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
128
  });
129

130
  testWidgets('SnackBar cancel test', (WidgetTester tester) async {
131
    int snackBarCount = 0;
132
    const Key tapTarget = Key('tap-target');
133
    int time;
134
    ScaffoldFeatureController<SnackBar, SnackBarClosedReason> lastController;
135 136 137
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
138
          builder: (BuildContext context) {
139
            return GestureDetector(
140 141
              onTap: () {
                snackBarCount += 1;
142 143
                lastController = Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text('bar$snackBarCount'),
144
                  duration: Duration(seconds: time),
145 146 147
                ));
              },
              behavior: HitTestBehavior.opaque,
148
              child: Container(
149 150
                height: 100.0,
                width: 100.0,
151 152
                key: tapTarget,
              ),
153 154
            );
          }
155 156
        ),
      ),
157 158 159 160
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
    time = 1000;
161
    await tester.tap(find.byKey(tapTarget)); // queue bar1
162
    final ScaffoldFeatureController<SnackBar, SnackBarClosedReason> firstController = lastController;
163
    time = 2;
164
    await tester.tap(find.byKey(tapTarget)); // queue bar2
165 166
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
167
    await tester.pump(); // schedule animation for bar1
168 169
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
170
    await tester.pump(); // begin animation
171 172
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
173
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
174 175
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
176
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
177 178
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
179
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
180 181
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
182
    await tester.pump(const Duration(milliseconds: 10000)); // 12.25s
183 184
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
185

186
    firstController.close(); // snackbar is manually dismissed
187

188
    await tester.pump(const Duration(milliseconds: 750)); // 13.00s // reverse animation is scheduled
189
    await tester.pump(); // begin animation
190 191
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
192
    await tester.pump(const Duration(milliseconds: 750)); // 13.75s // last frame of animation, snackbar removed from build, new snack bar put in its place
193 194
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
195
    await tester.pump(); // begin animation
196 197
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
198
    await tester.pump(const Duration(milliseconds: 750)); // 14.50s // animation last frame; two second timer starts here
199 200
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
201
    await tester.pump(const Duration(milliseconds: 750)); // 15.25s
202 203
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
204
    await tester.pump(const Duration(milliseconds: 750)); // 16.00s
205 206
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
207
    await tester.pump(const Duration(milliseconds: 750)); // 16.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
208
    await tester.pump(); // begin animation
209 210
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
211
    await tester.pump(const Duration(milliseconds: 750)); // 17.50s // last frame of animation, snackbar removed from build, new snack bar put in its place
212 213
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
214
  });
215

216
  testWidgets('SnackBar dismiss test', (WidgetTester tester) async {
217
    int snackBarCount = 0;
218
    const Key tapTarget = Key('tap-target');
219 220 221
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
222
          builder: (BuildContext context) {
223
            return GestureDetector(
224 225
              onTap: () {
                snackBarCount += 1;
226 227
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text('bar$snackBarCount'),
228
                  duration: const Duration(seconds: 2),
229 230 231
                ));
              },
              behavior: HitTestBehavior.opaque,
232
              child: Container(
233 234
                height: 100.0,
                width: 100.0,
235 236
                key: tapTarget,
              ),
237 238
            );
          }
239 240
        ),
      ),
241 242 243
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
244 245
    await tester.tap(find.byKey(tapTarget)); // queue bar1
    await tester.tap(find.byKey(tapTarget)); // queue bar2
246 247
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
248
    await tester.pump(); // schedule animation for bar1
249 250
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
251
    await tester.pump(); // begin animation
252 253
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
254
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
255
    await tester.drag(find.text('bar1'), const Offset(0.0, 50.0));
256
    await tester.pump(); // bar1 dismissed, bar2 begins animating
257 258
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
259 260
  });

261
  testWidgets('SnackBar cannot be tapped twice', (WidgetTester tester) async {
262
    int tapCount = 0;
263 264 265
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
266
          builder: (BuildContext context) {
267
            return GestureDetector(
268
              onTap: () {
269
                Scaffold.of(context).showSnackBar(SnackBar(
270
                  content: const Text('I am a snack bar.'),
271
                  duration: const Duration(seconds: 2),
272
                  action: SnackBarAction(
273 274 275
                    label: 'ACTION',
                    onPressed: () {
                      ++tapCount;
276 277
                    },
                  ),
278 279
                ));
              },
280
              child: const Text('X'),
281 282
            );
          }
283 284
        ),
      ),
285
    ));
286 287 288
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));
289

290
    expect(tapCount, equals(0));
291
    await tester.tap(find.text('ACTION'));
292
    expect(tapCount, equals(1));
293
    await tester.tap(find.text('ACTION'));
294
    expect(tapCount, equals(1));
295 296
    await tester.pump();
    await tester.tap(find.text('ACTION'));
297
    expect(tapCount, equals(1));
298
  });
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  testWidgets('Light theme SnackBar has dark background', (WidgetTester tester) async {
    final ThemeData lightTheme = ThemeData.light();
    await tester.pumpWidget(
      MaterialApp(
        theme: lightTheme,
        home: Scaffold(
          body: Builder(
              builder: (BuildContext context) {
                return GestureDetector(
                  onTap: () {
                    Scaffold.of(context).showSnackBar(
                      SnackBar(
                        content: const Text('I am a snack bar.'),
                        duration: const Duration(seconds: 2),
                        action: SnackBarAction(
                          label: 'ACTION',
                          onPressed: () { },
                        ),
                      ),
                    );
                  },
                  child: const Text('X'),
                );
              }
          ),
        ),
      ),
    );

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));

    final RenderPhysicalModel renderModel = tester.renderObject(
        find.widgetWithText(Material, 'I am a snack bar.').first
    );
    // There is a somewhat complicated background color calculation based
    // off of the surface color. For the default light theme it
    // should be this value.
    expect(renderModel.color, equals(const Color(0xFF333333)));
  });

  testWidgets('Dark theme SnackBar has light background', (WidgetTester tester) async {
    final ThemeData darkTheme = ThemeData.dark();
    await tester.pumpWidget(
      MaterialApp(
        theme: darkTheme,
        home: Scaffold(
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: const Text('I am a snack bar.'),
                      duration: const Duration(seconds: 2),
                      action: SnackBarAction(
                        label: 'ACTION',
                        onPressed: () { },
                      ),
                    ),
                  );
                },
                child: const Text('X'),
              );
            }
          ),
        ),
      ),
    );

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));

    final RenderPhysicalModel renderModel = tester.renderObject(
      find.widgetWithText(Material, 'I am a snack bar.').first
    );
    expect(renderModel.color, equals(darkTheme.colorScheme.onSurface));
  });

jslavitz's avatar
jslavitz committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  testWidgets('Snackbar labels can be colored', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: const Text('I am a snack bar.'),
                      duration: const Duration(seconds: 2),
                      action: SnackBarAction(
                        textColor: Colors.lightBlue,
                        disabledTextColor: Colors.red,
                        label: 'ACTION',
397
                        onPressed: () { },
jslavitz's avatar
jslavitz committed
398 399 400 401
                      ),
                    ),
                  );
                },
402
                child: const Text('X'),
jslavitz's avatar
jslavitz committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
              );
            }
          ),
        ),
      ),
    );

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));

    final Element actionTextBox = tester.element(find.text('ACTION'));
    final Widget textWidget = actionTextBox.widget;
    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(actionTextBox);
    if (textWidget is Text) {
      TextStyle effectiveStyle = textWidget.style;
      effectiveStyle = defaultTextStyle.style.merge(textWidget.style);
      expect(effectiveStyle.color, Colors.lightBlue);
421
    } else {
jslavitz's avatar
jslavitz committed
422
      expect(false, true);
423
    }
jslavitz's avatar
jslavitz committed
424 425
  });

426
  testWidgets('SnackBar button text alignment', (WidgetTester tester) async {
427 428
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
429
        data: const MediaQueryData(
430
          padding: EdgeInsets.only(
431 432 433 434 435 436
            left: 10.0,
            top: 20.0,
            right: 30.0,
            bottom: 40.0,
          ),
        ),
437 438
        child: Scaffold(
          body: Builder(
439
            builder: (BuildContext context) {
440
              return GestureDetector(
441
                onTap: () {
442
                  Scaffold.of(context).showSnackBar(SnackBar(
443 444
                    content: const Text('I am a snack bar.'),
                    duration: const Duration(seconds: 2),
445
                    action: SnackBarAction(label: 'ACTION', onPressed: () { }),
446 447
                  ));
                },
448
                child: const Text('X'),
449 450 451 452 453
              );
            }
          ),
        ),
      ),
454 455 456
    ));
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
457
    await tester.pump(const Duration(milliseconds: 750)); // Animation last frame.
458

459 460 461 462 463 464
    final Offset textBottomLeft = tester.getBottomLeft(find.text('I am a snack bar.'));
    final Offset textBottomRight = tester.getBottomRight(find.text('I am a snack bar.'));
    final Offset actionTextBottomLeft = tester.getBottomLeft(find.text('ACTION'));
    final Offset actionTextBottomRight = tester.getBottomRight(find.text('ACTION'));
    final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
    final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
465

466
    expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
467
    expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0 + 40.0); // margin + bottom padding
468
    expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
469
    expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
470
    expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0 + 40.0); // margin + bottom padding
471
  });
472

473 474 475 476 477 478 479 480 481 482 483 484
  testWidgets(
    'Custom padding between SnackBar and its contents when set to SnackBarBehavior.fixed',
    (WidgetTester tester) async {
      await tester.pumpWidget(MaterialApp(
        home: MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.only(
              left: 10.0,
              top: 20.0,
              right: 30.0,
              bottom: 40.0,
            ),
485
          ),
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(icon: Icon(Icons.favorite), title: Text('Animutation')),
                BottomNavigationBarItem(icon: Icon(Icons.block), title: Text('Zombo.com')),
              ],
            ),
            body: Builder(
              builder: (BuildContext context) {
                return GestureDetector(
                  onTap: () {
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: const Text('I am a snack bar.'),
                      duration: const Duration(seconds: 2),
                      action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                    ));
                  },
                  child: const Text('X'),
                );
              }
            ),
507 508
          ),
        ),
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
      ));
      await tester.tap(find.text('X'));
      await tester.pump(); // start animation
      await tester.pump(const Duration(milliseconds: 750)); // Animation last frame.

      final Offset textBottomLeft = tester.getBottomLeft(find.text('I am a snack bar.'));
      final Offset textBottomRight = tester.getBottomRight(find.text('I am a snack bar.'));
      final Offset actionTextBottomLeft = tester.getBottomLeft(find.text('ACTION'));
      final Offset actionTextBottomRight = tester.getBottomRight(find.text('ACTION'));
      final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
      final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));

      expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
      expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0); // margin (with no bottom padding)
      expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
      expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
      expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0); // margin (with no bottom padding)
526
    });
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541
  testWidgets('SnackBar should push FloatingActionButton above', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(
            left: 10.0,
            top: 20.0,
            right: 30.0,
            bottom: 40.0,
          ),
        ),
        child: Scaffold(
          floatingActionButton: FloatingActionButton(
            child: const Icon(Icons.send),
542
            onPressed: () {},
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
          ),
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).showSnackBar(SnackBar(
                    content: const Text('I am a snack bar.'),
                    duration: const Duration(seconds: 2),
                    action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                  ));
                },
                child: const Text('X'),
              );
            }
          ),
        ),
      ),
    ));

562 563
    // Get the Rect of the FAB to compare after the SnackBar appears.
    final Rect originalFabRect = tester.getRect(find.byType(FloatingActionButton));
564 565 566 567 568

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750)); // Animation last frame.

569
    final Rect fabRect = tester.getRect(find.byType(FloatingActionButton));
570

571 572
    // FAB should shift upwards after SnackBar appears.
    expect(fabRect.center.dy, lessThan(originalFabRect.center.dy));
573

574
    final Offset snackBarTopRight = tester.getTopRight(find.byType(SnackBar));
575

576 577
    // FAB's surrounding padding is set to [kFloatingActionButtonMargin] in floating_action_button_location.dart by default.
    const int defaultFabPadding = 16;
578

579 580 581
    // FAB should be positioned above the SnackBar by the default padding.
    expect(fabRect.bottomRight.dy, snackBarTopRight.dy - defaultFabPadding);
  });
582

583
  testWidgets('Floating SnackBar button text alignment', (WidgetTester tester) async {
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(
        snackBarTheme: const SnackBarThemeData(behavior: SnackBarBehavior.floating,)
      ),
      home: MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(
            left: 10.0,
            top: 20.0,
            right: 30.0,
            bottom: 40.0,
          ),
        ),
        child: Scaffold(
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).showSnackBar(SnackBar(
                    content: const Text('I am a snack bar.'),
                    duration: const Duration(seconds: 2),
                    action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                  ));
                },
                child: const Text('X'),
              );
            }
          ),
        ),
      ),
    ));
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750)); // Animation last frame.

    final Offset textBottomLeft = tester.getBottomLeft(find.text('I am a snack bar.'));
    final Offset textBottomRight = tester.getBottomRight(find.text('I am a snack bar.'));
    final Offset actionTextBottomLeft = tester.getBottomLeft(find.text('ACTION'));
    final Offset actionTextBottomRight = tester.getBottomRight(find.text('ACTION'));
    final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
    final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));

    expect(textBottomLeft.dx - snackBarBottomLeft.dx, 31.0 + 10.0); // margin + left padding
    expect(snackBarBottomLeft.dy - textBottomLeft.dy, 27.0); // margin (with no bottom padding)
    expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0);
    expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0); // margin + right padding
    expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 27.0); // margin (with no bottom padding)
631
  });
632

633 634 635 636 637 638
  testWidgets(
    'Custom padding between SnackBar and its contents when set to SnackBarBehavior.floating',
    (WidgetTester tester) async {
      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(
          snackBarTheme: const SnackBarThemeData(behavior: SnackBarBehavior.floating)
639
        ),
640 641 642 643 644 645 646 647
        home: MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.only(
              left: 10.0,
              top: 20.0,
              right: 30.0,
              bottom: 40.0,
            ),
648
          ),
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
          child: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(icon: Icon(Icons.favorite), title: Text('Animutation')),
                BottomNavigationBarItem(icon: Icon(Icons.block), title: Text('Zombo.com')),
              ],
            ),
            body: Builder(
              builder: (BuildContext context) {
                return GestureDetector(
                  onTap: () {
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: const Text('I am a snack bar.'),
                      duration: const Duration(seconds: 2),
                      action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                    ));
                  },
                  child: const Text('X'),
                );
              }
            ),
670 671
          ),
        ),
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
      ));
      await tester.tap(find.text('X'));
      await tester.pump(); // start animation
      await tester.pump(const Duration(milliseconds: 750)); // Animation last frame.

      final Offset textBottomLeft = tester.getBottomLeft(find.text('I am a snack bar.'));
      final Offset textBottomRight = tester.getBottomRight(find.text('I am a snack bar.'));
      final Offset actionTextBottomLeft = tester.getBottomLeft(find.text('ACTION'));
      final Offset actionTextBottomRight = tester.getBottomRight(find.text('ACTION'));
      final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
      final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));

      expect(textBottomLeft.dx - snackBarBottomLeft.dx, 31.0 + 10.0); // margin + left padding
      expect(snackBarBottomLeft.dy - textBottomLeft.dy, 27.0); // margin (with no bottom padding)
      expect(actionTextBottomLeft.dx - textBottomRight.dx, 16.0);
      expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 31.0 + 30.0); // margin + right padding
      expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 27.0); // margin (with no bottom padding)
689
    });
690

691
  testWidgets('SnackBarClosedReason', (WidgetTester tester) async {
692
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
693 694 695
    bool actionPressed = false;
    SnackBarClosedReason closedReason;

696 697
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
698
        key: scaffoldKey,
699
        body: Builder(
700
          builder: (BuildContext context) {
701
            return GestureDetector(
702
              onTap: () {
703
                Scaffold.of(context).showSnackBar(SnackBar(
704
                  content: const Text('snack'),
705
                  duration: const Duration(seconds: 2),
706
                  action: SnackBarAction(
707 708 709
                    label: 'ACTION',
                    onPressed: () {
                      actionPressed = true;
710
                    },
711
                  ),
712
                )).closed.then<void>((SnackBarClosedReason reason) {
713 714 715
                  closedReason = reason;
                });
              },
716
              child: const Text('X'),
717 718
            );
          },
719 720
        ),
      ),
721 722 723 724 725 726 727 728 729
    ));

    // Pop up the snack bar and then press its action button.
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));
    expect(actionPressed, isFalse);
    await tester.tap(find.text('ACTION'));
    expect(actionPressed, isTrue);
730
    // Closed reason is only set when the animation is complete.
731
    await tester.pump(const Duration(milliseconds: 250));
732 733 734
    expect(closedReason, isNull);
    // Wait for animation to complete.
    await tester.pumpAndSettle(const Duration(seconds: 1));
735 736 737 738 739 740
    expect(closedReason, equals(SnackBarClosedReason.action));

    // Pop up the snack bar and then swipe downwards to dismiss it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
741
    await tester.drag(find.text('snack'), const Offset(0.0, 50.0));
742
    await tester.pumpAndSettle(const Duration(seconds: 1));
743 744 745 746 747 748
    expect(closedReason, equals(SnackBarClosedReason.swipe));

    // Pop up the snack bar and then remove it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    scaffoldKey.currentState.removeCurrentSnackBar();
749
    await tester.pumpAndSettle(const Duration(seconds: 1));
750 751 752 753 754 755
    expect(closedReason, equals(SnackBarClosedReason.remove));

    // Pop up the snack bar and then hide it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    scaffoldKey.currentState.hideCurrentSnackBar();
756
    await tester.pumpAndSettle(const Duration(seconds: 1));
757 758 759 760
    expect(closedReason, equals(SnackBarClosedReason.hide));

    // Pop up the snack bar and then let it time out.
    await tester.tap(find.text('X'));
761 762 763
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 1500));
764
    await tester.pump(); // begin animation
765
    await tester.pumpAndSettle(const Duration(seconds: 1));
766 767 768
    expect(closedReason, equals(SnackBarClosedReason.timeout));
  });

769
  testWidgets('accessible navigation behavior with action', (WidgetTester tester) async {
770
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
771

772 773 774 775 776 777 778 779 780 781 782 783 784 785
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
        data: const MediaQueryData(accessibleNavigation: true),
        child: Scaffold(
          key: scaffoldKey,
          body: Builder(
            builder: (BuildContext context) {
              return GestureDetector(
                onTap: () {
                  Scaffold.of(context).showSnackBar(SnackBar(
                    content: const Text('snack'),
                    duration: const Duration(seconds: 1),
                    action: SnackBarAction(
                      label: 'ACTION',
786
                      onPressed: () { },
787 788 789 790 791 792
                    ),
                  ));
                },
                child: const Text('X'),
              );
            },
793 794
          ),
        ),
795 796 797 798 799 800 801 802 803 804 805 806 807
      ),
    ));
    await tester.tap(find.text('X'));
    await tester.pump();
    // Find action immediately
    expect(find.text('ACTION'), findsOneWidget);
    // Snackbar doesn't close
    await tester.pump(const Duration(seconds: 10));
    expect(find.text('ACTION'), findsOneWidget);
    await tester.tap(find.text('ACTION'));
    await tester.pump();
    // Snackbar closes immediately
    expect(find.text('ACTION'), findsNothing);
808 809 810 811
  });

  testWidgets('contributes dismiss semantics', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
812
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
813

814 815
    await tester.pumpWidget(MaterialApp(
        home: MediaQuery(
816 817 818
            data: const MediaQueryData(accessibleNavigation: true),
            child: Scaffold(
                key: scaffoldKey,
819
                body: Builder(
820
                  builder: (BuildContext context) {
821
                    return GestureDetector(
822
                        onTap: () {
823
                          Scaffold.of(context).showSnackBar(SnackBar(
824 825
                            content: const Text('snack'),
                            duration: const Duration(seconds: 1),
826
                            action: SnackBarAction(
827
                                label: 'ACTION',
828
                                onPressed: () { },
829 830 831
                            ),
                          ));
                        },
832
                        child: const Text('X'),
833 834
                    );
                  },
835 836 837
                ),
            ),
        ),
838 839 840 841
    ));
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

842
    expect(tester.getSemantics(find.text('snack')), matchesSemantics(
843 844 845 846 847 848 849 850 851 852
      isLiveRegion: true,
      hasDismissAction: true,
      hasScrollDownAction: true,
      hasScrollUpAction: true,
      label: 'snack',
      textDirection: TextDirection.ltr,
    ));
    handle.dispose();
  });

jslavitz's avatar
jslavitz committed
853 854
  testWidgets('SnackBar default display duration test', (WidgetTester tester) async {
    const String helloSnackBar = 'Hello SnackBar';
855
    const Key tapTarget = Key('tap-target');
856 857 858
    await tester.pumpWidget(MaterialApp(
        home: Scaffold(
            body: Builder(
jslavitz's avatar
jslavitz committed
859
                builder: (BuildContext context) {
860
                  return GestureDetector(
jslavitz's avatar
jslavitz committed
861 862
                      onTap: () {
                        Scaffold.of(context).showSnackBar(const SnackBar(
863
                            content: Text(helloSnackBar),
jslavitz's avatar
jslavitz committed
864 865 866
                        ));
                      },
                      behavior: HitTestBehavior.opaque,
867
                      child: Container(
jslavitz's avatar
jslavitz committed
868 869
                          height: 100.0,
                          width: 100.0,
870 871
                          key: tapTarget,
                      ),
jslavitz's avatar
jslavitz committed
872 873
                  );
                }
874 875
            ),
        ),
jslavitz's avatar
jslavitz committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    ));
    expect(find.text(helloSnackBar), findsNothing);
    await tester.tap(find.byKey(tapTarget));
    expect(find.text(helloSnackBar), findsNothing);
    await tester.pump(); // schedule animation
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(); // begin animation
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; four second timer starts here
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 750)); // 3.00s
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 750)); // 3.75s
    expect(find.text(helloSnackBar), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 1000)); // 4.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
    await tester.pump(); // begin animation
    expect(find.text(helloSnackBar), findsOneWidget); // frame 0 of dismiss animation
    await tester.pump(const Duration(milliseconds: 750)); // 5.50s // last frame of animation, snackbar removed from build
    expect(find.text(helloSnackBar), findsNothing);
  });

901
  testWidgets('SnackBar handles updates to accessibleNavigation', (WidgetTester tester) async {
902
    Future<void> boilerplate({ bool accessibleNavigation }) {
903 904 905 906 907
      return tester.pumpWidget(MaterialApp(
          home: MediaQuery(
              data: MediaQueryData(accessibleNavigation: accessibleNavigation),
              child: Scaffold(
                  body: Builder(
908
                      builder: (BuildContext context) {
909
                        return GestureDetector(
910
                            onTap: () {
911
                              Scaffold.of(context).showSnackBar(SnackBar(
912
                                  content: const Text('test'),
913
                                  action: SnackBarAction(label: 'foo', onPressed: () { }),
914 915 916 917 918 919
                              ));
                            },
                            behavior: HitTestBehavior.opaque,
                            child: const Text('X'),
                        );
                      }
920 921 922
                  ),
              ),
          ),
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
      ));
    }

    await boilerplate(accessibleNavigation: false);
    expect(find.text('test'), findsNothing);
    await tester.tap(find.text('X'));
    await tester.pump(); // schedule animation
    expect(find.text('test'), findsOneWidget);
    await tester.pump(); // begin animation
    await tester.pump(const Duration(milliseconds: 4750)); // 4.75s
    expect(find.text('test'), findsOneWidget);

    // Enabled accessible navigation
    await boilerplate(accessibleNavigation: true);

    await tester.pump(const Duration(milliseconds: 4000)); // 8.75s
    await tester.pump();
    expect(find.text('test'), findsOneWidget);

    // disable accessible navigation
    await boilerplate(accessibleNavigation: false);
    await tester.pumpAndSettle(const Duration(milliseconds: 5750));

    expect(find.text('test'), findsNothing);
  });

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
  testWidgets('Snackbar asserts if passed a null duration', (WidgetTester tester) async {
    const Key tapTarget = Key('tap-target');
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (BuildContext context) {
            return GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text(nonconst('hello')),
                  duration: null,
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                height: 100.0,
                width: 100.0,
966
                key: tapTarget,
967 968 969 970 971 972 973 974 975 976
              ),
            );
          },
        ),
      ),
    ));

    await tester.tap(find.byKey(tapTarget));
    expect(tester.takeException(), isNotNull);
  });
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  testWidgets('Snackbar calls onVisible once', (WidgetTester tester) async {
    const Key tapTarget = Key('tap-target');
    int called = 0;
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (BuildContext context) {
            return GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: const Text('hello'),
                  duration: const Duration(seconds: 1),
                  onVisible: () {
                    called += 1;
                  },
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget,
              ),
            );
          },
        ),
      ),
    ));

    await tester.tap(find.byKey(tapTarget));
    await tester.pump(); // start animation
    await tester.pumpAndSettle();

    expect(find.text('hello'), findsOneWidget);
    expect(called, 1);
  });

  testWidgets('Snackbar does not call onVisible when it is queued', (WidgetTester tester) async {
    const Key tapTarget = Key('tap-target');
    int called = 0;
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Builder(
          builder: (BuildContext context) {
            return GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: const Text('hello'),
                  duration: const Duration(seconds: 1),
                  onVisible: () {
                    called += 1;
                  },
                ));
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: const Text('hello 2'),
                  duration: const Duration(seconds: 1),
                  onVisible: () {
                    called += 1;
                  },
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget,
              ),
            );
          },
        ),
      ),
    ));

    await tester.tap(find.byKey(tapTarget));
    await tester.pump(); // start animation
    await tester.pumpAndSettle();

    expect(find.text('hello'), findsOneWidget);
    expect(called, 1);
  });
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

  group('SnackBar position', () {
    for (final SnackBarBehavior behavior in SnackBarBehavior.values) {
      final SnackBar snackBar = SnackBar(
        content: const Text('SnackBar text'),
        behavior: behavior,
      );

      testWidgets(
        '$behavior should align SnackBar with the bottom of Scaffold '
        'when Scaffold has no other elements',
        (WidgetTester tester) async {
          await tester.pumpWidget(
            MaterialApp(
              home: Scaffold(
                body: Container(),
              ),
            ),
          );

          final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
          scaffoldState.showSnackBar(snackBar);

          await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

          final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
          final Offset scaffoldBottomRight = tester.getBottomRight(find.byType(Scaffold));

          expect(snackBarBottomRight, equals(scaffoldBottomRight));

          final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
          final Offset scaffoldBottomLeft = tester.getBottomLeft(find.byType(Scaffold));

          expect(snackBarBottomLeft, equals(scaffoldBottomLeft));
        },
      );

      testWidgets(
        '$behavior should align SnackBar with the top of BottomNavigationBar '
        'when Scaffold has no FloatingActionButton',
        (WidgetTester tester) async {
          final UniqueKey boxKey = UniqueKey();
          await tester.pumpWidget(
            MaterialApp(
              home: Scaffold(
                body: Container(),
                bottomNavigationBar: SizedBox(key: boxKey, width: 800, height: 60),
              ),
            ),
          );

          final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
          scaffoldState.showSnackBar(snackBar);

          await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

          final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
          final Offset bottomNavigationBarTopRight = tester.getTopRight(find.byKey(boxKey));

          expect(snackBarBottomRight, equals(bottomNavigationBarTopRight));

          final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
          final Offset bottomNavigationBarTopLeft = tester.getTopLeft(find.byKey(boxKey));

          expect(snackBarBottomLeft, equals(bottomNavigationBarTopLeft));
        },
      );

      testWidgets(
        'Padding of $behavior is not consumed by viewInsets',
        (WidgetTester tester) async {
          final Widget child = Directionality(
            textDirection: TextDirection.ltr,
            child: Scaffold(
              resizeToAvoidBottomInset: false,
              floatingActionButton: FloatingActionButton(
                child: const Icon(Icons.send),
                onPressed: () {},
              ),
              body: Builder(
                builder: (BuildContext context) {
                  return GestureDetector(
                    onTap: () {
                      Scaffold.of(context).showSnackBar(
                        SnackBar(
                          content: const Text('I am a snack bar.'),
                          duration: const Duration(seconds: 2),
                          action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                          behavior: behavior,
                        ),
                      );
                    },
                    child: const Text('X'),
                  );
                },
              ),
            ),
          );

          await tester.pumpWidget(
            MediaQuery(
              data: const MediaQueryData(
                padding: EdgeInsets.only(bottom: 20.0),
              ),
              child: child,
            ),
          );
          await tester.tap(find.text('X'));
          await tester.pumpAndSettle(); // Show snackbar
          final Offset initialBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
          final Offset initialBottomRight = tester.getBottomRight(find.byType(SnackBar));
          // Consume bottom padding - as if by the keyboard opening
          await tester.pumpWidget(
            MediaQuery(
              data: const MediaQueryData(
                padding: EdgeInsets.zero,
                viewPadding: EdgeInsets.all(20),
                viewInsets: EdgeInsets.all(100),
              ),
              child: child,
            ),
          );
          await tester.tap(find.text('X'));
          await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

          final Offset finalBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
          final Offset finalBottomRight = tester.getBottomRight(find.byType(SnackBar));

          expect(initialBottomLeft, finalBottomLeft);
          expect(initialBottomRight, finalBottomRight);
        },
      );
    }

    testWidgets(
      '${SnackBarBehavior.fixed} should align SnackBar with the bottom of Scaffold '
      'when Scaffold has a FloatingActionButton',
      (WidgetTester tester) async {
        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: Container(),
              floatingActionButton: FloatingActionButton(onPressed: () {}),
            ),
          ),
        );

        final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
        scaffoldState.showSnackBar(
          const SnackBar(
            content: Text('Snackbar text'),
            behavior: SnackBarBehavior.fixed,
          ),
        );

        await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

        final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
        final Offset scaffoldBottomRight = tester.getBottomRight(find.byType(Scaffold));

        expect(snackBarBottomRight, equals(scaffoldBottomRight));

        final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
        final Offset scaffoldBottomLeft = tester.getBottomLeft(find.byType(Scaffold));

        expect(snackBarBottomLeft, equals(scaffoldBottomLeft));
      },
    );

    testWidgets(
      '${SnackBarBehavior.floating} should align SnackBar with the top of FloatingActionButton'
      'when Scaffold has a FloatingActionButton',
      (WidgetTester tester) async {
        await tester.pumpWidget(MaterialApp(
          home: Scaffold(
            floatingActionButton: FloatingActionButton(
              child: const Icon(Icons.send),
              onPressed: () {},
            ),
            body: Builder(
              builder: (BuildContext context) {
                return GestureDetector(
                  onTap: () {
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: const Text('I am a snack bar.'),
                      duration: const Duration(seconds: 2),
                      action: SnackBarAction(label: 'ACTION', onPressed: () {}),
                      behavior: SnackBarBehavior.floating,
                    ));
                  },
                  child: const Text('X'),
                );
              },
            ),
          ),
        ));
        await tester.tap(find.text('X'));
        await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

        final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
        final Offset floatingActionButtonTopLeft = tester.getTopLeft(
          find.byType(FloatingActionButton),
        );

        // Since padding between the SnackBar and the FAB is created by the SnackBar,
        // the bottom offset of the SnackBar should be equal to the top offset of the FAB
        expect(snackBarBottomLeft.dy, floatingActionButtonTopLeft.dy);
      },
    );

    testWidgets(
      '${SnackBarBehavior.fixed} should align SnackBar with the top of BottomNavigationBar '
      'when Scaffold has a BottomNavigationBar and FloatingActionButton',
      (WidgetTester tester) async {
        final UniqueKey boxKey = UniqueKey();
        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: Container(),
              bottomNavigationBar: SizedBox(key: boxKey, width: 800, height: 60),
              floatingActionButton: FloatingActionButton(onPressed: () {}),
            ),
          ),
        );

        final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
        scaffoldState.showSnackBar(
          const SnackBar(
            content: Text('SnackBar text'),
            behavior: SnackBarBehavior.fixed,
          ),
        );

        await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

        final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
        final Offset bottomNavigationBarTopRight = tester.getTopRight(find.byKey(boxKey));

        expect(snackBarBottomRight, equals(bottomNavigationBarTopRight));

        final Offset snackBarBottomLeft = tester.getBottomLeft(find.byType(SnackBar));
        final Offset bottomNavigationBarTopLeft = tester.getTopLeft(find.byKey(boxKey));

        expect(snackBarBottomLeft, equals(bottomNavigationBarTopLeft));
      },
    );

    testWidgets(
      '${SnackBarBehavior.floating} should align SnackBar with the top of FloatingActionButton '
      'when Scaffold has BottomNavigationBar and FloatingActionButton',
      (WidgetTester tester) async {
        final UniqueKey boxKey = UniqueKey();
        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: Container(),
              bottomNavigationBar: SizedBox(key: boxKey, width: 800, height: 60),
              floatingActionButton: FloatingActionButton(onPressed: () {}),
            ),
          ),
        );

        final ScaffoldState scaffoldState = tester.state(find.byType(Scaffold));
        scaffoldState.showSnackBar(
          const SnackBar(
            content: Text('SnackBar text'),
            behavior: SnackBarBehavior.floating,
          ),
        );

        await tester.pumpAndSettle(); // Have the SnackBar fully animate out.

        final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar));
        final Offset fabTopRight = tester.getTopRight(find.byType(FloatingActionButton));

        expect(snackBarBottomRight.dy, equals(fabTopRight.dy));
      },
    );
  });
1337
}