page_test.dart 31.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Dan Field's avatar
Dan Field committed
5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/cupertino.dart' show CupertinoPageRoute;
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10 11

import '../rendering/mock_canvas.dart';
12 13

void main() {
Dan Field's avatar
Dan Field committed
14
  testWidgets('test page transition', (WidgetTester tester) async {
15
    await tester.pumpWidget(
16
      MaterialApp(
17
        home: const Material(child: Text('Page 1')),
18 19
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
20
            return const Material(child: Text('Page 2'));
21
          },
22
        },
23
      ),
24 25
    );

26
    final Offset widget1TopLeft = tester.getTopLeft(find.text('Page 1'));
27 28 29 30 31

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 1));

32
    FadeTransition widget2Opacity =
33
        tester.element(find.text('Page 2')).findAncestorWidgetOfExactType<FadeTransition>();
34
    Offset widget2TopLeft = tester.getTopLeft(find.text('Page 2'));
35 36
    final Size widget2Size = tester.getSize(find.text('Page 2'));

37
    // Android transition is vertical only.
38
    expect(widget1TopLeft.dx == widget2TopLeft.dx, true);
39
    // Page 1 is above page 2 mid-transition.
40
    expect(widget1TopLeft.dy < widget2TopLeft.dy, true);
41 42 43
    // Animation begins 3/4 of the way up the page.
    expect(widget2TopLeft.dy < widget2Size.height / 4.0, true);
    // Animation starts with page 2 being near transparent.
44
    expect(widget2Opacity.opacity.value < 0.01, true);
45

46
    await tester.pump(const Duration(milliseconds: 300));
47 48 49 50 51 52 53 54 55

    // Page 2 covers page 1.
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 1));

56
    widget2Opacity =
57
        tester.element(find.text('Page 2')).findAncestorWidgetOfExactType<FadeTransition>();
58 59 60
    widget2TopLeft = tester.getTopLeft(find.text('Page 2'));

    // Page 2 starts to move down.
61
    expect(widget1TopLeft.dy < widget2TopLeft.dy, true);
62
    // Page 2 starts to lose opacity.
63
    expect(widget2Opacity.opacity.value < 1.0, true);
64

65
    await tester.pump(const Duration(milliseconds: 300));
66 67 68

    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), findsNothing);
Dan Field's avatar
Dan Field committed
69
  }, variant: TargetPlatformVariant.only(TargetPlatform.android));
70

Dan Field's avatar
Dan Field committed
71
  testWidgets('test page transition', (WidgetTester tester) async {
72
    final Key page2Key = UniqueKey();
73
    await tester.pumpWidget(
74
      MaterialApp(
75
        home: const Material(child: Text('Page 1')),
76 77
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
78
            return Material(
79 80 81
              key: page2Key,
              child: const Text('Page 2'),
            );
82
          },
83
        },
84
      ),
85 86
    );

87
    final Offset widget1InitialTopLeft = tester.getTopLeft(find.text('Page 1'));
88 89

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
90

91
    await tester.pump();
92
    await tester.pump(const Duration(milliseconds: 150));
93

94 95
    Offset widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));
    Offset widget2TopLeft = tester.getTopLeft(find.text('Page 2'));
96
    final RenderDecoratedBox box = tester.element(find.byKey(page2Key))
97
        .findAncestorRenderObjectOfType<RenderDecoratedBox>();
98

99
    // Page 1 is moving to the left.
100
    expect(widget1TransientTopLeft.dx < widget1InitialTopLeft.dx, true);
101
    // Page 1 isn't moving vertically.
102
    expect(widget1TransientTopLeft.dy == widget1InitialTopLeft.dy, true);
103
    // iOS transition is horizontal only.
104
    expect(widget1InitialTopLeft.dy == widget2TopLeft.dy, true);
105
    // Page 2 is coming in from the right.
106
    expect(widget2TopLeft.dx > widget1InitialTopLeft.dx, true);
107
    // The shadow should be drawn to one screen width to the left of where
108 109
    // the page 2 box is. `paints` tests relative to the painter's given canvas
    // rather than relative to the screen so assert that it's one screen
110
    // width to the left of 0 offset box rect and nothing is drawn inside the
111 112
    // box's rect.
    expect(box, paints..rect(
Dan Field's avatar
Dan Field committed
113
      rect: const Rect.fromLTWH(-800.0, 0.0, 800.0, 600.0)
114
    ));
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    await tester.pumpAndSettle();

    // Page 2 covers page 1.
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));
    widget2TopLeft = tester.getTopLeft(find.text('Page 2'));

    // Page 1 is coming back from the left.
130
    expect(widget1TransientTopLeft.dx < widget1InitialTopLeft.dx, true);
131
    // Page 1 isn't moving vertically.
132
    expect(widget1TransientTopLeft.dy == widget1InitialTopLeft.dy, true);
133
    // iOS transition is horizontal only.
134
    expect(widget1InitialTopLeft.dy == widget2TopLeft.dy, true);
135
    // Page 2 is leaving towards the right.
136
    expect(widget2TopLeft.dx > widget1InitialTopLeft.dx, true);
137 138 139 140 141 142 143 144 145 146

    await tester.pumpAndSettle();

    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), findsNothing);

    widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));

    // Page 1 is back where it started.
    expect(widget1InitialTopLeft == widget1TransientTopLeft, true);
Dan Field's avatar
Dan Field committed
147
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
148

Dan Field's avatar
Dan Field committed
149
  testWidgets('test fullscreen dialog transition', (WidgetTester tester) async {
150
    await tester.pumpWidget(
Dan Field's avatar
Dan Field committed
151 152
      const MaterialApp(
        home: Material(child: Text('Page 1')),
153
      ),
154 155
    );

156
    final Offset widget1InitialTopLeft = tester.getTopLeft(find.text('Page 1'));
157

158
    tester.state<NavigatorState>(find.byType(Navigator)).push(MaterialPageRoute<void>(
159
      builder: (BuildContext context) {
160
        return const Material(child: Text('Page 2'));
161 162 163 164 165 166 167
      },
      fullscreenDialog: true,
    ));

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

168 169
    Offset widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));
    Offset widget2TopLeft = tester.getTopLeft(find.text('Page 2'));
170 171 172 173

    // Page 1 doesn't move.
    expect(widget1TransientTopLeft == widget1InitialTopLeft, true);
    // Fullscreen dialogs transitions vertically only.
174
    expect(widget1InitialTopLeft.dx == widget2TopLeft.dx, true);
175
    // Page 2 is coming in from the bottom.
176
    expect(widget2TopLeft.dy > widget1InitialTopLeft.dy, true);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    await tester.pumpAndSettle();

    // Page 2 covers page 1.
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));
    widget2TopLeft = tester.getTopLeft(find.text('Page 2'));

    // Page 1 doesn't move.
    expect(widget1TransientTopLeft == widget1InitialTopLeft, true);
    // Fullscreen dialogs transitions vertically only.
194
    expect(widget1InitialTopLeft.dx == widget2TopLeft.dx, true);
195
    // Page 2 is leaving towards the bottom.
196
    expect(widget2TopLeft.dy > widget1InitialTopLeft.dy, true);
197 198 199 200 201 202 203 204 205 206

    await tester.pumpAndSettle();

    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), findsNothing);

    widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));

    // Page 1 is back where it started.
    expect(widget1InitialTopLeft == widget1TransientTopLeft, true);
Dan Field's avatar
Dan Field committed
207
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
208 209 210

  testWidgets('test no back gesture on Android', (WidgetTester tester) async {
    await tester.pumpWidget(
211
      MaterialApp(
212
        home: const Scaffold(body: Text('Page 1')),
213 214
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
215
            return const Scaffold(body: Text('Page 2'));
216 217
          },
        },
218
      ),
219 220 221 222 223 224 225 226 227
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
    await tester.pumpAndSettle();

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Drag from left edge to invoke the gesture.
228
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0));
229
    await gesture.moveBy(const Offset(400.0, 0.0));
230
    await tester.pump();
231 232 233 234 235

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Page 2 didn't move
236
    expect(tester.getTopLeft(find.text('Page 2')), Offset.zero);
Dan Field's avatar
Dan Field committed
237
  }, variant: TargetPlatformVariant.only(TargetPlatform.android));
238

Dan Field's avatar
Dan Field committed
239
  testWidgets('test back gesture', (WidgetTester tester) async {
240
    await tester.pumpWidget(
241
      MaterialApp(
242
        home: const Scaffold(body: Text('Page 1')),
243 244
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
245
            return const Scaffold(body: Text('Page 2'));
246 247
          },
        },
248
      ),
249 250 251 252 253 254 255 256 257
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
    await tester.pumpAndSettle();

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Drag from left edge to invoke the gesture.
258
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0));
259
    await gesture.moveBy(const Offset(400.0, 0.0));
260
    await tester.pump();
261 262 263 264

    // Page 1 is now visible.
    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), isOnstage);
265 266

    // The route widget position needs to track the finger position very exactly.
267
    expect(tester.getTopLeft(find.text('Page 2')), const Offset(400.0, 0.0));
268 269 270 271

    await gesture.moveBy(const Offset(-200.0, 0.0));
    await tester.pump();

272
    expect(tester.getTopLeft(find.text('Page 2')), const Offset(200.0, 0.0));
273 274 275 276

    await gesture.moveBy(const Offset(-100.0, 200.0));
    await tester.pump();

277
    expect(tester.getTopLeft(find.text('Page 2')), const Offset(100.0, 0.0));
Dan Field's avatar
Dan Field committed
278
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
279

280 281
  testWidgets('back gesture while OS changes', (WidgetTester tester) async {
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
282 283
      '/': (BuildContext context) => Material(
        child: FlatButton(
284
          child: const Text('PUSH'),
285 286 287
          onPressed: () { Navigator.of(context).pushNamed('/b'); },
        ),
      ),
288
      '/b': (BuildContext context) => Container(child: const Text('HELLO')),
289 290
    };
    await tester.pumpWidget(
291 292
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
293 294 295 296
        routes: routes,
      ),
    );
    await tester.tap(find.text('PUSH'));
297
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    expect(find.text('PUSH'), findsNothing);
    expect(find.text('HELLO'), findsOneWidget);
    final Offset helloPosition1 = tester.getCenter(find.text('HELLO'));
    final TestGesture gesture = await tester.startGesture(const Offset(2.5, 300.0));
    await tester.pump(const Duration(milliseconds: 20));
    await gesture.moveBy(const Offset(100.0, 0.0));
    expect(find.text('PUSH'), findsNothing);
    expect(find.text('HELLO'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 20));
    expect(find.text('PUSH'), findsOneWidget);
    expect(find.text('HELLO'), findsOneWidget);
    final Offset helloPosition2 = tester.getCenter(find.text('HELLO'));
    expect(helloPosition1.dx, lessThan(helloPosition2.dx));
    expect(helloPosition1.dy, helloPosition2.dy);
    expect(Theme.of(tester.element(find.text('HELLO'))).platform, TargetPlatform.iOS);
    await tester.pumpWidget(
314 315
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.android),
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        routes: routes,
      ),
    );
    // Now we have to let the theme animation run through.
    // This takes three frames (including the first one above):
    //  1. Start the Theme animation. It's at t=0 so everything else is identical.
    //  2. Start any animations that are informed by the Theme, for example, the
    //     DefaultTextStyle, on the first frame that the theme is not at t=0. In
    //     this case, it's at t=1.0 of the theme animation, so this is also the
    //     frame in which the theme animation ends.
    //  3. End all the other animations.
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
    expect(Theme.of(tester.element(find.text('HELLO'))).platform, TargetPlatform.android);
    final Offset helloPosition3 = tester.getCenter(find.text('HELLO'));
    expect(helloPosition3, helloPosition2);
    expect(find.text('PUSH'), findsOneWidget);
    expect(find.text('HELLO'), findsOneWidget);
    await gesture.moveBy(const Offset(100.0, 0.0));
    await tester.pump(const Duration(milliseconds: 20));
    expect(find.text('PUSH'), findsOneWidget);
    expect(find.text('HELLO'), findsOneWidget);
    final Offset helloPosition4 = tester.getCenter(find.text('HELLO'));
    expect(helloPosition3.dx, lessThan(helloPosition4.dx));
    expect(helloPosition3.dy, helloPosition4.dy);
    await gesture.moveBy(const Offset(500.0, 0.0));
    await gesture.up();
342
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 3);
343 344
    expect(find.text('PUSH'), findsOneWidget);
    expect(find.text('HELLO'), findsNothing);
345 346

    await tester.pumpWidget(
347
      MaterialApp(
Dan Field's avatar
Dan Field committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        theme: ThemeData(platform: TargetPlatform.macOS),
        routes: routes,
      ),
    );
    await tester.tap(find.text('PUSH'));
    expect(await tester.pumpAndSettle(const Duration(minutes: 1)), 2);
    expect(find.text('PUSH'), findsNothing);
    expect(find.text('HELLO'), findsOneWidget);
    final Offset helloPosition5 = tester.getCenter(find.text('HELLO'));
    await gesture.down(const Offset(2.5, 300.0));
    await tester.pump(const Duration(milliseconds: 20));
    await gesture.moveBy(const Offset(100.0, 0.0));
    expect(find.text('PUSH'), findsNothing);
    expect(find.text('HELLO'), findsOneWidget);
    await tester.pump(const Duration(milliseconds: 20));
    expect(find.text('PUSH'), findsOneWidget);
    expect(find.text('HELLO'), findsOneWidget);
    final Offset helloPosition6 = tester.getCenter(find.text('HELLO'));
    expect(helloPosition5.dx, lessThan(helloPosition6.dx));
    expect(helloPosition5.dy, helloPosition6.dy);
    expect(Theme.of(tester.element(find.text('HELLO'))).platform, TargetPlatform.macOS);
  });

  testWidgets('test no back gesture on fullscreen dialogs', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(body: Text('Page 1')),
375
      ),
376 377
    );

378
    tester.state<NavigatorState>(find.byType(Navigator)).push(MaterialPageRoute<void>(
379
      builder: (BuildContext context) {
380
        return const Scaffold(body: Text('Page 2'));
381 382 383 384 385 386 387 388 389
      },
      fullscreenDialog: true,
    ));
    await tester.pumpAndSettle();

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Drag from left edge to invoke the gesture.
390
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0));
391
    await gesture.moveBy(const Offset(400.0, 0.0));
392
    await tester.pump();
393 394 395 396 397

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Page 2 didn't move
398
    expect(tester.getTopLeft(find.text('Page 2')), Offset.zero);
Dan Field's avatar
Dan Field committed
399
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
400 401 402

  testWidgets('test adaptable transitions switch during execution', (WidgetTester tester) async {
    await tester.pumpWidget(
403
      MaterialApp(
404 405 406 407 408 409 410 411
        theme: ThemeData(
          platform: TargetPlatform.android,
          pageTransitionsTheme: const PageTransitionsTheme(
            builders: <TargetPlatform, PageTransitionsBuilder>{
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
            },
          ),
        ),
412
        home: const Material(child: Text('Page 1')),
413 414
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
415
            return const Material(child: Text('Page 2'));
416 417
          },
        },
418
      ),
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    );

    final Offset widget1InitialTopLeft = tester.getTopLeft(find.text('Page 1'));

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    Offset widget2TopLeft = tester.getTopLeft(find.text('Page 2'));
    final Size widget2Size = tester.getSize(find.text('Page 2'));

    // Android transition is vertical only.
    expect(widget1InitialTopLeft.dx == widget2TopLeft.dx, true);
    // Page 1 is above page 2 mid-transition.
    expect(widget1InitialTopLeft.dy < widget2TopLeft.dy, true);
    // Animation begins from the top of the page.
    expect(widget2TopLeft.dy < widget2Size.height, true);

    await tester.pump(const Duration(milliseconds: 300));

    // Page 2 covers page 1.
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    // Re-pump the same app but with iOS instead of Android.
    await tester.pumpWidget(
445
      MaterialApp(
446
        home: const Material(child: Text('Page 1')),
447 448
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
449
            return const Material(child: Text('Page 2'));
450 451
          },
        },
452
      ),
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pop();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));

    Offset widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));
    widget2TopLeft = tester.getTopLeft(find.text('Page 2'));

    // Page 1 is coming back from the left.
    expect(widget1TransientTopLeft.dx < widget1InitialTopLeft.dx, true);
    // Page 1 isn't moving vertically.
    expect(widget1TransientTopLeft.dy == widget1InitialTopLeft.dy, true);
    // iOS transition is horizontal only.
    expect(widget1InitialTopLeft.dy == widget2TopLeft.dy, true);
    // Page 2 is leaving towards the right.
    expect(widget2TopLeft.dx > widget1InitialTopLeft.dx, true);

    await tester.pump(const Duration(milliseconds: 300));

    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), findsNothing);

    widget1TransientTopLeft = tester.getTopLeft(find.text('Page 1'));

    // Page 1 is back where it started.
    expect(widget1InitialTopLeft == widget1TransientTopLeft, true);
Dan Field's avatar
Dan Field committed
480
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

  testWidgets('throws when builder returns null', (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: Text('Home'),
    ));
    // No exceptions yet.
    expect(tester.takeException(), isNull);

    tester
        .state<NavigatorState>(find.byType(Navigator))
        .push(MaterialPageRoute<void>(
          settings: const RouteSettings(name: 'broken'),
          builder: (BuildContext context) => null,
        ));
    await tester.pumpAndSettle();
    // An exception should've been thrown because the `builder` returned null.
497
    final dynamic exception = tester.takeException();
Dan Field's avatar
Dan Field committed
498
    expect(exception, isFlutterError);
499 500 501 502 503
    expect(exception.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   The builder for route "broken" returned null.\n'
      '   Route builders must never return null.\n'
    ));
504
  });
505

Dan Field's avatar
Dan Field committed
506
  testWidgets('test edge swipe then drop back at starting point works', (WidgetTester tester) async {
507 508 509 510 511 512 513 514
    await tester.pumpWidget(
      MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
          return MaterialPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              final String pageNumber = settings.name == '/' ? '1' : '2';
              return Center(child: Text('Page $pageNumber'));
515
            },
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
          );
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    final TestGesture gesture = await tester.startGesture(const Offset(5, 200));
    await gesture.moveBy(const Offset(300, 0));
    await tester.pump();
    // Bring it exactly back such that there's nothing to animate when releasing.
    await gesture.moveBy(const Offset(-300, 0));
    await gesture.up();
    await tester.pump();

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);
Dan Field's avatar
Dan Field committed
539
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
540

Dan Field's avatar
Dan Field committed
541
  testWidgets('test edge swipe then drop back at ending point works', (WidgetTester tester) async {
542 543 544 545 546 547 548 549
    await tester.pumpWidget(
      MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
          return MaterialPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              final String pageNumber = settings.name == '/' ? '1' : '2';
              return Center(child: Text('Page $pageNumber'));
550
            },
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
          );
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

    final TestGesture gesture = await tester.startGesture(const Offset(5, 200));
    // The width of the page.
    await gesture.moveBy(const Offset(800, 0));
    await gesture.up();
    await tester.pump();

    expect(find.text('Page 1'), isOnstage);
    expect(find.text('Page 2'), findsNothing);
Dan Field's avatar
Dan Field committed
572
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

  testWidgets('Back swipe dismiss interrupted by route push', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/28728
    final GlobalKey scaffoldKey = GlobalKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          key: scaffoldKey,
          body: Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.push<void>(scaffoldKey.currentContext, MaterialPageRoute<void>(
                  builder: (BuildContext context) {
                    return const Scaffold(
                      body: Center(child: Text('route')),
                    );
                  },
                ));
              },
              child: const Text('push'),
            ),
          ),
        ),
      ),
    );

    // Check the basic iOS back-swipe dismiss transition. Dragging the pushed
    // route halfway across the screen will trigger the iOS dismiss animation

    await tester.tap(find.text('push'));
    await tester.pumpAndSettle();
    expect(find.text('route'), findsOneWidget);
    expect(find.text('push'), findsNothing);

    TestGesture gesture = await tester.startGesture(const Offset(5, 300));
    await gesture.moveBy(const Offset(400, 0));
    await gesture.up();
    await tester.pump();
    expect( // The 'route' route has been dragged to the right, halfway across the screen
      tester.getTopLeft(find.ancestor(of: find.text('route'), matching: find.byType(Scaffold))),
      const Offset(400, 0),
    );
    expect( // The 'push' route is sliding in from the left.
      tester.getTopLeft(find.ancestor(of: find.text('push'), matching: find.byType(Scaffold))).dx,
      lessThan(0),
    );
    await tester.pumpAndSettle();
    expect(find.text('push'), findsOneWidget);
    expect(
      tester.getTopLeft(find.ancestor(of: find.text('push'), matching: find.byType(Scaffold))),
      Offset.zero,
    );
    expect(find.text('route'), findsNothing);


629 630 631
    // Run the dismiss animation 60%, which exposes the route "push" button,
    // and then press the button. A drag dropped animation is 400ms when dropped
    // exactly halfway. It follows a curve that is very steep initially.
632 633 634 635 636 637 638

    await tester.tap(find.text('push'));
    await tester.pumpAndSettle();
    expect(find.text('route'), findsOneWidget);
    expect(find.text('push'), findsNothing);

    gesture = await tester.startGesture(const Offset(5, 300));
639
    await gesture.moveBy(const Offset(400, 0)); // Drag halfway.
640
    await gesture.up();
641 642 643 644 645 646 647 648 649 650 651
    await tester.pump(); // Trigger the dropped snapping animation.
    expect(
      tester.getTopLeft(find.ancestor(of: find.text('route'), matching: find.byType(Scaffold))),
      const Offset(400, 0),
    );
    // Let the dismissing snapping animation go 60%.
    await tester.pump(const Duration(milliseconds: 240));
    expect(
      tester.getTopLeft(find.ancestor(of: find.text('route'), matching: find.byType(Scaffold))).dx,
      moreOrLessEquals(798, epsilon: 1),
    );
652 653 654 655 656 657 658 659 660 661 662 663

    // Use the navigator to push a route instead of tapping the 'push' button.
    // The topmost route (the one that's animating away), ignores input while
    // the pop is underway because route.navigator.userGestureInProgress.
    Navigator.push<void>(scaffoldKey.currentContext, MaterialPageRoute<void>(
      builder: (BuildContext context) {
        return const Scaffold(
          body: Center(child: Text('route')),
        );
      },
    ));

664 665 666
    await tester.pumpAndSettle();
    expect(find.text('route'), findsOneWidget);
    expect(find.text('push'), findsNothing);
Dan Field's avatar
Dan Field committed
667
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

  testWidgets('During back swipe the route ignores input', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/39989

    final GlobalKey homeScaffoldKey = GlobalKey();
    final GlobalKey pageScaffoldKey = GlobalKey();
    int homeTapCount = 0;
    int pageTapCount = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          key: homeScaffoldKey,
          body: GestureDetector(
            onTap: () {
              homeTapCount += 1;
            }
          ),
        ),
      ),
    );

    await tester.tap(find.byKey(homeScaffoldKey));
    expect(homeTapCount, 1);
    expect(pageTapCount, 0);

    Navigator.push<void>(homeScaffoldKey.currentContext, MaterialPageRoute<void>(
      builder: (BuildContext context) {
        return Scaffold(
          key: pageScaffoldKey,
          appBar: AppBar(title: const Text('Page')),
          body: Padding(
            padding: const EdgeInsets.all(16),
            child: GestureDetector(
              onTap: () {
                pageTapCount += 1;
              }
            ),
          ),
        );
      },
    ));

    await tester.pumpAndSettle();
    await tester.tap(find.byKey(pageScaffoldKey));
    expect(homeTapCount, 1);
    expect(pageTapCount, 1);

    // Start the basic iOS back-swipe dismiss transition. Drag the pushed
    // "page" route halfway across the screen. The underlying "home" will
    // start sliding in from the left.

    final TestGesture gesture = await tester.startGesture(const Offset(5, 300));
    await gesture.moveBy(const Offset(400, 0));
    await tester.pump();
    expect(tester.getTopLeft(find.byKey(pageScaffoldKey)), const Offset(400, 0));
    expect(tester.getTopLeft(find.byKey(homeScaffoldKey)).dx, lessThan(0));

    // Tapping on the "page" route doesn't trigger the GestureDetector because
    // it's being dragged.
    await tester.tap(find.byKey(pageScaffoldKey));
    expect(homeTapCount, 1);
    expect(pageTapCount, 1);

    // Tapping the "page" route's back button doesn't do anything either.
    await tester.tap(find.byTooltip('Back'));
    await tester.pumpAndSettle();
    expect(tester.getTopLeft(find.byKey(pageScaffoldKey)), const Offset(400, 0));
    expect(tester.getTopLeft(find.byKey(homeScaffoldKey)).dx, lessThan(0));
Dan Field's avatar
Dan Field committed
737
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

  testWidgets('After a pop caused by a back-swipe, input reaches the exposed route', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/41024

    final GlobalKey homeScaffoldKey = GlobalKey();
    final GlobalKey pageScaffoldKey = GlobalKey();
    int homeTapCount = 0;
    int pageTapCount = 0;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          key: homeScaffoldKey,
          body: GestureDetector(
            onTap: () {
              homeTapCount += 1;
            }
          ),
        ),
      ),
    );

    await tester.tap(find.byKey(homeScaffoldKey));
    expect(homeTapCount, 1);
    expect(pageTapCount, 0);

    final ValueNotifier<bool> notifier = Navigator.of(homeScaffoldKey.currentContext).userGestureInProgressNotifier;
    expect(notifier.value, false);

    Navigator.push<void>(homeScaffoldKey.currentContext, MaterialPageRoute<void>(
      builder: (BuildContext context) {
        return Scaffold(
          key: pageScaffoldKey,
          appBar: AppBar(title: const Text('Page')),
          body: Padding(
            padding: const EdgeInsets.all(16),
            child: GestureDetector(
              onTap: () {
                pageTapCount += 1;
              }
            ),
          ),
        );
      },
    ));

    await tester.pumpAndSettle();
    await tester.tap(find.byKey(pageScaffoldKey));
    expect(homeTapCount, 1);
    expect(pageTapCount, 1);

    // Trigger the basic iOS back-swipe dismiss transition. Drag the pushed
    // "page" route more than halfway across the screen and then release it.

    final TestGesture gesture = await tester.startGesture(const Offset(5, 300));
    await gesture.moveBy(const Offset(500, 0));
    await tester.pump();
    expect(tester.getTopLeft(find.byKey(pageScaffoldKey)), const Offset(500, 0));
    expect(tester.getTopLeft(find.byKey(homeScaffoldKey)).dx, lessThan(0));
    expect(notifier.value, true);
    await gesture.up();
    await tester.pumpAndSettle();
    expect(notifier.value, false);
    expect(find.byKey(pageScaffoldKey), findsNothing);

    // The back-swipe dismiss pop transition has finished and input on the
    // home page still works.
    await tester.tap(find.byKey(homeScaffoldKey));
    expect(homeTapCount, 2);
    expect(pageTapCount, 1);
Dan Field's avatar
Dan Field committed
808
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
809

Dan Field's avatar
Dan Field committed
810
  testWidgets('A MaterialPageRoute should slide out with CupertinoPageTransition when a compatible PageRoute is pushed on top of it', (WidgetTester tester) async {
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
    // Regression test for https://github.com/flutter/flutter/issues/44864.

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
        home: Scaffold(
          appBar: AppBar(title: const Text('Title')),
        ),
      ),
    );

    final Offset titleInitialTopLeft = tester.getTopLeft(find.text('Title'));

    tester.state<NavigatorState>(find.byType(Navigator)).push<void>(
      CupertinoPageRoute<void>(builder: (BuildContext context) => const Placeholder()),
    );

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 150));

    final Offset titleTransientTopLeft = tester.getTopLeft(find.text('Title'));

    // Title of the first route slides to the left.
    expect(titleInitialTopLeft.dy, equals(titleTransientTopLeft.dy));
    expect(titleInitialTopLeft.dx, greaterThan(titleTransientTopLeft.dx));
Dan Field's avatar
Dan Field committed
836
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
837
}