modal_barrier_test.dart 14.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
yjbanov's avatar
yjbanov committed
2 3 4 5
// 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
import 'package:flutter/widgets.dart';
10
import 'package:flutter/gestures.dart' show kSecondaryButton, PointerDeviceKind;
yjbanov's avatar
yjbanov committed
11

12 13
import 'semantics_tester.dart';

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

  setUp(() {
    tapped = false;
22
    tapTarget = GestureDetector(
yjbanov's avatar
yjbanov committed
23 24 25
      onTap: () {
        tapped = true;
      },
26
      child: const SizedBox(
yjbanov's avatar
yjbanov committed
27 28
        width: 10.0,
        height: 10.0,
29 30
        child: Text('target', textDirection: TextDirection.ltr),
      ),
yjbanov's avatar
yjbanov committed
31
    );
32 33 34 35 36 37 38 39 40 41 42 43

    hovered = false;
    hoverTarget = MouseRegion(
      onHover: (_) { hovered = true; },
      onEnter: (_) { hovered = true; },
      onExit: (_) { hovered = true; },
      child: const SizedBox(
        width: 10.0,
        height: 10.0,
        child: Text('target', textDirection: TextDirection.ltr),
      ),
    );
yjbanov's avatar
yjbanov committed
44 45
  });

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

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  testWidgets('ModalBarrier prevents hover interactions with widgets behind it', (WidgetTester tester) async {
    final Widget subject = Stack(
      textDirection: TextDirection.ltr,
      children: <Widget>[
        hoverTarget,
        const ModalBarrier(dismissible: false),
      ],
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(gesture.removePointer);
    // Start out of hoverTarget
    await gesture.moveTo(const Offset(100, 100));

    await tester.pumpWidget(subject);
    // Move into hoverTarget and tap
    await gesture.down(const Offset(5, 5));
    await tester.pumpWidget(subject);
    await gesture.up();
    await tester.pumpWidget(subject);

    // Move out
    await gesture.moveTo(const Offset(100, 100));
    await tester.pumpWidget(subject);

    expect(hovered, isFalse,
      reason: 'because the hover is not prevented by ModalBarrier');
  });

91
  testWidgets('ModalBarrier does not prevent interactions with widgets in front of it', (WidgetTester tester) async {
92
    final Widget subject = Stack(
93
      textDirection: TextDirection.ltr,
94
      children: <Widget>[
95
        const ModalBarrier(dismissible: false),
96
        tapTarget,
97
      ],
98
    );
yjbanov's avatar
yjbanov committed
99

100 101 102
    await tester.pumpWidget(subject);
    await tester.tap(find.text('target'));
    await tester.pumpWidget(subject);
103
    expect(tapped, isTrue,
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      reason: 'because the tap is prevented by ModalBarrier');
  });

  testWidgets('ModalBarrier does not prevent interactions with translucent widgets in front of it', (WidgetTester tester) async {
    bool dragged = false;
    final Widget subject = Stack(
      textDirection: TextDirection.ltr,
      children: <Widget>[
        const ModalBarrier(dismissible: false),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onHorizontalDragStart: (_) {
            dragged = true;
          },
          child: const Center(
            child: Text('target', textDirection: TextDirection.ltr),
          ),
        ),
      ],
    );

    await tester.pumpWidget(subject);
    await tester.dragFrom(
      tester.getBottomRight(find.byType(GestureDetector)) - const Offset(10, 10),
      const Offset(-20, 0),
    );
    await tester.pumpWidget(subject);
    expect(dragged, isTrue,
      reason: 'because the drag is prevented by ModalBarrier');
yjbanov's avatar
yjbanov committed
133 134
  });

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  testWidgets('ModalBarrier does not prevent hover interactions with widgets in front of it', (WidgetTester tester) async {
    final Widget subject = Stack(
      textDirection: TextDirection.ltr,
      children: <Widget>[
        const ModalBarrier(dismissible: false),
        hoverTarget,
      ],
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    addTearDown(gesture.removePointer);
    // Start out of hoverTarget
    await gesture.moveTo(const Offset(100, 100));
    await tester.pumpWidget(subject);
    expect(hovered, isFalse);

    // Move into hoverTarget
    await gesture.moveTo(const Offset(5, 5));
    await tester.pumpWidget(subject);
    expect(hovered, isTrue,
      reason: 'because the hover is prevented by ModalBarrier');
    hovered = false;

    // Move out
    await gesture.moveTo(const Offset(100, 100));
    await tester.pumpWidget(subject);
    expect(hovered, isTrue,
      reason: 'because the hover is prevented by ModalBarrier');
    hovered = false;
  });

166
  testWidgets('ModalBarrier pops the Navigator when dismissed by primay tap', (WidgetTester tester) async {
167
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
168 169
      '/': (BuildContext context) => const FirstWidget(),
      '/modal': (BuildContext context) => const SecondWidget(),
170
    };
yjbanov's avatar
yjbanov committed
171

172
    await tester.pumpWidget(MaterialApp(routes: routes));
yjbanov's avatar
yjbanov committed
173

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

177
    // Tapping on X routes to the barrier
178
    await tester.tap(find.text('X'));
179 180
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition
yjbanov's avatar
yjbanov committed
181

182 183 184 185 186 187
    // Press the barrier; it shouldn't dismiss yet
    final TestGesture gesture = await tester.press(
      find.byKey(const ValueKey<String>('barrier')),
    );
    await tester.pumpAndSettle(); // begin transition
    expect(find.byKey(const ValueKey<String>('barrier')), findsOneWidget);
188

189 190 191
    // Release the pointer; the barrier should be dismissed
    await gesture.up();
    await tester.pumpAndSettle(const Duration(seconds: 1)); // end transition
192 193 194 195
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
      reason: 'The route should have been dismissed by tapping the barrier.');
  });

196
  testWidgets('ModalBarrier pops the Navigator when dismissed by non-primary tap', (WidgetTester tester) async {
197
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
198 199
      '/': (BuildContext context) => const FirstWidget(),
      '/modal': (BuildContext context) => const SecondWidget(),
200 201 202 203 204 205 206 207 208 209 210 211
    };

    await tester.pumpWidget(MaterialApp(routes: routes));

    // Initially the barrier is not visible
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing);

    // Tapping on X routes to the barrier
    await tester.tap(find.text('X'));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

212 213 214 215 216 217 218
    // Press the barrier; it shouldn't dismiss yet
    final TestGesture gesture = await tester.press(
      find.byKey(const ValueKey<String>('barrier')),
      buttons: kSecondaryButton,
    );
    await tester.pumpAndSettle(); // begin transition
    expect(find.byKey(const ValueKey<String>('barrier')), findsOneWidget);
219

220 221 222
    // Release the pointer; the barrier should be dismissed
    await gesture.up();
    await tester.pumpAndSettle(const Duration(seconds: 1)); // end transition
223
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
224
      reason: 'The route should have been dismissed by tapping the barrier.');
yjbanov's avatar
yjbanov committed
225
  });
226

227 228
  testWidgets('ModalBarrier may pop the Navigator when competing with other gestures', (WidgetTester tester) async {
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
229 230
      '/': (BuildContext context) => const FirstWidget(),
      '/modal': (BuildContext context) => const SecondWidgetWithCompetence(),
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    };

    await tester.pumpWidget(MaterialApp(routes: routes));

    // Initially the barrier is not visible
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing);

    // Tapping on X routes to the barrier
    await tester.tap(find.text('X'));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    // Tap on the barrier to dismiss it
    await tester.tap(find.byKey(const ValueKey<String>('barrier')));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
      reason: 'The route should have been dismissed by tapping the barrier.');
  });

252 253 254
  testWidgets('ModalBarrier does not pop the Navigator with a WillPopScope that returns false', (WidgetTester tester) async {
    bool willPopCalled = false;
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
255
      '/': (BuildContext context) => const FirstWidget(),
256 257
      '/modal': (BuildContext context) => Stack(
        children: <Widget>[
258
          const SecondWidget(),
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
          WillPopScope(
            child: const SizedBox(),
            onWillPop: () async {
              willPopCalled = true;
              return false;
            },
          ),
      ],),
    };

    await tester.pumpWidget(MaterialApp(routes: routes));

    // Initially the barrier is not visible
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing);

    // Tapping on X routes to the barrier
    await tester.tap(find.text('X'));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    expect(willPopCalled, isFalse);

    // Tap on the barrier to attempt to dismiss it
    await tester.tap(find.byKey(const ValueKey<String>('barrier')));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    expect(find.byKey(const ValueKey<String>('barrier')), findsOneWidget,
      reason: 'The route should still be present if the pop is vetoed.');

    expect(willPopCalled, isTrue);
  });

  testWidgets('ModalBarrier pops the Navigator with a WillPopScope that returns true', (WidgetTester tester) async {
    bool willPopCalled = false;
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
295
      '/': (BuildContext context) => const FirstWidget(),
296 297
      '/modal': (BuildContext context) => Stack(
        children: <Widget>[
298
          const SecondWidget(),
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
          WillPopScope(
            child: const SizedBox(),
            onWillPop: () async {
              willPopCalled = true;
              return true;
            },
          ),
        ],),
    };

    await tester.pumpWidget(MaterialApp(routes: routes));

    // Initially the barrier is not visible
    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing);

    // Tapping on X routes to the barrier
    await tester.tap(find.text('X'));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    expect(willPopCalled, isFalse);

    // Tap on the barrier to attempt to dismiss it
    await tester.tap(find.byKey(const ValueKey<String>('barrier')));
    await tester.pump(); // begin transition
    await tester.pump(const Duration(seconds: 1)); // end transition

    expect(find.byKey(const ValueKey<String>('barrier')), findsNothing,
      reason: 'The route should not be present if the pop is permitted.');

    expect(willPopCalled, isTrue);
  });

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

336
    final TestSemantics expectedSemantics = TestSemantics.root();
337 338 339 340 341
    expect(semantics, hasSemantics(expectedSemantics));

    semantics.dispose();
  });

342
  testWidgets('Dismissible ModalBarrier includes button in semantic tree on iOS', (WidgetTester tester) async {
343
    final SemanticsTester semantics = SemanticsTester(tester);
344 345
    await tester.pumpWidget(const Directionality(
      textDirection: TextDirection.ltr,
346
      child: ModalBarrier(
347 348 349 350
        dismissible: true,
        semanticsLabel: 'Dismiss',
      ),
    ));
351

352
    final TestSemantics expectedSemantics = TestSemantics.root(
353
      children: <TestSemantics>[
354
        TestSemantics.rootChild(
355
          rect: TestSemantics.fullScreen,
356 357 358
          actions: SemanticsAction.tap.index,
          label: 'Dismiss',
          textDirection: TextDirection.ltr,
359
        ),
360
      ],
361
    );
362
    expect(semantics, hasSemantics(expectedSemantics, ignoreId: true));
363

364
    semantics.dispose();
Dan Field's avatar
Dan Field committed
365
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
366 367

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

371
    final TestSemantics expectedSemantics = TestSemantics.root();
372 373
    expect(semantics, hasSemantics(expectedSemantics));

374 375
    semantics.dispose();
  });
yjbanov's avatar
yjbanov committed
376 377
}

378
class FirstWidget extends StatelessWidget {
379
  const FirstWidget({ Key key }) : super(key: key);
380
  @override
yjbanov's avatar
yjbanov committed
381
  Widget build(BuildContext context) {
382 383 384 385 386 387 388 389
    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(context, '/modal');
      },
      child: Container(
        child: const Text('X'),
      ),
    );
yjbanov's avatar
yjbanov committed
390 391 392
  }
}

393
class SecondWidget extends StatelessWidget {
394
  const SecondWidget({ Key key }) : super(key: key);
395
  @override
yjbanov's avatar
yjbanov committed
396
  Widget build(BuildContext context) {
397 398 399 400
    return const ModalBarrier(
      key: ValueKey<String>('barrier'),
      dismissible: true,
    );
yjbanov's avatar
yjbanov committed
401 402
  }
}
403 404

class SecondWidgetWithCompetence extends StatelessWidget {
405
  const SecondWidgetWithCompetence({ Key key }) : super(key: key);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        const ModalBarrier(
          key: ValueKey<String>('barrier'),
          dismissible: true,
        ),
        GestureDetector(
          onVerticalDragStart: (_) {},
          behavior: HitTestBehavior.translucent,
          child: Container(),
        )
      ],
    );
  }
}