synchronous_future_test.dart 1.57 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/foundation.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9

void main() {
  test('SynchronousFuture control test', () async {
10
    final Future<int> future = SynchronousFuture<int>(42);
11

12
    int? result;
13
    future.then<void>((int value) { result = value; });
14 15 16 17

    expect(result, equals(42));
    result = null;

18
    final Future<int> futureWithTimeout = future.timeout(const Duration(milliseconds: 1));
19
    futureWithTimeout.then<void>((int value) { result = value; });
20 21 22 23 24
    expect(result, isNull);
    await futureWithTimeout;
    expect(result, equals(42));
    result = null;

25
    final Stream<int> stream = future.asStream();
26 27 28 29

    expect(await stream.single, equals(42));

    bool ranAction = false;
Ian Hickson's avatar
Ian Hickson committed
30
    final Future<int> completeResult = future.whenComplete(() { // ignore: void_checks, https://github.com/dart-lang/linter/issues/1675
31
      ranAction = true;
Ian Hickson's avatar
Ian Hickson committed
32
      // verify that whenComplete does NOT propagate its return value:
33
      return Future<int>.value(31);
34 35 36 37 38 39 40
    });

    expect(ranAction, isTrue);
    ranAction = false;

    expect(await completeResult, equals(42));

41
    Object? exception;
42
    try {
Ian Hickson's avatar
Ian Hickson committed
43
      await future.whenComplete(() { // ignore: void_checks, https://github.com/dart-lang/linter/issues/1675
44
        throw ArgumentError();
45 46 47 48 49 50
      });
      // Unreached.
      expect(false, isTrue);
    } catch (e) {
      exception = e;
    }
51
    expect(exception, isArgumentError);
52 53
  });
}