persistent_bottom_sheet_test.dart 3.68 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

void main() {
9
  testWidgets('Verify that a BottomSheet can be rebuilt with ScaffoldFeatureController.setState()', (WidgetTester tester) async {
10 11 12
    final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
    PersistentBottomSheetController<Null> bottomSheet;
    int buildCount = 0;
13

14
    await tester.pumpWidget(new MaterialApp(
15 16
      home: new Scaffold(
        key: scaffoldKey,
17
        body: const Center(child: const Text('body'))
18 19
      )
    ));
20

21
    bottomSheet = scaffoldKey.currentState.showBottomSheet<Null>((_) {
22 23 24 25 26 27 28
      return new Builder(
        builder: (BuildContext context) {
          buildCount += 1;
          return new Container(height: 200.0);
        }
      );
    });
29

30
    await tester.pump();
31
    expect(buildCount, equals(1));
32

33
    bottomSheet.setState(() { });
34
    await tester.pump();
35
    expect(buildCount, equals(2));
36 37
  });

38 39 40 41 42 43
  testWidgets('Verify that a scrollable BottomSheet can be dismissed', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        key: scaffoldKey,
44
        body: const Center(child: const Text('body'))
45 46 47 48 49 50
      )
    ));

    scaffoldKey.currentState.showBottomSheet<Null>((BuildContext context) {
      return new ListView(
        shrinkWrap: true,
51
        primary: false,
52
        children: <Widget>[
53 54 55
          new Container(height: 100.0, child: const Text('One')),
          new Container(height: 100.0, child: const Text('Two')),
          new Container(height: 100.0, child: const Text('Three')),
56 57 58 59
        ],
      );
    });

60
    await tester.pumpAndSettle();
61 62 63

    expect(find.text('Two'), findsOneWidget);

64 65
    await tester.drag(find.text('Two'), const Offset(0.0, 400.0));
    await tester.pumpAndSettle();
66 67 68 69

    expect(find.text('Two'), findsNothing);
  });

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  testWidgets('showBottomSheet()', (WidgetTester tester) async {
    final GlobalKey key = new GlobalKey();
    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        body: new Placeholder(key: key),
      )
    ));

    int buildCount = 0;
    showBottomSheet<Null>(
      context: key.currentContext,
      builder: (BuildContext context) {
        return new Builder(
          builder: (BuildContext context) {
            buildCount += 1;
            return new Container(height: 200.0);
          }
        );
      },
    );
    await tester.pump();
    expect(buildCount, equals(1));
  });

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 131 132 133 134 135
  testWidgets('Scaffold removes top MediaQuery padding', (WidgetTester tester) async {
    BuildContext scaffoldContext;
    BuildContext bottomSheetContext;

    await tester.pumpWidget(new MaterialApp(
      home: new MediaQuery(
        data: const MediaQueryData(
          padding: const EdgeInsets.all(50.0),
        ),
        child: new Scaffold(
          resizeToAvoidBottomPadding: false,
          body: new Builder(
            builder: (BuildContext context) {
              scaffoldContext = context;
              return new Container();
            }
          ),
        ),
      )
    ));

    await tester.pump();

    showBottomSheet<Null>(
      context: scaffoldContext,
      builder: (BuildContext context) {
        bottomSheetContext = context;
        return new Container();
      },
    );

    await tester.pump();

    expect(
      MediaQuery.of(bottomSheetContext).padding,
      const EdgeInsets.only(
        bottom: 50.0,
        left: 50.0,
        right: 50.0,
      ),
    );
  });
136
}