debug_test.dart 8.73 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 8 9 10
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('debugCheckHasMaterial control test', (WidgetTester tester) async {
11
    await tester.pumpWidget(const Center(child: Chip(label: Text('label'))));
12 13
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
14
    final FlutterError error = exception as FlutterError;
15 16 17 18 19 20 21 22 23 24
    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
25 26
    expect(error.diagnostics[3], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[4], isA<DiagnosticsBlock>());
27
    expect(
28
      error.toStringDeep(), startsWith(
29 30
      'FlutterError\n'
      '   No Material widget found.\n'
31 32
      '   Chip widgets require a Material widget ancestor within the\n'
      '   closest LookupBoundary.\n'
33
      '   In Material Design, most widgets are conceptually "printed" on a\n'
34
      "   sheet of material. In Flutter's material library, that material\n"
35 36 37 38 39 40 41 42
      '   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'
43
      '     Chip\n'
44
      '   The ancestors of this widget were:\n'
45 46 47
      '     Center\n'
      // End of ancestor chain omitted, not relevant for test.
    ));
48 49
  });

50
  testWidgets('debugCheckHasMaterialLocalizations control test', (WidgetTester tester) async {
51
    await tester.pumpWidget(const Center(child: BackButton()));
52 53
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
54
    final FlutterError error = exception as FlutterError;
55 56 57 58 59 60 61 62 63 64
    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
65 66
    expect(error.diagnostics[4], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[5], isA<DiagnosticsBlock>());
67
    expect(
68
      error.toStringDeep(), startsWith(
69 70 71 72
      'FlutterError\n'
      '   No MaterialLocalizations found.\n'
      '   BackButton widgets require MaterialLocalizations to be provided\n'
      '   by a Localizations widget ancestor.\n'
73 74
      '   The material library uses Localizations to generate messages,\n'
      '   labels, and abbreviations.\n'
75 76 77 78 79 80 81
      '   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'
82 83 84
      '     Center\n'
      // End of ancestor chain omitted, not relevant for test.
    ));
85 86
  });

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

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

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

    expect(exceptions.length, 1);
185
    // ignore: avoid_dynamic_calls
186 187 188 189 190 191 192 193 194 195 196 197 198
    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',
      ),
    );
199
    expect(error.toStringDeep(), startsWith(
200 201
      'FlutterError\n'
      '   No ScaffoldMessenger widget found.\n'
202 203
      '   SnackBarAction widgets require a ScaffoldMessenger widget\n'
      '   ancestor.\n'
204 205
      '   The specific widget that could not find a ScaffoldMessenger\n'
      '   ancestor was:\n'
206
      '     SnackBarAction\n'
207
      '   The ancestors of this widget were:\n'
208 209 210
      '     TextButtonTheme\n'
      '     Padding\n'
      '     Row\n'
211 212
    ));
    expect(error.toStringDeep(), endsWith(
213 214
      '     [root]\n'
      '   Typically, the ScaffoldMessenger widget is introduced by the\n'
215
      '   MaterialApp at the top of your application widget tree.\n'
216 217
    ));
  });
218
}