navigator_test.dart 3.34 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';

Adam Barth's avatar
Adam Barth committed
9 10
class FirstComponent extends StatelessComponent {
  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
class SecondComponent extends StatefulComponent {
Adam Barth's avatar
Adam Barth committed
26 27
  SecondComponentState createState() => new SecondComponentState();
}
28

Adam Barth's avatar
Adam Barth committed
29 30
class SecondComponentState extends State<SecondComponent> {
  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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

class ThirdComponent extends StatelessComponent {
  ThirdComponent({ this.targetKey, this.onException });

  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 component', () {
68 69
    testWidgets((WidgetTester tester) {
      final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
Ian Hickson's avatar
Ian Hickson committed
70 71
        '/': (RouteArguments args) => new FirstComponent(),
        '/second': (RouteArguments args) => new SecondComponent(),
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 104 105

  test('Navigator.openTransaction fails gracefully when not found in context', () {
    testWidgets((WidgetTester tester) {
      Key targetKey = new Key('foo');
      dynamic exception;
      Widget widget = new ThirdComponent(
        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
}