scaffold_test.dart 13.8 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';

8
import '../painting/mocks_for_image_cache.dart';
9

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

    expect(tester.getTopLeft(find.byType(Center)), const Offset(0.0, 0.0));
  });

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
testWidgets('Opaque bar pushes contents down', (WidgetTester tester) async {
    BuildContext childContext;
    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();
            },
          ),
        ),
      ),
    ));

    expect(MediaQuery.of(childContext).padding.top, 0);
    // 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.
    expect(tester.getRect(find.byType(Container)), Rect.fromLTRB(0, 44, 800, 600));
  });

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  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);

73
    BuildContext childContext;
74 75 76 77 78 79 80 81
    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'),
          ),
82 83 84 85 86 87
          child: Builder(
            builder: (BuildContext context) {
              childContext = context;
              return Container();
            },
          ),
88 89 90 91 92
        ),
      ),
    ));

    expect(tester.getSize(find.byType(Container)).height, 600.0 - 100.0);
93 94 95
    // The shouldn't see a media query view inset because it was consumed by
    // the scaffold.
    expect(MediaQuery.of(childContext).viewInsets.bottom, 0);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    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);
  });

114
  testWidgets('Contents are between opaque bars', (WidgetTester tester) async {
115
    const Center page1Center = Center();
116 117

    await tester.pumpWidget(
118 119 120
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
xster's avatar
xster committed
121 122
            backgroundColor: CupertinoColors.white,
            items: const <BottomNavigationBarItem>[
123 124 125
              BottomNavigationBarItem(
                icon: ImageIcon(TestImageProvider(24, 24)),
                title: Text('Tab 1'),
xster's avatar
xster committed
126
              ),
127 128 129
              BottomNavigationBarItem(
                icon: ImageIcon(TestImageProvider(24, 24)),
                title: Text('Tab 2'),
xster's avatar
xster committed
130 131 132 133 134 135
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int index) {
            return index == 0
                ? const CupertinoPageScaffold(
136
                  navigationBar: CupertinoNavigationBar(
xster's avatar
xster committed
137
                    backgroundColor: CupertinoColors.white,
138
                    middle: Text('Title'),
xster's avatar
xster committed
139 140 141
                  ),
                  child: page1Center,
                )
142
                : Stack();
xster's avatar
xster committed
143 144
          },
        ),
145 146 147 148 149
      ),
    );

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

151
  testWidgets('Contents have automatic sliver padding between translucent bars', (WidgetTester tester) async {
152
    final Container content = Container(height: 600.0, width: 600.0);
153 154

    await tester.pumpWidget(
155 156
      CupertinoApp(
        home: MediaQuery(
xster's avatar
xster committed
157
          data: const MediaQueryData(
158
            padding: EdgeInsets.symmetric(vertical: 20.0),
xster's avatar
xster committed
159
          ),
160 161
          child: CupertinoTabScaffold(
            tabBar: CupertinoTabBar(
xster's avatar
xster committed
162
              items: const <BottomNavigationBarItem>[
163 164 165
                BottomNavigationBarItem(
                  icon: ImageIcon(TestImageProvider(24, 24)),
                  title: Text('Tab 1'),
166
                ),
167 168 169
                BottomNavigationBarItem(
                  icon: ImageIcon(TestImageProvider(24, 24)),
                  title: Text('Tab 2'),
170
                ),
xster's avatar
xster committed
171 172 173 174
              ],
            ),
            tabBuilder: (BuildContext context, int index) {
              return index == 0
175
                  ? CupertinoPageScaffold(
xster's avatar
xster committed
176
                    navigationBar: const CupertinoNavigationBar(
177
                      middle: Text('Title'),
xster's avatar
xster committed
178
                    ),
179
                    child: ListView(
xster's avatar
xster committed
180 181 182 183 184
                      children: <Widget>[
                        content,
                      ],
                    ),
                  )
185
                  : Stack();
186
            },
xster's avatar
xster committed
187 188
          ),
        ),
189 190 191 192 193 194 195 196
      ),
    );

    // 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.
    await tester.drag(find.byWidget(content), const Offset(0.0, -400.0));
xster's avatar
xster committed
197 198
    // Let it bounce back.
    await tester.pump();
199 200 201 202 203 204
    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);
  });

205 206 207 208
  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(
209 210 211
      CupertinoApp(
        home: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
xster's avatar
xster committed
212
            items: const <BottomNavigationBarItem>[
213 214 215
              BottomNavigationBarItem(
                icon: ImageIcon(TestImageProvider(24, 24)),
                title: Text('Tab 1'),
xster's avatar
xster committed
216
              ),
217 218 219
              BottomNavigationBarItem(
                icon: ImageIcon(TestImageProvider(24, 24)),
                title: Text('Tab 2'),
xster's avatar
xster committed
220 221 222 223 224 225
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int index) {
            // For 1-indexed readability.
            ++index;
226
            return CupertinoTabView(
xster's avatar
xster committed
227
              builder: (BuildContext context) {
228 229 230
                return CupertinoPageScaffold(
                  navigationBar: CupertinoNavigationBar(
                    middle: Text('Page 1 of tab $index'),
xster's avatar
xster committed
231
                  ),
232 233
                  child: Center(
                    child: CupertinoButton(
xster's avatar
xster committed
234 235 236
                      child: const Text('Next'),
                      onPressed: () {
                        Navigator.of(context).push(
237
                          CupertinoPageRoute<void>(
xster's avatar
xster committed
238
                            builder: (BuildContext context) {
239 240 241
                              return CupertinoPageScaffold(
                                navigationBar: CupertinoNavigationBar(
                                  middle: Text('Page 2 of tab $index'),
xster's avatar
xster committed
242
                                ),
243 244
                                child: Center(
                                  child: CupertinoButton(
xster's avatar
xster committed
245 246 247 248 249
                                    child: const Text('Back'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                  ),
250 251 252 253
                                ),
                              );
                            },
                          ),
xster's avatar
xster committed
254 255 256 257 258 259 260 261 262
                        );
                      },
                    ),
                  ),
                );
              },
            );
          },
        ),
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
      ),
    );

    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();
279
    await tester.pump(const Duration(milliseconds: 500));
280 281 282 283 284 285 286 287 288 289 290 291 292 293

    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();
294
    await tester.pump(const Duration(milliseconds: 500));
295 296 297 298 299 300 301 302 303 304 305 306 307

    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();
308
    await tester.pump(const Duration(milliseconds: 500));
309 310 311 312

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

  testWidgets('Decorated with white background by default', (WidgetTester tester) async {
    await tester.pumpWidget(
316 317
      const CupertinoApp(
        home: CupertinoPageScaffold(
318
          child: Center(),
xster's avatar
xster committed
319
        ),
320 321 322 323 324 325 326 327 328 329 330 331
      ),
    );

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

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.color, CupertinoColors.white);
  });

  testWidgets('Overrides background color', (WidgetTester tester) async {
    await tester.pumpWidget(
332 333
      const CupertinoApp(
        home: CupertinoPageScaffold(
334 335
          child: Center(),
          backgroundColor: Color(0xFF010203),
xster's avatar
xster committed
336
        ),
337 338 339 340 341 342 343 344 345
      ),
    );

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

    final BoxDecoration decoration = decoratedBox.decoration;
    expect(decoration.color, const Color(0xFF010203));
  });
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

  testWidgets('Lists in CupertinoPageScaffold scroll to the top when status bar tapped', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        builder: (BuildContext context, Widget child) {
          // Acts as a 20px status bar at the root of the app.
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(padding: const EdgeInsets.only(top: 20)),
            child: child,
          );
        },
        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);
  });
398
}