modal_barrier_test.dart 4.88 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 76 77
    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
78

79
    // Tap on the barrier to dismiss it
80 81 82
    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
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 102 103 104 105 106
    final SemanticsTester semantics = new SemanticsTester(tester);
    await tester.pumpWidget(const ModalBarrier(dismissible: true));

    final TestSemantics expectedSemantics = new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
107
          id: 1,
108 109 110
          rect: TestSemantics.fullScreen,
          children: <TestSemantics>[
            new TestSemantics(
111
              id: 2,
112 113 114 115 116 117 118 119 120
              rect: TestSemantics.fullScreen,
              actions: SemanticsAction.tap.index,
            ),
          ]
        ),
      ]
    );
    expect(semantics, hasSemantics(expectedSemantics));

121 122 123 124 125 126 127 128 129 130 131
    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));

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

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

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