snack_bar_test.dart 52.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 6 7 8
// TODO(shihaohong): remove ignoring deprecated member use analysis
// when Scaffold.shouldSnackBarIgnoreFABRect parameter is removed.
// ignore_for_file: deprecated_member_use_from_same_package

9
import 'package:flutter/material.dart';
10 11
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
12 13

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

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

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

190
    firstController.close(); // snackbar is manually dismissed
191

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

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

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

294
    expect(tapCount, equals(0));
295
    await tester.tap(find.text('ACTION'));
296
    expect(tapCount, equals(1));
297
    await tester.tap(find.text('ACTION'));
298
    expect(tapCount, equals(1));
299 300
    await tester.pump();
    await tester.tap(find.text('ACTION'));
301
    expect(tapCount, equals(1));
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 381 382 383 384
  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
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  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',
401
                        onPressed: () { },
jslavitz's avatar
jslavitz committed
402 403 404 405
                      ),
                    ),
                  );
                },
406
                child: const Text('X'),
jslavitz's avatar
jslavitz committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
              );
            }
          ),
        ),
      ),
    );

    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);
425
    } else {
jslavitz's avatar
jslavitz committed
426
      expect(false, true);
427
    }
jslavitz's avatar
jslavitz committed
428 429
  });

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

463 464 465 466 467 468
    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));
469

470
    expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
471
    expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0 + 40.0); // margin + bottom padding
472
    expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
473
    expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
474
    expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0 + 40.0); // margin + bottom padding
475
  }, skip: isBrowser);
476

477 478 479 480 481 482 483 484 485 486 487 488
  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,
            ),
489
          ),
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
          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'),
                );
              }
            ),
511 512
          ),
        ),
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
      ));
      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)
    },
    skip: isBrowser,
  );
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547
  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),
548
            onPressed: () {},
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
          ),
          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'),
              );
            }
          ),
        ),
      ),
    ));

568 569
    // Get the Rect of the FAB to compare after the SnackBar appears.
    final Rect originalFabRect = tester.getRect(find.byType(FloatingActionButton));
570 571 572 573 574

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

575
    final Rect fabRect = tester.getRect(find.byType(FloatingActionButton));
576

577 578
    // FAB should shift upwards after SnackBar appears.
    expect(fabRect.center.dy, lessThan(originalFabRect.center.dy));
579

580
    final Offset snackBarTopRight = tester.getTopRight(find.byType(SnackBar));
581

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

585 586 587
    // FAB should be positioned above the SnackBar by the default padding.
    expect(fabRect.bottomRight.dy, snackBarTopRight.dy - defaultFabPadding);
  });
588

589
  testWidgets('Floating SnackBar button text alignment', (WidgetTester tester) async {
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 631 632 633 634 635 636
    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)
637
  }, skip: isBrowser);
638

639 640 641 642 643 644
  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)
645
        ),
646 647 648 649 650 651 652 653
        home: MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.only(
              left: 10.0,
              top: 20.0,
              right: 30.0,
              bottom: 40.0,
            ),
654
          ),
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
          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'),
                );
              }
            ),
676 677
          ),
        ),
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
      ));
      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)
    },
    skip: isBrowser,
  );
698

699
  testWidgets('SnackBarClosedReason', (WidgetTester tester) async {
700
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
701 702 703
    bool actionPressed = false;
    SnackBarClosedReason closedReason;

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

    // 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);
738
    // Closed reason is only set when the animation is complete.
739
    await tester.pump(const Duration(milliseconds: 250));
740 741 742
    expect(closedReason, isNull);
    // Wait for animation to complete.
    await tester.pumpAndSettle(const Duration(seconds: 1));
743 744 745 746 747 748
    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));
749
    await tester.drag(find.text('snack'), const Offset(0.0, 50.0));
750
    await tester.pumpAndSettle(const Duration(seconds: 1));
751 752 753 754 755 756
    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();
757
    await tester.pumpAndSettle(const Duration(seconds: 1));
758 759 760 761 762 763
    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();
764
    await tester.pumpAndSettle(const Duration(seconds: 1));
765 766 767 768
    expect(closedReason, equals(SnackBarClosedReason.hide));

    // Pop up the snack bar and then let it time out.
    await tester.tap(find.text('X'));
769 770 771
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 1500));
772
    await tester.pump(); // begin animation
773
    await tester.pumpAndSettle(const Duration(seconds: 1));
774 775 776
    expect(closedReason, equals(SnackBarClosedReason.timeout));
  });

777
  testWidgets('accessible navigation behavior with action', (WidgetTester tester) async {
778
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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',
794
                      onPressed: () { },
795 796 797 798 799 800
                    ),
                  ));
                },
                child: const Text('X'),
              );
            },
801 802
          ),
        ),
803 804 805 806 807 808 809 810 811 812 813 814 815
      ),
    ));
    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);
816 817 818 819
  });

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

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

850
    expect(tester.getSemantics(find.text('snack')), matchesSemantics(
851 852 853 854 855 856 857 858 859 860
      isLiveRegion: true,
      hasDismissAction: true,
      hasScrollDownAction: true,
      hasScrollUpAction: true,
      label: 'snack',
      textDirection: TextDirection.ltr,
    ));
    handle.dispose();
  });

jslavitz's avatar
jslavitz committed
861 862
  testWidgets('SnackBar default display duration test', (WidgetTester tester) async {
    const String helloSnackBar = 'Hello SnackBar';
863
    const Key tapTarget = Key('tap-target');
864 865 866
    await tester.pumpWidget(MaterialApp(
        home: Scaffold(
            body: Builder(
jslavitz's avatar
jslavitz committed
867
                builder: (BuildContext context) {
868
                  return GestureDetector(
jslavitz's avatar
jslavitz committed
869 870
                      onTap: () {
                        Scaffold.of(context).showSnackBar(const SnackBar(
871
                            content: Text(helloSnackBar),
jslavitz's avatar
jslavitz committed
872 873 874
                        ));
                      },
                      behavior: HitTestBehavior.opaque,
875
                      child: Container(
jslavitz's avatar
jslavitz committed
876 877
                          height: 100.0,
                          width: 100.0,
878 879
                          key: tapTarget,
                      ),
jslavitz's avatar
jslavitz committed
880 881
                  );
                }
882 883
            ),
        ),
jslavitz's avatar
jslavitz committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    ));
    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);
  });

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

    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);
  });

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
  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,
974
                key: tapTarget,
975 976 977 978 979 980 981 982 983 984
              ),
            );
          },
        ),
      ),
    ));

    await tester.tap(find.byKey(tapTarget));
    expect(tester.takeException(), isNotNull);
  });
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 1058 1059 1060 1061 1062 1063 1064 1065

  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);
  });
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 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

  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 {
          // TODO(shihaohong): Remove this flag once the migration to fix
          // SnackBarBehavior.floating is complete.
          Scaffold.shouldSnackBarIgnoreFABRect = true;

          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));
          // TODO(shihaohong): Remove this flag once the migration to fix
          // SnackBarBehavior.floating is complete.
          Scaffold.shouldSnackBarIgnoreFABRect = false;
        },
      );

      testWidgets(
        '$behavior should align SnackBar with the top of BottomNavigationBar '
        'when Scaffold has no FloatingActionButton',
        (WidgetTester tester) async {
          // TODO(shihaohong): Remove this flag once the migration to fix
          // SnackBarBehavior.floating is complete.
          Scaffold.shouldSnackBarIgnoreFABRect = true;
          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));
          // TODO(shihaohong): Remove this flag once the migration to fix
          // SnackBarBehavior.floating is complete.
          Scaffold.shouldSnackBarIgnoreFABRect = false;
        },
      );

      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));
      },
    );
  });
1358
}