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

5 6 7 8 9 10
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=123"
@Tags(<String>['no-shuffle'])

11
import 'package:flutter/cupertino.dart';
12 13
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
14
import 'package:flutter/material.dart';
15
import 'package:flutter/rendering.dart';
16 17 18 19 20 21

import 'package:flutter_test/flutter_test.dart';

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

void main() {
22
  testWidgets('Verify that a tap on modal barrier dismisses an action sheet', (WidgetTester tester) async {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

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

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

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

41
  testWidgets('Verify that a tap on title section (not buttons) does not dismiss an action sheet', (WidgetTester tester) async {
42 43 44 45 46 47 48 49 50 51
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

    await tester.tap(find.text('Go'));
    await tester.pump();
52
    await tester.pump(const Duration(seconds: 5));
53 54 55 56 57 58 59 60 61 62 63

    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(
64
        CupertinoActionSheetAction(
65 66
          isDestructiveAction: true,
          child: const Text('Ok'),
67
          onPressed: () { },
68 69 70 71 72 73
        ),
      ),
    );

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

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
74
    expect(widget.style.color, const CupertinoDynamicColor.withBrightnessAndContrast(
75 76 77 78 79 80 81 82 83 84 85 86 87 88
      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;
89
    late StateSetter stateSetter;
90 91 92 93 94 95

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

    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(
126
      actionTextStyle('action').color!.value,
127 128 129 130 131 132 133
      const Color.fromARGB(255, 0, 122, 255).value,
    );

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

    expect(
134
      actionTextStyle('action').color!.value,
135 136
      const Color.fromARGB(255, 10, 132, 255).value,
    );
137 138 139 140 141
  });

  testWidgets('Action sheet default text style', (WidgetTester tester) async {
    await tester.pumpWidget(
      boilerplate(
142
        CupertinoActionSheetAction(
143 144
          isDefaultAction: true,
          child: const Text('Ok'),
145
          onPressed: () { },
146 147 148 149 150 151
        ),
      ),
    );

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

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
152
    expect(widget.style.fontWeight, equals(FontWeight.w600));
153 154
  });

155
  testWidgets('Action sheet text styles are correct when both title and message are included', (WidgetTester tester) async {
156 157 158 159
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
160
          message: Text('An action sheet'),
161 162 163 164 165 166 167
        ),
      ),
    );

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

168 169
    final DefaultTextStyle titleStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'Action Sheet'));
    final DefaultTextStyle messageStyle = tester.firstWidget(find.widgetWithText(DefaultTextStyle, 'An action sheet'));
170

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
171 172
    expect(titleStyle.style.fontWeight, FontWeight.w600);
    expect(messageStyle.style.fontWeight, FontWeight.w400);
173 174
  });

175
  testWidgets('Action sheet text styles are correct when title but no message is included', (WidgetTester tester) async {
176 177 178 179 180 181 182 183 184 185 186
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          title: Text('Action Sheet'),
        ),
      ),
    );

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

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

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
189
    expect(titleStyle.style.fontWeight, FontWeight.w400);
190 191
  });

192
  testWidgets('Action sheet text styles are correct when message but no title is included', (WidgetTester tester) async {
193 194 195 196 197 198 199 200 201 202 203
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
        const CupertinoActionSheet(
          message: Text('An action sheet'),
        ),
      ),
    );

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

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

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
206
    expect(messageStyle.style.fontWeight, FontWeight.w600);
207 208 209
  });

  testWidgets('Content section but no actions', (WidgetTester tester) async {
210
    final ScrollController scrollController = ScrollController();
211 212
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
213
        CupertinoActionSheet(
214 215 216 217 218 219 220 221 222 223 224 225 226 227
          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).
228 229 230 231
    expect(
      tester.getBottomLeft(find.byType(ClipRRect)),
      tester.getBottomLeft(find.byType(CupertinoActionSheet)) - const Offset(-8.0, 10.0),
    );
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    // 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 {
247
    final ScrollController actionScrollController = ScrollController();
248 249
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
250
        CupertinoActionSheet(
251
          actions: <Widget>[
252
            CupertinoActionSheetAction(
253
              child: const Text('One'),
254
              onPressed: () { },
255
            ),
256
            CupertinoActionSheetAction(
257
              child: const Text('Two'),
258
              onPressed: () { },
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            ),
          ],
          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).
279 280 281 282
    expect(
      tester.getTopLeft(finder),
      tester.getTopLeft(find.byType(CupertinoActionSheet)) + const Offset(8.0, 10.0),
    );
283

284 285 286 287 288 289 290 291
    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')),
    );
292 293 294
  });

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

    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 {
360
    final ScrollController messageScrollController = ScrollController();
361
    late double screenHeight;
362 363
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
364
        Builder(builder: (BuildContext context) {
365
          screenHeight = MediaQuery.of(context).size.height;
366
          return MediaQuery(
367
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
368
            child: CupertinoActionSheet(
369
              title: const Text('The title'),
370
              message: Text('Very long content' * 200),
371
              actions: <Widget>[
372
                CupertinoActionSheetAction(
373
                  child: const Text('One'),
374
                  onPressed: () { },
375
                ),
376
                CupertinoActionSheetAction(
377
                  child: const Text('Two'),
378
                  onPressed: () { },
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                ),
              ],
              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);
  });

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  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);
  });

431 432 433 434
  testWidgets('Tap on button calls onPressed', (WidgetTester tester) async {
    bool wasPressed = false;
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
435 436
        Builder(builder: (BuildContext context) {
          return CupertinoActionSheet(
437
            actions: <Widget>[
438
              CupertinoActionSheetAction(
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                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);
  });

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

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

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

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

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

521
    expect(tester.getSize(find.byType(CupertinoActionSheet)).height, moreOrLessEquals(132.33333333333334));
522 523 524 525 526
  });

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

    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(
554
        CupertinoActionSheet(
555
          title: const Text('The title'),
556
          message: Text('Very long content' * 200),
557
          actions: <Widget>[
558
            CupertinoActionSheetAction(
559
              child: const Text('One'),
560
              onPressed: () { },
561
            ),
562
            CupertinoActionSheetAction(
563
              child: const Text('Two'),
564
              onPressed: () { },
565 566
            ),
          ],
567
          cancelButton: CupertinoActionSheetAction(
568
            child: const Text('Cancel'),
569
            onPressed: () { },
570 571 572 573 574 575 576 577
          ),
        ),
      ),
    );

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

578
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(112.33333333333331));
579 580 581 582 583
  });

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

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

612
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(168.66666666666669));
613 614 615 616 617
  });

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

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

650
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(84.33333333333337));
651 652 653 654 655
  });

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

    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(
678
        CupertinoActionSheet(
679
          title: const Text('The title'),
680
          message: Text('Very long content' * 200),
681
          actions: <Widget>[
682
            CupertinoActionSheetAction(
683
              child: const Text('One'),
684
              onPressed: () { },
685
            ),
686
            CupertinoActionSheetAction(
687
              child: const Text('Two'),
688
              onPressed: () { },
689 690 691 692 693 694 695 696 697
            ),
          ],
        ),
      ),
    );

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

698
    expect(findScrollableActionsSectionRenderBox(tester).size.height, moreOrLessEquals(84.33333333333337));
699 700 701 702 703
  });

  testWidgets('Action sheet with just cancel button is correct', (WidgetTester tester) async {
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
704 705
        CupertinoActionSheet(
          cancelButton: CupertinoActionSheetAction(
706
            child: const Text('Cancel'),
707
            onPressed: () { },
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
          ),
        ),
      ),
    );

    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(
725 726 727
        Builder(builder: (BuildContext context) {
          return CupertinoActionSheet(
            cancelButton: CupertinoActionSheetAction(
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 753 754 755 756 757 758
              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(
759
        CupertinoActionSheet(
760 761 762
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
763
            CupertinoActionSheetAction(
764
              child: const Text('One'),
765
              onPressed: () { },
766
            ),
767
            CupertinoActionSheetAction(
768
              child: const Text('Two'),
769
              onPressed: () { },
770 771
            ),
          ],
772
          cancelButton: CupertinoActionSheetAction(
773
            child: const Text('Cancel'),
774
            onPressed: () { },
775 776 777 778 779 780 781 782 783 784 785
          ),
        ),
      ),
    );

    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);
786 787 788 789
    expect(
      tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'One')).dy,
      moreOrLessEquals(469.66666666666663),
    );
790 791 792 793 794 795
    expect(tester.getBottomLeft(find.widgetWithText(CupertinoActionSheetAction, 'Two')).dy, 526.0);
  });

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

    // 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));
824
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
825 826

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

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

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

    await tester.pump(const Duration(milliseconds: 60));
836
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(320.8, epsilon: 0.1));
837 838

    await tester.pump(const Duration(milliseconds: 60));
839
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(319.3, epsilon: 0.1));
840 841 842

    // Action sheet has reached final height
    await tester.pump(const Duration(milliseconds: 60));
843
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(319.3, epsilon: 0.1));
844 845 846 847

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

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

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

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

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

    await tester.pump(const Duration(milliseconds: 60));
863
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(598.5, epsilon: 0.1));
864 865 866 867 868 869 870 871 872

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

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

    // 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));
901
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
902 903

    await tester.pump(const Duration(milliseconds: 60));
904
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(374.3, epsilon: 0.1));
905 906

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

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

    await tester.pump(const Duration(milliseconds: 60));
914
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(374.3, epsilon: 0.1));
915 916

    await tester.pump(const Duration(milliseconds: 60));
917
    expect(tester.getTopLeft(find.byType(CupertinoActionSheet)).dy, moreOrLessEquals(470.0, epsilon: 0.1));
918 919 920 921 922 923 924 925 926 927 928

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

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


  testWidgets('Action sheet semantics', (WidgetTester tester) async {
929
    final SemanticsTester semantics = SemanticsTester(tester);
930 931 932

    await tester.pumpWidget(
      createAppWithButtonThatLaunchesActionSheet(
933
        CupertinoActionSheet(
934 935 936
          title: const Text('The title'),
          message: const Text('The message'),
          actions: <Widget>[
937
            CupertinoActionSheetAction(
938
              child: const Text('One'),
939
              onPressed: () { },
940
            ),
941
            CupertinoActionSheetAction(
942
              child: const Text('Two'),
943
              onPressed: () { },
944 945
            ),
          ],
946
          cancelButton: CupertinoActionSheetAction(
947
            child: const Text('Cancel'),
948
            onPressed: () { },
949 950 951 952 953 954 955 956 957 958 959
          ),
        ),
      ),
    );

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

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

    semantics.dispose();
  });
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 1070 1071 1072 1073 1074 1075

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

  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);
    addTearDown(gesture.removePointer);
    await tester.pumpAndSettle();
    expect(
      RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
      kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
    );
  });
1109 1110 1111
}

RenderBox findScrollableActionsSectionRenderBox(WidgetTester tester) {
1112 1113
  final RenderObject actionsSection = tester.renderObject(
    find.byElementPredicate((Element element) {
1114 1115 1116 1117
      return element.widget.runtimeType.toString() == '_CupertinoAlertActionSection';
    }),
  );
  assert(actionsSection is RenderBox);
1118
  return actionsSection as RenderBox;
1119 1120 1121
}

Widget createAppWithButtonThatLaunchesActionSheet(Widget actionSheet) {
1122 1123 1124 1125
  return CupertinoApp(
    home: Center(
      child: Builder(builder: (BuildContext context) {
        return CupertinoButton(
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
          onPressed: () {
            showCupertinoModalPopup<void>(
              context: context,
              builder: (BuildContext context) {
                return actionSheet;
              },
            );
          },
          child: const Text('Go'),
        );
      }),
    ),
  );
}

Widget boilerplate(Widget child) {
1142
  return Directionality(
1143 1144 1145
    textDirection: TextDirection.ltr,
    child: child,
  );
1146
}