mocks_for_image_cache.dart 4.51 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
class TestImageInfo extends ImageInfo {
  const TestImageInfo(this.value, {
13 14 15 16
    required super.image,
    super.scale,
    super.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

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

  @override
29
  int get hashCode => Object.hash(value, image, scale, debugLabel);
30 31 32

  @override
  bool operator ==(Object other) {
33
    if (other.runtimeType != runtimeType) {
34
      return false;
35
    }
36 37 38 39 40 41
    return other is TestImageInfo
        && other.value == value
        && other.image.isCloneOf(image)
        && other.scale == scale
        && other.debugLabel == debugLabel;
  }
42 43
}

44
class TestImageProvider extends ImageProvider<int> {
45
  const TestImageProvider(this.key, this.imageValue, { required this.image });
46

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 loadImage(int key, ImageDecoderCallback 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(super.key, super.imageValue, { required super.image });
69 70

  @override
71
  ImageStreamCompleter loadImage(int key, ImageDecoderCallback 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
class ErrorImageProvider extends ImageProvider<ErrorImageProvider> {
88 89 90 91 92
  @override
  ImageStreamCompleter loadImage(ErrorImageProvider key, ImageDecoderCallback decode) {
    throw Error();
  }

93 94 95 96 97
  @override
  ImageStreamCompleter loadBuffer(ErrorImageProvider key, DecoderBufferCallback decode) {
    throw Error();
  }

98 99 100 101 102
  @override
  Future<ErrorImageProvider> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture<ErrorImageProvider>(this);
  }
}
103

104 105
class ObtainKeyErrorImageProvider extends ImageProvider<ObtainKeyErrorImageProvider> {
  @override
106
  ImageStreamCompleter loadImage(ObtainKeyErrorImageProvider key, ImageDecoderCallback decode) {
107 108 109
    throw Error();
  }

110 111 112 113 114
  @override
  ImageStreamCompleter loadBuffer(ObtainKeyErrorImageProvider key, DecoderBufferCallback decode) {
    throw UnimplementedError();
  }

115 116 117 118 119 120
  @override
  Future<ObtainKeyErrorImageProvider> obtainKey(ImageConfiguration configuration) {
    throw Error();
  }
}

121 122
class LoadErrorImageProvider extends ImageProvider<LoadErrorImageProvider> {
  @override
123
  ImageStreamCompleter loadImage(LoadErrorImageProvider key, ImageDecoderCallback decode) {
124 125 126
    throw Error();
  }

127 128 129 130 131
  @override
  ImageStreamCompleter loadBuffer(LoadErrorImageProvider key, DecoderBufferCallback decode) {
    throw UnimplementedError();
  }

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

138
class LoadErrorCompleterImageProvider extends ImageProvider<LoadErrorCompleterImageProvider> {
139
  @override
140
  ImageStreamCompleter loadImage(LoadErrorCompleterImageProvider key, ImageDecoderCallback decode) {
141
    final Completer<ImageInfo> completer = Completer<ImageInfo>.sync();
142 143
    completer.completeError(Error());
    return OneFrameImageStreamCompleter(completer.future);
144 145
  }

146
  @override
147 148 149 150 151
  Future<LoadErrorCompleterImageProvider> obtainKey(ImageConfiguration configuration) {
    return SynchronousFuture<LoadErrorCompleterImageProvider>(this);
  }
}

152
class TestImageStreamCompleter extends ImageStreamCompleter {
153
  void testSetImage(ui.Image image) {
154
    setImage(ImageInfo(image: image));
155 156
  }
}