navigator_test.dart 2.26 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
      )
    );
  }
}

void main() {
44
  test('Can navigator navigate to and from a stateful component', () {
45 46
    testWidgets((WidgetTester tester) {
      final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
Ian Hickson's avatar
Ian Hickson committed
47 48
        '/': (RouteArguments args) => new FirstComponent(),
        '/second': (RouteArguments args) => new SecondComponent(),
49
      };
50

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

53 54
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNull);
55

56 57
      tester.tap(tester.findText('X'));
      tester.pump(const Duration(milliseconds: 10));
58

59 60
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNotNull);
61

62 63 64
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(milliseconds: 10));
      tester.pump(const Duration(seconds: 1));
65

66 67 68 69 70
      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));
71

72 73 74
      expect(tester.findText('X'), isNotNull);
      expect(tester.findText('Y'), isNull);
    });
75 76
  });
}