dialog_test.dart 6.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

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

    await tester.pumpWidget(new MaterialApp(
      home: new Material(
        child: new Center(
          child: new Builder(
            builder: (BuildContext context) {
              return new RaisedButton(
                onPressed: () {
20
                  showDialog<void>(
21
                    context: context,
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
                    builder: (BuildContext context) {
                      return new CupertinoAlertDialog(
                        title: const Text('The title'),
                        content: const Text('The content'),
                        actions: <Widget>[
                          const CupertinoDialogAction(
                            child: const Text('Cancel'),
                          ),
                          new CupertinoDialogAction(
                            isDestructiveAction: true,
                            onPressed: () {
                              didDelete = true;
                              Navigator.pop(context);
                            },
                            child: const Text('Delete'),
                          ),
                        ],
                      );
                    },
41 42
                  );
                },
43
                child: const Text('Go'),
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
              );
            },
          ),
        ),
      ),
    ));

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

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

    expect(didDelete, isFalse);

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

    expect(didDelete, isTrue);

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

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

68
  testWidgets('Dialog destructive action styles', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
69
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
70
      isDestructiveAction: true,
71
      child: const Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
72
    )));
73

74
    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));
75 76 77 78

    expect(widget.style.color.red, greaterThan(widget.style.color.blue));
    expect(widget.style.color.alpha, lessThan(255));
  });
79 80

  testWidgets('Dialog default action styles', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
81
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
82 83
      isDefaultAction: true,
      child: const Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
84
    )));
85 86 87 88 89 90 91

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

    expect(widget.style.fontWeight, equals(FontWeight.w600));
  });

  testWidgets('Default and destructive style', (WidgetTester tester) async {
Ian Hickson's avatar
Ian Hickson committed
92
    await tester.pumpWidget(boilerplate(const CupertinoDialogAction(
93 94 95
      isDefaultAction: true,
      isDestructiveAction: true,
      child: const Text('Ok'),
Ian Hickson's avatar
Ian Hickson committed
96
    )));
97 98 99 100 101 102

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

    expect(widget.style.fontWeight, equals(FontWeight.w600));
    expect(widget.style.color.red, greaterThan(widget.style.color.blue));
  });
103 104 105 106 107 108 109 110 111 112

  testWidgets('Message is scrollable, has correct padding with large text sizes',
      (WidgetTester tester) async {
    final ScrollController scrollController = new ScrollController(keepScrollOffset: true);
    await tester.pumpWidget(
      new MaterialApp(home: new Material(
        child: new Center(
          child: new Builder(builder: (BuildContext context) {
            return new RaisedButton(
              onPressed: () {
113
                showDialog<void>(
114
                  context: context,
115
                  builder: (BuildContext context) {
116 117 118 119 120
                    return new MediaQuery(
                      data: MediaQuery.of(context).copyWith(textScaleFactor: 3.0),
                      child: new CupertinoAlertDialog(
                        title: const Text('The Title'),
                        content: new Text('Very long content ' * 20),
121
                        actions: const <Widget>[
122 123 124 125 126 127 128 129 130 131 132
                          const CupertinoDialogAction(
                            child: const Text('Cancel'),
                          ),
                          const CupertinoDialogAction(
                            isDestructiveAction: true,
                            child: const Text('OK'),
                          ),
                        ],
                        scrollController: scrollController,
                      ),
                    );
133
                  },
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                );
              },
              child: const Text('Go'),
            );
          }),
        ),
      )),
    );

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

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

    expect(scrollController.offset, 0.0);
    scrollController.jumpTo(100.0);
    expect(scrollController.offset, 100.0);

152
    // Find the actual dialog box. The first decorated box is the popup barrier.
153 154 155
    expect(tester.getSize(find.byType(DecoratedBox).at(1)), equals(const Size(270.0, 560.0)));

    // Check sizes/locations of the text.
156
    expect(tester.getSize(find.text('The Title')), equals(const Size(230.0, 171.0)));
157 158 159 160 161 162 163 164 165
    expect(tester.getSize(find.text('Cancel')), equals(const Size(75.0, 300.0)));
    expect(tester.getSize(find.text('OK')), equals(const Size(75.0, 100.0)));
    expect(tester.getTopLeft(find.text('The Title')), equals(const Offset(285.0, 40.0)));

    // The Cancel and OK buttons have different Y values because "Cancel" is
    // wrapping (as it should with large text sizes like this).
    expect(tester.getTopLeft(find.text('Cancel')), equals(const Offset(295.0, 250.0)));
    expect(tester.getTopLeft(find.text('OK')), equals(const Offset(430.0, 350.0)));
  });
166
}
Ian Hickson's avatar
Ian Hickson committed
167 168 169 170 171 172 173

Widget boilerplate(Widget child) {
  return new Directionality(
    textDirection: TextDirection.ltr,
    child: child,
  );
}