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

5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/widgets.dart';

8
typedef TestCallback = void Function(BuildContext context);
9 10

class TestWidget extends StatefulWidget {
11
  const TestWidget(this.callback, { Key key }) : super(key: key);
12 13 14 15

  final TestCallback callback;

  @override
16
  TestWidgetState createState() => TestWidgetState();
17 18 19 20 21 22 23 24 25 26
}

class TestWidgetState extends State<TestWidget> {
  @override
  void dispose() {
    widget.callback(context);
    super.dispose();
  }

  @override
Ian Hickson's avatar
Ian Hickson committed
27
  Widget build(BuildContext context) => const Text('test', textDirection: TextDirection.ltr);
28 29 30
}

void main() {
31
  testWidgets('dependOnInheritedWidgetOfExactType() called from dispose() throws error', (WidgetTester tester) async {
32 33
    bool disposeCalled = false;
    await tester.pumpWidget(
34
      TestWidget((BuildContext context) {
35
        disposeCalled = true;
36
        context.dependOnInheritedWidgetOfExactType<InheritedWidget>();
37 38
      }),
    );
39
    await tester.pumpWidget(Container());
40 41 42 43
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

44
  testWidgets('getElementForInheritedWidgetOfExactType() called from dispose() throws error', (WidgetTester tester) async {
45 46
    bool disposeCalled = false;
    await tester.pumpWidget(
47
      TestWidget((BuildContext context) {
48
        disposeCalled = true;
49
        context.getElementForInheritedWidgetOfExactType<InheritedWidget>();
50 51
      }),
    );
52
    await tester.pumpWidget(Container());
53 54 55 56
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

57
  testWidgets('findAncestorWidgetOfExactType() called from dispose() throws error', (WidgetTester tester) async {
58 59
    bool disposeCalled = false;
    await tester.pumpWidget(
60
      TestWidget((BuildContext context) {
61
        disposeCalled = true;
62
        context.findAncestorWidgetOfExactType<Container>();
63 64
      }),
    );
65
    await tester.pumpWidget(Container());
66 67 68 69
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

70
  testWidgets('findAncestorStateOfType() called from dispose() throws error', (WidgetTester tester) async {
71 72
    bool disposeCalled = false;
    await tester.pumpWidget(
73
      TestWidget((BuildContext context) {
74
        disposeCalled = true;
75
        context.findAncestorStateOfType<State>();
76 77
      }),
    );
78
    await tester.pumpWidget(Container());
79 80 81 82
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

83
  testWidgets('findAncestorRenderObjectOfType() called from dispose() throws error', (WidgetTester tester) async {
84 85
    bool disposeCalled = false;
    await tester.pumpWidget(
86
      TestWidget((BuildContext context) {
87
        disposeCalled = true;
88
        context.findAncestorRenderObjectOfType<RenderObject>();
89 90
      }),
    );
91
    await tester.pumpWidget(Container());
92 93 94 95 96 97 98
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

  testWidgets('visitAncestorElements() called from dispose() throws error', (WidgetTester tester) async {
    bool disposeCalled = false;
    await tester.pumpWidget(
99
      TestWidget((BuildContext context) {
100
        disposeCalled = true;
101
        context.visitAncestorElements((Element element) => true);
102 103
      }),
    );
104
    await tester.pumpWidget(Container());
105 106 107 108 109 110 111
    expect(disposeCalled, isTrue);
    expect(tester.takeException(), isFlutterError);
  });

  testWidgets('dispose() method does not unconditionally throw an error', (WidgetTester tester) async {
    bool disposeCalled = false;
    await tester.pumpWidget(
112
      TestWidget((BuildContext context) {
113 114 115
        disposeCalled = true;
      }),
    );
116
    await tester.pumpWidget(Container());
117 118 119 120 121 122
    expect(disposeCalled, isTrue);
  });



}