bottom_sheet_test.dart 4.27 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 10
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

void main() {
11
  test('Verify that a tap dismisses a modal BottomSheet', () {
12 13
    testWidgets((WidgetTester tester) {
      BuildContext context;
14 15
      bool showBottomSheetThenCalled = false;

16 17 18 19 20 21 22 23 24 25 26 27
      tester.pumpWidget(new MaterialApp(
          routes: <String, RouteBuilder>{
            '/': (RouteArguments args) {
              context = args.context;
              return new Container();
            }
          }
      ));

      tester.pump();
      expect(tester.findText('BottomSheet'), isNull);

28 29 30 31
      showModalBottomSheet(
        context: context,
        builder: (BuildContext context) => new Text('BottomSheet')
      ).then((_) {
32 33 34
        showBottomSheetThenCalled = true;
      });

35 36 37
      tester.pump(); // bottom sheet show animation starts
      tester.pump(new Duration(seconds: 1)); // animation done
      expect(tester.findText('BottomSheet'), isNotNull);
Hixie's avatar
Hixie committed
38
      expect(showBottomSheetThenCalled, isFalse);
39 40 41 42

      // Tap on the the bottom sheet itself to dismiss it
      tester.tap(tester.findText('BottomSheet'));
      tester.pump(); // bottom sheet dismiss animation starts
Hixie's avatar
Hixie committed
43
      expect(showBottomSheetThenCalled, isTrue);
44 45
      tester.pump(new Duration(seconds: 1)); // last frame of animation (sheet is entirely off-screen, but still present)
      tester.pump(new Duration(seconds: 1)); // frame after the animation (sheet has been removed)
46 47
      expect(tester.findText('BottomSheet'), isNull);

48
      showModalBottomSheet(context: context, builder: (BuildContext context) => new Text('BottomSheet'));
49 50 51 52 53 54 55 56
      tester.pump(); // bottom sheet show animation starts
      tester.pump(new Duration(seconds: 1)); // animation done
      expect(tester.findText('BottomSheet'), isNotNull);

      // Tap above the the bottom sheet to dismiss it
      tester.tapAt(new Point(20.0, 20.0));
      tester.pump(); // bottom sheet dismiss animation starts
      tester.pump(new Duration(seconds: 1)); // animation done
57
      tester.pump(new Duration(seconds: 1)); // rebuild frame
58 59 60 61
      expect(tester.findText('BottomSheet'), isNull);
    });
  });

62 63
  test('Verify that a downwards fling dismisses a persistent BottomSheet', () {
    testWidgets((WidgetTester tester) {
64
      GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
65 66
      bool showBottomSheetThenCalled = false;

67
      tester.pumpWidget(new MaterialApp(
68 69 70 71 72 73
        routes: <String, RouteBuilder>{
          '/': (RouteArguments args) {
            return new Scaffold(
              key: scaffoldKey,
              body: new Center(child: new Text('body'))
            );
74
          }
75
        }
76 77
      ));

78
      expect(showBottomSheetThenCalled, isFalse);
79 80
      expect(tester.findText('BottomSheet'), isNull);

Adam Barth's avatar
Adam Barth committed
81
      scaffoldKey.currentState.showBottomSheet((BuildContext context) {
82 83 84 85 86
        return new Container(
          margin: new EdgeDims.all(40.0),
          child: new Text('BottomSheet')
        );
      }).closed.then((_) {
87 88
        showBottomSheetThenCalled = true;
      });
89

90 91 92
      expect(showBottomSheetThenCalled, isFalse);
      expect(tester.findText('BottomSheet'), isNull);

93
      tester.pump(); // bottom sheet show animation starts
94 95 96 97

      expect(showBottomSheetThenCalled, isFalse);
      expect(tester.findText('BottomSheet'), isNotNull);

98
      tester.pump(new Duration(seconds: 1)); // animation done
99 100

      expect(showBottomSheetThenCalled, isFalse);
101 102 103
      expect(tester.findText('BottomSheet'), isNotNull);

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

      expect(showBottomSheetThenCalled, isTrue);
      expect(tester.findText('BottomSheet'), isNotNull);

109
      tester.pump(); // bottom sheet dismiss animation starts
110 111 112 113

      expect(showBottomSheetThenCalled, isTrue);
      expect(tester.findText('BottomSheet'), isNotNull);

114
      tester.pump(new Duration(seconds: 1)); // animation done
115

116
      expect(showBottomSheetThenCalled, isTrue);
117 118 119 120
      expect(tester.findText('BottomSheet'), isNull);
    });
  });

121
}