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

import 'package:flutter/cupertino.dart';
6 7
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
8
import 'package:flutter/material.dart';
9
import 'package:flutter/rendering.dart';
10 11 12 13 14 15

import 'package:flutter_test/flutter_test.dart';

import '../widgets/semantics_tester.dart';

void main() {
16
  testWidgets('Verify that a tap on modal barrier dismisses an action sheet', (WidgetTester tester) async {
17 18 19 20 21 22 23 24 25
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
26
    await tester.pump();
27

28
    expect(find.text('Action Sheet'), findsOneWidget);
29

30 31 32
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pump();
    expect(find.text('Action Sheet'), findsNothing);
33 34
  });

35
  testWidgets('Verify that a tap on title section (not buttons) does not dismiss an action sheet', (WidgetTester tester) async {
36 37 38 39 40 41 42 43 44 45
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();
46
    await tester.pump(const Duration(seconds: 5));
47 48 49 50 51 52 53 54 55 56 57

    expect(find.text('Action Sheet'), findsOneWidget);

    await tester.tap(find.text('Action Sheet'));
    await tester.pump();
    expect(find.text('Action Sheet'), findsOneWidget);
  });

  testWidgets('Action sheet destructive text style', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
58
        CupertinoActionSheetAction(
59 60
          isDestructiveAction: true,
          child: const Text('Ok'),
61
          onPressed: () { },
62 63 64 65 66 67
        ),
      ),
    );

    final DefaultTextStyle widget = tester.widget(find.widgetWithText(DefaultTextStyle, 'Ok'));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
68
    expect(widget.style.color, const CupertinoDynamicColor.withBrightnessAndContrast(
69 70 71 72 73 74 75 76 77 78 79 80 81 82
      color: Color.fromARGB(255, 255, 59, 48),
      darkColor: Color.fromARGB(255, 255, 69, 58),
      highContrastColor: Color.fromARGB(255, 215, 0, 21),
      darkHighContrastColor: Color.fromARGB(255, 255, 105, 97),
    ));
  });

  testWidgets('Action sheet dark mode', (WidgetTester tester) async {
    final Widget action = CupertinoActionSheetAction(
      child: const Text('action'),
      onPressed: () {},
    );

    Brightness brightness = Brightness.light;
83
    late StateSetter stateSetter;
84 85 86 87 88 89

    TextStyle actionTextStyle(String text) {
      return tester.widget<DefaultTextStyle>(
        find.descendant(
          of: find.widgetWithText(CupertinoActionSheetAction, text),
          matching: find.byType(DefaultTextStyle),
90
        ),
Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
91
      ).style;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }

    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        StatefulBuilder(
          builder: (BuildContext context, StateSetter setter) {
            stateSetter = setter;
            return CupertinoTheme(
              data: CupertinoThemeData(
                brightness: brightness,
                primaryColor: const CupertinoDynamicColor.withBrightnessAndContrast(
                  color: Color.fromARGB(255, 0, 122, 255),
                  darkColor: Color.fromARGB(255, 10, 132, 255),
                  highContrastColor: Color.fromARGB(255, 0, 64, 221),
                  darkHighContrastColor: Color.fromARGB(255, 64, 156, 255),
                ),
              ),
              child: CupertinoActionSheet(actions: <Widget>[action]),
            );
          },
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    expect(
120
      actionTextStyle('action').color!.value,
121 122 123 124 125 126 127
      const Color.fromARGB(255, 0, 122, 255).value,
    );

    stateSetter(() { brightness = Brightness.dark; });
    await tester.pump();

    expect(
128
      actionTextStyle('action').color!.value,
129 130
      const Color.fromARGB(255, 10, 132, 255).value,
    );
131 132 133 134 135
  });

  testWidgets('Action sheet default text style', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
136
        CupertinoActionSheetAction(
137 138
          isDefaultAction: true,
          child: const Text('Ok'),
139
          onPressed: () { },
140 141 142 143 144 145
        ),
      ),
    );

    final DefaultTextStyle widget = tester.widget(find.widgetWithText(DefaultTextStyle, 'Ok'));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
146
    expect(widget.style.fontWeight, equals(FontWeight.w600));
147 148
  });

149
  testWidgets('Action sheet text styles are correct when both title and message are included', (WidgetTester tester) async {
150 151 152 153
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
154
          message: Text('An action sheet'),
155 156 157 158 159 160 161
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

162 163
    final DefaultTextStyle titleStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'Action Sheet'));
    final DefaultTextStyle messageStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'An action sheet'));
164

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
165 166
    expect(titleStyle.style.fontWeight, FontWeight.w600);
    expect(messageStyle.style.fontWeight, FontWeight.w400);
167 168
  });

169
  testWidgets('Action sheet text styles are correct when title but no message is included', (WidgetTester tester) async {
170 171 172 173 174 175 176 177 178 179 180
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

181
    final DefaultTextStyle titleStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'Action Sheet'));
182

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
183
    expect(titleStyle.style.fontWeight, FontWeight.w400);
184 185
  });

186
  testWidgets('Action sheet text styles are correct when message but no title is included', (WidgetTester tester) async {
187 188 189 190 191 192 193 194 195 196 197
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          message: Text('An action sheet'),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

198
    final DefaultTextStyle messageStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'An action sheet'));
199

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
200
    expect(messageStyle.style.fontWeight, FontWeight.w600);
201 202 203
  });

  testWidgets('Content section but no actions', (WidgetTester tester) async {
204
    final ScrollController scrollController = ScrollController();
205 206
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
207
        CupertinoActionSheet(
208 209 210 211 212 213 214 215 216 217 218 219 220 221
          title: const Text('The title'),
          message: const Text('The message.'),
          messageScrollController: scrollController,
        ),
      ),
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    // Content section should be at the bottom left of action sheet
    // (minus padding).
222 223 224 225
    expect(
      tester.getBottomLeft(find.byType(ClipRRect)),
      tester.getBottomLeft(find.byType(CupertinoActionSheet)) - const Offset(-8.0, 10.0),
    );
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

    // Check that the dialog size is the same as the content section size
    // (minus padding).
    expect(
      tester.getSize(find.byType(ClipRRect)).height,
      tester.getSize(find.byType(CupertinoActionSheet)).height  - 20.0,
    );

    expect(
      tester.getSize(find.byType(ClipRRect)).width,
      tester.getSize(find.byType(CupertinoActionSheet)).width - 16.0,
    );
  });

  testWidgets('Actions but no content section', (WidgetTester tester) async {
241
    final ScrollController actionScrollController = ScrollController();
242 243
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
244
        CupertinoActionSheet(
245
          actions: <Widget>[
246
            CupertinoActionSheetAction(
247
              child: const Text('One'),
248
              onPressed: () { },
249
            ),
250
            CupertinoActionSheetAction(
251
              child: const Text('Two'),
252
              onPressed: () { },
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
            ),
          ],
          actionScrollController: actionScrollController,
        ),
      ),
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    final Finder finder = find.byElementPredicate(
      (Element element) {
        return element.widget.runtimeType.toString() == '_CupertinoAlertActionSection';
      },
    );

    // Check that the title/message section is not displayed (action section is
    // at the top of the action sheet + padding).
273 274 275 276
    expect(
      tester.getTopLeft(finder),
      tester.getTopLeft(find.byType(CupertinoActionSheet)) + const Offset(8.0, 10.0),
    );
277

278 279 280 281 282 283 284 285
    expect(
      tester.getTopLeft(find.byType(CupertinoActionSheet)) + const Offset(8.0, 10.0),
      tester.getTopLeft(find.widgetWithText(CupertinoActionSheetAction, 'One')),
    );
    expect(
      tester.getBottomLeft(find.byType(CupertinoActionSheet)) + const Offset(8.0, -10.0),
      tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Two')),
    );
286 287 288
  });

  testWidgets('Action section is scrollable', (WidgetTester tester) async {
289
    final ScrollController actionScrollController = ScrollController();
290 291
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
292 293
        Builder(builder: (BuildContext context) {
          return MediaQuery(
294
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
295
            child: CupertinoActionSheet(
296 297 298
              title: const Text('The title'),
              message: const Text('The message.'),
              actions: <Widget>[
299
                CupertinoActionSheetAction(
300
                  child: const Text('One'),
301
                  onPressed: () { },
302
                ),
303
                CupertinoActionSheetAction(
304
                  child: const Text('Two'),
305
                  onPressed: () { },
306
                ),
307
                CupertinoActionSheetAction(
308
                  child: const Text('Three'),
309
                  onPressed: () { },
310
                ),
311
                CupertinoActionSheetAction(
312
                  child: const Text('Four'),
313
                  onPressed: () { },
314
                ),
315
                CupertinoActionSheetAction(
316
                  child: const Text('Five'),
317
                  onPressed: () { },
318 319 320 321 322 323
                ),
              ],
              actionScrollController: actionScrollController,
            ),
          );
        }),
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
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    // Check that the action buttons list is scrollable.
    expect(actionScrollController.offset, 0.0);
    actionScrollController.jumpTo(100.0);
    expect(actionScrollController.offset, 100.0);
    actionScrollController.jumpTo(0.0);

    // Check that the action buttons are aligned vertically.
    expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'One')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'Two')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'Three')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'Four')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoActionSheetAction, 'Five')).dx, equals(400.0));

    // Check that the action buttons are the correct heights.
    expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'One')).height, equals(92.0));
    expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Two')).height, equals(92.0));
    expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Three')).height, equals(92.0));
    expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Four')).height, equals(92.0));
    expect(tester.getSize(find.widgetWithText(CupertinoActionSheetAction, 'Five')).height, equals(92.0));
  });

  testWidgets('Content section is scrollable', (WidgetTester tester) async {
354
    final ScrollController messageScrollController = ScrollController();
355
    late double screenHeight;
356 357
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
358
        Builder(builder: (BuildContext context) {
359
          screenHeight = MediaQuery.sizeOf(context).height;
360
          return MediaQuery(
361
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
362
            child: CupertinoActionSheet(
363
              title: const Text('The title'),
364
              message: Text('Very long content' * 200),
365
              actions: <Widget>[
366
                CupertinoActionSheetAction(
367
                  child: const Text('One'),
368
                  onPressed: () { },
369
                ),
370
                CupertinoActionSheetAction(
371
                  child: const Text('Two'),
372
                  onPressed: () { },
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                ),
              ],
              messageScrollController: messageScrollController,
            ),
          );
        }),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    expect(messageScrollController.offset, 0.0);
    messageScrollController.jumpTo(100.0);
    expect(messageScrollController.offset, 100.0);
    // Set the scroll position back to zero.
    messageScrollController.jumpTo(0.0);

    // Expect the action sheet to take all available height.
    expect(tester.getSize(find.byType(CupertinoActionSheet)).height, screenHeight);
  });

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  testWidgets('CupertinoActionSheet scrollbars controllers should be different', (WidgetTester tester) async {
    // https://github.com/flutter/flutter/pull/81278
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        CupertinoActionSheet(
            title: const Text('The title'),
            message: Text('Very long content' * 200),
            actions: <Widget>[
              CupertinoActionSheetAction(
                child: const Text('One'),
                onPressed: () { },
              ),
            ],
          )
        ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    final List<CupertinoScrollbar> scrollbars =
      find.descendant(
        of: find.byType(CupertinoActionSheet),
        matching: find.byType(CupertinoScrollbar),
      ).evaluate().map((Element e) => e.widget as CupertinoScrollbar).toList();

    expect(scrollbars.length, 2);
    expect(scrollbars[0].controller != scrollbars[1].controller, isTrue);
  });

425 426 427 428
  testWidgets('Tap on button calls onPressed', (WidgetTester tester) async {
    bool wasPressed = false;
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
429 430
        Builder(builder: (BuildContext context) {
          return CupertinoActionSheet(
431
            actions: <Widget>[
432
              CupertinoActionSheetAction(
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
                child: const Text('One'),
                onPressed: () {
                  wasPressed = true;
                  Navigator.pop(context);
                },
              ),
            ],
          );
        }),
      ),
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(wasPressed, isFalse);

    await tester.tap(find.text('One'));

    expect(wasPressed, isTrue);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

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

462
  testWidgets('Action sheet width is correct when given infinite horizontal space', (WidgetTester tester) async {
463 464
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
465
        Row(
466
          children: <Widget>[
467
            CupertinoActionSheet(
468
              actions: <Widget>[
469
                CupertinoActionSheetAction(
470
                  child: const Text('One'),
471
                  onPressed: () { },
472
                ),
473
                CupertinoActionSheetAction(
474
                  child: const Text('Two'),
475
                  onPressed: () { },
476 477 478 479 480 481 482 483 484 485 486 487 488 489
                ),
              ],
            ),
          ],
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    expect(tester.getSize(find.byType(CupertinoActionSheet)).width, 600.0);
  });

490
  testWidgets('Action sheet height is correct when given infinite vertical space', (WidgetTester tester) async {
491 492
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
493
        Column(
494
          children: <Widget>[
495
            CupertinoActionSheet(
496
              actions: <Widget>[
497
                CupertinoActionSheetAction(
498
                  child: const Text('One'),
499
                  onPressed: () { },
500
                ),
501
                CupertinoActionSheetAction(
502
                  child: const Text('Two'),
503
                  onPressed: () { },
504 505 506 507 508 509 510 511 512 513 514
                ),
              ],
            ),
          ],
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

515
    expect(tester.getSize(find.byType(CupertinoActionSheet)).height, moreOrLessEquals(132.33333333333334));
516 517 518 519 520
  });

  testWidgets('1 action button with cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
521
        CupertinoActionSheet(
522
          title: const Text('The title'),
523
          message: Text('Very long content' * 200),
524
          actions: <Widget>[
525
            CupertinoActionSheetAction(
526
              child: const Text('One'),
527
              onPressed: () { },
528 529
            ),
          ],
530
          cancelButton: CupertinoActionSheetAction(
531
            child: const Text('Cancel'),
532
            onPressed: () { },
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    // Action section is size of one action button.
    expect(findScrollableActionsSectionRenderBox(tester).size.height, 56.0);
  });

  testWidgets('2 action buttons with cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
548
        CupertinoActionSheet(
549
          title: const Text('The title'),
550
          message: Text('Very long content' * 200),
551
          actions: <Widget>[
552
            CupertinoActionSheetAction(
553
              child: const Text('One'),
554
              onPressed: () { },
555
            ),
556
            CupertinoActionSheetAction(
557
              child: const Text('Two'),
558
              onPressed: () { },
559 560
            ),
          ],
561
          cancelButton: CupertinoActionSheetAction(
562
            child: const Text('Cancel'),
563
            onPressed: () { },
564 565 566 567 568 569 570 571
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

572
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(112.33333333333331));
573 574 575 576 577
  });

  testWidgets('3 action buttons with cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
578
        CupertinoActionSheet(
579
          title: const Text('The title'),
580
          message: Text('Very long content' * 200),
581
          actions: <Widget>[
582
            CupertinoActionSheetAction(
583
              child: const Text('One'),
584
              onPressed: () { },
585
            ),
586
            CupertinoActionSheetAction(
587
              child: const Text('Two'),
588
              onPressed: () { },
589
            ),
590
            CupertinoActionSheetAction(
591
              child: const Text('Three'),
592
              onPressed: () { },
593 594
            ),
          ],
595
          cancelButton: CupertinoActionSheetAction(
596
            child: const Text('Cancel'),
597
            onPressed: () { },
598 599 600 601 602 603 604 605
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

606
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(168.66666666666669));
607 608 609 610 611
  });

  testWidgets('4+ action buttons with cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
612
        CupertinoActionSheet(
613
          title: const Text('The title'),
614
          message: Text('Very long content' * 200),
615
          actions: <Widget>[
616
            CupertinoActionSheetAction(
617
              child: const Text('One'),
618
              onPressed: () { },
619
            ),
620
            CupertinoActionSheetAction(
621
              child: const Text('Two'),
622
              onPressed: () { },
623
            ),
624
            CupertinoActionSheetAction(
625
              child: const Text('Three'),
626
              onPressed: () { },
627
            ),
628
            CupertinoActionSheetAction(
629
              child: const Text('Four'),
630
              onPressed: () { },
631 632
            ),
          ],
633
          cancelButton: CupertinoActionSheetAction(
634
            child: const Text('Cancel'),
635
            onPressed: () { },
636 637 638 639 640 641 642 643
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

644
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(84.33333333333337));
645 646 647 648 649
  });

  testWidgets('1 action button without cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
650
        CupertinoActionSheet(
651
          title: const Text('The title'),
652
          message: Text('Very long content' * 200),
653
          actions: <Widget>[
654
            CupertinoActionSheetAction(
655
              child: const Text('One'),
656
              onPressed: () { },
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
            ),
          ],
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    expect(findScrollableActionsSectionRenderBox(tester).size.height, 56.0);
  });

  testWidgets('2+ action buttons without cancel button', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
672
        CupertinoActionSheet(
673
          title: const Text('The title'),
674
          message: Text('Very long content' * 200),
675
          actions: <Widget>[
676
            CupertinoActionSheetAction(
677
              child: const Text('One'),
678
              onPressed: () { },
679
            ),
680
            CupertinoActionSheetAction(
681
              child: const Text('Two'),
682
              onPressed: () { },
683 684 685 686 687 688 689 690 691
            ),
          ],
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

692
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(84.33333333333337));
693 694 695 696 697
  });

  testWidgets('Action sheet with just cancel button is correct', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
698 699
        CupertinoActionSheet(
          cancelButton: CupertinoActionSheetAction(
700
            child: const Text('Cancel'),
701
            onPressed: () { },
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    // Height should be cancel button height + padding
    expect(tester.getSize(find.byType(CupertinoActionSheet)).height, 76.0);
    expect(tester.getSize(find.byType(CupertinoActionSheet)).width, 600.0);
  });

  testWidgets('Cancel button tap calls onPressed', (WidgetTester tester) async {
    bool wasPressed = false;
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
719 720 721
        Builder(builder: (BuildContext context) {
          return CupertinoActionSheet(
            cancelButton: CupertinoActionSheetAction(
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
              child: const Text('Cancel'),
              onPressed: () {
                wasPressed = true;
                Navigator.pop(context);
              },
            ),
          );
        }),
      ),
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(wasPressed, isFalse);

    await tester.tap(find.text('Cancel'));

    expect(wasPressed, isTrue);

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

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

  testWidgets('Layout is correct when cancel button is present', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
753
        CupertinoActionSheet(
754 755 756
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
757
            CupertinoActionSheetAction(
758
              child: const Text('One'),
759
              onPressed: () { },
760
            ),
761
            CupertinoActionSheetAction(
762
              child: const Text('Two'),
763
              onPressed: () { },
764 765
            ),
          ],
766
          cancelButton: CupertinoActionSheetAction(
767
            child: const Text('Cancel'),
768
            onPressed: () { },
769 770 771 772 773 774 775 776 777 778 779
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Cancel')).dy, 590.0);
780 781 782 783
    expect(
      tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'One')).dy,
      moreOrLessEquals(469.66666666666663),
    );
784 785 786 787 788 789
    expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Two')).dy, 526.0);
  });

  testWidgets('Enter/exit animation is correct', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
790
        CupertinoActionSheet(
791 792 793
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
794
            CupertinoActionSheetAction(
795
              child: const Text('One'),
796
              onPressed: () { },
797
            ),
798
            CupertinoActionSheetAction(
799
              child: const Text('Two'),
800
              onPressed: () { },
801 802
            ),
          ],
803
          cancelButton: CupertinoActionSheetAction(
804
            child: const Text('Cancel'),
805
            onPressed: () { },
806 807 808 809 810 811 812 813 814 815 816 817
          ),
        ),
      ),
    );

    // Enter animation
    await tester.tap(find.text('Go'));

    await tester.pump();
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, 600.0);

    await tester.pump(const Duration(milliseconds: 60));
818
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
819 820

    await tester.pump(const Duration(milliseconds: 60));
821
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(374.3, epsilon: 0.1));
822 823

    await tester.pump(const Duration(milliseconds: 60));
824
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(337.1, epsilon: 0.1));
825 826

    await tester.pump(const Duration(milliseconds: 60));
827
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(325.3, epsilon: 0.1));
828 829

    await tester.pump(const Duration(milliseconds: 60));
830
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(320.8, epsilon: 0.1));
831 832

    await tester.pump(const Duration(milliseconds: 60));
833
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(319.3, epsilon: 0.1));
834 835 836

    // Action sheet has reached final height
    await tester.pump(const Duration(milliseconds: 60));
837
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(319.3, epsilon: 0.1));
838 839 840 841

    // Exit animation
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pump();
842
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(319.3, epsilon: 0.1));
843 844

    await tester.pump(const Duration(milliseconds: 60));
845
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(449.3, epsilon: 0.1));
846 847

    await tester.pump(const Duration(milliseconds: 60));
848
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(544.9, epsilon: 0.1));
849 850

    await tester.pump(const Duration(milliseconds: 60));
851
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(582.1, epsilon: 0.1));
852 853

    await tester.pump(const Duration(milliseconds: 60));
854
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(593.9, epsilon: 0.1));
855 856

    await tester.pump(const Duration(milliseconds: 60));
857
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(598.5, epsilon: 0.1));
858 859 860 861 862 863

    // Action sheet has disappeared
    await tester.pump(const Duration(milliseconds: 60));
    expect(find.byType(CupertinoActionSheet), findsNothing);
  });

864
  testWidgets('Modal barrier is pressed during transition', (WidgetTester tester) async {
865 866
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
867
        CupertinoActionSheet(
868 869 870
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
871
            CupertinoActionSheetAction(
872
              child: const Text('One'),
873
              onPressed: () { },
874
            ),
875
            CupertinoActionSheetAction(
876
              child: const Text('Two'),
877
              onPressed: () { },
878 879
            ),
          ],
880
          cancelButton: CupertinoActionSheetAction(
881
            child: const Text('Cancel'),
882
            onPressed: () { },
883 884 885 886 887 888 889 890 891 892 893 894
          ),
        ),
      ),
    );

    // Enter animation
    await tester.tap(find.text('Go'));

    await tester.pump();
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, 600.0);

    await tester.pump(const Duration(milliseconds: 60));
895
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
896 897

    await tester.pump(const Duration(milliseconds: 60));
898
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(374.3, epsilon: 0.1));
899 900

    await tester.pump(const Duration(milliseconds: 60));
901
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(337.1, epsilon: 0.1));
902

903
    // Exit animation
904 905 906
    await tester.tapAt(const Offset(20.0, 20.0));
    await tester.pump(const Duration(milliseconds: 60));

907 908
    await tester.pump(const Duration(milliseconds: 60));
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(374.3, epsilon: 0.1));
909

910 911
    await tester.pump(const Duration(milliseconds: 60));
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
912

913 914
    await tester.pump(const Duration(milliseconds: 60));
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, 600.0);
915 916

    // Action sheet has disappeared
917
    await tester.pump(const Duration(milliseconds: 60));
918 919 920 921 922
    expect(find.byType(CupertinoActionSheet), findsNothing);
  });


  testWidgets('Action sheet semantics', (WidgetTester tester) async {
923
    final SemanticsTester semantics = SemanticsTester(tester);
924 925 926

    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
927
        CupertinoActionSheet(
928 929 930
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
931
            CupertinoActionSheetAction(
932
              child: const Text('One'),
933
              onPressed: () { },
934
            ),
935
            CupertinoActionSheetAction(
936
              child: const Text('Two'),
937
              onPressed: () { },
938 939
            ),
          ],
940
          cancelButton: CupertinoActionSheetAction(
941
            child: const Text('Cancel'),
942
            onPressed: () { },
943 944 945 946 947 948
          ),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
949
    await tester.pumpAndSettle();
950 951 952 953

    expect(
      semantics,
      hasSemantics(
954
        TestSemantics.root(
955
          children: <TestSemantics>[
956
            TestSemantics(
957
              children: <TestSemantics>[
958
                TestSemantics(
959
                  children: <TestSemantics>[
960
                    TestSemantics(
961
                      flags: <SemanticsFlag>[
962 963
                        SemanticsFlag.scopesRoute,
                        SemanticsFlag.namesRoute,
964
                      ],
965
                      label: 'Alert',
966
                      children: <TestSemantics>[
967
                        TestSemantics(
968 969 970 971 972 973 974 975 976 977 978
                          flags: <SemanticsFlag>[
                            SemanticsFlag.hasImplicitScrolling,
                          ],
                          children: <TestSemantics>[
                            TestSemantics(
                              label: 'The title',
                            ),
                            TestSemantics(
                              label: 'The message',
                            ),
                          ],
979
                        ),
980
                        TestSemantics(
981
                          flags: <SemanticsFlag>[
982
                            SemanticsFlag.hasImplicitScrolling,
983
                          ],
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
                          children: <TestSemantics>[
                            TestSemantics(
                              flags: <SemanticsFlag>[
                                SemanticsFlag.isButton,
                              ],
                              actions: <SemanticsAction>[
                                SemanticsAction.tap,
                              ],
                              label: 'One',
                            ),
                            TestSemantics(
                              flags: <SemanticsFlag>[
                                SemanticsFlag.isButton,
                              ],
                              actions: <SemanticsAction>[
                                SemanticsAction.tap,
                              ],
                              label: 'Two',
                            ),
1003 1004
                          ],
                        ),
1005
                        TestSemantics(
1006 1007 1008 1009 1010 1011
                          flags: <SemanticsFlag>[
                            SemanticsFlag.isButton,
                          ],
                          actions: <SemanticsAction>[
                            SemanticsAction.tap,
                          ],
1012
                          label: 'Cancel',
1013 1014 1015
                        ),
                      ],
                    ),
1016
                  ],
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                ),
              ],
            ),
          ],
        ),
        ignoreId: true,
        ignoreRect: true,
        ignoreTransform: true,
      ),
    );

    semantics.dispose();
  });
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 1066 1067 1068 1069

  testWidgets('Conflicting scrollbars are not applied by ScrollBehavior to CupertinoActionSheet', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/83819
    final ScrollController actionScrollController = ScrollController();
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        Builder(builder: (BuildContext context) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
            child: CupertinoActionSheet(
              title: const Text('The title'),
              message: const Text('The message.'),
              actions: <Widget>[
                CupertinoActionSheetAction(
                  child: const Text('One'),
                  onPressed: () { },
                ),
                CupertinoActionSheetAction(
                  child: const Text('Two'),
                  onPressed: () { },
                ),
              ],
              actionScrollController: actionScrollController,
            ),
          );
        }),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();

    // The inherited ScrollBehavior should not apply Scrollbars since they are
    // already built in to the widget.
    expect(find.byType(Scrollbar), findsNothing);
    expect(find.byType(RawScrollbar), findsNothing);
    // Built in CupertinoScrollbars should only number 2: one for the actions,
    // one for the content.
    expect(find.byType(CupertinoScrollbar), findsNWidgets(2));
  }, variant: TargetPlatformVariant.all());
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

  testWidgets('Hovering over Cupertino action sheet action updates cursor to clickable on Web', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        CupertinoActionSheet(
            title: const Text('The title'),
            message: const Text('Message'),
            actions: <Widget>[
              CupertinoActionSheetAction(
                child: const Text('One'),
                onPressed: () { },
              ),
            ],
          )
        ),
    );
    await tester.tap(find.text('Go'));
    await tester.pump();

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: const Offset(10, 10));
    await tester.pumpAndSettle();
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);

    final Offset actionSheetAction = tester.getCenter(find.text('One'));
    await gesture.moveTo(actionSheetAction);
    await tester.pumpAndSettle();
    expect(
      RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
      kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
    );
  });
1102 1103 1104
}

RenderBox findScrollableActionsSectionRenderBox(WidgetTester tester) {
1105 1106
  final RenderObject actionsSection = tester.renderObject(
    find.byElementPredicate((Element element) {
1107 1108 1109 1110
      return element.widget.runtimeType.toString() == '_CupertinoAlertActionSection';
    }),
  );
  assert(actionsSection is RenderBox);
1111
  return actionsSection as RenderBox;
1112 1113 1114
}

Widget createAppWithButtonThatLaunchesActionSheet(Widget actionSheet) {
1115 1116 1117 1118
  return CupertinoApp(
    home: Center(
      child: Builder(builder: (BuildContext context) {
        return CupertinoButton(
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
          onPressed: () {
            showCupertinoModalPopup<void>(
              context: context,
              builder: (BuildContext context) {
                return actionSheet;
              },
            );
          },
          child: const Text('Go'),
        );
      }),
    ),
  );
}

Widget boilerplate(Widget child) {
1135
  return Directionality(
1136 1137 1138
    textDirection: TextDirection.ltr,
    child: child,
  );
1139
}