debug_test.dart 13.3 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 ListTile());
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 28
    expect(
      error.toStringDeep(),
29 30 31 32
      'FlutterError\n'
      '   No Material widget found.\n'
      '   ListTile widgets require a Material widget ancestor.\n'
      '   In material design, most widgets are conceptually "printed" on a\n'
33
      "   sheet of material. In Flutter's material library, that material\n"
34 35 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'
      '     ListTile\n'
      '   The ancestors of this widget were:\n'
44
      '     [root]\n',
45 46 47
    );
  });

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

84
  testWidgets('debugCheckHasScaffold control test', (WidgetTester tester) async {
85 86
    await tester.pumpWidget(
      MaterialApp(
87 88 89 90 91 92 93
        theme: ThemeData(
          pageTransitionsTheme: const PageTransitionsTheme(
            builders: <TargetPlatform, PageTransitionsBuilder>{
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
            },
          ),
        ),
94 95
        home: Builder(
          builder: (BuildContext context) {
96 97 98 99
            showBottomSheet<void>(
              context: context,
              builder: (BuildContext context) => Container(),
            );
100
            return Container();
101
          },
102 103 104 105 106
        ),
      ),
    );
    final dynamic exception = tester.takeException();
    expect(exception, isFlutterError);
107
    final FlutterError error = exception as FlutterError;
108
    expect(error.diagnostics.length, 5);
Dan Field's avatar
Dan Field committed
109 110
    expect(error.diagnostics[2], isA<DiagnosticsProperty<Element>>());
    expect(error.diagnostics[3], isA<DiagnosticsBlock>());
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    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',
      ),
    );
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      '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'
128
      '     RepaintBoundary-[GlobalKey#00000]\n'
129 130 131 132 133 134 135 136
      '     IgnorePointer\n'
      '     AnimatedBuilder\n'
      '     FadeTransition\n'
      '     FractionalTranslation\n'
      '     SlideTransition\n'
      '     _FadeUpwardsPageTransition\n'
      '     AnimatedBuilder\n'
      '     RepaintBoundary\n'
137
      '     FocusTrap\n'
138 139 140
      '     _FocusMarker\n'
      '     Semantics\n'
      '     FocusScope\n'
141
      '     PrimaryScrollController\n'
142 143
      '     _ActionsMarker\n'
      '     Actions\n'
144
      '     Builder\n'
145 146 147
      '     PageStorage\n'
      '     Offstage\n'
      '     _ModalScopeStatus\n'
148 149 150
      '     UnmanagedRestorationScope\n'
      '     RestorationScope\n'
      '     AnimatedBuilder\n'
151
      '     _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#00000]\n'
152
      '     Semantics\n'
153
      '     _EffectiveTickerMode\n'
154
      '     TickerMode\n'
155
      '     _OverlayEntryWidget-[LabeledGlobalKey<_OverlayEntryWidgetState>#00000]\n'
156
      '     _Theatre\n'
157
      '     Overlay-[LabeledGlobalKey<OverlayState>#00000]\n'
158
      '     UnmanagedRestorationScope\n'
159 160 161 162 163
      '     _FocusMarker\n'
      '     Semantics\n'
      '     FocusScope\n'
      '     AbsorbPointer\n'
      '     Listener\n'
164
      '     HeroControllerScope\n'
165
      '     Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#00000]\n'
166
      '     DefaultSelectionStyle\n'
167 168 169 170 171 172 173
      '     IconTheme\n'
      '     IconTheme\n'
      '     _InheritedCupertinoTheme\n'
      '     CupertinoTheme\n'
      '     _InheritedTheme\n'
      '     Theme\n'
      '     AnimatedTheme\n'
174
      '     DefaultSelectionStyle\n'
175 176
      '     _ScaffoldMessengerScope\n'
      '     ScaffoldMessenger\n'
177 178 179 180 181 182 183
      '     Builder\n'
      '     DefaultTextStyle\n'
      '     CustomPaint\n'
      '     Banner\n'
      '     CheckedModeBanner\n'
      '     Title\n'
      '     Directionality\n'
184
      '     _LocalizationsScope-[GlobalKey#00000]\n'
185 186 187 188
      '     Semantics\n'
      '     Localizations\n'
      '     MediaQuery\n'
      '     _MediaQueryFromWindow\n'
189 190 191 192
      '     _FocusMarker\n'
      '     Focus\n'
      '     _FocusTraversalGroupMarker\n'
      '     FocusTraversalGroup\n'
193
      '     _ActionsMarker\n'
194 195 196 197 198
      '     Actions\n'
      '     _ShortcutsMarker\n'
      '     Semantics\n'
      '     _FocusMarker\n'
      '     Focus\n'
199 200 201 202 203
      '     DefaultTextEditingShortcuts\n'
      '     _ShortcutsMarker\n'
      '     Semantics\n'
      '     _FocusMarker\n'
      '     Focus\n'
204
      '     Shortcuts\n'
205 206
      '     _SharedAppModel\n'
      '     SharedAppData\n'
207 208 209 210
      '     UnmanagedRestorationScope\n'
      '     RestorationScope\n'
      '     UnmanagedRestorationScope\n'
      '     RootRestorationScope\n'
211
      '     WidgetsApp-[GlobalObjectKey _MaterialAppState#00000]\n'
212 213 214
      '     Semantics\n'
      '     _FocusMarker\n'
      '     Focus\n'
215
      '     HeroControllerScope\n'
216 217 218 219
      '     ScrollConfiguration\n'
      '     MaterialApp\n'
      '     [root]\n'
      '   Typically, the Scaffold widget is introduced by the MaterialApp\n'
220
      '   or WidgetsApp widget at the top of your application widget tree.\n'
221
    ));
222
  });
223 224

  testWidgets('debugCheckHasScaffoldMessenger control test', (WidgetTester tester) async {
225 226
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
227 228
    final SnackBar snackBar = SnackBar(
      content: const Text('Snack'),
229
      action: SnackBarAction(label: 'Test', onPressed: () {}),
230 231 232 233 234 235
    );
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(),
        child: ScaffoldMessenger(
236
          key: scaffoldMessengerKey,
237 238 239
          child: Builder(
            builder: (BuildContext context) {
              return Scaffold(
240
                key: scaffoldKey,
241 242
                body: Container(),
              );
243 244
            },
          ),
245
        ),
246
      ),
247 248
    ));
    final List<dynamic> exceptions = <dynamic>[];
249
    final FlutterExceptionHandler? oldHandler = FlutterError.onError;
250 251 252 253
    FlutterError.onError = (FlutterErrorDetails details) {
      exceptions.add(details.exception);
    };
    // ScaffoldMessenger shows SnackBar.
254
    scaffoldMessengerKey.currentState!.showSnackBar(snackBar);
255 256 257 258 259 260 261 262
    await tester.pumpAndSettle();

    // Pump widget to rebuild without ScaffoldMessenger
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(),
        child: Scaffold(
263
          key: scaffoldKey,
264 265 266 267
          body: Container(),
        ),
      ),
    ));
268 269 270 271
    // Tap SnackBarAction to dismiss.
    // The SnackBarAction should assert we still have an ancestor
    // ScaffoldMessenger in order to dismiss the SnackBar from the
    // Scaffold.
272 273 274 275
    await tester.tap(find.text('Test'));
    FlutterError.onError = oldHandler;

    expect(exceptions.length, 1);
276
    // ignore: avoid_dynamic_calls
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    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',
      ),
    );
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   No ScaffoldMessenger widget found.\n'
293 294
      '   SnackBarAction widgets require a ScaffoldMessenger widget\n'
      '   ancestor.\n'
295 296
      '   The specific widget that could not find a ScaffoldMessenger\n'
      '   ancestor was:\n'
297
      '     SnackBarAction\n'
298
      '   The ancestors of this widget were:\n'
299 300 301 302 303 304 305 306
      '     TextButtonTheme\n'
      '     Padding\n'
      '     Row\n'
      '     Padding\n'
      '     MediaQuery\n'
      '     Padding\n'
      '     SafeArea\n'
      '     FadeTransition\n'
307
      '     DefaultSelectionStyle\n'
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
      '     IconTheme\n'
      '     IconTheme\n'
      '     _InheritedCupertinoTheme\n'
      '     CupertinoTheme\n'
      '     _InheritedTheme\n'
      '     Theme\n'
      '     DefaultTextStyle\n'
      '     AnimatedDefaultTextStyle\n'
      '     _InkFeatures-[GlobalKey#00000 ink renderer]\n'
      '     NotificationListener<LayoutChangedNotification>\n'
      '     PhysicalModel\n'
      '     AnimatedPhysicalModel\n'
      '     Material\n'
      '     FractionalTranslation\n'
      '     SlideTransition\n'
      '     Listener\n'
      '     _GestureSemantics\n'
      '     RawGestureDetector\n'
      '     GestureDetector\n'
      "     Dismissible-[<'dismissible'>]\n"
      '     Semantics\n'
      '     Align\n'
      '     AnimatedBuilder\n'
      '     ClipRect\n'
      '     KeyedSubtree-[GlobalKey#00000]\n'
      '     _EffectiveTickerMode\n'
      '     TickerMode\n'
      '     Offstage\n'
      '     SizedBox\n'
      '     Hero\n'
      '     SnackBar-[#00000]\n'
      '     MediaQuery\n'
      '     LayoutId-[<_ScaffoldSlot.snackBar>]\n'
      '     CustomMultiChildLayout\n'
      '     AnimatedBuilder\n'
      '     DefaultTextStyle\n'
      '     AnimatedDefaultTextStyle\n'
      '     _InkFeatures-[GlobalKey#00000 ink renderer]\n'
      '     NotificationListener<LayoutChangedNotification>\n'
      '     PhysicalModel\n'
      '     AnimatedPhysicalModel\n'
      '     Material\n'
350 351 352
      '     _ScrollNotificationObserverScope\n'
      '     NotificationListener<ScrollNotification>\n'
      '     ScrollNotificationObserver\n'
353 354
      '     _ScaffoldScope\n'
      '     Scaffold-[LabeledGlobalKey<ScaffoldState>#00000]\n'
355 356 357 358
      '     MediaQuery\n'
      '     Directionality\n'
      '     [root]\n'
      '   Typically, the ScaffoldMessenger widget is introduced by the\n'
359
      '   MaterialApp at the top of your application widget tree.\n',
360 361
    ));
  });
362
}