fix: mock flame images (#332)

pull/336/head
arturplaczek 3 years ago committed by GitHub
parent 6ed62f37e8
commit 0a52583519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,5 @@
// ignore_for_file: invalid_use_of_protected_member // ignore_for_file: invalid_use_of_protected_member
import 'dart:typed_data';
import 'package:flame/assets.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
@ -13,8 +9,6 @@ import 'package:pinball_flame/pinball_flame.dart';
import '../../../helpers/helpers.dart'; import '../../../helpers/helpers.dart';
class _MockImages extends Mock implements Images {}
class _MockCallback extends Mock { class _MockCallback extends Mock {
void call(); void call();
} }
@ -24,13 +18,7 @@ void main() {
const animationDuration = 6; const animationDuration = 6;
setUp(() async { setUp(() async {
// TODO(arturplaczek): need to find for a better solution for loading image await mockFlameImages();
// or use original images from BonusAnimation.loadAssets()
final image = await decodeImageFromList(Uint8List.fromList(fakeImage));
final images = _MockImages();
when(() => images.fromCache(any())).thenReturn(image);
when(() => images.load(any())).thenAnswer((_) => Future.value(image));
Flame.images = images;
}); });
group('loads SpriteAnimationWidget correctly for', () { group('loads SpriteAnimationWidget correctly for', () {

@ -1,11 +1,8 @@
// ignore_for_file: prefer_const_constructors // ignore_for_file: prefer_const_constructors
import 'dart:async'; import 'dart:async';
import 'dart:typed_data';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/assets.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
@ -18,8 +15,6 @@ import 'package:pinball_ui/pinball_ui.dart';
import '../../../helpers/helpers.dart'; import '../../../helpers/helpers.dart';
class _MockImages extends Mock implements Images {}
class _MockGameBloc extends Mock implements GameBloc {} class _MockGameBloc extends Mock implements GameBloc {}
void main() { void main() {
@ -34,15 +29,9 @@ void main() {
); );
setUp(() async { setUp(() async {
gameBloc = _MockGameBloc(); await mockFlameImages();
// TODO(arturplaczek): need to find for a better solution for loading gameBloc = _MockGameBloc();
// image or use original images from BonusAnimation.loadAssets()
final image = await decodeImageFromList(Uint8List.fromList(fakeImage));
final images = _MockImages();
when(() => images.fromCache(any())).thenReturn(image);
when(() => images.load(any())).thenAnswer((_) => Future.value(image));
Flame.images = images;
whenListen( whenListen(
gameBloc, gameBloc,

@ -1,10 +1,8 @@
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/flame.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/select_character/select_character.dart'; import 'package:pinball/select_character/select_character.dart';
import 'package:pinball_theme/pinball_theme.dart';
import '../../../helpers/helpers.dart'; import '../../../helpers/helpers.dart';
@ -21,14 +19,12 @@ void main() {
late CharacterThemeCubit characterThemeCubit; late CharacterThemeCubit characterThemeCubit;
setUp(() async { setUp(() async {
Flame.images.prefix = ''; await mockFlameImages();
await Flame.images.load(const DashTheme().animation.keyName);
await Flame.images.load(const AndroidTheme().animation.keyName);
await Flame.images.load(const DinoTheme().animation.keyName);
await Flame.images.load(const SparkyTheme().animation.keyName);
game = _MockPinballGame(); game = _MockPinballGame();
gameFlowController = _MockGameFlowController(); gameFlowController = _MockGameFlowController();
characterThemeCubit = _MockCharacterThemeCubit(); characterThemeCubit = _MockCharacterThemeCubit();
whenListen( whenListen(
characterThemeCubit, characterThemeCubit,
const Stream<CharacterThemeState>.empty(), const Stream<CharacterThemeState>.empty(),

@ -5,70 +5,3 @@ import 'package:pinball/game/game.dart';
class FakeContact extends Fake implements Contact {} class FakeContact extends Fake implements Contact {}
class FakeGameEvent extends Fake implements GameEvent {} class FakeGameEvent extends Fake implements GameEvent {}
const fakeImage = <int>[
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
0x00,
0x00,
0x00,
0x0D,
0x49,
0x48,
0x44,
0x52,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x01,
0x08,
0x06,
0x00,
0x00,
0x00,
0x1F,
0x15,
0xC4,
0x89,
0x00,
0x00,
0x00,
0x0A,
0x49,
0x44,
0x41,
0x54,
0x78,
0x9C,
0x63,
0x00,
0x01,
0x00,
0x00,
0x05,
0x00,
0x01,
0x0D,
0x0A,
0x2D,
0xB4,
0x00,
0x00,
0x00,
0x00,
0x49,
0x45,
0x4E,
0x44,
0xAE,
];

@ -2,6 +2,7 @@ export 'builders.dart';
export 'fakes.dart'; export 'fakes.dart';
export 'forge2d.dart'; export 'forge2d.dart';
export 'key_testers.dart'; export 'key_testers.dart';
export 'mock_flame_images.dart';
export 'pump_app.dart'; export 'pump_app.dart';
export 'test_games.dart'; export 'test_games.dart';
export 'text_span.dart'; export 'text_span.dart';

@ -0,0 +1,92 @@
import 'dart:typed_data';
import 'package:flame/assets.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class _MockImages extends Mock implements Images {}
/// {@template mock_flame_images}
/// Mock for flame images instance.
///
/// Using real images blocks the tests, for this reason we need fake image
/// everywhere we use [Images.fromCache] or [Images.load].
/// {@endtemplate}
// TODO(arturplaczek): need to find for a better solution for loading image
// or use original images.
Future<void> mockFlameImages() async {
final image = await decodeImageFromList(Uint8List.fromList(_fakeImage));
final images = _MockImages();
when(() => images.fromCache(any())).thenReturn(image);
when(() => images.load(any())).thenAnswer((_) => Future.value(image));
Flame.images = images;
}
const _fakeImage = <int>[
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
0x00,
0x00,
0x00,
0x0D,
0x49,
0x48,
0x44,
0x52,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x01,
0x08,
0x06,
0x00,
0x00,
0x00,
0x1F,
0x15,
0xC4,
0x89,
0x00,
0x00,
0x00,
0x0A,
0x49,
0x44,
0x41,
0x54,
0x78,
0x9C,
0x63,
0x00,
0x01,
0x00,
0x00,
0x05,
0x00,
0x01,
0x0D,
0x0A,
0x2D,
0xB4,
0x00,
0x00,
0x00,
0x00,
0x49,
0x45,
0x4E,
0x44,
0xAE,
];

@ -1,5 +1,4 @@
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
@ -17,11 +16,8 @@ void main() {
late CharacterThemeCubit characterThemeCubit; late CharacterThemeCubit characterThemeCubit;
setUp(() async { setUp(() async {
Flame.images.prefix = ''; await mockFlameImages();
await Flame.images.load(const DashTheme().animation.keyName);
await Flame.images.load(const AndroidTheme().animation.keyName);
await Flame.images.load(const DinoTheme().animation.keyName);
await Flame.images.load(const SparkyTheme().animation.keyName);
characterThemeCubit = _MockCharacterThemeCubit(); characterThemeCubit = _MockCharacterThemeCubit();
whenListen( whenListen(
characterThemeCubit, characterThemeCubit,

Loading…
Cancel
Save