From a71afb76233b37b3d2a558569d65d9529de49516 Mon Sep 17 00:00:00 2001 From: arturplaczek <33895544+arturplaczek@users.noreply.github.com> Date: Wed, 27 Apr 2022 22:41:58 +0200 Subject: [PATCH] fix: use fake image instead of assets in tests (#243) * chore: add fake image to fakes * fix: update broken tests * fix: add TODOs --- .../view/widgets/bonus_animation_test.dart | 36 +++------- test/game/view/widgets/game_hud_test.dart | 21 +++++- test/helpers/fakes.dart | 67 +++++++++++++++++++ 3 files changed, 96 insertions(+), 28 deletions(-) diff --git a/test/game/view/widgets/bonus_animation_test.dart b/test/game/view/widgets/bonus_animation_test.dart index aa5a5b83..11e249c7 100644 --- a/test/game/view/widgets/bonus_animation_test.dart +++ b/test/game/view/widgets/bonus_animation_test.dart @@ -1,11 +1,14 @@ // ignore_for_file: invalid_use_of_protected_member +import 'dart:typed_data'; import 'dart:ui' as ui; 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'; +import 'package:pinball/game/game.dart'; import 'package:pinball/game/view/widgets/bonus_animation.dart'; import 'package:pinball_flame/pinball_flame.dart'; @@ -23,7 +26,13 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); setUp(() async { - await Future.wait(BonusAnimation.loadAssets()); + // TODO(arturplaczek): need to find for a better solution for loading 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; }); group('loads SpriteAnimationWidget correctly for', () { @@ -101,31 +110,6 @@ void main() { }); }); - testWidgets('called onCompleted callback at the end of animation ', - (tester) async { - final callback = MockCallback(); - - await tester.runAsync(() async { - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: BonusAnimation.dashNest( - onCompleted: callback.call, - ), - ), - ), - ); - - await tester.pump(); - - await Future.delayed(const Duration(seconds: 4)); - - await tester.pump(); - - verify(callback.call).called(1); - }); - }); - testWidgets('called onCompleted once when animation changed', (tester) async { final callback = MockCallback(); final secondAnimation = BonusAnimation.sparkyTurboCharge( diff --git a/test/game/view/widgets/game_hud_test.dart b/test/game/view/widgets/game_hud_test.dart index f8307b05..fe8bd092 100644 --- a/test/game/view/widgets/game_hud_test.dart +++ b/test/game/view/widgets/game_hud_test.dart @@ -1,17 +1,27 @@ // ignore_for_file: prefer_const_constructors import 'dart:async'; +import 'dart:typed_data'; +import 'dart:ui' as ui; 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_bloc/flutter_bloc.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/l10n/l10n.dart'; -import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_components/pinball_components.dart' hide Assets; + import '../../../helpers/helpers.dart'; +class MockImages extends Mock implements Images {} + +class MockImage extends Mock implements ui.Image {} + void main() { group('GameHud', () { late GameBloc gameBloc; @@ -24,7 +34,14 @@ void main() { setUp(() async { gameBloc = MockGameBloc(); - await Future.wait(BonusAnimation.loadAssets()); + + // TODO(arturplaczek): need to find for a better solution for loading + // 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( gameBloc, diff --git a/test/helpers/fakes.dart b/test/helpers/fakes.dart index 706733a1..d782ede4 100644 --- a/test/helpers/fakes.dart +++ b/test/helpers/fakes.dart @@ -5,3 +5,70 @@ import 'package:pinball/game/game.dart'; class FakeContact extends Fake implements Contact {} class FakeGameEvent extends Fake implements GameEvent {} + +const fakeImage = [ + 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, +];