bottom_sheet_test.dart 5.56 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 8 9
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

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

13
    await tester.pumpWidget(new MaterialApp(
14 15 16 17 18 19 20 21
      home: new Builder(
        builder: (BuildContext context) {
          savedContext = context;
          return new Container();
        }
      )
    ));

22
    await tester.pump();
23 24
    expect(find.text('BottomSheet'), findsNothing);

25
    bool showBottomSheetThenCalled = false;
26
    showModalBottomSheet<Null>(
27 28
      context: savedContext,
      builder: (BuildContext context) => new Text('BottomSheet')
29
    ).then<Null>((Null result) {
30
      expectSync(result, isNull);
31
      showBottomSheetThenCalled = true;
32
    });
33

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

    // Tap on the the bottom sheet itself to dismiss it
40 41
    await tester.tap(find.text('BottomSheet'));
    await tester.pump(); // bottom sheet dismiss animation starts
42
    expect(showBottomSheetThenCalled, isTrue);
43 44
    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)
45 46
    expect(find.text('BottomSheet'), findsNothing);

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

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

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

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

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

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

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

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

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

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

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

105 106
    await tester.fling(find.text('BottomSheet'), const Offset(0.0, 20.0), 1000.0);
    await tester.pump(); // drain the microtask queue (Future completion callback)
107

108 109
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsOneWidget);
110

111
    await tester.pump(); // bottom sheet dismiss animation starts
112

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

116
    await tester.pump(const Duration(seconds: 1)); // animation done
117

118 119
    expect(showBottomSheetThenCalled, isTrue);
    expect(find.text('BottomSheet'), findsNothing);
120 121
  });

122 123 124 125 126 127 128 129 130 131 132
  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
    GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        key: scaffoldKey,
        body: new Center(child: new Text('body'))
      )
    ));

133
    scaffoldKey.currentState.showBottomSheet<Null>((BuildContext context) {
134
      return new Container(
135
        margin: const EdgeInsets.all(40.0),
136 137 138 139 140
        child: new Text('BottomSheet')
      );
    });

    await tester.pump(); // bottom sheet show animation starts
141
    await tester.pump(const Duration(seconds: 1)); // animation done
142 143 144 145 146
    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
147
    await tester.pump(const Duration(seconds: 1)); // animation done
148 149 150

    expect(find.text('BottomSheet'), findsNothing);
  });
151
}