snack_bar_test.dart 44.3 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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
  }, skip: isBrowser);
472

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

512 513 514 515 516 517
    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));
518 519

    expect(textBottomLeft.dx - snackBarBottomLeft.dx, 24.0 + 10.0); // margin + left padding
520
    expect(snackBarBottomLeft.dy - textBottomLeft.dy, 17.0); // margin (with no bottom padding)
521 522
    expect(actionTextBottomLeft.dx - textBottomRight.dx, 24.0);
    expect(snackBarBottomRight.dx - actionTextBottomRight.dx, 24.0 + 30.0); // margin + right padding
523
    expect(snackBarBottomRight.dy - actionTextBottomRight.dy, 17.0); // margin (with no bottom padding)
524
  }, skip: isBrowser);
525

526 527 528 529 530 531 532 533 534 535 536 537 538 539
  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),
540
            onPressed: () {},
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 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
          ),
          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'),
              );
            }
          ),
        ),
      ),
    ));

    final Offset floatingActionButtonOriginBottomCenter = tester.getCenter(find.byType(FloatingActionButton));

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

    final Offset snackBarTopCenter = tester.getCenter(find.byType(SnackBar));
    final Offset floatingActionButtonBottomCenter = tester.getCenter(find.byType(FloatingActionButton));

    expect(floatingActionButtonOriginBottomCenter.dy > floatingActionButtonBottomCenter.dy, true);
    expect(snackBarTopCenter.dy > floatingActionButtonBottomCenter.dy, true);
  });

  testWidgets('Floating SnackBar button text alignment', (WidgetTester tester) async {
    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)
621
  }, skip: isBrowser);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

  testWidgets('Floating SnackBar is positioned above BottomNavigationBar', (WidgetTester tester) async {
    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(
          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'),
              );
            }
          ),
        ),
      ),
    ));
    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)
677
  }, skip: isBrowser);
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

  testWidgets('Floating SnackBar is positioned above FloatingActionButton', (WidgetTester tester) async {
    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(
          floatingActionButton: FloatingActionButton(
              child: const Icon(Icons.send),
696
              onPressed: () {},
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
          ),
          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 snackBarBottomCenter = tester.getBottomLeft(find.byType(SnackBar));
    final Offset floatingActionButtonTopCenter = tester.getTopLeft(find.byType(FloatingActionButton));

    // Since padding and margin is handled inside snackBarBox,
    // the bottom offset of snackbar should equal with top offset of FAB
    expect(snackBarBottomCenter.dy == floatingActionButtonTopCenter.dy, true);
  });

727 728 729 730 731 732 733
  testWidgets('SnackBar bottom padding 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),
734
        onPressed: () {},
735 736 737 738 739 740 741 742 743 744
      ),
      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: () {}),
745
                ),
746 747 748 749 750 751 752 753 754 755 756 757 758
              );
            },
            child: const Text('X'),
          );
        }
      ),
    ));

    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.only(bottom: 20.0),
        ),
759
        child: child,
760
      ),
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle(); // Show snackbar
    final Offset initialPoint = tester.getCenter(find.byType(SnackBar));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          viewPadding: EdgeInsets.only(bottom: 20),
          viewInsets: EdgeInsets.only(bottom: 300),
        ),
        child: child,
      ),
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();
    final Offset finalPoint = tester.getCenter(find.byType(SnackBar));
    expect(initialPoint, finalPoint);
  });

782
  testWidgets('SnackBarClosedReason', (WidgetTester tester) async {
783
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
784 785 786
    bool actionPressed = false;
    SnackBarClosedReason closedReason;

787 788
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
789
        key: scaffoldKey,
790
        body: Builder(
791
          builder: (BuildContext context) {
792
            return GestureDetector(
793
              onTap: () {
794
                Scaffold.of(context).showSnackBar(SnackBar(
795
                  content: const Text('snack'),
796
                  duration: const Duration(seconds: 2),
797
                  action: SnackBarAction(
798 799 800
                    label: 'ACTION',
                    onPressed: () {
                      actionPressed = true;
801
                    },
802
                  ),
803
                )).closed.then<void>((SnackBarClosedReason reason) {
804 805 806
                  closedReason = reason;
                });
              },
807
              child: const Text('X'),
808 809
            );
          },
810 811
        ),
      ),
812 813 814 815 816 817 818 819 820
    ));

    // 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);
821
    // Closed reason is only set when the animation is complete.
822
    await tester.pump(const Duration(milliseconds: 250));
823 824 825
    expect(closedReason, isNull);
    // Wait for animation to complete.
    await tester.pumpAndSettle(const Duration(seconds: 1));
826 827 828 829 830 831
    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));
832
    await tester.drag(find.text('snack'), const Offset(0.0, 50.0));
833
    await tester.pumpAndSettle(const Duration(seconds: 1));
834 835 836 837 838 839
    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();
840
    await tester.pumpAndSettle(const Duration(seconds: 1));
841 842 843 844 845 846
    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();
847
    await tester.pumpAndSettle(const Duration(seconds: 1));
848 849 850 851
    expect(closedReason, equals(SnackBarClosedReason.hide));

    // Pop up the snack bar and then let it time out.
    await tester.tap(find.text('X'));
852 853 854
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 1500));
855
    await tester.pump(); // begin animation
856
    await tester.pumpAndSettle(const Duration(seconds: 1));
857 858 859
    expect(closedReason, equals(SnackBarClosedReason.timeout));
  });

860
  testWidgets('accessible navigation behavior with action', (WidgetTester tester) async {
861
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876
    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',
877
                      onPressed: () { },
878 879 880 881 882 883
                    ),
                  ));
                },
                child: const Text('X'),
              );
            },
884 885
          ),
        ),
886 887 888 889 890 891 892 893 894 895 896 897 898
      ),
    ));
    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);
899 900 901 902
  });

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

905 906
    await tester.pumpWidget(MaterialApp(
        home: MediaQuery(
907 908 909
            data: const MediaQueryData(accessibleNavigation: true),
            child: Scaffold(
                key: scaffoldKey,
910
                body: Builder(
911
                  builder: (BuildContext context) {
912
                    return GestureDetector(
913
                        onTap: () {
914
                          Scaffold.of(context).showSnackBar(SnackBar(
915 916
                            content: const Text('snack'),
                            duration: const Duration(seconds: 1),
917
                            action: SnackBarAction(
918
                                label: 'ACTION',
919
                                onPressed: () { },
920 921 922
                            ),
                          ));
                        },
923
                        child: const Text('X'),
924 925
                    );
                  },
926 927 928
                ),
            ),
        ),
929 930 931 932
    ));
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

933
    expect(tester.getSemantics(find.text('snack')), matchesSemantics(
934 935 936 937 938 939 940 941 942 943
      isLiveRegion: true,
      hasDismissAction: true,
      hasScrollDownAction: true,
      hasScrollUpAction: true,
      label: 'snack',
      textDirection: TextDirection.ltr,
    ));
    handle.dispose();
  });

jslavitz's avatar
jslavitz committed
944 945
  testWidgets('SnackBar default display duration test', (WidgetTester tester) async {
    const String helloSnackBar = 'Hello SnackBar';
946
    const Key tapTarget = Key('tap-target');
947 948 949
    await tester.pumpWidget(MaterialApp(
        home: Scaffold(
            body: Builder(
jslavitz's avatar
jslavitz committed
950
                builder: (BuildContext context) {
951
                  return GestureDetector(
jslavitz's avatar
jslavitz committed
952 953
                      onTap: () {
                        Scaffold.of(context).showSnackBar(const SnackBar(
954
                            content: Text(helloSnackBar),
jslavitz's avatar
jslavitz committed
955 956 957
                        ));
                      },
                      behavior: HitTestBehavior.opaque,
958
                      child: Container(
jslavitz's avatar
jslavitz committed
959 960
                          height: 100.0,
                          width: 100.0,
961 962
                          key: tapTarget,
                      ),
jslavitz's avatar
jslavitz committed
963 964
                  );
                }
965 966
            ),
        ),
jslavitz's avatar
jslavitz committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    ));
    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);
  });

992
  testWidgets('SnackBar handles updates to accessibleNavigation', (WidgetTester tester) async {
993
    Future<void> boilerplate({ bool accessibleNavigation }) {
994 995 996 997 998
      return tester.pumpWidget(MaterialApp(
          home: MediaQuery(
              data: MediaQueryData(accessibleNavigation: accessibleNavigation),
              child: Scaffold(
                  body: Builder(
999
                      builder: (BuildContext context) {
1000
                        return GestureDetector(
1001
                            onTap: () {
1002
                              Scaffold.of(context).showSnackBar(SnackBar(
1003
                                  content: const Text('test'),
1004
                                  action: SnackBarAction(label: 'foo', onPressed: () { }),
1005 1006 1007 1008 1009 1010
                              ));
                            },
                            behavior: HitTestBehavior.opaque,
                            child: const Text('X'),
                        );
                      }
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
      ));
    }

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

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  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,
1057
                key: tapTarget,
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
              ),
            );
          },
        ),
      ),
    ));

    await tester.tap(find.byKey(tapTarget));
    expect(tester.takeException(), isNotNull);
  });
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

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