modal_barrier_test.dart 2.89 KB
Newer Older
yjbanov's avatar
yjbanov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2016 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';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

void main() {
  bool tapped;
  Widget tapTarget;

  setUp(() {
    tapped = false;
    tapTarget = new GestureDetector(
      onTap: () {
        tapped = true;
      },
      child: new SizedBox(
        width: 10.0,
        height: 10.0,
        child: new Text('target')
      )
    );
  });

28 29 30 31 32 33 34
  testWidgets('ModalBarrier prevents interactions with widgets behind it', (WidgetTester tester) {
    Widget subject = new Stack(
      children: <Widget>[
        tapTarget,
        new ModalBarrier(dismissable: false),
      ]
    );
yjbanov's avatar
yjbanov committed
35

36 37 38 39 40
    tester.pumpWidget(subject);
    tester.tap(find.text('target'));
    tester.pumpWidget(subject);
    expect(tapped, isFalse,
      reason: 'because the tap is prevented by ModalBarrier');
yjbanov's avatar
yjbanov committed
41 42
  });

43 44 45 46 47 48 49
  testWidgets('ModalBarrier does not prevent interactions with widgets in front of it', (WidgetTester tester) {
    Widget subject = new Stack(
      children: <Widget>[
        new ModalBarrier(dismissable: false),
        tapTarget,
      ]
    );
yjbanov's avatar
yjbanov committed
50

51 52 53 54 55
    tester.pumpWidget(subject);
    tester.tap(find.text('target'));
    tester.pumpWidget(subject);
    expect(tapped, isTrue,
      reason: 'because the tap is not prevented by ModalBarrier');
yjbanov's avatar
yjbanov committed
56 57
  });

58 59 60 61 62
  testWidgets('ModalBarrier pops the Navigator when dismissed', (WidgetTester tester) {
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) => new FirstWidget(),
      '/modal': (BuildContext context) => new SecondWidget(),
    };
yjbanov's avatar
yjbanov committed
63

64
    tester.pumpWidget(new MaterialApp(routes: routes));
yjbanov's avatar
yjbanov committed
65

66 67
    // Initially the barrier is not visible
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing);
yjbanov's avatar
yjbanov committed
68

69 70 71 72
    // Tapping on X routes to the barrier
    tester.tap(find.text('X'));
    tester.pump();  // begin transition
    tester.pump(const Duration(seconds: 1));  // end transition
yjbanov's avatar
yjbanov committed
73

74 75 76 77
    // Tap on the barrier to dismiss it
    tester.tap(find.byKey(const ValueKey<String>('barrier')));
    tester.pump();  // begin transition
    tester.pump(const Duration(seconds: 1));  // end transition
yjbanov's avatar
yjbanov committed
78

79 80
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
      reason: 'because the barrier was dismissed');
yjbanov's avatar
yjbanov committed
81 82 83
  });
}

84
class FirstWidget extends StatelessWidget {
85
  @override
yjbanov's avatar
yjbanov committed
86
  Widget build(BuildContext context) {
87 88 89 90 91 92 93 94
  return new GestureDetector(
    onTap: () {
      Navigator.pushNamed(context, '/modal');
    },
    child: new Container(
      child: new Text('X')
    )
  );
yjbanov's avatar
yjbanov committed
95 96 97
  }
}

98
class SecondWidget extends StatelessWidget {
99
  @override
yjbanov's avatar
yjbanov committed
100
  Widget build(BuildContext context) {
101 102 103 104
  return new ModalBarrier(
    key: const ValueKey<String>('barrier'),
    dismissable: true
  );
yjbanov's avatar
yjbanov committed
105 106
  }
}