-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also supply engine with an initial animation.
- Loading branch information
Showing
3 changed files
with
127 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
defmodule Xebow.EngineTest do | ||
use ExUnit.Case | ||
|
||
alias Xebow.{Animation, Engine, Frame} | ||
|
||
# Creates a Xebow.Paintable module that emits frames to the test suite process. | ||
defp paintable(%{test: test_name}) do | ||
process = self() | ||
module_name = String.to_atom("#{test_name}-paintable") | ||
|
||
Module.create( | ||
module_name, | ||
quote do | ||
def get_paint_fn do | ||
fn frame -> | ||
send(unquote(process), {:frame, frame}) | ||
end | ||
end | ||
end, | ||
Macro.Env.location(__ENV__) | ||
) | ||
|
||
%{paintable: module_name} | ||
end | ||
|
||
# Creates a single pixel, single frame animation. | ||
defp solid_animation(red \\ 255, green \\ 128, blue \\ 0) do | ||
pixels = [{0, 0}] | ||
color = Chameleon.RGB.new(red, green, blue) | ||
frame = Frame.solid_color(pixels, color) | ||
|
||
animation = | ||
Animation.new( | ||
type: Animation.Static, | ||
frames: [frame], | ||
delay_ms: 10, | ||
loop: 1 | ||
) | ||
|
||
{animation, frame} | ||
end | ||
|
||
setup [:paintable] | ||
|
||
test "renders a solid animation", %{paintable: paintable} do | ||
{animation, frame} = solid_animation() | ||
|
||
start_supervised!({Engine, {animation, [paintable]}}) | ||
|
||
assert_receive {:frame, ^frame} | ||
end | ||
|
||
test "renders a multi-frame, multi-pixel animation", %{paintable: paintable} do | ||
pixels = [ | ||
{0, 0}, | ||
{0, 1}, | ||
{1, 0}, | ||
{1, 1} | ||
] | ||
|
||
frames = [ | ||
Frame.solid_color(pixels, Chameleon.Keyword.new("red")), | ||
Frame.solid_color(pixels, Chameleon.Keyword.new("green")), | ||
Frame.solid_color(pixels, Chameleon.Keyword.new("blue")), | ||
Frame.solid_color(pixels, Chameleon.Keyword.new("white")) | ||
] | ||
|
||
animation = | ||
Animation.new( | ||
type: Animation.Static, | ||
frames: frames, | ||
delay_ms: 10, | ||
loop: 1 | ||
) | ||
|
||
start_supervised!({Engine, {animation, [paintable]}}) | ||
|
||
Enum.each(frames, fn frame -> | ||
assert_receive {:frame, ^frame} | ||
end) | ||
end | ||
|
||
test "can play a different animation", %{paintable: paintable} do | ||
{animation, _frame} = solid_animation() | ||
{animation2, frame2} = solid_animation(127, 127, 127) | ||
|
||
start_supervised!({Engine, {animation, [paintable]}}) | ||
|
||
:ok = Engine.play_animation(animation2) | ||
|
||
assert_receive {:frame, ^frame2} | ||
end | ||
|
||
test "can register and unregister paintables", %{paintable: paintable} do | ||
{animation, frame} = solid_animation() | ||
{animation2, frame2} = solid_animation(127, 127, 127) | ||
|
||
start_supervised!({Engine, {animation, []}}) | ||
|
||
:ok = Engine.register_paintable(paintable) | ||
|
||
assert_receive {:frame, ^frame} | ||
|
||
:ok = Engine.unregister_paintable(paintable) | ||
:ok = Engine.play_animation(animation2) | ||
|
||
refute_receive {:frame, ^frame2} | ||
end | ||
end |