debug_test.dart 8.87 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/foundation.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9

10
void main() {
11
  testWidgetsWithLeakTracking('debugCheckHasMaterial control test', (WidgetTester tester) async {
12
    await tester.pumpWidget(const Center(child: Chip(label: Text('label'))));
13 14
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
15
    final FlutterError error = exception as FlutterError;
16 17 18 19 20 21 22 23 24 25
    expect(error.diagnostics.length, 5);
    expect(error.diagnostics[2].level, DiagnosticLevel.hint);
    expect(
      error.diagnostics[2].toStringDeep(),
      equalsIgnoringHashCodes(
        'To introduce a Material widget, you can either directly include\n'
        'one, or use a widget that contains Material itself, such as a\n'
        'Card, Dialog, Drawer, or Scaffold.\n',
      ),
    );
Dan Field's avatar
Dan Field committed
26 27
    expect(error.diagnostics[3], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[4], isA<DiagnosticsBlock>());
28
    expect(
29
      error.toStringDeep(), startsWith(
30 31
      'FlutterError\n'
      '   No Material widget found.\n'
32 33
      '   Chip widgets require a Material widget ancestor within the\n'
      '   closest LookupBoundary.\n'
34
      '   In Material Design, most widgets are conceptually "printed" on a\n'
35
      "   sheet of material. In Flutter's material library, that material\n"
36 37 38 39 40 41 42 43
      '   is represented by the Material widget. It is the Material widget\n'
      '   that renders ink splashes, for instance. Because of this, many\n'
      '   material library widgets require that there be a Material widget\n'
      '   in the tree above them.\n'
      '   To introduce a Material widget, you can either directly include\n'
      '   one, or use a widget that contains Material itself, such as a\n'
      '   Card, Dialog, Drawer, or Scaffold.\n'
      '   The specific widget that could not find a Material ancestor was:\n'
44
      '     Chip\n'
45
      '   The ancestors of this widget were:\n'
46 47 48
      '     Center\n'
      // End of ancestor chain omitted, not relevant for test.
    ));
49 50
  });

51
  testWidgetsWithLeakTracking('debugCheckHasMaterialLocalizations control test', (WidgetTester tester) async {
52
    await tester.pumpWidget(const Center(child: BackButton()));
53 54
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
55
    final FlutterError error = exception as FlutterError;
56 57 58 59 60 61 62 63 64 65
    expect(error.diagnostics.length, 6);
    expect(error.diagnostics[3].level, DiagnosticLevel.hint);
    expect(
      error.diagnostics[3].toStringDeep(),
      equalsIgnoringHashCodes(
        'To introduce a MaterialLocalizations, either use a MaterialApp at\n'
        'the root of your application to include them automatically, or\n'
        'add a Localization widget with a MaterialLocalizations delegate.\n',
      ),
    );
Dan Field's avatar
Dan Field committed
66 67
    expect(error.diagnostics[4], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[5], isA<DiagnosticsBlock>());
68
    expect(
69
      error.toStringDeep(), startsWith(
70 71 72 73
      'FlutterError\n'
      '   No MaterialLocalizations found.\n'
      '   BackButton widgets require MaterialLocalizations to be provided\n'
      '   by a Localizations widget ancestor.\n'
74 75
      '   The material library uses Localizations to generate messages,\n'
      '   labels, and abbreviations.\n'
76 77 78 79 80 81 82
      '   To introduce a MaterialLocalizations, either use a MaterialApp at\n'
      '   the root of your application to include them automatically, or\n'
      '   add a Localization widget with a MaterialLocalizations delegate.\n'
      '   The specific widget that could not find a MaterialLocalizations\n'
      '   ancestor was:\n'
      '     BackButton\n'
      '   The ancestors of this widget were:\n'
83 84 85
      '     Center\n'
      // End of ancestor chain omitted, not relevant for test.
    ));
86 87
  });

88
  testWidgetsWithLeakTracking('debugCheckHasScaffold control test', (WidgetTester tester) async {
89 90
    await tester.pumpWidget(
      MaterialApp(
91 92 93 94 95 96 97
        theme: ThemeData(
          pageTransitionsTheme: const PageTransitionsTheme(
            builders: <TargetPlatform, PageTransitionsBuilder>{
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
            },
          ),
        ),
98 99
        home: Builder(
          builder: (BuildContext context) {
100
            showBottomSheet(
101 102 103
              context: context,
              builder: (BuildContext context) => Container(),
            );
104
            return Container();
105
          },
106 107 108 109 110
        ),
      ),
    );
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
111
    final FlutterError error = exception as FlutterError;
112
    expect(error.diagnostics.length, 5);
Dan Field's avatar
Dan Field committed
113 114
    expect(error.diagnostics[2], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[3], isA<DiagnosticsBlock>());
115 116 117 118 119 120 121 122
    expect(error.diagnostics[4].level, DiagnosticLevel.hint);
    expect(
      error.diagnostics[4].toStringDeep(),
      equalsIgnoringHashCodes(
        'Typically, the Scaffold widget is introduced by the MaterialApp\n'
        'or WidgetsApp widget at the top of your application widget tree.\n',
      ),
    );
123
    expect(error.toStringDeep(), startsWith(
124 125 126 127 128 129 130 131
      'FlutterError\n'
      '   No Scaffold widget found.\n'
      '   Builder widgets require a Scaffold widget ancestor.\n'
      '   The specific widget that could not find a Scaffold ancestor was:\n'
      '     Builder\n'
      '   The ancestors of this widget were:\n'
      '     Semantics\n'
      '     Builder\n'
132 133
    ));
    expect(error.toStringDeep(), endsWith(
134 135
      '     [root]\n'
      '   Typically, the Scaffold widget is introduced by the MaterialApp\n'
136
      '   or WidgetsApp widget at the top of your application widget tree.\n'
137
    ));
138
  });
139

140
  testWidgetsWithLeakTracking('debugCheckHasScaffoldMessenger control test', (WidgetTester tester) async {
141 142
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
143 144
    final SnackBar snackBar = SnackBar(
      content: const Text('Snack'),
145
      action: SnackBarAction(label: 'Test', onPressed: () {}),
146 147 148
    );
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
149 150 151 152 153 154 155 156 157
      child: ScaffoldMessenger(
        key: scaffoldMessengerKey,
        child: Builder(
          builder: (BuildContext context) {
            return Scaffold(
              key: scaffoldKey,
              body: Container(),
            );
          },
158
        ),
159
      ),
160 161
    ));
    final List<dynamic> exceptions = <dynamic>[];
162
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
163 164 165 166
    FlutterError.onError = (FlutterErrorDetails details) {
      exceptions.add(details.exception);
    };
    // ScaffoldMessenger shows SnackBar.
167
    scaffoldMessengerKey.currentState!.showSnackBar(snackBar);
168 169 170 171 172
    await tester.pumpAndSettle();

    // Pump widget to rebuild without ScaffoldMessenger
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
173 174 175
      child: Scaffold(
        key: scaffoldKey,
        body: Container(),
176 177
      ),
    ));
178 179 180 181
    // Tap SnackBarAction to dismiss.
    // The SnackBarAction should assert we still have an ancestor
    // ScaffoldMessenger in order to dismiss the SnackBar from the
    // Scaffold.
182 183 184 185
    await tester.tap(find.text('Test'));
    FlutterError.onError = oldHandler;

    expect(exceptions.length, 1);
186
    // ignore: avoid_dynamic_calls
187 188 189 190 191 192 193 194 195 196 197 198 199
    expect(exceptions.single.runtimeType, FlutterError);
    final FlutterError error = exceptions.first as FlutterError;
    expect(error.diagnostics.length, 5);
    expect(error.diagnostics[2], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[3], isA<DiagnosticsBlock>());
    expect(error.diagnostics[4].level, DiagnosticLevel.hint);
    expect(
      error.diagnostics[4].toStringDeep(),
      equalsIgnoringHashCodes(
        'Typically, the ScaffoldMessenger widget is introduced by the\n'
        'MaterialApp at the top of your application widget tree.\n',
      ),
    );
200
    expect(error.toStringDeep(), startsWith(
201 202
      'FlutterError\n'
      '   No ScaffoldMessenger widget found.\n'
203 204
      '   SnackBarAction widgets require a ScaffoldMessenger widget\n'
      '   ancestor.\n'
205 206
      '   The specific widget that could not find a ScaffoldMessenger\n'
      '   ancestor was:\n'
207
      '     SnackBarAction\n'
208
      '   The ancestors of this widget were:\n'
209 210 211
      '     TextButtonTheme\n'
      '     Padding\n'
      '     Row\n'
212 213
    ));
    expect(error.toStringDeep(), endsWith(
214 215
      '     [root]\n'
      '   Typically, the ScaffoldMessenger widget is introduced by the\n'
216
      '   MaterialApp at the top of your application widget tree.\n'
217 218
    ));
  });
219
}