routes_test.dart 9.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2015 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';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

final List<String> results = <String>[];

11 12
Set<TestRoute> routes = new Set<TestRoute>();

13 14 15 16 17 18 19 20 21 22 23 24
class TestRoute extends Route<String> {
  TestRoute(this.name);
  final String name;

  List<OverlayEntry> get overlayEntries => _entries;

  List<OverlayEntry> _entries = <OverlayEntry>[];

  void log(String s) {
    results.add('$name: $s');
  }

Hixie's avatar
Hixie committed
25
  void install(OverlayEntry insertionPoint) {
26 27 28 29 30 31
    log('install');
    OverlayEntry entry = new OverlayEntry(
      builder: (BuildContext context) => new Container(),
      opaque: true
    );
    _entries.add(entry);
Hixie's avatar
Hixie committed
32
    navigator.overlay?.insert(entry, above: insertionPoint);
33
    routes.add(this);
34 35 36 37 38 39 40 41 42 43 44 45
  }

  void didPush() {
    log('didPush');
  }

  void didReplace(TestRoute oldRoute) {
    log('didReplace ${oldRoute.name}');
  }

  bool didPop(String result) {
    log('didPop $result');
46 47 48 49
    bool returnValue;
    if (returnValue = super.didPop(result))
      dispose();
    return returnValue;
50 51 52 53 54 55
  }

  void didPopNext(TestRoute nextRoute) {
    log('didPopNext ${nextRoute.name}');
  }

Hixie's avatar
Hixie committed
56 57
  void didChangeNext(TestRoute nextRoute) {
    log('didChangeNext ${nextRoute?.name}');
58 59 60 61 62 63
  }

  void dispose() {
    log('dispose');
    _entries.forEach((OverlayEntry entry) { entry.remove(); });
    _entries.clear();
64
    routes.remove(this);
65 66 67 68 69 70 71
  }

}

void runNavigatorTest(
  WidgetTester tester,
  NavigatorState host,
Hixie's avatar
Hixie committed
72
  NavigatorTransactionCallback test,
73 74 75
  List<String> expectations
) {
  expect(host, isNotNull);
Hixie's avatar
Hixie committed
76
  host.openTransaction(test);
77 78 79 80 81 82
  expect(results, equals(expectations));
  results.clear();
  tester.pump();
}

void main() {
83
  test('Route management - push, replace, pop', () {
84 85 86 87 88 89 90 91 92 93
    testWidgets((WidgetTester tester) {
      GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
      tester.pumpWidget(new Navigator(
        key: navigatorKey,
        onGenerateRoute: (_) => new TestRoute('initial')
      ));
      NavigatorState host = navigatorKey.currentState;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
94
        (NavigatorTransaction transaction) {
95 96 97 98
        },
        [
          'initial: install',
          'initial: didPush',
Hixie's avatar
Hixie committed
99
          'initial: didChangeNext null',
100 101 102 103 104 105
        ]
      );
      TestRoute second;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
106
        (NavigatorTransaction transaction) {
107 108 109 110 111
          transaction.push(second = new TestRoute('second'));
        },
        [
          'second: install',
          'second: didPush',
Hixie's avatar
Hixie committed
112 113
          'second: didChangeNext null',
          'initial: didChangeNext second',
114 115 116 117 118
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
119
        (NavigatorTransaction transaction) {
120 121 122 123 124
          transaction.push(new TestRoute('third'));
        },
        [
          'third: install',
          'third: didPush',
Hixie's avatar
Hixie committed
125 126
          'third: didChangeNext null',
          'second: didChangeNext third',
127 128 129 130 131
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
132
        (NavigatorTransaction transaction) {
133 134 135 136 137
          transaction.replace(oldRoute: second, newRoute: new TestRoute('two'));
        },
        [
          'two: install',
          'two: didReplace second',
Hixie's avatar
Hixie committed
138 139
          'two: didChangeNext third',
          'initial: didChangeNext two',
140 141 142 143 144 145
          'second: dispose',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
146
        (NavigatorTransaction transaction) {
147 148 149 150
          transaction.pop('hello');
        },
        [
          'third: didPop hello',
151
          'third: dispose',
152 153 154 155 156 157
          'two: didPopNext third',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
158
        (NavigatorTransaction transaction) {
159 160 161 162
          transaction.pop('good bye');
        },
        [
          'two: didPop good bye',
163
          'two: dispose',
164 165 166
          'initial: didPopNext two',
        ]
      );
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      tester.pumpWidget(new Container());
      expect(results, equals(['initial: dispose']));
      expect(routes.isEmpty, isTrue);
      results.clear();
    });
  });

  test('Route management - push, remove, pop', () {
    testWidgets((WidgetTester tester) {
      GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
      tester.pumpWidget(new Navigator(
        key: navigatorKey,
        onGenerateRoute: (_) => new TestRoute('first')
      ));
      NavigatorState host = navigatorKey.currentState;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
185
        (NavigatorTransaction transaction) {
186 187 188 189
        },
        [
          'first: install',
          'first: didPush',
Hixie's avatar
Hixie committed
190
          'first: didChangeNext null',
191 192 193 194 195 196
        ]
      );
      TestRoute second;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
197
        (NavigatorTransaction transaction) {
198 199 200 201 202
          transaction.push(second = new TestRoute('second'));
        },
        [
          'second: install',
          'second: didPush',
Hixie's avatar
Hixie committed
203 204
          'second: didChangeNext null',
          'first: didChangeNext second',
205 206 207 208 209
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
210
        (NavigatorTransaction transaction) {
211 212 213 214 215
          transaction.push(new TestRoute('third'));
        },
        [
          'third: install',
          'third: didPush',
Hixie's avatar
Hixie committed
216 217
          'third: didChangeNext null',
          'second: didChangeNext third',
218 219 220 221 222
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
223
        (NavigatorTransaction transaction) {
224 225 226 227 228 229 230 231 232
          transaction.removeRouteBefore(second);
        },
        [
          'first: dispose',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
233
        (NavigatorTransaction transaction) {
234 235 236 237 238 239 240 241 242 243 244
          transaction.pop('good bye');
        },
        [
          'third: didPop good bye',
          'third: dispose',
          'second: didPopNext third',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
245
        (NavigatorTransaction transaction) {
246 247 248 249 250
          transaction.push(new TestRoute('three'));
        },
        [
          'three: install',
          'three: didPush',
Hixie's avatar
Hixie committed
251 252
          'three: didChangeNext null',
          'second: didChangeNext three',
253 254 255 256 257 258
        ]
      );
      TestRoute four;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
259
        (NavigatorTransaction transaction) {
260 261 262 263 264
          transaction.push(four = new TestRoute('four'));
        },
        [
          'four: install',
          'four: didPush',
Hixie's avatar
Hixie committed
265 266
          'four: didChangeNext null',
          'three: didChangeNext four',
267 268 269 270 271
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
272
        (NavigatorTransaction transaction) {
273 274 275
          transaction.removeRouteBefore(four);
        },
        [
Hixie's avatar
Hixie committed
276
          'second: didChangeNext four',
277 278 279 280 281 282
          'three: dispose',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
283
        (NavigatorTransaction transaction) {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
          transaction.pop('the end');
        },
        [
          'four: didPop the end',
          'four: dispose',
          'second: didPopNext four',
        ]
      );
      tester.pumpWidget(new Container());
      expect(results, equals(['second: dispose']));
      expect(routes.isEmpty, isTrue);
      results.clear();
    });
  });

  test('Route management - push, replace, popUntil', () {
    testWidgets((WidgetTester tester) {
      GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
      tester.pumpWidget(new Navigator(
        key: navigatorKey,
        onGenerateRoute: (_) => new TestRoute('A')
      ));
      NavigatorState host = navigatorKey.currentState;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
310
        (NavigatorTransaction transaction) {
311 312 313 314
        },
        [
          'A: install',
          'A: didPush',
Hixie's avatar
Hixie committed
315
          'A: didChangeNext null',
316 317 318 319 320
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
321
        (NavigatorTransaction transaction) {
322 323 324 325 326
          transaction.push(new TestRoute('B'));
        },
        [
          'B: install',
          'B: didPush',
Hixie's avatar
Hixie committed
327 328
          'B: didChangeNext null',
          'A: didChangeNext B',
329 330 331 332 333 334
        ]
      );
      TestRoute routeC;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
335
        (NavigatorTransaction transaction) {
336 337 338 339 340
          transaction.push(routeC = new TestRoute('C'));
        },
        [
          'C: install',
          'C: didPush',
Hixie's avatar
Hixie committed
341 342
          'C: didChangeNext null',
          'B: didChangeNext C',
343 344 345 346 347 348
        ]
      );
      TestRoute routeB;
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
349
        (NavigatorTransaction transaction) {
350 351 352 353 354
          transaction.replaceRouteBefore(anchorRoute: routeC, newRoute: routeB = new TestRoute('b'));
        },
        [
          'b: install',
          'b: didReplace B',
Hixie's avatar
Hixie committed
355 356
          'b: didChangeNext C',
          'A: didChangeNext b',
357 358 359 360 361 362
          'B: dispose',
        ]
      );
      runNavigatorTest(
        tester,
        host,
Hixie's avatar
Hixie committed
363
        (NavigatorTransaction transaction) {
364 365 366 367 368 369 370 371 372 373 374 375
          transaction.popUntil(routeB);
        },
        [
          'C: didPop null',
          'C: dispose',
          'b: didPopNext C',
        ]
      );
      tester.pumpWidget(new Container());
      expect(results, equals(['A: dispose', 'b: dispose']));
      expect(routes.isEmpty, isTrue);
      results.clear();
376 377 378
    });
  });
}