Skip to content

Commit

Permalink
fix: #1966 unit test for Particles (#2097)
Browse files Browse the repository at this point in the history
fix: 🐛 Unit tests for particles #1966: Unit test for Particles
  • Loading branch information
ShwetaChauhan18 authored Oct 20, 2022
1 parent 8a136c9 commit 59bd7eb
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/flame/test/particles/circle_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:canvas_test/canvas_test.dart';
import 'package:flame/components.dart';
import 'package:flame/particles.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockParticle extends Mock implements CircleParticle {}

void main() {
group('CircleParticle', () {
test('Should render this Particle to given Canvas', () {
final particle = MockParticle();

final canvas = MockCanvas();
ParticleSystemComponent(particle: particle).render(canvas);

verify(() => particle.render(canvas)).called(1);
});

testWithFlameGame(
'Consider composing this with other Particle to achieve needed effects',
(game) async {
final childParticle = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 2,
);

final component = ParticleSystemComponent(
particle: childParticle,
);

game.add(component);
await game.ready();
game.update(1);

expect(childParticle.progress, 0.5);
});
});
}
82 changes: 82 additions & 0 deletions packages/flame/test/particles/computed_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:flame/particles.dart';
import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('ComputedParticle', () {
testWithFlameGame(
'Particle container which delegates rendering particle on each frame',
(game) async {
final cellSize = game.size / 5.0;
final halfCellSize = cellSize / 2;

final particle = ComputedParticle(
renderer: (canvas, particle) {
canvas.drawCircle(
Offset.zero,
particle.progress * halfCellSize.x,
Paint()
..color = Color.lerp(
Colors.red,
Colors.blue,
particle.progress,
)!,
);
},
lifespan: 2,
);

final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);
expect(particle.progress, 0.5);

game.update(1);
expect(particle.progress, 1);
});

testWithFlameGame('Particle to use custom tweening', (game) async {
final cellSize = game.size / 5.0;
final halfCellSize = cellSize / 2;
final steppedTween = StepTween(begin: 0, end: 5);

final particle = ComputedParticle(
lifespan: 2,
renderer: (canvas, particle) {
const steps = 5;
final steppedProgress =
steppedTween.transform(particle.progress) / steps;

canvas.drawCircle(
Offset.zero,
(1 - steppedProgress) * halfCellSize.x,
Paint()
..color = Color.lerp(
Colors.red,
Colors.blue,
steppedProgress,
)!,
);
},
);

final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);
expect(particle.progress, 0.5);

game.update(1);
expect(particle.progress, 1);
});
});
}
37 changes: 37 additions & 0 deletions packages/flame/test/particles/curved_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame/src/particles/curved_particle.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('CurvedParticle', () {
test('A Particle which applies certain Curve', () async {
final particle = CurvedParticle();

expect(particle.curve, Curves.linear);
});

test('A Particle which applies certain Curve', () async {
final particle = CurvedParticle(curve: Curves.decelerate);

expect(particle.curve, Curves.decelerate);
});

testWithFlameGame(
'A Particle which applies certain Curve for easing or other purposes'
' to its progress getter.', (game) async {
final particle = CurvedParticle(lifespan: 2);
final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);

expect(particle.curve, Curves.linear);
expect(particle.progress, 0.5);
});
});
}
35 changes: 35 additions & 0 deletions packages/flame/test/particles/moving_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flame/extensions.dart';
import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame/src/particles/circle_particle.dart';
import 'package:flame/src/particles/moving_particle.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('MovingParticle', () {
testWithFlameGame(
'Particle which is moving from one predefined position to another one',
(game) async {
final childParticle = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 2,
);

final particle = MovingParticle(
from: Vector2(-20, -20),
to: Vector2(20, 20),
child: childParticle,
);

final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);
expect(particle.progress, 1.0);
});
});
}
42 changes: 42 additions & 0 deletions packages/flame/test/particles/scaled_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:math';

import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame/src/particles/computed_particle.dart';
import 'package:flame/src/particles/rotating_particle.dart';
import 'package:flame/src/particles/scaled_particle.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('ScaledParticle', () {
testWithFlameGame(
'A particle which rotates its child over the lifespan between two '
'given bounds in radians', (game) async {
final paint = Paint()..color = Colors.red;
final rect = ComputedParticle(
renderer: (canvas, _) => canvas.drawRect(
Rect.fromCenter(center: Offset.zero, width: 10, height: 10),
paint,
),
);

final particle = ScaledParticle(
lifespan: 2,
child: rect.rotating(to: pi / 2),
);

final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);

expect(particle.scale, 1.0);
expect(particle.child, isInstanceOf<RotatingParticle>());
expect(particle.child.progress, 0.5);
});
});
}
45 changes: 45 additions & 0 deletions packages/flame/test/particles/sprite_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:canvas_test/canvas_test.dart';
import 'package:flame/extensions.dart';
import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame/src/particles/sprite_particle.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockParticle extends Mock implements SpriteParticle {}

Future<void> main() async {
// Generate an image
final image = await generateImage();

group('SpriteParticle', () {
test('Should render this Particle to given Canvas', () {
final particle = MockParticle();

final canvas = MockCanvas();
ParticleSystemComponent(particle: particle).render(canvas);

verify(() => particle.render(canvas)).called(1);
});

final sprite = Sprite(image);
testWithFlameGame(
'SpriteParticle allows easily embed Flames Sprite into the effect',
(game) async {
final particle = SpriteParticle(
sprite: sprite,
size: Vector2(50, 50),
lifespan: 2,
);

final component = ParticleSystemComponent(
particle: particle,
);

game.add(component);
await game.ready();
game.update(1);
expect(particle.progress, 0.5);
});
});
}

0 comments on commit 59bd7eb

Please sign in to comment.