Skip to content

Commit

Permalink
DRY up FakeCodec (#149381)
Browse files Browse the repository at this point in the history
- dedupe the same class in two places
- renamed it to not clash with `FakeCodec` defined somewhere else
  • Loading branch information
kevmoo authored May 31, 2024
1 parent 6193615 commit de26ec8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import 'dart:ui' show Codec, FrameInfo, ImmutableBuffer;
import 'dart:ui' show Codec, ImmutableBuffer;

import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';

import '../image_data.dart';
import '../rendering/rendering_tester.dart';
import 'noop_codec.dart';

void main() {
TestRenderingFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -227,10 +228,6 @@ void main() {
debugNetworkImageHttpClientProvider = null;
}, skip: isBrowser); // [intended] Browser does not resolve images this way.

Future<Codec> decoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) async {
return FakeCodec();
}

test('Network image sets tag', () async {
const String url = 'http://test.png';
const int chunkSize = 8;
Expand All @@ -245,7 +242,7 @@ void main() {

const NetworkImage provider = NetworkImage(url);

final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, decoder) as MultiFrameImageStreamCompleter;
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, noopCodec) as MultiFrameImageStreamCompleter;

expect(completer.debugLabel, url);
});
Expand Down Expand Up @@ -319,19 +316,3 @@ class _FakeHttpClientResponse extends Fake implements HttpClientResponse {
return futureValue ?? futureValue as E; // Mirrors the implementation in Stream.
}
}

class FakeCodec implements Codec {
@override
void dispose() {}

@override
int get frameCount => throw UnimplementedError();

@override
Future<FrameInfo> getNextFrame() {
throw UnimplementedError();
}

@override
int get repetitionCount => throw UnimplementedError();
}
31 changes: 6 additions & 25 deletions packages/flutter/test/painting/image_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter_test/flutter_test.dart';
import '../image_data.dart';
import '../rendering/rendering_tester.dart';
import 'mocks_for_image_cache.dart';
import 'noop_codec.dart';

void main() {
TestRenderingFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -88,22 +89,18 @@ void main() {
final FileImage provider = FileImage(file);

expect(provider.loadBuffer(provider, (ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) async {
return Future<Codec>.value(FakeCodec());
return Future<Codec>.value(NoopCodec());
}), isA<MultiFrameImageStreamCompleter>());

expect(await error.future, isStateError);
});

Future<Codec> decoder(ImmutableBuffer buffer, {int? cacheWidth, int? cacheHeight, bool? allowUpscaling}) async {
return FakeCodec();
}

test('File image sets tag', () async {
final MemoryFileSystem fs = MemoryFileSystem();
final File file = fs.file('/blue.png')..createSync(recursive: true)..writeAsBytesSync(kBlueSquarePng);
final FileImage provider = FileImage(file);

final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, decoder) as MultiFrameImageStreamCompleter;
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, noopCodec) as MultiFrameImageStreamCompleter;

expect(completer.debugLabel, file.path);
});
Expand All @@ -112,7 +109,7 @@ void main() {
final Uint8List bytes = Uint8List.fromList(kBlueSquarePng);
final MemoryImage provider = MemoryImage(bytes);

final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, decoder) as MultiFrameImageStreamCompleter;
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(provider, noopCodec) as MultiFrameImageStreamCompleter;

expect(completer.debugLabel, 'MemoryImage(${describeIdentity(bytes)})');
});
Expand All @@ -121,7 +118,7 @@ void main() {
const String asset = 'images/blue.png';
final ExactAssetImage provider = ExactAssetImage(asset, bundle: _TestAssetBundle());
final AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration.empty);
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(key, decoder) as MultiFrameImageStreamCompleter;
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(key, noopCodec) as MultiFrameImageStreamCompleter;

expect(completer.debugLabel, asset);
});
Expand All @@ -131,7 +128,7 @@ void main() {
final ResizeImage provider = ResizeImage(MemoryImage(bytes), width: 40, height: 40);
final MultiFrameImageStreamCompleter completer = provider.loadBuffer(
await provider.obtainKey(ImageConfiguration.empty),
decoder,
noopCodec,
) as MultiFrameImageStreamCompleter;

expect(completer.debugLabel, 'MemoryImage(${describeIdentity(bytes)}) - Resized(40×40)');
Expand Down Expand Up @@ -167,22 +164,6 @@ void main() {
});
}

class FakeCodec implements Codec {
@override
void dispose() {}

@override
int get frameCount => throw UnimplementedError();

@override
Future<FrameInfo> getNextFrame() {
throw UnimplementedError();
}

@override
int get repetitionCount => throw UnimplementedError();
}

class _TestAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) async {
Expand Down
27 changes: 27 additions & 0 deletions packages/flutter/test/painting/noop_codec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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:ui';

class NoopCodec implements Codec {
@override
void dispose() {}

@override
int get frameCount => throw UnimplementedError();

@override
Future<FrameInfo> getNextFrame() => throw UnimplementedError();

@override
int get repetitionCount => throw UnimplementedError();
}

Future<Codec> noopCodec(
ImmutableBuffer buffer, {
int? cacheWidth,
int? cacheHeight,
bool? allowUpscaling,
}) async =>
NoopCodec();

0 comments on commit de26ec8

Please sign in to comment.