Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

fix: resolve image caching issue on flame_game template #143

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Unicorn extends PositionedEntity with HasGameRef {

@override
Future<void> onLoad() async {
final animation = await gameRef.loadSpriteAnimation(
Assets.images.unicornAnimation.path,
final animation = SpriteAnimation.fromFrameData(
gameRef.images.fromCache(Assets.images.unicornAnimation.path),
SpriteAnimationData.sequenced(
amount: 16,
stepTime: 0.1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:flame/cache.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flutter/painting.dart';
Expand All @@ -10,8 +11,9 @@ class VeryGoodFlameGame extends FlameGame {
required this.l10n,
required this.effectPlayer,
required this.textStyle,
required Images images,
}) {
images.prefix = '';
this.images = images;
}

final AppLocalizations l10n;
Expand Down
1 change: 1 addition & 0 deletions src/very_good_flame_game/lib/game/view/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class _GameViewState extends State<GameView> {
l10n: context.l10n,
effectPlayer: context.read<AudioCubit>().effectPlayer,
textStyle: textStyle,
images: context.read<PreloadCubit>().images,
);
return Stack(
children: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: cascade_invocations

import 'package:audioplayers/audioplayers.dart';
import 'package:flame/cache.dart';
import 'package:flame/extensions.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
Expand All @@ -18,29 +19,34 @@ class _VeryGoodFlameGame extends VeryGoodFlameGame {
required super.l10n,
required super.effectPlayer,
required super.textStyle,
required super.images,
});

@override
Future<void> onLoad() async {}
}

void main() {
final l10n = _MockAppLocalizations();
_VeryGoodFlameGame createFlameGame() {
return _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: _MockAudioPlayer(),
textStyle: const TextStyle(),
);
}

group('$CounterComponent', () {
late AppLocalizations l10n;

setUp(() {
l10n = _MockAppLocalizations();

when(() => l10n.counterText(any())).thenAnswer(
(invocation) => 'counterText: ${invocation.positionalArguments[0]}',
);
});

VeryGoodFlameGame createFlameGame() {
return _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: _MockAudioPlayer(),
textStyle: const TextStyle(),
images: Images(),
);
}

testWithGame(
'has all components',
createFlameGame,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// ignore_for_file: cascade_invocations

import 'dart:ui';

import 'package:audioplayers/audioplayers.dart';
import 'package:flame/components.dart';
import 'package:flame/cache.dart';
import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/painting.dart';
Expand All @@ -13,6 +15,8 @@ import 'package:very_good_flame_game/l10n/l10n.dart';

class _FakeAssetSource extends Fake implements AssetSource {}

class _MockImages extends Mock implements Images {}

class _MockAppLocalizations extends Mock implements AppLocalizations {}

class _MockAudioPlayer extends Mock implements AudioPlayer {}
Expand All @@ -22,6 +26,7 @@ class _VeryGoodFlameGame extends VeryGoodFlameGame {
required super.l10n,
required super.effectPlayer,
required super.textStyle,
required super.images,
});

@override
Expand All @@ -31,28 +36,37 @@ class _VeryGoodFlameGame extends VeryGoodFlameGame {
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

final l10n = _MockAppLocalizations();
final audioPlayer = _MockAudioPlayer();
final flameTester = FlameTester(
() => _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: audioPlayer,
textStyle: const TextStyle(),
),
);

group('TappingBehavior', () {
setUpAll(() {
late AppLocalizations l10n;
late AudioPlayer audioPlayer;
late Images images;

VeryGoodFlameGame createFlameGame() {
return _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: audioPlayer,
textStyle: const TextStyle(),
images: images,
);
}

setUpAll(() async {
registerFallbackValue(_FakeAssetSource());
});

setUp(() {
setUp(() async {
l10n = _MockAppLocalizations();
when(() => l10n.counterText(any())).thenReturn('counterText');

audioPlayer = _MockAudioPlayer();
when(() => audioPlayer.play(any())).thenAnswer((_) async {});

images = _MockImages();
final image = await _fakeImage();
when(() => images.fromCache(any())).thenReturn(image);
});

flameTester.testGameWidget(
FlameTester(createFlameGame).testGameWidget(
'when tapped, starts playing the animation',
setUp: (game, tester) async {
await game.ensureAdd(
Expand Down Expand Up @@ -81,3 +95,14 @@ void main() {
);
});
}

Future<Image> _fakeImage() async {
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
canvas.drawRect(
const Rect.fromLTWH(0, 0, 1, 1),
Paint()..color = const Color(0xFF000000),
);
final picture = recorder.endRecording();
return picture.toImage(1, 1);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: cascade_invocations

import 'package:audioplayers/audioplayers.dart';
import 'package:flame/cache.dart';
import 'package:flame/extensions.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/painting.dart';
Expand All @@ -10,15 +11,20 @@ import 'package:very_good_flame_game/game/entities/unicorn/behaviors/behaviors.d
import 'package:very_good_flame_game/game/game.dart';
import 'package:very_good_flame_game/l10n/l10n.dart';

class _FakeImage extends Fake implements Image {}

class _MockAppLocalizations extends Mock implements AppLocalizations {}

class _MockAudioPlayer extends Mock implements AudioPlayer {}

class _MockImages extends Mock implements Images {}

class _VeryGoodFlameGame extends VeryGoodFlameGame {
_VeryGoodFlameGame({
required super.l10n,
required super.effectPlayer,
required super.textStyle,
required super.images,
});

@override
Expand All @@ -28,20 +34,27 @@ class _VeryGoodFlameGame extends VeryGoodFlameGame {
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

final l10n = _MockAppLocalizations();
_VeryGoodFlameGame createFlameGame() {
return _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: _MockAudioPlayer(),
textStyle: const TextStyle(),
);
}

group('Unicorn', () {
late AppLocalizations l10n;
late Images images;

setUp(() {
l10n = _MockAppLocalizations();
images = _MockImages();

when(() => l10n.counterText(any())).thenReturn('counterText');
when(() => images.fromCache(any())).thenReturn(_FakeImage());
});

_VeryGoodFlameGame createFlameGame() {
return _VeryGoodFlameGame(
l10n: l10n,
effectPlayer: _MockAudioPlayer(),
textStyle: const TextStyle(),
images: images,
);
}

testWithGame(
'has all behaviors',
createFlameGame,
Expand Down
24 changes: 19 additions & 5 deletions src/very_good_flame_game/test/game/view/game_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:audioplayers/audioplayers.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flame/cache.dart';
import 'package:flame_audio/bgm.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -18,12 +20,19 @@ import '../../helpers/helpers.dart';

class _FakeAssetSource extends Fake implements AssetSource {}

class _FakeImage extends Fake implements ui.Image {}

class _MockAudioCubit extends MockCubit<AudioState> implements AudioCubit {}

class _MockAudioPlayer extends Mock implements AudioPlayer {}

class _MockImages extends Mock implements Images {}

class _MockBgm extends Mock implements Bgm {}

class _MockPreloadCubit extends MockCubit<PreloadState>
implements PreloadCubit {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
// https://github.com/material-foundation/flutter-packages/issues/286#issuecomment-1406343761
Expand All @@ -49,16 +58,21 @@ void main() {

group('GamePage', () {
late PreloadCubit preloadCubit;

setUp(() {
preloadCubit = MockPreloadCubit();
when(() => preloadCubit.audio).thenReturn(AudioCache(prefix: ''));
});
late Images images;

setUpAll(() {
registerFallbackValue(_FakeAssetSource());
});

setUp(() {
images = _MockImages();
when(() => images.fromCache(any())).thenReturn(_FakeImage());

preloadCubit = _MockPreloadCubit();
when(() => preloadCubit.audio).thenReturn(AudioCache(prefix: ''));
when(() => preloadCubit.images).thenReturn(images);
});

testWidgets('is routable', (tester) async {
await tester.runAsync(() async {
await tester.pumpApp(
Expand Down
Loading