app_test.dart 4 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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_test/flutter_test.dart';
import 'package:flutter/material.dart';

8 9 10 11 12 13 14 15 16 17 18 19 20 21
class StateMarker extends StatefulWidget {
  StateMarker({ Key key, this.child }) : super(key: key);

  final Widget child;

  @override
  StateMarkerState createState() => new StateMarkerState();
}

class StateMarkerState extends State<StateMarker> {
  String marker;

  @override
  Widget build(BuildContext context) {
22 23
    if (widget.child != null)
      return widget.child;
24 25 26 27
    return new Container();
  }
}

28 29 30 31 32
void main() {
  testWidgets('Can nest apps', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new MaterialApp(
33
          home: const Text('Home sweet home'),
34 35
        ),
      ),
36 37 38 39 40 41
    );

    expect(find.text('Home sweet home'), findsOneWidget);
  });

  testWidgets('Focus handling', (WidgetTester tester) async {
42
    final FocusNode focusNode = new FocusNode();
43 44 45
    await tester.pumpWidget(new MaterialApp(
      home: new Material(
        child: new Center(
46 47 48
          child: new TextField(focusNode: focusNode, autofocus: true),
        ),
      ),
49 50
    ));

51
    expect(focusNode.hasFocus, isTrue);
52
  });
53

54 55 56 57 58 59 60
  testWidgets('Can place app inside FocusScope', (WidgetTester tester) async {
    final FocusScopeNode focusScopeNode = new FocusScopeNode();

    await tester.pumpWidget(new FocusScope(
      autofocus: true,
      node: focusScopeNode,
      child: new MaterialApp(
61
        home: const Text('Home'),
62 63 64 65 66 67
      ),
    ));

    expect(find.text('Home'), findsOneWidget);
  });

68 69 70
  testWidgets('Can show grid without losing sync', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
71 72
        home: new StateMarker(),
      ),
73 74
    );

75
    final StateMarkerState state1 = tester.state(find.byType(StateMarker));
76 77 78 79 80
    state1.marker = 'original';

    await tester.pumpWidget(
      new MaterialApp(
        debugShowMaterialGrid: true,
81 82
        home: new StateMarker(),
      ),
83 84
    );

85
    final StateMarkerState state2 = tester.state(find.byType(StateMarker));
86 87 88
    expect(state1, equals(state2));
    expect(state2.marker, equals('original'));
  });
89 90 91 92 93 94 95 96 97

  testWidgets('Do not rebuild page on the second frame of the route transition', (WidgetTester tester) async {
    int buildCounter = 0;
    await tester.pumpWidget(
      new MaterialApp(
        home: new Builder(
          builder: (BuildContext context) {
            return new Material(
              child: new RaisedButton(
98
                child: const Text('X'),
99 100
                onPressed: () { Navigator.of(context).pushNamed('/next'); },
              ),
101 102 103 104 105 106 107 108 109
            );
          }
        ),
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return new Builder(
              builder: (BuildContext context) {
                ++buildCounter;
                return new Container();
110
              },
111
            );
112 113 114
          },
        },
      ),
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    );

    expect(buildCounter, 0);
    await tester.tap(find.text('X'));
    expect(buildCounter, 0);
    await tester.pump();
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(buildCounter, 1);
    await tester.pump(const Duration(seconds: 1));
    expect(buildCounter, 2);
  });

134
  testWidgets('Cannot pop the initial route', (WidgetTester tester) async {
135
    await tester.pumpWidget(new MaterialApp(home: const Text('Home')));
136 137 138

    expect(find.text('Home'), findsOneWidget);

139 140
    final NavigatorState navigator = tester.state(find.byType(Navigator));
    final bool result = await navigator.maybePop();
141 142 143 144 145

    expect(result, isFalse);

    expect(find.text('Home'), findsOneWidget);
  });
146
}