navigator_and_layers_test.dart 2.22 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10

import 'test_widgets.dart';

class TestCustomPainter extends CustomPainter {
11
  TestCustomPainter({ required this.log, required this.name });
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

  final List<String> log;
  final String name;

  @override
  void paint(Canvas canvas, Size size) {
    log.add(name);
  }

  @override
  bool shouldRepaint(TestCustomPainter oldPainter) {
    return name != oldPainter.name
        || log != oldPainter.log;
  }
}

void main() {
  testWidgets('Do we paint when coming back from a navigation', (WidgetTester tester) async {
    final List<String> log = <String>[];
    log.add('0');
    await tester.pumpWidget(
33
      MaterialApp(
34
        routes: <String, WidgetBuilder>{
35
          '/': (BuildContext context) => RepaintBoundary(
36 37 38 39 40 41
            child: RepaintBoundary(
              child: FlipWidget(
                left: CustomPaint(
                  painter: TestCustomPainter(
                    log: log,
                    name: 'left',
42
                  ),
43 44 45 46 47
                ),
                right: CustomPaint(
                  painter: TestCustomPainter(
                    log: log,
                    name: 'right',
48 49 50 51 52
                  ),
                ),
              ),
            ),
          ),
53
          '/second': (BuildContext context) => Container(),
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        },
      ),
    );
    log.add('1');
    final NavigatorState navigator = tester.state<NavigatorState>(find.byType(Navigator));
    navigator.pushNamed('/second');
    log.add('2');
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
    log.add('3');
    flipStatefulWidget(tester, skipOffstage: false);
    log.add('4');
    navigator.pop();
    log.add('5');
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
    log.add('6');
    flipStatefulWidget(tester);
    expect(await tester.pumpAndSettle(), 1);
    log.add('7');
    expect(log, <String>[
      '0',
      'left',
      '1',
      '2',
      '3',
      '4',
      '5',
      'right',
      '6',
      'left',
      '7',
    ]);
  });
}