persistent_bottom_sheet_test.dart 5.44 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
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
11
    PersistentBottomSheetController<void> bottomSheet;
12
    int buildCount = 0;
13

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

21
    bottomSheet = scaffoldKey.currentState.showBottomSheet<void>((_) {
22
      return Builder(
23 24
        builder: (BuildContext context) {
          buildCount += 1;
25
          return Container(height: 200.0);
26 27 28
        }
      );
    });
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
  testWidgets('Verify that a scrollable BottomSheet can be dismissed', (WidgetTester tester) async {
39
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
40

41 42
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
43
        key: scaffoldKey,
44 45
        body: const Center(child: Text('body')),
      ),
46 47
    ));

48
    scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
49
      return ListView(
50
        shrinkWrap: true,
51
        primary: false,
52
        children: <Widget>[
53 54 55
          Container(height: 100.0, child: const Text('One')),
          Container(height: 100.0, child: const Text('Two')),
          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
  testWidgets('showBottomSheet()', (WidgetTester tester) async {
71 72 73 74
    final GlobalKey key = GlobalKey();
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
        body: Placeholder(key: key),
75
      ),
76 77 78
    ));

    int buildCount = 0;
79
    showBottomSheet<void>(
80 81
      context: key.currentContext,
      builder: (BuildContext context) {
82
        return Builder(
83 84
          builder: (BuildContext context) {
            buildCount += 1;
85
            return Container(height: 200.0);
86 87 88 89 90 91 92 93
          }
        );
      },
    );
    await tester.pump();
    expect(buildCount, equals(1));
  });

94 95 96 97
  testWidgets('Scaffold removes top MediaQuery padding', (WidgetTester tester) async {
    BuildContext scaffoldContext;
    BuildContext bottomSheetContext;

98 99
    await tester.pumpWidget(MaterialApp(
      home: MediaQuery(
100
        data: const MediaQueryData(
101
          padding: EdgeInsets.all(50.0),
102
        ),
103
        child: Scaffold(
104
          resizeToAvoidBottomPadding: false,
105
          body: Builder(
106 107
            builder: (BuildContext context) {
              scaffoldContext = context;
108
              return Container();
109 110 111
            }
          ),
        ),
112
      ),
113 114 115 116
    ));

    await tester.pump();

117
    showBottomSheet<void>(
118 119 120
      context: scaffoldContext,
      builder: (BuildContext context) {
        bottomSheetContext = context;
121
        return Container();
122 123 124 125 126 127 128 129 130 131 132 133 134 135
      },
    );

    await tester.pump();

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

  testWidgets('Scaffold.bottomSheet', (WidgetTester tester) async {
138
    final Key bottomSheetKey = UniqueKey();
139 140

    await tester.pumpWidget(
141 142
      MaterialApp(
        home: Scaffold(
143
          body: const Placeholder(),
144
          bottomSheet: Container(
145 146 147
            key: bottomSheetKey,
            alignment: Alignment.center,
            height: 200.0,
148
            child: Builder(
149
              builder: (BuildContext context) {
150
                return RaisedButton(
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
                  child: const Text('showModalBottomSheet'),
                  onPressed: () {
                    showModalBottomSheet<void>(
                      context: context,
                      builder: (BuildContext context) => const Text('modal bottom sheet'),
                    );
                  },
                );
              },
            ),
          ),
        ),
      ),
    );

    expect(find.text('showModalBottomSheet'), findsOneWidget);
    expect(tester.getSize(find.byKey(bottomSheetKey)), const Size(800.0, 200.0));
    expect(tester.getTopLeft(find.byKey(bottomSheetKey)), const Offset(0.0, 400.0));

    // Show the modal bottomSheet
    await tester.tap(find.text('showModalBottomSheet'));
    await tester.pumpAndSettle();
    expect(find.text('modal bottom sheet'), findsOneWidget);

    // Dismiss the modal bottomSheet
    await tester.tap(find.text('modal bottom sheet'));
    await tester.pumpAndSettle();
    expect(find.text('modal bottom sheet'), findsNothing);
    expect(find.text('showModalBottomSheet'), findsOneWidget);

    // Remove the persistent bottomSheet
    await tester.pumpWidget(
183 184
      const MaterialApp(
        home: Scaffold(
185
          bottomSheet: null,
186
          body: Placeholder(),
187 188 189 190 191 192 193
        ),
      ),
    );
    await tester.pumpAndSettle();
    expect(find.text('showModalBottomSheet'), findsNothing);
    expect(find.byKey(bottomSheetKey), findsNothing);
  });
194
}