scaffold_test.dart 12 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 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_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
10
  testWidgets('Scaffold control test', (WidgetTester tester) async {
11
    Key bodyKey = new UniqueKey();
12
    await tester.pumpWidget(new Scaffold(
13 14 15
      appBar: new AppBar(title: new Text('Title')),
      body: new Container(key: bodyKey)
    ));
16

17 18
    RenderBox bodyBox = tester.renderObject(find.byKey(bodyKey));
    expect(bodyBox.size, equals(new Size(800.0, 544.0)));
19

20
    await tester.pumpWidget(new MediaQuery(
21 22 23 24 25 26
      data: new MediaQueryData(padding: new EdgeInsets.only(bottom: 100.0)),
      child: new Scaffold(
        appBar: new AppBar(title: new Text('Title')),
        body: new Container(key: bodyKey)
      )
    ));
27

28 29
    bodyBox = tester.renderObject(find.byKey(bodyKey));
    expect(bodyBox.size, equals(new Size(800.0, 444.0)));
30

31
    await tester.pumpWidget(new MediaQuery(
32 33 34 35 36 37 38
      data: new MediaQueryData(padding: new EdgeInsets.only(bottom: 100.0)),
      child: new Scaffold(
        appBar: new AppBar(title: new Text('Title')),
        body: new Container(key: bodyKey),
        resizeToAvoidBottomPadding: false
      )
    ));
39

40 41
    bodyBox = tester.renderObject(find.byKey(bodyKey));
    expect(bodyBox.size, equals(new Size(800.0, 544.0)));
42
  });
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

  testWidgets('Floating action animation', (WidgetTester tester) async {
    await tester.pumpWidget(new Scaffold(
      floatingActionButton: new FloatingActionButton(
        key: new Key("one"),
        onPressed: null,
        child: new Text("1")
      )
    ));

    expect(tester.binding.transientCallbackCount, 0);

    await tester.pumpWidget(new Scaffold(
      floatingActionButton: new FloatingActionButton(
        key: new Key("two"),
        onPressed: null,
        child: new Text("2")
      )
    ));

    expect(tester.binding.transientCallbackCount, greaterThan(0));
    await tester.pumpWidget(new Container());
    expect(tester.binding.transientCallbackCount, 0);
    await tester.pumpWidget(new Scaffold());
    expect(tester.binding.transientCallbackCount, 0);

    await tester.pumpWidget(new Scaffold(
      floatingActionButton: new FloatingActionButton(
        key: new Key("one"),
        onPressed: null,
        child: new Text("1")
      )
    ));

    expect(tester.binding.transientCallbackCount, greaterThan(0));
  });
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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

  testWidgets('Drawer scrolling', (WidgetTester tester) async {
    GlobalKey<ScrollableState<Scrollable>> drawerKey =
        new GlobalKey<ScrollableState<Scrollable>>(debugLabel: 'drawer');
    Key appBarKey = new Key('appBar');
    const double appBarHeight = 256.0;

    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          appBarBehavior: AppBarBehavior.under,
          appBar: new AppBar(
            key: appBarKey,
            expandedHeight: appBarHeight,
            title: new Text('Title'),
            flexibleSpace: new FlexibleSpaceBar(title: new Text('Title')),
          ),
          drawer: new Drawer(
            child: new Block(
              scrollableKey: drawerKey,
              children: new List<Widget>.generate(10,
                (int index) => new SizedBox(height: 100.0, child: new Text('D$index'))
              )
            )
          ),
          body: new Block(
            padding: const EdgeInsets.only(top: appBarHeight),
            children: new List<Widget>.generate(10,
              (int index) => new SizedBox(height: 100.0, child: new Text('B$index'))
            ),
          ),
        )
      )
    );

    ScaffoldState state = tester.firstState(find.byType(Scaffold));
    state.openDrawer();

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(drawerKey.currentState.scrollOffset, equals(0));

    const double scrollDelta = 80.0;
    await tester.scroll(find.byKey(drawerKey), const Offset(0.0, -scrollDelta));
    await tester.pump();

    expect(drawerKey.currentState.scrollOffset, equals(scrollDelta));

    RenderBox renderBox = tester.renderObject(find.byKey(appBarKey));
    expect(renderBox.size.height, equals(appBarHeight));
  });
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  testWidgets('Tapping the status bar scrolls to top on iOS', (WidgetTester tester) async {
    final GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
    final Key appBarKey = new UniqueKey();

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.iOS),
        home: new MediaQuery(
          data: new MediaQueryData(padding: const EdgeInsets.only(top: 25.0)), // status bar
          child: new Scaffold(
            scrollableKey: scrollableKey,
            appBar: new AppBar(
              key: appBarKey,
              title: new Text('Title')
            ),
            body: new Block(
              scrollableKey: scrollableKey,
              initialScrollOffset: 500.0,
              children: new List<Widget>.generate(20,
                (int index) => new SizedBox(height: 100.0, child: new Text('$index'))
              )
            )
          )
        )
      )
    );

    expect(scrollableKey.currentState.scrollOffset, equals(500.0));
    await tester.tapAt(const Point(100.0, 10.0));
    await tester.pump();
    await tester.pump(new Duration(seconds: 1));
    expect(scrollableKey.currentState.scrollOffset, equals(0.0));
  });

  testWidgets('Tapping the status bar does not scroll to top on Android', (WidgetTester tester) async {
    final GlobalKey<ScrollableState> scrollableKey = new GlobalKey<ScrollableState>();
    final Key appBarKey = new UniqueKey();

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new MediaQuery(
          data: new MediaQueryData(padding: const EdgeInsets.only(top: 25.0)), // status bar
          child: new Scaffold(
            scrollableKey: scrollableKey,
            appBar: new AppBar(
              key: appBarKey,
              title: new Text('Title')
            ),
            body: new Block(
              scrollableKey: scrollableKey,
              initialScrollOffset: 500.0,
              children: new List<Widget>.generate(20,
                (int index) => new SizedBox(height: 100.0, child: new Text('$index'))
              )
            )
          )
        )
      )
    );

    expect(scrollableKey.currentState.scrollOffset, equals(500.0));
    await tester.tapAt(const Point(100.0, 10.0));
    await tester.pump();
    await tester.pump(new Duration(seconds: 1));
    expect(scrollableKey.currentState.scrollOffset, equals(500.0));
  });
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

  testWidgets('Bottom sheet cannot overlap app bar', (WidgetTester tester) async {
    Key sheetKey = new UniqueKey();

    await tester.pumpWidget(
      new MaterialApp(
        theme: new ThemeData(platform: TargetPlatform.android),
        home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Title')
          ),
          body: new Builder(
            builder: (BuildContext context) {
              return new GestureDetector(
                onTap: () {
                  Scaffold.of(context).showBottomSheet((BuildContext context) {
                    return new Container(
                      key: sheetKey,
                      decoration: new BoxDecoration(backgroundColor: Colors.blue[500])
                    );
                  });
                },
                child: new Text('X')
              );
            }
          )
        )
      )
    );

    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(seconds: 1));

    RenderBox appBarBox = tester.renderObject(find.byType(AppBar));
    RenderBox sheetBox = tester.renderObject(find.byKey(sheetKey));

    Point appBarBottomRight = appBarBox.localToGlobal(appBarBox.size.bottomRight(Point.origin));
    Point sheetTopRight = sheetBox.localToGlobal(sheetBox.size.topRight(Point.origin));

    expect(appBarBottomRight, equals(sheetTopRight));
  });
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  testWidgets('Persistent bottom buttons are persistent', (WidgetTester tester) async {
    bool didPressButton = false;
    await tester.pumpWidget(
      new MaterialApp(
        home: new Scaffold(
          body: new ScrollableViewport(
            child: new Container(
              decoration: new BoxDecoration(
                backgroundColor: Colors.amber[500],
              ),
              height: 5000.0,
              child: new Text('body'),
            ),
          ),
          persistentFooterButtons: <Widget>[
            new FlatButton(
              onPressed: () {
                didPressButton = true;
              },
              child: new Text('X'),
            )
          ],
        ),
      ),
    );

    await tester.scroll(find.text('body'), const Offset(0.0, -1000.0));
    expect(didPressButton, isFalse);
    await tester.tap(find.text('X'));
    expect(didPressButton, isTrue);
  });

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  group('back arrow', () {
    Future<Null> expectBackIcon(WidgetTester tester, TargetPlatform platform, IconData expectedIcon) async {
      GlobalKey rootKey = new GlobalKey();
      final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
        '/': (_) => new Container(key: rootKey, child: new Text('Home')),
        '/scaffold': (_) => new Scaffold(
            appBar: new AppBar(),
            body: new Text('Scaffold'),
        )
      };
      await tester.pumpWidget(
        new MaterialApp(theme: new ThemeData(platform: platform), routes: routes)
      );

      Navigator.pushNamed(rootKey.currentContext, '/scaffold');
      await tester.pump();
      await tester.pump(const Duration(seconds: 1));

      Icon icon = tester.widget(find.byType(Icon));
      expect(icon.icon, expectedIcon);
    }

    testWidgets('Back arrow uses correct default on Android', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.android, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on Fuchsia', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.fuchsia, Icons.arrow_back);
    });

    testWidgets('Back arrow uses correct default on iOS', (WidgetTester tester) async {
      await expectBackIcon(tester, TargetPlatform.iOS, Icons.arrow_back_ios);
    });
  });
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

  group('body size', () {
    testWidgets('body size with container', (WidgetTester tester) async {
      Key testKey = new UniqueKey();
      await tester.pumpWidget(
        new Scaffold(body: new Container(key: testKey))
      );
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
      expect(tester.renderObject/*<RenderBox>*/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0));
    });

    testWidgets('body size with sized container', (WidgetTester tester) async {
      Key testKey = new UniqueKey();
      await tester.pumpWidget(
        new Scaffold(body: new Container(key: testKey, height: 100.0))
      );
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 100.0));
      expect(tester.renderObject/*<RenderBox>*/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0));
    });

    testWidgets('body size with centered container', (WidgetTester tester) async {
      Key testKey = new UniqueKey();
      await tester.pumpWidget(
        new Scaffold(body: new Center(child: new Container(key: testKey)))
      );
      expect(tester.element(find.byKey(testKey)).size, const Size(800.0, 600.0));
      expect(tester.renderObject/*<RenderBox>*/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0));
    });

    testWidgets('body size with button', (WidgetTester tester) async {
      Key testKey = new UniqueKey();
      await tester.pumpWidget(
        new Scaffold(body: new FlatButton(key: testKey, onPressed: () { }, child: new Text('')))
      );
      expect(tester.element(find.byKey(testKey)).size, const Size(88.0, 36.0));
      expect(tester.renderObject/*<RenderBox>*/(find.byKey(testKey)).localToGlobal(Point.origin), const Point(0.0, 0.0));
    });
  });
346
}