modal_barrier_test.dart 4.92 KB
Newer Older
yjbanov's avatar
yjbanov committed
1 2 3 4 5
// 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';
6
import 'package:flutter/foundation.dart';
yjbanov's avatar
yjbanov committed
7
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
yjbanov's avatar
yjbanov committed
9 10
import 'package:flutter/widgets.dart';

11 12
import 'semantics_tester.dart';

yjbanov's avatar
yjbanov committed
13 14 15 16 17 18 19 20 21 22
void main() {
  bool tapped;
  Widget tapTarget;

  setUp(() {
    tapped = false;
    tapTarget = new GestureDetector(
      onTap: () {
        tapped = true;
      },
23
      child: const SizedBox(
yjbanov's avatar
yjbanov committed
24 25
        width: 10.0,
        height: 10.0,
Ian Hickson's avatar
Ian Hickson committed
26
        child: const Text('target', textDirection: TextDirection.ltr)
yjbanov's avatar
yjbanov committed
27 28 29 30
      )
    );
  });

31
  testWidgets('ModalBarrier prevents interactions with widgets behind it', (WidgetTester tester) async {
32
    final Widget subject = new Stack(
33
      textDirection: TextDirection.ltr,
34 35
      children: <Widget>[
        tapTarget,
36
        const ModalBarrier(dismissible: false),
37 38
      ]
    );
yjbanov's avatar
yjbanov committed
39

40 41 42
    await tester.pumpWidget(subject);
    await tester.tap(find.text('target'));
    await tester.pumpWidget(subject);
43 44
    expect(tapped, isFalse,
      reason: 'because the tap is prevented by ModalBarrier');
yjbanov's avatar
yjbanov committed
45 46
  });

47
  testWidgets('ModalBarrier does not prevent interactions with widgets in front of it', (WidgetTester tester) async {
48
    final Widget subject = new Stack(
49
      textDirection: TextDirection.ltr,
50
      children: <Widget>[
51
        const ModalBarrier(dismissible: false),
52 53 54
        tapTarget,
      ]
    );
yjbanov's avatar
yjbanov committed
55

56 57 58
    await tester.pumpWidget(subject);
    await tester.tap(find.text('target'));
    await tester.pumpWidget(subject);
59 60
    expect(tapped, isTrue,
      reason: 'because the tap is not prevented by ModalBarrier');
yjbanov's avatar
yjbanov committed
61 62
  });

63
  testWidgets('ModalBarrier pops the Navigator when dismissed', (WidgetTester tester) async {
64 65 66 67
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (BuildContext context) => new FirstWidget(),
      '/modal': (BuildContext context) => new SecondWidget(),
    };
yjbanov's avatar
yjbanov committed
68

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

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

74
    // Tapping on X routes to the barrier
75
    await tester.tap(find.text('X'));
76 77
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition
yjbanov's avatar
yjbanov committed
78

79
    // Tap on the barrier to dismiss it
80
    await tester.tap(find.byKey(const ValueKey<String>('barrier')));
81 82
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition
yjbanov's avatar
yjbanov committed
83

84
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
85
      reason: 'The route should have been dismissed by tapping the barrier.');
yjbanov's avatar
yjbanov committed
86
  });
87 88 89 90 91 92 93 94 95 96 97

  testWidgets('Undismissible ModalBarrier hidden in semantic tree', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);
    await tester.pumpWidget(const ModalBarrier(dismissible: false));

    final TestSemantics expectedSemantics = new TestSemantics.root();
    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
  });

98 99 100
  testWidgets('Dismissible ModalBarrier includes button in semantic tree on iOS', (WidgetTester tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;

101
    final SemanticsTester semantics = new SemanticsTester(tester);
102 103 104 105 106 107 108
    await tester.pumpWidget(const Directionality(
      textDirection: TextDirection.ltr,
      child: const ModalBarrier(
        dismissible: true,
        semanticsLabel: 'Dismiss',
      ),
    ));
109 110 111 112 113

    final TestSemantics expectedSemantics = new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
          rect: TestSemantics.fullScreen,
114 115 116
          actions: SemanticsAction.tap.index,
          label: 'Dismiss',
          textDirection: TextDirection.ltr,
117 118 119
        ),
      ]
    );
120
    expect(semantics, hasSemantics(expectedSemantics, ignoreId: true));
121

122 123 124 125 126 127 128 129 130 131 132
    semantics.dispose();
    debugDefaultTargetPlatformOverride = null;
  });

  testWidgets('Dismissible ModalBarrier is hidden on Android (back button is used to dismiss)', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);
    await tester.pumpWidget(const ModalBarrier(dismissible: true));

    final TestSemantics expectedSemantics = new TestSemantics.root();
    expect(semantics, hasSemantics(expectedSemantics));

133 134
    semantics.dispose();
  });
yjbanov's avatar
yjbanov committed
135 136
}

137
class FirstWidget extends StatelessWidget {
138
  @override
yjbanov's avatar
yjbanov committed
139
  Widget build(BuildContext context) {
140 141 142 143 144
  return new GestureDetector(
    onTap: () {
      Navigator.pushNamed(context, '/modal');
    },
    child: new Container(
145
      child: const Text('X')
146 147
    )
  );
yjbanov's avatar
yjbanov committed
148 149 150
  }
}

151
class SecondWidget extends StatelessWidget {
152
  @override
yjbanov's avatar
yjbanov committed
153
  Widget build(BuildContext context) {
154
  return const ModalBarrier(
155
    key: const ValueKey<String>('barrier'),
156
    dismissible: true
157
  );
yjbanov's avatar
yjbanov committed
158 159
  }
}