image_test_utils.dart 1.54 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
10
import 'package:flutter/painting.dart';
11 12 13 14 15 16 17 18

import 'image_data.dart';

class TestImageProvider extends ImageProvider<TestImageProvider> {
  TestImageProvider(this.testImage);

  final ui.Image testImage;

19
  final Completer<ImageInfo> _completer = Completer<ImageInfo>.sync();
20 21 22 23
  ImageConfiguration configuration;

  @override
  Future<TestImageProvider> obtainKey(ImageConfiguration configuration) {
24
    return SynchronousFuture<TestImageProvider>(this);
25 26 27 28 29 30 31 32 33 34
  }

  @override
  ImageStream resolve(ImageConfiguration config) {
    configuration = config;
    return super.resolve(configuration);
  }

  @override
  ImageStreamCompleter load(TestImageProvider key) =>
35
      OneFrameImageStreamCompleter(_completer.future);
36 37

  ImageInfo complete() {
38
    final ImageInfo imageInfo = ImageInfo(image: testImage);
39 40 41 42 43 44 45 46 47
    _completer.complete(imageInfo);
    return imageInfo;
  }

  @override
  String toString() => '${describeIdentity(this)}()';
}

Future<ui.Image> createTestImage() {
48 49
  final Completer<ui.Image> uiImage = Completer<ui.Image>();
  ui.decodeImageFromList(Uint8List.fromList(kTransparentImage), uiImage.complete);
50 51
  return uiImage.future;
}
52 53 54 55 56

class FakeImageConfiguration implements ImageConfiguration {
  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}