dialog_test.dart 56.8 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
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
8
library;
9

10
import 'dart:math';
11
import 'dart:ui';
12

13
import 'package:flutter/cupertino.dart';
14
import 'package:flutter/foundation.dart';
15
import 'package:flutter/material.dart';
16
import 'package:flutter/rendering.dart';
17 18
import 'package:flutter_test/flutter_test.dart';

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

21
void main() {
22
  testWidgets('Alert dialog control test', (WidgetTester tester) async {
23 24
    bool didDelete = false;

25 26 27
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
28
          return CupertinoAlertDialog(
29 30 31 32 33 34
            title: const Text('The title'),
            content: const Text('The content'),
            actions: <Widget>[
              const CupertinoDialogAction(
                child: Text('Cancel'),
              ),
35
              CupertinoDialogAction(
36
                isDestructiveAction: true,
37
                onPressed: () {
38 39
                  didDelete = true;
                  Navigator.pop(context);
40
                },
41 42 43 44 45
                child: const Text('Delete'),
              ),
            ],
          );
        },
46
      ),
47
    );
48 49 50 51 52 53 54 55 56

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

    expect(didDelete, isFalse);

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

57
    expect(didDelete, isTrue);
58 59 60
    expect(find.text('Delete'), findsNothing);
  });

61
  testWidgets('Dialog not barrier dismissible by default', (WidgetTester tester) async {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    await tester.pumpWidget(createAppWithCenteredButton(const Text('Go')));

    final BuildContext context = tester.element(find.text('Go'));

    showCupertinoDialog<void>(
      context: context,
      builder: (BuildContext context) {
        return Container(
          width: 100.0,
          height: 100.0,
          alignment: Alignment.center,
          child: const Text('Dialog'),
        );
      },
    );

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(find.text('Dialog'), findsOneWidget);

    // Tap on the barrier, which shouldn't do anything this time.
    await tester.tapAt(const Offset(10.0, 10.0));

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(find.text('Dialog'), findsOneWidget);

  });

89
  testWidgets('Dialog configurable to be barrier dismissible', (WidgetTester tester) async {
90 91 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
    await tester.pumpWidget(createAppWithCenteredButton(const Text('Go')));

    final BuildContext context = tester.element(find.text('Go'));

    showCupertinoDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return Container(
          width: 100.0,
          height: 100.0,
          alignment: Alignment.center,
          child: const Text('Dialog'),
        );
      },
    );

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(find.text('Dialog'), findsOneWidget);

    // Tap off the barrier.
    await tester.tapAt(const Offset(10.0, 10.0));

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(find.text('Dialog'), findsNothing);
  });

117
  testWidgets('Dialog destructive action style', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
118
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
119
      isDestructiveAction: true,
120
      child: Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
121
    )));
122

123
    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));
124

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
125
    expect(widget.style.color!.withAlpha(255), CupertinoColors.systemRed.color);
126
  });
127

128
  testWidgets('Dialog default action style', (WidgetTester tester) async {
129 130
    await tester.pumpWidget(
      CupertinoTheme(
131 132 133 134 135 136
        data: const CupertinoThemeData(
          primaryColor: CupertinoColors.systemGreen,
        ),
        child: boilerplate(const CupertinoDialogAction(
          child: Text('Ok'),
        )),
137 138 139 140 141 142 143 144
      ),
    );

    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));

    expect(widget.style.color!.withAlpha(255), CupertinoColors.systemGreen.color);
  });

145
  testWidgets('Dialog dark theme', (WidgetTester tester) async {
146 147 148 149 150 151 152 153
    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(platformBrightness: Brightness.dark),
          child: CupertinoAlertDialog(
            title: const Text('The Title'),
            content: const Text('Content'),
            actions: <Widget>[
154 155 156 157 158
              CupertinoDialogAction(
                isDefaultAction: true,
                onPressed: () {},
                child: const Text('Cancel'),
              ),
159 160 161 162 163 164 165
              const CupertinoDialogAction(child: Text('OK')),
            ],
          ),
        ),
      ),
    );

166
    final RichText cancelText = tester.widget<RichText>(
167 168 169 170
      find.descendant(of: find.text('Cancel'), matching: find.byType(RichText)),
    );

    expect(
171
      cancelText.text.style!.color!.value,
172 173 174 175 176 177 178 179 180
      0xFF0A84FF, // dark elevated color of systemBlue.
    );

    expect(
      find.byType(CupertinoAlertDialog),
      paints..rect(color: const Color(0xBF1E1E1E)),
    );
  });

181
  testWidgets('Has semantic annotations', (WidgetTester tester) async {
182
    final SemanticsTester semantics = SemanticsTester(tester);
183
    await tester.pumpWidget(const MaterialApp(home: Material(
184 185 186 187 188 189 190
      child: CupertinoAlertDialog(
        title: Text('The Title'),
        content: Text('Content'),
        actions: <Widget>[
          CupertinoDialogAction(child: Text('Cancel')),
          CupertinoDialogAction(child: Text('OK')),
        ],
191
      ),
192 193 194 195 196
    )));

    expect(
      semantics,
      hasSemantics(
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        TestSemantics.root(
          children: <TestSemantics>[
            TestSemantics(
              children: <TestSemantics>[
                TestSemantics(
                  children: <TestSemantics>[
                    TestSemantics(
                      flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                      children: <TestSemantics>[
                        TestSemantics(
                          flags: <SemanticsFlag>[SemanticsFlag.scopesRoute, SemanticsFlag.namesRoute],
                          label: 'Alert',
                          children: <TestSemantics>[
                            TestSemantics(
                              flags: <SemanticsFlag>[
                                SemanticsFlag.hasImplicitScrolling,
                              ],
                              children: <TestSemantics>[
                                TestSemantics(label: 'The Title'),
                                TestSemantics(label: 'Content'),
                              ],
                            ),
                            TestSemantics(
                              flags: <SemanticsFlag>[
                                SemanticsFlag.hasImplicitScrolling,
                              ],
                              children: <TestSemantics>[
                                TestSemantics(
                                  flags: <SemanticsFlag>[SemanticsFlag.isButton],
                                  label: 'Cancel',
                                ),
                                TestSemantics(
                                  flags: <SemanticsFlag>[SemanticsFlag.isButton],
                                  label: 'OK',
                                ),
                              ],
                            ),
                          ],
                        ),
                      ],
                    ),
238 239
                  ],
                ),
240 241 242 243 244 245 246
              ],
            ),
          ],
        ),
        ignoreId: true,
        ignoreRect: true,
        ignoreTransform: true,
247
      ),
248
    );
249 250 251 252

    semantics.dispose();
  });

253
  testWidgets('Dialog default action style', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
254
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
255
      isDefaultAction: true,
256
      child: Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
257
    )));
258 259 260

    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
261
    expect(widget.style.fontWeight, equals(FontWeight.w600));
262 263
  });

264
  testWidgets('Dialog default and destructive action styles', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
265
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
266 267
      isDefaultAction: true,
      isDestructiveAction: true,
268
      child: Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
269
    )));
270 271 272

    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
273 274
    expect(widget.style.color!.withAlpha(255), CupertinoColors.systemRed.color);
    expect(widget.style.fontWeight, equals(FontWeight.w600));
275 276
  });

277
  testWidgets('Dialog disabled action style', (WidgetTester tester) async {
278 279 280 281 282 283
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
      child: Text('Ok'),
    )));

    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
284 285
    expect(widget.style.color!.opacity, greaterThanOrEqualTo(127 / 255));
    expect(widget.style.color!.opacity, lessThanOrEqualTo(128 / 255));
286 287
  });

288
  testWidgets('Dialog enabled action style', (WidgetTester tester) async {
289 290 291 292 293 294 295
    await tester.pumpWidget(boilerplate(CupertinoDialogAction(
      child: const Text('Ok'),
      onPressed: () {},
    )));

    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));

Alexandre Ardhuin's avatar
Alexandre Ardhuin committed
296
    expect(widget.style.color!.opacity, equals(1.0));
297
  });
298

299
  testWidgets('Message is scrollable, has correct padding with large text sizes', (WidgetTester tester) async {
300
    final ScrollController scrollController = ScrollController();
301
    addTearDown(scrollController.dispose);
302
    await tester.pumpWidget(
303 304
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
305
          return MediaQuery(
306
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
307
            child: CupertinoAlertDialog(
308
              title: const Text('The Title'),
309
              content: Text('Very long content ' * 20),
310 311 312 313 314 315 316 317 318 319 320 321
              actions: const <Widget>[
                CupertinoDialogAction(
                  child: Text('Cancel'),
                ),
                CupertinoDialogAction(
                  isDestructiveAction: true,
                  child: Text('OK'),
                ),
              ],
              scrollController: scrollController,
            ),
          );
322 323
        },
      ),
324 325 326
    );

    await tester.tap(find.text('Go'));
327
    await tester.pumpAndSettle();
328 329 330 331

    expect(scrollController.offset, 0.0);
    scrollController.jumpTo(100.0);
    expect(scrollController.offset, 100.0);
332 333
    // Set the scroll position back to zero.
    scrollController.jumpTo(0.0);
334

335
    await tester.pumpAndSettle();
336

337 338
    // Expect the modal dialog box to take all available height.
    expect(
339
      tester.getSize(find.byType(ClipRRect)),
340
      equals(const Size(310.0, 560.0 - 24.0 * 2)),
341
    );
342

343 344 345 346
    // Check sizes/locations of the text. The text is large so these 2 buttons are stacked.
    // Visually the "Cancel" button and "OK" button are the same height when using the
    // regular font. However, when using the test font, "Cancel" becomes 2 lines which
    // is why the height we're verifying for "Cancel" is larger than "OK".
347

348 349 350
    if (!kIsWeb || isCanvasKit) { // https://github.com/flutter/flutter/issues/99933
      expect(tester.getSize(find.text('The Title')), equals(const Size(270.0, 132.0)));
    }
351
    expect(tester.getTopLeft(find.text('The Title')), equals(const Offset(265.0, 80.0 + 24.0)));
352 353
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Cancel')), equals(const Size(310.0, 148.0)));
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'OK')), equals(const Size(310.0, 98.0)));
354 355
  });

356
  testWidgets('Dialog respects small constraints.', (WidgetTester tester) async {
357
    final ScrollController scrollController = ScrollController();
358
    addTearDown(scrollController.dispose);
359
    await tester.pumpWidget(
360 361
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
362 363
          return Center(
            child: ConstrainedBox(
364 365
              // Constrain the dialog to a tiny size and ensure it respects
              // these exact constraints.
366 367
              constraints: BoxConstraints.tight(const Size(200.0, 100.0)),
              child: CupertinoAlertDialog(
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                title: const Text('The Title'),
                content: const Text('The message'),
                actions: const <Widget>[
                  CupertinoDialogAction(
                    child: Text('Option 1'),
                  ),
                  CupertinoDialogAction(
                    child: Text('Option 2'),
                  ),
                  CupertinoDialogAction(
                    child: Text('Option 3'),
                  ),
                ],
                scrollController: scrollController,
              ),
            ),
          );
        },
      ),
    );

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

    const double topAndBottomMargin = 40.0;
393 394
    const double topAndBottomPadding = 24.0 * 2;
    const double leftAndRightPadding = 40.0 * 2;
395 396 397
    final Finder modalFinder = find.byType(ClipRRect);
    expect(
      tester.getSize(modalFinder),
398
      equals(const Size(200.0 - leftAndRightPadding, 100.0 - topAndBottomMargin - topAndBottomPadding)),
399 400 401
    );
  });

402
  testWidgets('Button list is scrollable, has correct position with large text sizes.', (WidgetTester tester) async {
403
    final ScrollController actionScrollController = ScrollController();
404
    addTearDown(actionScrollController.dispose);
405 406 407
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
408
          return MediaQuery(
409
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
410
            child: CupertinoAlertDialog(
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
              title: const Text('The title'),
              content: const Text('The content.'),
              actions: const <Widget>[
                CupertinoDialogAction(
                  child: Text('One'),
                ),
                CupertinoDialogAction(
                  child: Text('Two'),
                ),
                CupertinoDialogAction(
                  child: Text('Three'),
                ),
                CupertinoDialogAction(
                  child: Text('Chocolate Brownies'),
                ),
                CupertinoDialogAction(
                  isDestructiveAction: true,
                  child: Text('Cancel'),
                ),
              ],
              actionScrollController: actionScrollController,
            ),
          );
434 435
        },
      ),
436 437 438 439 440 441 442
    );

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

    await tester.pump();

    // Check that the action buttons list is scrollable.
443 444 445 446
    expect(actionScrollController.offset, 0.0);
    actionScrollController.jumpTo(100.0);
    expect(actionScrollController.offset, 100.0);
    actionScrollController.jumpTo(0.0);
447 448 449 450 451 452 453 454 455 456 457

    // Check that the action buttons are aligned vertically.
    expect(tester.getCenter(find.widgetWithText(CupertinoDialogAction, 'One')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoDialogAction, 'Two')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoDialogAction, 'Three')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoDialogAction, 'Chocolate Brownies')).dx, equals(400.0));
    expect(tester.getCenter(find.widgetWithText(CupertinoDialogAction, 'Cancel')).dx, equals(400.0));

    // Check that the action buttons are the correct heights.
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'One')).height, equals(98.0));
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Two')).height, equals(98.0));
458 459
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Three')).height, equals(98.0));
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Chocolate Brownies')).height, equals(248.0));
460 461 462
    expect(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Cancel')).height, equals(148.0));
  });

463
  testWidgets('Title Section is empty, Button section is not empty.', (WidgetTester tester) async {
464
    const double textScaleFactor = 1.0;
465
    final ScrollController actionScrollController = ScrollController();
466
    addTearDown(actionScrollController.dispose);
467
    await tester.pumpWidget(
468 469
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
470
          return MediaQuery(
471
            data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
472
            child: CupertinoAlertDialog(
473 474 475 476 477 478 479 480 481 482 483
              actions: const <Widget>[
                CupertinoDialogAction(
                  child: Text('One'),
                ),
                CupertinoDialogAction(
                  child: Text('Two'),
                ),
              ],
              actionScrollController: actionScrollController,
            ),
          );
484
        },
485
      ),
486 487 488 489 490
    );

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

    await tester.pump();
491 492 493 494 495 496 497 498 499 500 501 502 503 504

    // Check that the dialog size is the same as the actions section size. This
    // ensures that an empty content section doesn't accidentally render some
    // empty space in the dialog.
    final Finder contentSectionFinder = find.byElementPredicate((Element element) {
      return element.widget.runtimeType.toString() == '_CupertinoAlertActionSection';
    });

    final Finder modalBoundaryFinder = find.byType(ClipRRect);

    expect(
      tester.getSize(contentSectionFinder),
      tester.getSize(modalBoundaryFinder),
    );
505 506

    // Check that the title/message section is not displayed
507 508
    expect(actionScrollController.offset, 0.0);
    expect(tester.getTopLeft(find.widgetWithText(CupertinoDialogAction, 'One')).dy, equals(277.5));
509 510

    // Check that the button's vertical size is the same.
511 512 513 514
    expect(
      tester.getSize(find.widgetWithText(CupertinoDialogAction, 'One')).height,
      equals(tester.getSize(find.widgetWithText(CupertinoDialogAction, 'Two')).height),
    );
515 516
  });

517
  testWidgets('Button section is empty, Title section is not empty.', (WidgetTester tester) async {
518
    const double textScaleFactor = 1.0;
519
    final ScrollController scrollController = ScrollController();
520
    addTearDown(scrollController.dispose);
521
    await tester.pumpWidget(
522 523
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
524
          return MediaQuery(
525
            data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
526
            child: CupertinoAlertDialog(
527 528 529 530 531 532 533
              title: const Text('The title'),
              content: const Text('The content.'),
              scrollController: scrollController,
            ),
          );
        },
      ),
534 535 536 537 538 539 540 541 542
    );

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

    await tester.pump();

    // Check that there's no button action section.
    expect(scrollController.offset, 0.0);
    expect(find.widgetWithText(CupertinoDialogAction, 'One'), findsNothing);
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

    // Check that the dialog size is the same as the content section size. This
    // ensures that an empty button section doesn't accidentally render some
    // empty space in the dialog.
    final Finder contentSectionFinder = find.byElementPredicate((Element element) {
      return element.widget.runtimeType.toString() == '_CupertinoAlertContentSection';
    });

    final Finder modalBoundaryFinder = find.byType(ClipRRect);

    expect(
      tester.getSize(contentSectionFinder),
      tester.getSize(modalBoundaryFinder),
    );
  });

559
  testWidgets('Actions section height for 1 button is height of button.', (WidgetTester tester) async {
560
    final ScrollController scrollController = ScrollController();
561
    addTearDown(scrollController.dispose);
562 563 564
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
565
          return CupertinoAlertDialog(
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
            title: const Text('The Title'),
            content: const Text('The message'),
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('OK'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox okButtonBox = findActionButtonRenderBoxByTitle(tester, 'OK');
    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    expect(okButtonBox.size.width, actionsSectionBox.size.width);
    expect(okButtonBox.size.height, actionsSectionBox.size.height);
  });

589
  testWidgets('Actions section height for 2 side-by-side buttons is height of tallest button.', (WidgetTester tester) async {
590
    final ScrollController scrollController = ScrollController();
591
    addTearDown(scrollController.dispose);
592
    late double dividerWidth; // Will be set when the dialog builder runs. Needs a BuildContext.
593 594 595
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
596
          dividerWidth = 0.3;
597
          return CupertinoAlertDialog(
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
            title: const Text('The Title'),
            content: const Text('The message'),
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('OK'),
              ),
              CupertinoDialogAction(
                isDestructiveAction: true,
                child: Text('Cancel'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox okButtonBox = findActionButtonRenderBoxByTitle(tester, 'OK');
    final RenderBox cancelButtonBox = findActionButtonRenderBoxByTitle(tester, 'Cancel');
    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    expect(okButtonBox.size.width, cancelButtonBox.size.width);

    expect(
      actionsSectionBox.size.width,
      okButtonBox.size.width + cancelButtonBox.size.width + dividerWidth,
    );

    expect(
      actionsSectionBox.size.height,
      max(okButtonBox.size.height, cancelButtonBox.size.height),
    );
  });

635
  testWidgets('Actions section height for 2 stacked buttons with enough room is height of both buttons.', (WidgetTester tester) async {
636
    final ScrollController scrollController = ScrollController();
637
    addTearDown(scrollController.dispose);
638
    const double dividerThickness = 0.3;
639 640 641
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
642
          return CupertinoAlertDialog(
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
            title: const Text('The Title'),
            content: const Text('The message'),
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('OK'),
              ),
              CupertinoDialogAction(
                isDestructiveAction: true,
                child: Text('This is too long to fit'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox okButtonBox = findActionButtonRenderBoxByTitle(tester, 'OK');
    final RenderBox longButtonBox = findActionButtonRenderBoxByTitle(tester, 'This is too long to fit');
    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    expect(okButtonBox.size.width, longButtonBox.size.width);

    expect(okButtonBox.size.width, actionsSectionBox.size.width);

    expect(
      okButtonBox.size.height + dividerThickness + longButtonBox.size.height,
      actionsSectionBox.size.height,
    );
  });

677
  testWidgets('Actions section height for 2 stacked buttons without enough room and regular font is 1.5 buttons tall.', (WidgetTester tester) async {
678
    final ScrollController scrollController = ScrollController();
679
    addTearDown(scrollController.dispose);
680 681 682
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
683
          return CupertinoAlertDialog(
684
            title: const Text('The Title'),
685
            content: Text('The message\n' * 40),
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('OK'),
              ),
              CupertinoDialogAction(
                isDestructiveAction: true,
                child: Text('This is too long to fit'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    expect(
      actionsSectionBox.size.height,
708
      67.80000000000001,
709 710 711
    );
  });

712
  testWidgets('Actions section height for 2 stacked buttons without enough room and large accessibility font is 50% of dialog height.', (WidgetTester tester) async {
713
    final ScrollController scrollController = ScrollController();
714
    addTearDown(scrollController.dispose);
715 716 717
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
718
          return MediaQuery(
719
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
720
            child: CupertinoAlertDialog(
721
              title: const Text('The Title'),
722
              content: Text('The message\n' * 20),
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
              actions: const <Widget>[
                CupertinoDialogAction(
                  child: Text('This button is multi line'),
                ),
                CupertinoDialogAction(
                  isDestructiveAction: true,
                  child: Text('This button is multi line'),
                ),
              ],
              scrollController: scrollController,
            ),
          );
        },
      ),
    );

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

    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

744
    // The two multiline buttons with large text are taller than 50% of the
745 746 747 748
    // dialog height, but with the accessibility layout policy, the 2 buttons
    // should be in a scrollable area equal to half the dialog height.
    expect(
      actionsSectionBox.size.height,
749
      280.0 - 24.0,
750 751 752
    );
  });

753
  testWidgets('Actions section height for 3 buttons without enough room is 1.5 buttons tall.', (WidgetTester tester) async {
754
    final ScrollController scrollController = ScrollController();
755
    addTearDown(scrollController.dispose);
756 757 758
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
759
          return CupertinoAlertDialog(
760
            title: const Text('The Title'),
761
            content: Text('The message\n' * 40),
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('Option 1'),
              ),
              CupertinoDialogAction(
                child: Text('Option 2'),
              ),
              CupertinoDialogAction(
                child: Text('Option 3'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox option1ButtonBox = findActionButtonRenderBoxByTitle(tester, 'Option 1');
    final RenderBox option2ButtonBox = findActionButtonRenderBoxByTitle(tester, 'Option 2');
    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    expect(option1ButtonBox.size.width, option2ButtonBox.size.width);
    expect(option1ButtonBox.size.width, actionsSectionBox.size.width);

790 791
    // Expected Height = button 1 + divider + 1/2 button 2 = 67.80000000000001
    const double expectedHeight = 67.80000000000001;
792 793
    expect(
      actionsSectionBox.size.height,
794
      moreOrLessEquals(expectedHeight),
795 796 797
    );
  });

798
  testWidgets('Actions section overscroll is painted white.', (WidgetTester tester) async {
799
    final ScrollController scrollController = ScrollController();
800
    addTearDown(scrollController.dispose);
801 802 803
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
804
          return CupertinoAlertDialog(
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
            title: const Text('The Title'),
            content: const Text('The message'),
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('Option 1'),
              ),
              CupertinoDialogAction(
                child: Text('Option 2'),
              ),
              CupertinoDialogAction(
                child: Text('Option 3'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

    // The way that overscroll white is accomplished in a scrollable action
    // section is that the custom RenderBox that lays out the buttons and draws
    // the dividers also paints a white background the size of Rect.largest.
    // That background ends up being clipped by the containing ScrollView.
    //
834 835
    // Here we test that the Rect(0.0, 0.0, renderBox.size.width, renderBox.size.height)
    // is contained within the painted Path.
836 837 838 839 840
    // We don't test for exclusion because for some reason the Path is reporting
    // that even points beyond Rect.largest are within the Path. That's not an
    // issue for our use-case, so we don't worry about it.
    expect(actionsSectionBox, paints..path(
      includes: <Offset>[
841
        Offset.zero,
842
        Offset(actionsSectionBox.size.width, actionsSectionBox.size.height),
843 844 845 846
      ],
    ));
  });

847
  testWidgets('Pressed button changes appearance and dividers disappear.', (WidgetTester tester) async {
848
    final ScrollController scrollController = ScrollController();
849
    addTearDown(scrollController.dispose);
850
    const double dividerThickness = 0.3;
851 852 853
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
854
          return CupertinoAlertDialog(
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
            title: const Text('The Title'),
            content: const Text('The message'),
            actions: const <Widget>[
              CupertinoDialogAction(
                child: Text('Option 1'),
              ),
              CupertinoDialogAction(
                child: Text('Option 2'),
              ),
              CupertinoDialogAction(
                child: Text('Option 3'),
              ),
            ],
            scrollController: scrollController,
          );
        },
      ),
    );

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

877 878
    const Color normalButtonBackgroundColor = Color(0xCCF2F2F2);
    const Color pressedButtonBackgroundColor = Color(0xFFE1E1E1);
879 880 881 882
    final RenderBox firstButtonBox = findActionButtonRenderBoxByTitle(tester, 'Option 1');
    final RenderBox secondButtonBox = findActionButtonRenderBoxByTitle(tester, 'Option 2');
    final RenderBox actionsSectionBox = findScrollableActionsSectionRenderBox(tester);

883
    final Offset pressedButtonCenter = Offset(
884 885 886
      secondButtonBox.size.width / 2.0,
      firstButtonBox.size.height + dividerThickness + (secondButtonBox.size.height / 2.0),
    );
887
    final Offset topDividerCenter = Offset(
888 889 890
      secondButtonBox.size.width / 2.0,
      firstButtonBox.size.height + (0.5 * dividerThickness),
    );
891
    final Offset bottomDividerCenter = Offset(
892
      secondButtonBox.size.width / 2.0,
893 894 895 896
      firstButtonBox.size.height +
          dividerThickness +
          secondButtonBox.size.height +
          (0.5 * dividerThickness),
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
    );

    // Before pressing the button, verify following expectations:
    // - Background includes the button that will be pressed
    // - Background excludes the divider above and below the button that will be pressed
    // - Pressed button background does NOT include the button that will be pressed
    expect(actionsSectionBox, paints
      ..path(
        color: normalButtonBackgroundColor,
        includes: <Offset>[
          pressedButtonCenter,
        ],
        excludes: <Offset>[
          topDividerCenter,
          bottomDividerCenter,
        ],
      )
      ..path(
        color: pressedButtonBackgroundColor,
        excludes: <Offset>[
          pressedButtonCenter,
        ],
      ),
    );

    // Press down on the button.
    final TestGesture gesture = await tester.press(find.widgetWithText(CupertinoDialogAction, 'Option 2'));
    await tester.pump();

    // While pressing the button, verify following expectations:
    // - Background excludes the pressed button
    // - Background includes the divider above and below the pressed button
    // - Pressed button background includes the pressed
    expect(actionsSectionBox, paints
      ..path(
        color: normalButtonBackgroundColor,
        // The background should contain the divider above and below the pressed
        // button. While pressed, surrounding dividers disappear, which means
        // they become part of the background.
        includes: <Offset>[
          topDividerCenter,
          bottomDividerCenter,
        ],
        // The background path should not include the tapped button background...
        excludes: <Offset>[
          pressedButtonCenter,
        ],
      )
      // For a pressed button, a dedicated path is painted with a pressed button
      // background color...
      ..path(
        color: pressedButtonBackgroundColor,
        includes: <Offset>[
          pressedButtonCenter,
        ],
      ),
    );

    // We must explicitly cause an "up" gesture to avoid a crash.
956
    // TODO(mattcarroll): remove this call, https://github.com/flutter/flutter/issues/19540
957
    await gesture.up();
958
  });
959

960
  testWidgets('ScaleTransition animation for showCupertinoDialog()', (WidgetTester tester) async {
961
    await tester.pumpWidget(
962 963 964
      CupertinoApp(
        home: Center(
          child: Builder(
965
            builder: (BuildContext context) {
966
              return CupertinoButton(
967 968 969 970
                onPressed: () {
                  showCupertinoDialog<void>(
                    context: context,
                    builder: (BuildContext context) {
971
                      return CupertinoAlertDialog(
972 973 974 975 976 977
                        title: const Text('The title'),
                        content: const Text('The content'),
                        actions: <Widget>[
                          const CupertinoDialogAction(
                            child: Text('Cancel'),
                          ),
978
                          CupertinoDialogAction(
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
                            isDestructiveAction: true,
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: const Text('Delete'),
                          ),
                        ],
                      );
                    },
                  );
                },
                child: const Text('Go'),
              );
            },
          ),
        ),
      ),
    );

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

    // Enter animation.
    await tester.pump();
    Transform transform = tester.widget(find.byType(Transform));
1003
    expect(transform.transform[0], moreOrLessEquals(1.3, epsilon: 0.001));
1004 1005 1006

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1007
    expect(transform.transform[0], moreOrLessEquals(1.145, epsilon: 0.001));
1008 1009 1010

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1011
    expect(transform.transform[0], moreOrLessEquals(1.044, epsilon: 0.001));
1012 1013 1014

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1015
    expect(transform.transform[0], moreOrLessEquals(1.013, epsilon: 0.001));
1016 1017 1018

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1019
    expect(transform.transform[0], moreOrLessEquals(1.003, epsilon: 0.001));
1020 1021 1022

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1023
    expect(transform.transform[0], moreOrLessEquals(1.000, epsilon: 0.001));
1024 1025 1026

    await tester.pump(const Duration(milliseconds: 50));
    transform = tester.widget(find.byType(Transform));
1027
    expect(transform.transform[0], moreOrLessEquals(1.000, epsilon: 0.001));
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

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

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));

    // No scaling on exit animation.
    expect(find.byType(Transform), findsNothing);
  });

1038
  testWidgets('FadeTransition animation for showCupertinoDialog()', (WidgetTester tester) async {
1039
    await tester.pumpWidget(
1040 1041 1042
      CupertinoApp(
        home: Center(
          child: Builder(
1043
            builder: (BuildContext context) {
1044
              return CupertinoButton(
1045 1046 1047 1048
                onPressed: () {
                  showCupertinoDialog<void>(
                    context: context,
                    builder: (BuildContext context) {
1049
                      return CupertinoAlertDialog(
1050 1051 1052 1053 1054 1055
                        title: const Text('The title'),
                        content: const Text('The content'),
                        actions: <Widget>[
                          const CupertinoDialogAction(
                            child: Text('Cancel'),
                          ),
1056
                          CupertinoDialogAction(
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
                            isDestructiveAction: true,
                            onPressed: () {
                              Navigator.pop(context);
                            },
                            child: const Text('Delete'),
                          ),
                        ],
                      );
                    },
                  );
                },
                child: const Text('Go'),
              );
            },
          ),
        ),
      ),
    );

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

    // Enter animation.
    await tester.pump();
1080 1081
    final Finder fadeTransitionFinder = find.ancestor(of: find.byType(CupertinoAlertDialog), matching: find.byType(FadeTransition));
    FadeTransition transition = tester.firstWidget(fadeTransitionFinder);
1082

1083 1084 1085
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.081, epsilon: 0.001));
1086

1087 1088 1089
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.332, epsilon: 0.001));
1090

1091 1092 1093
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.667, epsilon: 0.001));
1094

1095 1096 1097
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.918, epsilon: 0.001));
1098

1099 1100
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
1101
    expect(transition.opacity.value, moreOrLessEquals(1.0, epsilon: 0.001));
1102 1103 1104 1105

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

    // Exit animation, look at reverse FadeTransition.
1106 1107 1108
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(1.0, epsilon: 0.001));
1109

1110 1111 1112
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.918, epsilon: 0.001));
1113

1114 1115 1116
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.667, epsilon: 0.001));
1117

1118 1119 1120
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.332, epsilon: 0.001));
1121

1122 1123 1124
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
    expect(transition.opacity.value, moreOrLessEquals(0.081, epsilon: 0.001));
1125

1126 1127
    await tester.pump(const Duration(milliseconds: 50));
    transition = tester.firstWidget(fadeTransitionFinder);
1128
    expect(transition.opacity.value, moreOrLessEquals(0.0, epsilon: 0.001));
1129
  });
1130

1131
  testWidgets('Actions are accessible by key', (WidgetTester tester) async {
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
          return const CupertinoAlertDialog(
            title: Text('The Title'),
            content: Text('The message'),
            actions: <Widget>[
              CupertinoDialogAction(
                key: Key('option_1'),
                child: Text('Option 1'),
              ),
              CupertinoDialogAction(
                key: Key('option_2'),
                child: Text('Option 2'),
              ),
            ],
          );
        },
      ),
    );

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

    expect(find.byKey(const Key('option_1')), findsOneWidget);
    expect(find.byKey(const Key('option_2')), findsOneWidget);
    expect(find.byKey(const Key('option_3')), findsNothing);
  });
1160

1161
  testWidgets('Dialog widget insets by MediaQuery viewInsets', (WidgetTester tester) async {
1162 1163 1164
    await tester.pumpWidget(
      const MaterialApp(
        home: MediaQuery(
1165
          data: MediaQueryData(),
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
          child: CupertinoAlertDialog(content: Placeholder(fallbackHeight: 200.0)),
        ),
      ),
    );

    final Rect placeholderRectWithoutInsets = tester.getRect(find.byType(Placeholder));

    await tester.pumpWidget(
      const MaterialApp(
        home: MediaQuery(
          data: MediaQueryData(viewInsets: EdgeInsets.fromLTRB(40.0, 30.0, 20.0, 10.0)),
          child: CupertinoAlertDialog(content: Placeholder(fallbackHeight: 200.0)),
        ),
      ),
    );

    // no change yet because padding is animated
    expect(tester.getRect(find.byType(Placeholder)), placeholderRectWithoutInsets);

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

    // once animation settles the dialog is padded by the new viewInsets
    expect(tester.getRect(find.byType(Placeholder)), placeholderRectWithoutInsets.translate(10, 10));
  });
1190

1191
  testWidgets('Material2 - Default cupertino dialog golden', (WidgetTester tester) async {
1192 1193
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
1194
        useMaterial3: false,
1195 1196
        dialogBuilder: (BuildContext context) {
          return MediaQuery(
1197
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
            child: const RepaintBoundary(
              child: CupertinoAlertDialog(
                title: Text('Title'),
                content: Text('text'),
                actions: <Widget>[
                  CupertinoDialogAction(child: Text('No')),
                  CupertinoDialogAction(child: Text('OK')),
                ],
              ),
            ),
          );
1209
        },
1210 1211 1212 1213 1214 1215 1216 1217
      ),
    );

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

    await expectLater(
      find.byType(CupertinoAlertDialog),
1218 1219 1220 1221
      matchesGoldenFile('m2_dialog_test.cupertino.default.png'),
    );
  });

1222
  testWidgets('Material3 - Default cupertino dialog golden', (WidgetTester tester) async {
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        useMaterial3: true,
        dialogBuilder: (BuildContext context) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
            child: const RepaintBoundary(
              child: CupertinoAlertDialog(
                title: Text('Title'),
                content: Text('text'),
                actions: <Widget>[
                  CupertinoDialogAction(child: Text('No')),
                  CupertinoDialogAction(child: Text('OK')),
                ],
              ),
            ),
          );
        },
      ),
    );

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

    await expectLater(
      find.byType(CupertinoAlertDialog),
      matchesGoldenFile('m3_dialog_test.cupertino.default.png'),
1250 1251
    );
  });
1252

1253
  testWidgets('showCupertinoDialog - custom barrierLabel', (WidgetTester tester) async {
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(
      CupertinoApp(
        home: Builder(
          builder: (BuildContext context) {
            return Center(
              child: CupertinoButton(
                child: const Text('X'),
                onPressed: () {
                  showCupertinoDialog<void>(
                    context: context,
                    barrierLabel: 'Custom label',
                    builder: (BuildContext context) {
                      return const CupertinoAlertDialog(
                        title: Text('Title'),
                        content: Text('Content'),
                        actions: <Widget>[
                          CupertinoDialogAction(child: Text('Yes')),
                          CupertinoDialogAction(child: Text('No')),
                        ],
                      );
                    },
                  );
                },
              ),
            );
          },
        ),
      ),
    );

    expect(semantics, isNot(includesNodeWith(
      label: 'Custom label',
      flags: <SemanticsFlag>[SemanticsFlag.namesRoute],
    )));
1290
    semantics.dispose();
1291 1292
  });

1293
  testWidgets('CupertinoDialogRoute is state restorable', (WidgetTester tester) async {
1294
    await tester.pumpWidget(
1295
      const CupertinoApp(
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
        restorationScopeId: 'app',
        home: _RestorableDialogTestWidget(),
      ),
    );

    expect(find.byType(CupertinoAlertDialog), findsNothing);

    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    expect(find.byType(CupertinoAlertDialog), findsOneWidget);
    final TestRestorationData restorationData = await tester.getRestorationData();

    await tester.restartAndRestore();

    expect(find.byType(CupertinoAlertDialog), findsOneWidget);

    // Tap on the barrier.
    await tester.tapAt(const Offset(10.0, 10.0));
    await tester.pumpAndSettle();

    expect(find.byType(CupertinoAlertDialog), findsNothing);

    await tester.restoreFrom(restorationData);
    expect(find.byType(CupertinoAlertDialog), findsOneWidget);
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/33615
1322

1323
  testWidgets('Conflicting scrollbars are not applied by ScrollBehavior to CupertinoAlertDialog', (WidgetTester tester) async {
1324 1325 1326
    // Regression test for https://github.com/flutter/flutter/issues/83819
    const double textScaleFactor = 1.0;
    final ScrollController actionScrollController = ScrollController();
1327
    addTearDown(actionScrollController.dispose);
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
            child: CupertinoAlertDialog(
              title: const Text('Test Title'),
              content: const Text('Test Content'),
              actions: const <Widget>[
                CupertinoDialogAction(
                  child: Text('One'),
                ),
                CupertinoDialogAction(
                  child: Text('Two'),
                ),
              ],
              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());
1362

1363
  testWidgets('CupertinoAlertDialog scrollbars controllers should be different', (WidgetTester tester) async {
1364 1365 1366 1367
    // https://github.com/flutter/flutter/pull/81278
    await tester.pumpWidget(
      const MaterialApp(
        home: MediaQuery(
1368
          data: MediaQueryData(),
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
          child: CupertinoAlertDialog(
            actions: <Widget>[
              CupertinoDialogAction(child: Text('OK')),
            ],
            content: Placeholder(fallbackHeight: 200.0),
          ),
        ),
      ),
    );

    final List<CupertinoScrollbar> scrollbars =
      find.descendant(
        of: find.byType(CupertinoAlertDialog),
        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);
  });
1388 1389

  group('showCupertinoDialog avoids overlapping display features', () {
1390
    testWidgets('positioning using anchorPoint', (WidgetTester tester) async {
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
      await tester.pumpWidget(
        CupertinoApp(
          builder: (BuildContext context, Widget? child) {
            return MediaQuery(
              // Display has a vertical hinge down the middle
              data: const MediaQueryData(
                size: Size(800, 600),
                displayFeatures: <DisplayFeature>[
                  DisplayFeature(
                    bounds: Rect.fromLTRB(390, 0, 410, 600),
                    type: DisplayFeatureType.hinge,
                    state: DisplayFeatureState.unknown,
                  ),
                ],
              ),
              child: child!,
            );
          },
          home: const Center(child: Text('Test')),
        ),
      );

      final BuildContext context = tester.element(find.text('Test'));
      showCupertinoDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return const Placeholder();
        },
        anchorPoint: const Offset(1000, 0),
      );
      await tester.pumpAndSettle();

      // Should take the right side of the screen
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(410.0, 0.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(800.0, 600.0));
    });

1428
    testWidgets('positioning using Directionality', (WidgetTester tester) async {
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
      await tester.pumpWidget(
        CupertinoApp(
          builder: (BuildContext context, Widget? child) {
            return MediaQuery(
              // Display has a vertical hinge down the middle
              data: const MediaQueryData(
                size: Size(800, 600),
                displayFeatures: <DisplayFeature>[
                  DisplayFeature(
                    bounds: Rect.fromLTRB(390, 0, 410, 600),
                    type: DisplayFeatureType.hinge,
                    state: DisplayFeatureState.unknown,
                  ),
                ],
              ),
              child: Directionality(
                textDirection: TextDirection.rtl,
                child: child!,
              ),
            );
          },
          home: const Center(child: Text('Test')),
        ),
      );

      final BuildContext context = tester.element(find.text('Test'));
      showCupertinoDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return const Placeholder();
        },
      );
      await tester.pumpAndSettle();

      // Should take the right side of the screen
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(410.0, 0.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(800.0, 600.0));
    });

1468
    testWidgets('default positioning', (WidgetTester tester) async {
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
      await tester.pumpWidget(
        CupertinoApp(
          builder: (BuildContext context, Widget? child) {
            return MediaQuery(
              // Display has a vertical hinge down the middle
              data: const MediaQueryData(
                size: Size(800, 600),
                displayFeatures: <DisplayFeature>[
                  DisplayFeature(
                    bounds: Rect.fromLTRB(390, 0, 410, 600),
                    type: DisplayFeatureType.hinge,
                    state: DisplayFeatureState.unknown,
                  ),
                ],
              ),
              child: child!,
            );
          },
          home: const Center(child: Text('Test')),
        ),
      );

      final BuildContext context = tester.element(find.text('Test'));
      showCupertinoDialog<void>(
        context: context,
        builder: (BuildContext context) {
          return const Placeholder();
        },
      );
      await tester.pumpAndSettle();

      // By default it should place the dialog on the left screen
      expect(tester.getTopLeft(find.byType(Placeholder)), Offset.zero);
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(390.0, 600.0));
    });
  });
1505

1506
  testWidgets('Hovering over Cupertino alert dialog action updates cursor to clickable on Web', (WidgetTester tester) async {
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    await tester.pumpWidget(
      createAppWithButtonThatLaunchesDialog(
        dialogBuilder: (BuildContext context) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
            child: RepaintBoundary(
              child: CupertinoAlertDialog(
                title: const Text('Title'),
                content: const Text('text'),
                actions: <Widget>[
                  CupertinoDialogAction(
                    onPressed: () {},
                    child: const Text('NO'),
                  ),
                  CupertinoDialogAction(
                    onPressed: () {},
                    child: const Text('OK'),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );

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

    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 dialogAction = tester.getCenter(find.text('OK'));
    await gesture.moveTo(dialogAction);
    await tester.pumpAndSettle();
    expect(
      RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
      kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
    );
  });
1549
}
Ian Hickson's avatar
Ian Hickson committed
1550

1551 1552 1553
RenderBox findActionButtonRenderBoxByTitle(WidgetTester tester, String title) {
  final RenderObject buttonBox = tester.renderObject(find.widgetWithText(CupertinoDialogAction, title));
  assert(buttonBox is RenderBox);
1554
  return buttonBox as RenderBox;
1555 1556 1557
}

RenderBox findScrollableActionsSectionRenderBox(WidgetTester tester) {
1558 1559 1560
  final RenderObject actionsSection = tester.renderObject(find.byElementPredicate((Element element) {
    return element.widget.runtimeType.toString() == '_CupertinoAlertActionSection';
  }));
1561
  assert(actionsSection is RenderBox);
1562
  return actionsSection as RenderBox;
1563 1564
}

1565
Widget createAppWithButtonThatLaunchesDialog({
1566
  required WidgetBuilder dialogBuilder,
1567
  bool? useMaterial3,
1568
}) {
1569
  return MaterialApp(
1570
    theme: ThemeData(useMaterial3: useMaterial3),
1571 1572 1573
    home: Material(
      child: Center(
        child: Builder(builder: (BuildContext context) {
1574
          return ElevatedButton(
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
            onPressed: () {
              showDialog<void>(
                context: context,
                builder: dialogBuilder,
              );
            },
            child: const Text('Go'),
          );
        }),
      ),
    ),
  );
}

Ian Hickson's avatar
Ian Hickson committed
1589
Widget boilerplate(Widget child) {
1590
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
1591 1592 1593
    textDirection: TextDirection.ltr,
    child: child,
  );
1594
}
1595 1596 1597 1598 1599

Widget createAppWithCenteredButton(Widget child) {
  return MaterialApp(
    home: Material(
      child: Center(
1600
        child: ElevatedButton(
1601
          onPressed: null,
1602
          child: child,
1603
        ),
1604 1605
      ),
    ),
1606 1607
  );
}
1608 1609


1610
class _RestorableDialogTestWidget extends StatelessWidget {
1611 1612
  const _RestorableDialogTestWidget();

1613
  @pragma('vm:entry-point')
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
  static Route<Object?> _dialogBuilder(BuildContext context, Object? arguments) {
    return CupertinoDialogRoute<void>(
      context: context,
      builder: (BuildContext context) {
        return const CupertinoAlertDialog(
          title: Text('Title'),
          content: Text('Content'),
          actions: <Widget>[
            CupertinoDialogAction(child: Text('Yes')),
            CupertinoDialogAction(child: Text('No')),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text('Home'),
      ),
      child: Center(child: CupertinoButton(
        onPressed: () {
          Navigator.of(context).restorablePush(_dialogBuilder);
        },
        child: const Text('X'),
      )),
    );
  }
}