page_transitions_test.dart 12.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
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/material.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8

9
class TestOverlayRoute extends OverlayRoute<void> {
10
  TestOverlayRoute({ super.settings });
11
  @override
12
  Iterable<OverlayEntry> createOverlayEntries() sync* {
13
    yield OverlayEntry(builder: _build);
14
  }
15
  Widget _build(BuildContext context) => const Text('Overlay');
16 17
}

18
class PersistentBottomSheetTest extends StatefulWidget {
19
  const PersistentBottomSheetTest({ super.key });
20 21

  @override
22
  PersistentBottomSheetTestState createState() => PersistentBottomSheetTestState();
23 24 25
}

class PersistentBottomSheetTestState extends State<PersistentBottomSheetTest> {
26
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
27 28 29 30

  bool setStateCalled = false;

  void showBottomSheet() {
31
    _scaffoldKey.currentState!.showBottomSheet<void>((BuildContext context) {
32
      return const Text('bottomSheet');
33
    })
34
    .closed.whenComplete(() {
35 36 37 38 39 40 41 42
      setState(() {
        setStateCalled = true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
43
    return Scaffold(
44
      key: _scaffoldKey,
45
      body: const Text('Sheet'),
46 47 48 49
    );
  }
}

50
void main() {
51
  testWidgetsWithLeakTracking('Check onstage/offstage handling around transitions', (WidgetTester tester) async {
52 53
    final GlobalKey containerKey1 = GlobalKey();
    final GlobalKey containerKey2 = GlobalKey();
54
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
55 56
      '/': (_) => Container(key: containerKey1, child: const Text('Home')),
      '/settings': (_) => Container(key: containerKey2, child: const Text('Settings')),
57
    };
58

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

61
    expect(find.text('Home'), isOnstage);
62 63
    expect(find.text('Settings'), findsNothing);
    expect(find.text('Overlay'), findsNothing);
64

65 66 67
    expect(Navigator.canPop(containerKey1.currentContext!), isFalse);
    Navigator.pushNamed(containerKey1.currentContext!, '/settings');
    expect(Navigator.canPop(containerKey1.currentContext!), isTrue);
68

69
    await tester.pump();
70

71 72
    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings', skipOffstage: false), isOffstage);
73
    expect(find.text('Overlay'), findsNothing);
74

75
    await tester.pump(const Duration(milliseconds: 16));
76

77 78
    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings'), isOnstage);
79
    expect(find.text('Overlay'), findsNothing);
80

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

83
    expect(find.text('Home'), findsNothing);
84
    expect(find.text('Settings'), isOnstage);
85
    expect(find.text('Overlay'), findsNothing);
86

87
    Navigator.push(containerKey2.currentContext!, TestOverlayRoute());
88

89
    await tester.pump();
90

91
    expect(find.text('Home'), findsNothing);
92 93
    expect(find.text('Settings'), isOnstage);
    expect(find.text('Overlay'), isOnstage);
94

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

97
    expect(find.text('Home'), findsNothing);
98 99
    expect(find.text('Settings'), isOnstage);
    expect(find.text('Overlay'), isOnstage);
100

101 102
    expect(Navigator.canPop(containerKey2.currentContext!), isTrue);
    Navigator.pop(containerKey2.currentContext!);
103
    await tester.pump();
104

105
    expect(find.text('Home'), findsNothing);
106
    expect(find.text('Settings'), isOnstage);
107
    expect(find.text('Overlay'), findsNothing);
108

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

111
    expect(find.text('Home'), findsNothing);
112
    expect(find.text('Settings'), isOnstage);
113
    expect(find.text('Overlay'), findsNothing);
114

115 116
    expect(Navigator.canPop(containerKey2.currentContext!), isTrue);
    Navigator.pop(containerKey2.currentContext!);
117
    await tester.pump();
Hans Muller's avatar
Hans Muller committed
118
    await tester.pump();
119

120 121
    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings'), isOnstage);
122
    expect(find.text('Overlay'), findsNothing);
123

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

126
    expect(find.text('Home'), isOnstage);
127 128
    expect(find.text('Settings'), findsNothing);
    expect(find.text('Overlay'), findsNothing);
129

130
    expect(Navigator.canPop(containerKey1.currentContext!), isFalse);
131 132
  });

133
  testWidgetsWithLeakTracking('Check back gesture disables Heroes', (WidgetTester tester) async {
134 135
    final GlobalKey containerKey1 = GlobalKey();
    final GlobalKey containerKey2 = GlobalKey();
136 137
    const String kHeroTag = 'hero';
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
138
      '/': (_) => Scaffold(
139
        key: containerKey1,
140 141 142
        body: const ColoredBox(
          color: Color(0xff00ffff),
          child: Hero(
143
            tag: kHeroTag,
144 145 146
            child: Text('Home'),
          ),
        ),
147
      ),
148
      '/settings': (_) => Scaffold(
149
        key: containerKey2,
150
        body: Container(
151
          padding: const EdgeInsets.all(100.0),
152
          color: const Color(0xffff00ff),
153
          child: const Hero(
154
            tag: kHeroTag,
155 156 157
            child: Text('Settings'),
          ),
        ),
158 159 160
      ),
    };

Dan Field's avatar
Dan Field committed
161
    await tester.pumpWidget(MaterialApp(routes: routes));
162

163
    Navigator.pushNamed(containerKey1.currentContext!, '/settings');
164 165 166 167 168 169 170

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

    expect(find.text('Settings'), isOnstage);

    // Settings text is heroing to its new location
171 172 173 174 175
    Offset settingsOffset = tester.getTopLeft(find.text('Settings'));
    expect(settingsOffset.dx, greaterThan(0.0));
    expect(settingsOffset.dx, lessThan(100.0));
    expect(settingsOffset.dy, greaterThan(0.0));
    expect(settingsOffset.dy, lessThan(100.0));
176 177 178 179 180 181 182

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

    expect(find.text('Home'), findsNothing);
    expect(find.text('Settings'), isOnstage);

    // Drag from left edge to invoke the gesture.
183
    final TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0));
184
    await gesture.moveBy(const Offset(50.0, 0.0));
185 186 187 188 189 190 191
    await tester.pump();

    // Home is now visible.
    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings'), isOnstage);

    // Home page is sliding in from the left, no heroes.
192 193 194
    final Offset homeOffset = tester.getTopLeft(find.text('Home'));
    expect(homeOffset.dx, lessThan(0.0));
    expect(homeOffset.dy, 0.0);
195 196 197

    // Settings page is sliding off to the right, no heroes.
    settingsOffset = tester.getTopLeft(find.text('Settings'));
198 199
    expect(settingsOffset.dx, greaterThan(100.0));
    expect(settingsOffset.dy, 100.0);
200
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
201

202
  testWidgetsWithLeakTracking("Check back gesture doesn't start during transitions", (WidgetTester tester) async {
203 204
    final GlobalKey containerKey1 = GlobalKey();
    final GlobalKey containerKey2 = GlobalKey();
205
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
206 207
      '/': (_) => Scaffold(key: containerKey1, body: const Text('Home')),
      '/settings': (_) => Scaffold(key: containerKey2, body: const Text('Settings')),
208 209
    };

Dan Field's avatar
Dan Field committed
210
    await tester.pumpWidget(MaterialApp(routes: routes));
211

212
    Navigator.pushNamed(containerKey1.currentContext!, '/settings');
213 214 215 216 217 218 219 220 221 222

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

    // We are mid-transition, both pages are on stage.
    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings'), isOnstage);

    // Drag from left edge to invoke the gesture. (near bottom so we grab
    // the Settings page as it comes up).
223
    TestGesture gesture = await tester.startGesture(const Offset(5.0, 550.0));
224
    await gesture.moveBy(const Offset(500.0, 0.0));
225 226 227 228 229 230 231 232 233 234
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 1000));

    // The original forward navigation should have completed, instead of the
    // back gesture, since we were mid transition.
    expect(find.text('Home'), findsNothing);
    expect(find.text('Settings'), isOnstage);

    // Try again now that we're settled.
235
    gesture = await tester.startGesture(const Offset(5.0, 550.0));
236
    await gesture.moveBy(const Offset(500.0, 0.0));
237 238 239 240 241 242
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 1000));

    expect(find.text('Home'), isOnstage);
    expect(find.text('Settings'), findsNothing);
243
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
244 245

  // Tests bug https://github.com/flutter/flutter/issues/6451
246
  testWidgetsWithLeakTracking('Check back gesture with a persistent bottom sheet showing', (WidgetTester tester) async {
247 248
    final GlobalKey containerKey1 = GlobalKey();
    final GlobalKey containerKey2 = GlobalKey();
249
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
250 251
      '/': (_) => Scaffold(key: containerKey1, body: const Text('Home')),
      '/sheet': (_) => PersistentBottomSheetTest(key: containerKey2),
252 253
    };

Dan Field's avatar
Dan Field committed
254
    await tester.pumpWidget(MaterialApp(routes: routes));
255

256
    Navigator.pushNamed(containerKey1.currentContext!, '/sheet');
257 258 259 260 261 262 263

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

    expect(find.text('Home'), findsNothing);
    expect(find.text('Sheet'), isOnstage);

264 265 266 267 268 269 270
    // Drag from left edge to invoke the gesture. We should go back.
    TestGesture gesture = await tester.startGesture(const Offset(5.0, 100.0));
    await gesture.moveBy(const Offset(500.0, 0.0));
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

271
    Navigator.pushNamed(containerKey1.currentContext!, '/sheet');
272 273 274 275 276 277 278

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

    expect(find.text('Home'), findsNothing);
    expect(find.text('Sheet'), isOnstage);

279
    // Show the bottom sheet.
280
    final PersistentBottomSheetTestState sheet = containerKey2.currentState! as PersistentBottomSheetTestState;
281 282 283 284
    sheet.showBottomSheet();

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

285 286
    // Drag from left edge to invoke the gesture. Nothing should happen.
    gesture = await tester.startGesture(const Offset(5.0, 100.0));
287
    await gesture.moveBy(const Offset(500.0, 0.0));
288 289 290 291
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

292 293
    expect(find.text('Home'), findsNothing);
    expect(find.text('Sheet'), isOnstage);
294

295 296
    // Sheet did not call setState (since the gesture did nothing).
    expect(sheet.setStateCalled, isFalse);
297
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
298

299
  testWidgetsWithLeakTracking('Test completed future', (WidgetTester tester) async {
300
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
301 302
      '/': (_) => const Center(child: Text('home')),
      '/next': (_) => const Center(child: Text('next')),
303 304
    };

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

307
    final PageRoute<void> route = MaterialPageRoute<void>(
308
      settings: const RouteSettings(name: '/page'),
309
      builder: (BuildContext context) => const Center(child: Text('page')),
310 311 312
    );

    int popCount = 0;
313 314
    route.popped.whenComplete(() {
      popCount += 1;
315 316 317
    });

    int completeCount = 0;
318 319
    route.completed.whenComplete(() {
      completeCount += 1;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 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
    });

    expect(popCount, 0);
    expect(completeCount, 0);

    Navigator.push(tester.element(find.text('home')), route);

    expect(popCount, 0);
    expect(completeCount, 0);

    await tester.pump();

    expect(popCount, 0);
    expect(completeCount, 0);

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

    expect(popCount, 0);
    expect(completeCount, 0);

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

    expect(popCount, 0);
    expect(completeCount, 0);

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

    expect(popCount, 0);
    expect(completeCount, 0);

    Navigator.pop(tester.element(find.text('page')));

    expect(popCount, 0);
    expect(completeCount, 0);

    await tester.pump();

    expect(popCount, 1);
    expect(completeCount, 0);

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

    expect(popCount, 1);
    expect(completeCount, 0);

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

    expect(popCount, 1);
    expect(completeCount, 0);

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

    expect(popCount, 1);
    expect(completeCount, 1);
  });
375
}