dialog_test.dart 3.16 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<Null>(
21 22
                    context: context,
                    child: new CupertinoAlertDialog(
23 24
                      title: const Text('The title'),
                      content: const Text('The content'),
25
                      actions: <Widget>[
26
                        const CupertinoDialogAction(
27
                          child: const Text('Cancel'),
28 29
                        ),
                        new CupertinoDialogAction(
30
                          isDestructiveAction: true,
31 32 33 34
                          onPressed: () {
                            didDelete = true;
                            Navigator.pop(context);
                          },
35
                          child: const Text('Delete'),
36 37 38 39 40
                        ),
                      ],
                    ),
                  );
                },
41
                child: const Text('Go'),
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
              );
            },
          ),
        ),
      ),
    ));

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

66
  testWidgets('Dialog destructive action styles', (WidgetTester tester) async {
67
    await tester.pumpWidget(const CupertinoDialogAction(
68
      isDestructiveAction: true,
69
      child: const Text('Ok'),
70 71
    ));

72
    final DefaultTextStyle widget = tester.widget(find.byType(DefaultTextStyle));
73 74 75 76

    expect(widget.style.color.red, greaterThan(widget.style.color.blue));
    expect(widget.style.color.alpha, lessThan(255));
  });
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

  testWidgets('Dialog default action styles', (WidgetTester tester) async {
    await tester.pumpWidget(const CupertinoDialogAction(
      isDefaultAction: true,
      child: const Text('Ok'),
    ));

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

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

  testWidgets('Default and destructive style', (WidgetTester tester) async {
    await tester.pumpWidget(const CupertinoDialogAction(
      isDefaultAction: true,
      isDestructiveAction: true,
      child: const Text('Ok'),
    ));

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