navigator_and_layers_test.dart 2.31 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
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8 9 10 11

import 'test_widgets.dart';

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

  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() {
30
  testWidgetsWithLeakTracking('Do we paint when coming back from a navigation', (WidgetTester tester) async {
31 32 33
    final List<String> log = <String>[];
    log.add('0');
    await tester.pumpWidget(
34
      MaterialApp(
35
        routes: <String, WidgetBuilder>{
36
          '/': (BuildContext context) => RepaintBoundary(
37 38 39 40 41 42
            child: RepaintBoundary(
              child: FlipWidget(
                left: CustomPaint(
                  painter: TestCustomPainter(
                    log: log,
                    name: 'left',
43
                  ),
44 45 46 47 48
                ),
                right: CustomPaint(
                  painter: TestCustomPainter(
                    log: log,
                    name: 'right',
49 50 51 52 53
                  ),
                ),
              ),
            ),
          ),
54
          '/second': (BuildContext context) => Container(),
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 87
        },
      ),
    );
    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',
    ]);
  });
}