modal_barrier_test.dart 2.96 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
// 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';

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')
      )
    );
  });

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

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

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

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

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

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

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

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

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

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

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

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