synchronous_future_test.dart 1.36 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 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 'dart:async';

import 'package:flutter/foundation.dart';
8
import '../flutter_test_alternative.dart';
9 10 11

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

    int result;
15
    future.then<void>((int value) { result = value; });
16 17 18 19

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

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

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

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

    bool ranAction = false;
32
    final Future<int> completeResult = future.whenComplete(() {
33
      ranAction = true;
34
      return Future<int>.value(31);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    });

    expect(ranAction, isTrue);
    ranAction = false;

    expect(await completeResult, equals(42));

    Object exception;
    try {
      await future.whenComplete(() {
        throw null;
      });
      // Unreached.
      expect(false, isTrue);
    } catch (e) {
      exception = e;
    }
    expect(exception, isNullThrownError);
  });
}