synchronous_future_test.dart 1.35 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 '../flutter_test_alternative.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;
30
    final Future<int> completeResult = future.whenComplete(() {
31
      ranAction = true;
32
      return Future<int>.value(31);
33 34 35 36 37 38 39
    });

    expect(ranAction, isTrue);
    ranAction = false;

    expect(await completeResult, equals(42));

40
    Object? exception;
41 42
    try {
      await future.whenComplete(() {
43
        throw ArgumentError();
44 45 46 47 48 49
      });
      // Unreached.
      expect(false, isTrue);
    } catch (e) {
      exception = e;
    }
50
    expect(exception, isArgumentError);
51 52
  });
}