navigator_test.dart 3.3 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
Adam Barth's avatar
Adam Barth committed
6
import 'package:flutter/material.dart';
7 8
import 'package:test/test.dart';

9
class FirstWidget extends StatelessWidget {
Adam Barth's avatar
Adam Barth committed
10
  Widget build(BuildContext context) {
11
    return new GestureDetector(
12
      onTap: () {
Hixie's avatar
Hixie committed
13
        Navigator.pushNamed(context, '/second');
14
      },
15 16
      child: new Container(
        decoration: new BoxDecoration(
17
          backgroundColor: new Color(0xFFFFFF00)
18
        ),
19
        child: new Text('X')
20 21 22 23 24
      )
    );
  }
}

25 26
class SecondWidget extends StatefulWidget {
  SecondWidgetState createState() => new SecondWidgetState();
Adam Barth's avatar
Adam Barth committed
27
}
28

29
class SecondWidgetState extends State<SecondWidget> {
Adam Barth's avatar
Adam Barth committed
30
  Widget build(BuildContext context) {
31
    return new GestureDetector(
Hixie's avatar
Hixie committed
32
      onTap: () => Navigator.pop(context),
33 34
      child: new Container(
        decoration: new BoxDecoration(
35
          backgroundColor: new Color(0xFFFF00FF)
36
        ),
37
        child: new Text('Y')
38 39 40 41 42
      )
    );
  }
}

43
typedef void ExceptionCallback(dynamic exception);
44

45 46
class ThirdWidget extends StatelessWidget {
  ThirdWidget({ this.targetKey, this.onException });
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

  final Key targetKey;
  final ExceptionCallback onException;

  Widget build(BuildContext context) {
    return new GestureDetector(
      key: targetKey,
      onTap: () {
        try {
          Navigator.openTransaction(context, (_) { });
        } catch (e) {
          onException(e);
        }
      },
      behavior: HitTestBehavior.opaque
    );
  }
}

66
void main() {
67
  test('Can navigator navigate to and from a stateful widget', () {
68
    testWidgets((WidgetTester tester) {
69 70 71
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
        '/': (BuildContext context) => new FirstWidget(),
        '/second': (BuildContext context) => new SecondWidget(),
72
      };
73

Adam Barth's avatar
Adam Barth committed
74
      tester.pumpWidget(new MaterialApp(routes: routes));
75

76 77
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNull);
78

79 80
      tester.tap(tester.findText('X'));
      tester.pump(const Duration(milliseconds: 10));
81

82 83
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNotNull);
84

85 86 87
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(seconds: 1));
88

89 90 91 92 93
      tester.tap(tester.findText('Y'));
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(seconds: 1));
94

95 96 97
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNull);
    });
98
  });
99 100 101 102 103

  test('Navigator.openTransaction fails gracefully when not found in context', () {
    testWidgets((WidgetTester tester) {
      Key targetKey = new Key('foo');
      dynamic exception;
104
      Widget widget = new ThirdWidget(
105
        targetKey: targetKey,
106
        onException: (dynamic e) {
107 108 109 110 111 112 113 114 115
          exception = e;
        }
      );
      tester.pumpWidget(widget);
      tester.tap(tester.findElementByKey(targetKey));
      expect(exception, new isInstanceOf<WidgetError>());
      expect('$exception', startsWith('openTransaction called with a context'));
    });
  });
116
}