mocks_for_image_cache.dart 4.17 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// 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:ui' as ui show Image;

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

11 12 13 14 15 16
class TestImageInfo extends ImageInfo {
  const TestImageInfo(this.value, {
    required ui.Image image,
    double scale = 1.0,
    String? debugLabel,
  }) : super(image: image, scale: scale, debugLabel: debugLabel);
17

18 19 20
  final int value;

  @override
Ian Hickson's avatar
Ian Hickson committed
21
  String toString() => '${objectRuntimeType(this, 'TestImageInfo')}($value)';
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  @override
  TestImageInfo clone() {
    return TestImageInfo(value, image: image.clone(), scale: scale, debugLabel: debugLabel);
  }

  @override
  int get hashCode => hashValues(value, image, scale, debugLabel);

  @override
  bool operator ==(Object other) {
    if (other.runtimeType != runtimeType)
      return false;
    return other is TestImageInfo
        && other.value == value
        && other.image.isCloneOf(image)
        && other.scale == scale
        && other.debugLabel == debugLabel;
  }
41 42
}

43
class TestImageProvider extends ImageProvider<int> {
44
  const TestImageProvider(this.key, this.imageValue, { required this.image })
45 46
      : assert(image != null);

47
  final int key;
48
  final int imageValue;
49
  final ui.Image image;
50 51

  @override
52
  Future<int> obtainKey(ImageConfiguration configuration) {
53
    return Future<int>.value(key);
54 55 56
  }

  @override
57
  ImageStreamCompleter load(int key, DecoderCallback decode) {
58
    return OneFrameImageStreamCompleter(
59
      SynchronousFuture<ImageInfo>(TestImageInfo(imageValue, image: image.clone())),
60
    );
61 62 63
  }

  @override
Ian Hickson's avatar
Ian Hickson committed
64
  String toString() => '${objectRuntimeType(this, 'TestImageProvider')}($key, $imageValue)';
65
}
66

67
class FailingTestImageProvider extends TestImageProvider {
68
  const FailingTestImageProvider(int key, int imageValue, { required ui.Image image }) : super(key, imageValue, image: image);
69 70

  @override
71
  ImageStreamCompleter load(int key, DecoderCallback decode) {
72 73 74 75
    return OneFrameImageStreamCompleter(Future<ImageInfo>.sync(() => Future<ImageInfo>.error('loading failed!')));
  }
}

76
Future<ImageInfo> extractOneFrame(ImageStream stream) {
77
  final Completer<ImageInfo> completer = Completer<ImageInfo>();
78
  late ImageStreamListener listener;
79
  listener = ImageStreamListener((ImageInfo image, bool synchronousCall) {
80 81
    completer.complete(image);
    stream.removeListener(listener);
82
  });
83 84
  stream.addListener(listener);
  return completer.future;
85
}
86

87 88
class ErrorImageProvider extends ImageProvider<ErrorImageProvider> {
  @override
89
  ImageStreamCompleter load(ErrorImageProvider key, DecoderCallback decode) {
90 91 92 93 94 95 96 97
    throw Error();
  }

  @override
  Future<ErrorImageProvider> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture<ErrorImageProvider>(this);
  }
}
98

99 100
class ObtainKeyErrorImageProvider extends ImageProvider<ObtainKeyErrorImageProvider> {
  @override
101
  ImageStreamCompleter load(ObtainKeyErrorImageProvider key, DecoderCallback decode) {
102 103 104 105 106 107 108 109 110
    throw Error();
  }

  @override
  Future<ObtainKeyErrorImageProvider> obtainKey(ImageConfiguration configuration) {
    throw Error();
  }
}

111 112
class LoadErrorImageProvider extends ImageProvider<LoadErrorImageProvider> {
  @override
113
  ImageStreamCompleter load(LoadErrorImageProvider key, DecoderCallback decode) {
114 115 116
    throw Error();
  }

117
  @override
118 119 120 121 122
  Future<LoadErrorImageProvider> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture<LoadErrorImageProvider>(this);
  }
}

123
class LoadErrorCompleterImageProvider extends ImageProvider<LoadErrorCompleterImageProvider> {
124
  @override
125
  ImageStreamCompleter load(LoadErrorCompleterImageProvider key, DecoderCallback decode) {
126
    final Completer<ImageInfo> completer = Completer<ImageInfo>.sync();
127 128
    completer.completeError(Error());
    return OneFrameImageStreamCompleter(completer.future);
129 130
  }

131
  @override
132 133 134 135 136
  Future<LoadErrorCompleterImageProvider> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture<LoadErrorCompleterImageProvider>(this);
  }
}

137
class TestImageStreamCompleter extends ImageStreamCompleter {
138
  void testSetImage(ui.Image image) {
139
    setImage(ImageInfo(image: image));
140 141
  }
}