scaffold_test.dart 20.2 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 6
import 'dart:typed_data';

7 8 9
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';

10
import '../image_data.dart';
11
import '../rendering/mock_canvas.dart';
12

13
/// Integration tests testing both [CupertinoPageScaffold] and [CupertinoTabScaffold].
14 15 16
void main() {
  testWidgets('Contents are behind translucent bar', (WidgetTester tester) async {
    await tester.pumpWidget(
17 18
      const CupertinoApp(
        home: CupertinoPageScaffold(
xster's avatar
xster committed
19
          // Default nav bar is translucent.
20 21
          navigationBar: CupertinoNavigationBar(
            middle: Text('Title'),
xster's avatar
xster committed
22
          ),
23
          child: Center(),
xster's avatar
xster committed
24
        ),
25 26 27
      ),
    );

28
    expect(tester.getTopLeft(find.byType(Center)), Offset.zero);
29 30
  });

31
  testWidgets('Opaque bar pushes contents down', (WidgetTester tester) async {
32
    late BuildContext childContext;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(viewInsets: EdgeInsets.only(top: 20)),
        child: CupertinoPageScaffold(
          navigationBar: const CupertinoNavigationBar(
            middle: Text('Opaque'),
            backgroundColor: Color(0xFFF8F8F8),
          ),
          child: Builder(
            builder: (BuildContext context) {
              childContext = context;
              return Container();
            },
          ),
        ),
      ),
    ));

52
    expect(MediaQuery.of(childContext).padding.top, 0);
53 54 55
    // The top of the [Container] is 44 px from the top of the screen because
    // it's pushed down by the opaque navigation bar whose height is 44 px,
    // and the 20 px [MediaQuery] top padding is fully absorbed by the navigation bar.
Dan Field's avatar
Dan Field committed
56
    expect(tester.getRect(find.byType(Container)), const Rect.fromLTRB(0, 44, 800, 600));
57 58
  });

59 60 61 62 63 64 65 66 67 68 69
  testWidgets('dark mode and obstruction work', (WidgetTester tester) async {
    const Color dynamicColor = CupertinoDynamicColor.withBrightness(
      color: Color(0xFFF8F8F8),
      darkColor: Color(0xEE333333),
    );

    const CupertinoDynamicColor backgroundColor = CupertinoDynamicColor.withBrightness(
      color: Color(0xFFFFFFFF),
      darkColor: Color(0xFF000000),
    );

70
    late BuildContext childContext;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    Widget scaffoldWithBrightness(Brightness brightness) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData(
            platformBrightness: brightness,
            viewInsets: const EdgeInsets.only(top: 20),
          ),
          child: CupertinoPageScaffold(
            backgroundColor: backgroundColor,
            navigationBar: const CupertinoNavigationBar(
              middle: Text('Title'),
              backgroundColor: dynamicColor,
            ),
            child: Builder(
              builder: (BuildContext context) {
                childContext = context;
                return Container();
              },
            ),
          ),
        ),
      );
    }
    await tester.pumpWidget(scaffoldWithBrightness(Brightness.light));

97
    expect(MediaQuery.of(childContext).padding.top, 0);
98 99 100 101
    expect(find.byType(CupertinoPageScaffold), paints..rect(color: backgroundColor.color));

    await tester.pumpWidget(scaffoldWithBrightness(Brightness.dark));

102
    expect(MediaQuery.of(childContext).padding.top, greaterThan(0));
103 104 105
    expect(find.byType(CupertinoPageScaffold), paints..rect(color: backgroundColor.darkColor));
  });

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  testWidgets('Contents padding from viewInsets', (WidgetTester tester) async {
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
        child: CupertinoPageScaffold(
          navigationBar: const CupertinoNavigationBar(
            middle: Text('Opaque'),
            backgroundColor: Color(0xFFF8F8F8),
          ),
          child: Container(),
        ),
      ),
    ));

    expect(tester.getSize(find.byType(Container)).height, 600.0 - 44.0 - 100.0);

123
    late BuildContext childContext;
124 125 126 127 128 129 130 131
    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
        child: CupertinoPageScaffold(
          navigationBar: const CupertinoNavigationBar(
            middle: Text('Transparent'),
          ),
132 133 134 135 136 137
          child: Builder(
            builder: (BuildContext context) {
              childContext = context;
              return Container();
            },
          ),
138 139 140 141 142
        ),
      ),
    ));

    expect(tester.getSize(find.byType(Container)).height, 600.0 - 100.0);
143 144
    // The shouldn't see a media query view inset because it was consumed by
    // the scaffold.
145
    expect(MediaQuery.of(childContext).viewInsets.bottom, 0);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    await tester.pumpWidget(Directionality(
      textDirection: TextDirection.ltr,
      child: MediaQuery(
        data: const MediaQueryData(viewInsets: EdgeInsets.only(bottom: 100.0)),
        child: CupertinoPageScaffold(
          navigationBar: const CupertinoNavigationBar(
            middle: Text('Title'),
          ),
          resizeToAvoidBottomInset: false,
          child: Container(),
        ),
      ),
    ));

    expect(tester.getSize(find.byType(Container)).height, 600.0);
  });

164
  testWidgets('Contents bottom padding are not consumed by viewInsets when resizeToAvoidBottomInset overridden', (WidgetTester tester) async {
165 166 167 168 169 170 171 172 173 174 175 176 177 178
    const Widget child = CupertinoPageScaffold(
      resizeToAvoidBottomInset: false,
      navigationBar: CupertinoNavigationBar(
        middle: Text('Opaque'),
        backgroundColor: Color(0xFFF8F8F8),
      ),
      child: Placeholder(),
    );

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data:  MediaQueryData(viewInsets: EdgeInsets.only(bottom: 20.0)),
179
          child: child,
180
        ),
181
      ),
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    );

    final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
    // Consume bottom padding - as if by the keyboard opening
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData(
            padding: EdgeInsets.zero,
            viewPadding: EdgeInsets.only(bottom: 20),
            viewInsets: EdgeInsets.only(bottom: 300),
          ),
          child: child,
        ),
197
      ),
198 199 200 201 202
    );
    final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
    expect(initialPoint, finalPoint);
  });

203
  testWidgets('Contents are between opaque bars', (WidgetTester tester) async {
204
    const Center page1Center = Center();
205 206

    await tester.pumpWidget(
207 208 209
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
xster's avatar
xster committed
210
            backgroundColor: CupertinoColors.white,
211
            items: <BottomNavigationBarItem>[
212
              BottomNavigationBarItem(
213
                icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
214
                label: 'Tab 1',
xster's avatar
xster committed
215
              ),
216
              BottomNavigationBarItem(
217
                icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
218
                label: 'Tab 2',
xster's avatar
xster committed
219 220 221 222 223 224
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int index) {
            return index == 0
                ? const CupertinoPageScaffold(
225
                  navigationBar: CupertinoNavigationBar(
xster's avatar
xster committed
226
                    backgroundColor: CupertinoColors.white,
227
                    middle: Text('Title'),
xster's avatar
xster committed
228 229 230
                  ),
                  child: page1Center,
                )
231
                : Stack();
xster's avatar
xster committed
232 233
          },
        ),
234 235 236 237 238
      ),
    );

    expect(tester.getSize(find.byWidget(page1Center)).height, 600.0 - 44.0 - 50.0);
  });
239

240
  testWidgets('Contents have automatic sliver padding between translucent bars', (WidgetTester tester) async {
241
    const SizedBox content = SizedBox(height: 600.0, width: 600.0);
242 243

    await tester.pumpWidget(
244 245
      CupertinoApp(
        home: MediaQuery(
xster's avatar
xster committed
246
          data: const MediaQueryData(
247
            padding: EdgeInsets.symmetric(vertical: 20.0),
xster's avatar
xster committed
248
          ),
249 250
          child: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
251
              items: <BottomNavigationBarItem>[
252
                BottomNavigationBarItem(
253
                  icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
254
                  label: 'Tab 1',
255
                ),
256
                BottomNavigationBarItem(
257
                  icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
258
                  label: 'Tab 2',
259
                ),
xster's avatar
xster committed
260 261 262 263
              ],
            ),
            tabBuilder: (BuildContext context, int index) {
              return index == 0
264
                  ? CupertinoPageScaffold(
xster's avatar
xster committed
265
                    navigationBar: const CupertinoNavigationBar(
266
                      middle: Text('Title'),
xster's avatar
xster committed
267
                    ),
268
                    child: ListView(
269
                      children: const <Widget>[
xster's avatar
xster committed
270 271 272 273
                        content,
                      ],
                    ),
                  )
274
                  : Stack();
275
            },
xster's avatar
xster committed
276 277
          ),
        ),
278 279 280 281 282 283 284
      ),
    );

    // List content automatically padded by nav bar and top media query padding.
    expect(tester.getTopLeft(find.byWidget(content)).dy, 20.0 + 44.0);

    // Overscroll to the bottom.
285
    await tester.drag(find.byWidget(content), const Offset(0.0, -400.0), warnIfMissed: false); // can't be hit (it's empty) but we're aiming for the list really so it doesn't matter
xster's avatar
xster committed
286 287
    // Let it bounce back.
    await tester.pump();
288 289 290 291 292 293
    await tester.pump(const Duration(seconds: 1));

    // List content automatically padded by tab bar and bottom media query padding.
    expect(tester.getBottomLeft(find.byWidget(content)).dy, 600 - 20.0 - 50.0);
  });

294 295 296 297
  testWidgets('iOS independent tab navigation', (WidgetTester tester) async {
    // A full on iOS information architecture app with 2 tabs, and 2 pages
    // in each with independent navigation states.
    await tester.pumpWidget(
298 299 300
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
301
            items: <BottomNavigationBarItem>[
302
              BottomNavigationBarItem(
303
                icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
304
                label: 'Tab 1',
xster's avatar
xster committed
305
              ),
306
              BottomNavigationBarItem(
307
                icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
308
                label: 'Tab 2',
xster's avatar
xster committed
309 310 311 312 313 314
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int index) {
            // For 1-indexed readability.
            ++index;
315
            return CupertinoTabView(
xster's avatar
xster committed
316
              builder: (BuildContext context) {
317 318 319
                return CupertinoPageScaffold(
                  navigationBar: CupertinoNavigationBar(
                    middle: Text('Page 1 of tab $index'),
xster's avatar
xster committed
320
                  ),
321 322
                  child: Center(
                    child: CupertinoButton(
xster's avatar
xster committed
323 324
                      child: const Text('Next'),
                      onPressed: () {
325
                        Navigator.of(context).push(
326
                          CupertinoPageRoute<void>(
xster's avatar
xster committed
327
                            builder: (BuildContext context) {
328 329 330
                              return CupertinoPageScaffold(
                                navigationBar: CupertinoNavigationBar(
                                  middle: Text('Page 2 of tab $index'),
xster's avatar
xster committed
331
                                ),
332 333
                                child: Center(
                                  child: CupertinoButton(
xster's avatar
xster committed
334 335
                                    child: const Text('Back'),
                                    onPressed: () {
336
                                      Navigator.of(context).pop();
xster's avatar
xster committed
337 338
                                    },
                                  ),
339 340 341 342
                                ),
                              );
                            },
                          ),
xster's avatar
xster committed
343 344 345 346 347 348 349 350 351
                        );
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
      ),
    );

    expect(find.text('Page 1 of tab 1'), findsOneWidget);
    expect(find.text('Page 1 of tab 2'), findsNothing); // Lazy building so not built yet.

    await tester.tap(find.text('Tab 2'));
    await tester.pump();

    expect(find.text('Page 1 of tab 1'), findsNothing); // It's offstage now.
    expect(find.text('Page 1 of tab 1', skipOffstage: false), findsOneWidget);
    expect(find.text('Page 1 of tab 2'), findsOneWidget);

    // Navigate in tab 2.
    await tester.tap(find.text('Next'));
    await tester.pump();
368
    await tester.pump(const Duration(milliseconds: 500));
369 370 371 372 373 374 375 376 377 378 379 380 381 382

    expect(find.text('Page 2 of tab 2'), isOnstage);
    expect(find.text('Page 1 of tab 1', skipOffstage: false), isOffstage);

    await tester.tap(find.text('Tab 1'));
    await tester.pump();

    // Independent navigation stacks.
    expect(find.text('Page 1 of tab 1'), isOnstage);
    expect(find.text('Page 2 of tab 2', skipOffstage: false), isOffstage);

    // Navigate in tab 1.
    await tester.tap(find.text('Next'));
    await tester.pump();
383
    await tester.pump(const Duration(milliseconds: 500));
384 385 386 387 388 389 390 391 392 393 394 395 396

    expect(find.text('Page 2 of tab 1'), isOnstage);
    expect(find.text('Page 2 of tab 2', skipOffstage: false), isOffstage);

    await tester.tap(find.text('Tab 2'));
    await tester.pump();

    expect(find.text('Page 2 of tab 2'), isOnstage);
    expect(find.text('Page 2 of tab 1', skipOffstage: false), isOffstage);

    // Pop in tab 2
    await tester.tap(find.text('Back'));
    await tester.pump();
397
    await tester.pump(const Duration(milliseconds: 500));
398 399 400 401

    expect(find.text('Page 1 of tab 2'), isOnstage);
    expect(find.text('Page 2 of tab 1', skipOffstage: false), isOffstage);
  });
402 403 404

  testWidgets('Decorated with white background by default', (WidgetTester tester) async {
    await tester.pumpWidget(
405 406
      const CupertinoApp(
        home: CupertinoPageScaffold(
407
          child: Center(),
xster's avatar
xster committed
408
        ),
409 410 411
      ),
    );

412
    final DecoratedBox decoratedBox = tester.widgetList(find.byType(DecoratedBox)).elementAt(1) as DecoratedBox;
413 414
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

415
    final BoxDecoration decoration = decoratedBox.decoration as BoxDecoration;
416
    expect(decoration.color, isSameColorAs(CupertinoColors.white));
417 418 419 420
  });

  testWidgets('Overrides background color', (WidgetTester tester) async {
    await tester.pumpWidget(
421 422
      const CupertinoApp(
        home: CupertinoPageScaffold(
423
          backgroundColor: Color(0xFF010203),
424
          child: Center(),
xster's avatar
xster committed
425
        ),
426 427 428
      ),
    );

429
    final DecoratedBox decoratedBox = tester.widgetList(find.byType(DecoratedBox)).elementAt(1) as DecoratedBox;
430 431
    expect(decoratedBox.decoration.runtimeType, BoxDecoration);

432
    final BoxDecoration decoration = decoratedBox.decoration as BoxDecoration;
433 434
    expect(decoration.color, const Color(0xFF010203));
  });
435 436 437 438

  testWidgets('Lists in CupertinoPageScaffold scroll to the top when status bar tapped', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
439
        builder: (BuildContext context, Widget? child) {
440 441
          // Acts as a 20px status bar at the root of the app.
          return MediaQuery(
442
            data: MediaQuery.of(context).copyWith(padding: const EdgeInsets.only(top: 20)),
443
            child: child!,
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
          );
        },
        home: CupertinoPageScaffold(
          // Default nav bar is translucent.
          navigationBar: const CupertinoNavigationBar(
            middle: Text('Title'),
          ),
          child: ListView.builder(
            itemExtent: 50,
            itemBuilder: (BuildContext context, int index) => Text(index.toString()),
          ),
        ),
      ),
    );
    // Top media query padding 20 + translucent nav bar 44.
    expect(tester.getTopLeft(find.text('0')).dy, 64);
    expect(tester.getTopLeft(find.text('6')).dy, 364);

    await tester.fling(
      find.text('5'), // Find some random text on the screen.
      const Offset(0, -200),
      20,
    );

    await tester.pumpAndSettle();

    expect(tester.getTopLeft(find.text('6')).dy, moreOrLessEquals(166.833, epsilon: 0.1));
    expect(tester.getTopLeft(find.text('12')).dy, moreOrLessEquals(466.8333333333334, epsilon: 0.1));

    // The media query top padding is 20. Tapping at 20 should do nothing.
    await tester.tapAt(const Offset(400, 20));
    await tester.pumpAndSettle();
    expect(tester.getTopLeft(find.text('6')).dy, moreOrLessEquals(166.833, epsilon: 0.1));
    expect(tester.getTopLeft(find.text('12')).dy, moreOrLessEquals(466.8333333333334, epsilon: 0.1));

    // Tap 1 pixel higher.
    await tester.tapAt(const Offset(400, 19));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    expect(tester.getTopLeft(find.text('0')).dy, 64);
    expect(tester.getTopLeft(find.text('6')).dy, 364);
    expect(find.text('12'), findsNothing);
  });
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

  testWidgets('resizeToAvoidBottomInset is supported even when no navigationBar', (WidgetTester tester) async {
    Widget buildFrame(bool showNavigationBar, bool showKeyboard) {
      return CupertinoApp(
        home: MediaQuery(
          data: MediaQueryData(
            padding: EdgeInsets.zero,
            viewPadding: const EdgeInsets.only(bottom: 20),
            viewInsets: EdgeInsets.only(bottom: showKeyboard ? 300 : 20),
          ),
          child: CupertinoPageScaffold(
            resizeToAvoidBottomInset: true,
            navigationBar: showNavigationBar ? const CupertinoNavigationBar(
              middle: Text('Title'),
            ) : null,
            child: const Center(
              child: CupertinoTextField(),
            ),
          ),
        ),
      );
    }

    // When there is a nav bar and no keyboard.
    await tester.pumpWidget(buildFrame(true, false));
    final Offset positionNoInsetWithNavBar = tester.getTopLeft(find.byType(CupertinoTextField));

    // When there is a nav bar and keyboard, the CupertinoTextField moves up.
    await tester.pumpWidget(buildFrame(true, true));
    await tester.pumpAndSettle();
    final Offset positionWithInsetWithNavBar = tester.getTopLeft(find.byType(CupertinoTextField));
    expect(positionWithInsetWithNavBar.dy, lessThan(positionNoInsetWithNavBar.dy));

    // When there is no nav bar and no keyboard, the CupertinoTextField is still
    // centered.
    await tester.pumpWidget(buildFrame(false, false));
    final Offset positionNoInsetNoNavBar = tester.getTopLeft(find.byType(CupertinoTextField));
    expect(positionNoInsetNoNavBar, equals(positionNoInsetWithNavBar));

    // When there is a keyboard but no nav bar, the CupertinoTextField also
    // moves up to the same position as when there is a keyboard and nav bar.
    await tester.pumpWidget(buildFrame(false, true));
    await tester.pumpAndSettle();
    final Offset positionWithInsetNoNavBar = tester.getTopLeft(find.byType(CupertinoTextField));
    expect(positionWithInsetNoNavBar.dy, lessThan(positionNoInsetNoNavBar.dy));
    expect(positionWithInsetNoNavBar, equals(positionWithInsetWithNavBar));
  });
534 535 536 537 538 539

  testWidgets('textScaleFactor is set to 1.0', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Builder(builder: (BuildContext context) {
          return MediaQuery(
540
            data: MediaQuery.of(context).copyWith(textScaleFactor: 99),
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            child: const CupertinoPageScaffold(
              navigationBar: CupertinoNavigationBar(
                middle: Text('middle'),
                leading: Text('leading'),
                trailing: Text('trailing'),
              ),
              child: Text('content'),
            ),
          );
        }),
      ),
    );
    final Iterable<RichText> richTextList = tester.widgetList<RichText>(
      find.descendant(of: find.byType(CupertinoNavigationBar), matching: find.byType(RichText)),
    );

    expect(richTextList.length, greaterThan(0));
    expect(richTextList.any((RichText text) => text.textScaleFactor != 1), isFalse);

    expect(tester.widget<RichText>(find.descendant(of: find.text('content'), matching: find.byType(RichText))).textScaleFactor, 99);
  });
562
}