modal_bottom_sheet_test.dart 8.69 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
8
import 'package:flutter/gestures.dart';
9

10 11
import '../widgets/semantics_tester.dart';

12
void main() {
13
  testWidgets('Verify that a tap dismisses a modal BottomSheet', (WidgetTester tester) async {
14 15
    BuildContext savedContext;

16 17
    await tester.pumpWidget(MaterialApp(
      home: Builder(
18 19
        builder: (BuildContext context) {
          savedContext = context;
20
          return Container();
21
        }
22
      ),
23 24
    ));

25
    await tester.pump();
26 27
    expect(find.text('BottomSheet'), findsNothing);

28
    bool showBottomSheetThenCalled = false;
29
    showModalBottomSheet<void>(
30
      context: savedContext,
31
      builder: (BuildContext context) => const Text('BottomSheet'),
32
    ).then<void>((void value) {
33
      showBottomSheetThenCalled = true;
34
    });
35

36
    await tester.pump(); // bottom sheet show animation starts
37
    await tester.pump(const Duration(seconds: 1)); // animation done
38 39 40
    expect(find.text('BottomSheet'), findsOneWidget);
    expect(showBottomSheetThenCalled, isFalse);

41
    // Tap on the bottom sheet itself to dismiss it
42 43
    await tester.tap(find.text('BottomSheet'));
    await tester.pump(); // bottom sheet dismiss animation starts
44
    expect(showBottomSheetThenCalled, isTrue);
45 46
    await tester.pump(const Duration(seconds: 1)); // last frame of animation (sheet is entirely off-screen, but still present)
    await tester.pump(const Duration(seconds: 1)); // frame after the animation (sheet has been removed)
47 48
    expect(find.text('BottomSheet'), findsNothing);

49
    showBottomSheetThenCalled = false;
50
    showModalBottomSheet<void>(
51
      context: savedContext,
52
      builder: (BuildContext context) => const Text('BottomSheet'),
53
    ).then<void>((void value) {
54 55
      showBottomSheetThenCalled = true;
    });
56
    await tester.pump(); // bottom sheet show animation starts
57
    await tester.pump(const Duration(seconds: 1)); // animation done
58
    expect(find.text('BottomSheet'), findsOneWidget);
59
    expect(showBottomSheetThenCalled, isFalse);
60

61
    // Tap above the bottom sheet to dismiss it
62
    await tester.tapAt(const Offset(20.0, 20.0));
63
    await tester.pump(); // bottom sheet dismiss animation starts
64
    expect(showBottomSheetThenCalled, isTrue);
65 66
    await tester.pump(const Duration(seconds: 1)); // animation done
    await tester.pump(const Duration(seconds: 1)); // rebuild frame
67 68
    expect(find.text('BottomSheet'), findsNothing);
  });
69

70
  testWidgets('Verify that a downwards fling dismisses a persistent BottomSheet', (WidgetTester tester) async {
71
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
72 73
    bool showBottomSheetThenCalled = false;

74 75
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
76
        key: scaffoldKey,
77 78
        body: const Center(child: Text('body')),
      ),
79 80 81 82 83
    ));

    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsNothing);

84
    scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
85
      return Container(
86
        margin: const EdgeInsets.all(40.0),
87
        child: const Text('BottomSheet'),
88
      );
89
    }).closed.whenComplete(() {
90 91
      showBottomSheetThenCalled = true;
    });
92

93 94
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsNothing);
95

96
    await tester.pump(); // bottom sheet show animation starts
97

98 99
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
100

101
    await tester.pump(const Duration(seconds: 1)); // animation done
102

103 104
    expect(showBottomSheetThenCalled, isFalse);
    expect(find.text('BottomSheet'), findsOneWidget);
105

106 107 108 109 110
    // The fling below must be such that the velocity estimation examines an
    // offset greater than the kTouchSlop. Too slow or too short a distance, and
    // it won't trigger. Also, it musn't be so much that it drags the bottom
    // sheet off the screen, or we won't see it after we pump!
    await tester.fling(find.text('BottomSheet'), const Offset(0.0, 50.0), 2000.0);
111
    await tester.pump(); // drain the microtask queue (Future completion callback)
112

113 114
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsOneWidget);
115

116
    await tester.pump(); // bottom sheet dismiss animation starts
117

118 119
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsOneWidget);
120

121
    await tester.pump(const Duration(seconds: 1)); // animation done
122

123 124
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
125 126
  });

127 128
  testWidgets('Verify that dragging past the bottom dismisses a persistent BottomSheet', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/5528
129
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
130

131 132
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
133
        key: scaffoldKey,
134 135
        body: const Center(child: Text('body')),
      ),
136 137
    ));

138
    scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
139
      return Container(
140
        margin: const EdgeInsets.all(40.0),
141
        child: const Text('BottomSheet'),
142 143 144 145
      );
    });

    await tester.pump(); // bottom sheet show animation starts
146
    await tester.pump(const Duration(seconds: 1)); // animation done
147 148 149 150 151
    expect(find.text('BottomSheet'), findsOneWidget);

    await tester.fling(find.text('BottomSheet'), const Offset(0.0, 400.0), 1000.0);
    await tester.pump(); // drain the microtask queue (Future completion callback)
    await tester.pump(); // bottom sheet dismiss animation starts
152
    await tester.pump(const Duration(seconds: 1)); // animation done
153 154 155

    expect(find.text('BottomSheet'), findsNothing);
  });
156 157 158 159 160

  testWidgets('modal BottomSheet has no top MediaQuery', (WidgetTester tester) async {
    BuildContext outerContext;
    BuildContext innerContext;

161
    await tester.pumpWidget(Localizations(
162
      locale: const Locale('en', 'US'),
163
      delegates: const <LocalizationsDelegate<dynamic>>[
164 165 166
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
167
      child: Directionality(
168
        textDirection: TextDirection.ltr,
169
        child: MediaQuery(
170
          data: const MediaQueryData(
171
            padding: EdgeInsets.all(50.0),
172
          ),
173
          child: Navigator(
174
            onGenerateRoute: (_) {
175
              return PageRouteBuilder<void>(
176 177
                pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
                  outerContext = context;
178
                  return Container();
179 180 181 182
                },
              );
            },
          ),
183 184 185 186
        ),
      ),
    ));

187
    showModalBottomSheet<void>(
188 189 190
      context: outerContext,
      builder: (BuildContext context) {
        innerContext = context;
191
        return Container();
192 193 194 195 196 197 198 199 200 201 202 203 204 205
      },
    );
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(
      MediaQuery.of(outerContext).padding,
      const EdgeInsets.all(50.0),
    );
    expect(
      MediaQuery.of(innerContext).padding,
      const EdgeInsets.only(left: 50.0, right: 50.0, bottom: 50.0),
    );
  });
206 207

  testWidgets('modal BottomSheet has semantics', (WidgetTester tester) async {
208 209
    final SemanticsTester semantics = SemanticsTester(tester);
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
210

211 212
    await tester.pumpWidget(MaterialApp(
      home: Scaffold(
213
        key: scaffoldKey,
214 215
        body: const Center(child: Text('body')),
      ),
216 217 218 219
    ));


    showModalBottomSheet<void>(context: scaffoldKey.currentContext, builder: (BuildContext context) {
220
      return Container(
221
        child: const Text('BottomSheet'),
222 223 224 225 226 227
      );
    });

    await tester.pump(); // bottom sheet show animation starts
    await tester.pump(const Duration(seconds: 1)); // animation done

228
    expect(semantics, hasSemantics(TestSemantics.root(
229
      children: <TestSemantics>[
230
        TestSemantics.rootChild(
231
          children: <TestSemantics>[
232
            TestSemantics(
233 234 235 236 237 238 239
              label: 'Dialog',
              textDirection: TextDirection.ltr,
              flags: <SemanticsFlag>[
                SemanticsFlag.scopesRoute,
                SemanticsFlag.namesRoute,
              ],
              children: <TestSemantics>[
240
                TestSemantics(
241 242 243 244 245 246 247 248 249 250 251
                  label: 'BottomSheet',
                  textDirection: TextDirection.ltr,
                ),
              ],
            ),
          ],
        ),
      ],
    ), ignoreTransform: true, ignoreRect: true, ignoreId: true));
    semantics.dispose();
  });
252
}