blob: 7ef147919f5608fe04957e86a33f5cc88daba7f1 [file] [log] [blame]
// Copyright 2014 The Flutter 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 show Codec, FrameInfo, instantiateImageCodec;
import 'package:flutter/foundation.dart';
/// A [ui.Codec] implementation for testing that pre-fetches all the image
/// frames, and provides synchronous [getNextFrame] implementation.
/// This is useful for running in the test Zone, where it is tricky to receive
/// callbacks originating from the IO thread.
class FakeCodec implements ui.Codec {
FakeCodec._(this._frameCount, this._repetitionCount, this._frameInfos);
final int _frameCount;
final int _repetitionCount;
final List<ui.FrameInfo> _frameInfos;
int _nextFrame = 0;
/// Creates a FakeCodec from encoded image data.
/// Only call this method outside of the test zone.
static Future<FakeCodec> fromData(Uint8List data) async {
final ui.Codec codec = await ui.instantiateImageCodec(data);
final int frameCount = codec.frameCount;
final List<ui.FrameInfo> frameInfos = List<ui.FrameInfo>(frameCount);
for (int i = 0; i < frameCount; i += 1)
frameInfos[i] = await codec.getNextFrame();
return FakeCodec._(frameCount, codec.repetitionCount, frameInfos);
int get frameCount => _frameCount;
int get repetitionCount => _repetitionCount;
Future<ui.FrameInfo> getNextFrame() {
final SynchronousFuture<ui.FrameInfo> result =
_nextFrame = (_nextFrame + 1) % _frameCount;
return result;
void dispose() { }