scaffold_test.dart 11.4 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 14 15 16
void main() {
  testWidgets('Contents are behind translucent bar', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
17
          return new CupertinoPageRoute<void>(
18
            settings: settings,
19
            builder: (BuildContext context) {
20
              return const CupertinoPageScaffold(
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
                // Default nav bar is translucent.
                navigationBar: const CupertinoNavigationBar(
                  middle: const Text('Title'),
                ),
                child: const Center(),
              );
            },
          );
        },
      ),
    );

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

  testWidgets('Contents are between opaque bars', (WidgetTester tester) async {
37
    const Center page1Center = const Center();
38 39 40 41 42

    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
43
          return new CupertinoPageRoute<void>(
44
            settings: settings,
45
            builder: (BuildContext context) {
46 47
              return new CupertinoTabScaffold(
                tabBar: new CupertinoTabBar(
48
                  backgroundColor: CupertinoColors.white,
49
                  items: const <BottomNavigationBarItem>[
50 51 52 53 54 55 56 57 58
                    const BottomNavigationBarItem(
                      icon: const ImageIcon(const TestImageProvider(24, 24)),
                      title: const Text('Tab 1'),
                    ),
                    const BottomNavigationBarItem(
                      icon: const ImageIcon(const TestImageProvider(24, 24)),
                      title: const Text('Tab 2'),
                    ),
                  ],
59
                ),
60 61
                tabBuilder: (BuildContext context, int index) {
                  return index == 0
62
                      ? const CupertinoPageScaffold(
63 64 65 66 67 68 69
                        navigationBar: const CupertinoNavigationBar(
                          backgroundColor: CupertinoColors.white,
                          middle: const Text('Title'),
                        ),
                        child: page1Center,
                      )
                      : new Stack();
70 71 72 73 74 75 76 77 78 79
                }
              );
            },
          );
        },
      ),
    );

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

81 82 83 84 85 86 87
  testWidgets('Contents have automatic sliver padding between translucent bars', (WidgetTester tester) async {
    final Container content = new Container(height: 600.0, width: 600.0);

    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
88
          return new CupertinoPageRoute<void>(
89 90 91 92 93 94 95 96
            settings: settings,
            builder: (BuildContext context) {
              return new MediaQuery(
                data: const MediaQueryData(
                  padding: const EdgeInsets.symmetric(vertical: 20.0),
                ),
                child: new CupertinoTabScaffold(
                  tabBar: new CupertinoTabBar(
97
                    items: const <BottomNavigationBarItem>[
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                      const BottomNavigationBarItem(
                        icon: const ImageIcon(const TestImageProvider(24, 24)),
                        title: const Text('Tab 1'),
                      ),
                      const BottomNavigationBarItem(
                        icon: const ImageIcon(const TestImageProvider(24, 24)),
                        title: const Text('Tab 2'),
                      ),
                    ],
                  ),
                  tabBuilder: (BuildContext context, int index) {
                    return index == 0
                        ? new CupertinoPageScaffold(
                          navigationBar: const CupertinoNavigationBar(
                            middle: const Text('Title'),
                          ),
                          child: new ListView(
                            children: <Widget>[
                              content,
                            ],
                          ),
                        )
                        : new Stack();
                  }
                ),
              );
            },
          );
        },
      ),
    );

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

141 142 143 144 145 146 147
  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(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
148
          return new CupertinoPageRoute<void>(
149 150 151 152
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabScaffold(
                tabBar: new CupertinoTabBar(
153
                  items: const <BottomNavigationBarItem>[
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                    const BottomNavigationBarItem(
                      icon: const ImageIcon(const TestImageProvider(24, 24)),
                      title: const Text('Tab 1'),
                    ),
                    const BottomNavigationBarItem(
                      icon: const ImageIcon(const TestImageProvider(24, 24)),
                      title: const Text('Tab 2'),
                    ),
                  ],
                ),
                tabBuilder: (BuildContext context, int index) {
                  // For 1-indexed readability.
                  ++index;
                  return new CupertinoTabView(
                    builder: (BuildContext context) {
                      return new CupertinoPageScaffold(
                        navigationBar: new CupertinoNavigationBar(
                          middle: new Text('Page 1 of tab $index'),
                        ),
                        child: new Center(
                          child: new CupertinoButton(
                            child: const Text('Next'),
                            onPressed: () {
                              Navigator.of(context).push(
178
                                new CupertinoPageRoute<void>(
179 180 181 182 183 184 185 186
                                  builder: (BuildContext context) {
                                    return new CupertinoPageScaffold(
                                      navigationBar: new CupertinoNavigationBar(
                                        middle: new Text('Page 2 of tab $index'),
                                      ),
                                      child: new Center(
                                        child: new CupertinoButton(
                                          child: const Text('Back'),
187
                                          onPressed: () {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                                            Navigator.of(context).pop();
                                          },
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              );
                            },
                          ),
                        ),
                      );
                    },
                  );
                },
              );
            },
          );
        },
      ),
    );

    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();
    await tester.pump(const Duration(milliseconds: 300));

    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();
    await tester.pump(const Duration(milliseconds: 300));

    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();
    await tester.pump(const Duration(milliseconds: 300));

    expect(find.text('Page 1 of tab 2'), isOnstage);
    expect(find.text('Page 2 of tab 1', skipOffstage: false), isOffstage);
  });
257 258 259 260 261 262

  testWidgets('Decorated with white background by default', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
263
          return new CupertinoPageRoute<void>(
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
            settings: settings,
            builder: (BuildContext context) {
              return const CupertinoPageScaffold(
                child: const Center(),
              );
            },
          );
        },
      ),
    );

    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(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
287
          return new CupertinoPageRoute<void>(
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
            settings: settings,
            builder: (BuildContext context) {
              return const CupertinoPageScaffold(
                child: const Center(),
                backgroundColor: const Color(0xFF010203),
              );
            },
          );
        },
      ),
    );

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