Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for engine #47

Merged
merged 3 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/xebow/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ defmodule Xebow.Application do

use Application

@animation_type Xebow.Animation.types() |> List.first()
@animation Xebow.Animation.new(type: @animation_type)

def start(_type, _args) do
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
Expand Down Expand Up @@ -36,7 +39,7 @@ defmodule Xebow.Application do
# {Xebow.Worker, arg},
Xebow.HIDGadget,
Xebow.RGBMatrix,
{Xebow.Engine, [Xebow.RGBMatrix]},
{Xebow.Engine, {@animation, [Xebow.RGBMatrix]}},
Xebow.Keys
]
end
Expand Down
23 changes: 14 additions & 9 deletions lib/xebow/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ defmodule Xebow.Engine do
@doc """
Start the engine.

This function accepts the following arguments:
- `paintables` - A list of modules that implement the `Xebow.Paintable` behaviour.
This module registers its process globally and is expected to be started by
a supervisor.

This function accepts the following arguments as a tuple:
- `initial_animation` - The animation that plays when the engine starts.
- `paintables` - A list of modules to output `Xebow.Frame` to that implement
the `Xebow.Paintable` behavior. If you want to register your paintables
dynamically, set this to an empty list `[]`.
"""
@spec start_link(paintables :: list(module)) :: GenServer.on_start()
def start_link(paintables \\ []) do
GenServer.start_link(__MODULE__, {paintables}, name: __MODULE__)
@spec start_link({initial_animation :: Animation.t(), paintables :: list(module)}) ::
GenServer.on_start()
def start_link({initial_animation, paintables}) do
GenServer.start_link(__MODULE__, {initial_animation, paintables}, name: __MODULE__)
end

@doc """
Expand Down Expand Up @@ -83,18 +90,16 @@ defmodule Xebow.Engine do
# Server

@impl GenServer
def init({paintables}) do
def init({initial_animation, paintables}) do
send(self(), :get_next_frame)

[initial_animation_type | _] = Animation.types()
animation = Animation.new(type: initial_animation_type)
initial_state = %State{paintables: %{}}

state =
Enum.reduce(paintables, initial_state, fn paintable, state ->
add_paintable(paintable, state)
end)
|> set_animation(animation)
|> set_animation(initial_animation)

{:ok, state}
end
Expand Down
109 changes: 109 additions & 0 deletions test/engine_test.exs
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 \\ 127, 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()
{animation_2, frame_2} = solid_animation(127, 127, 127)

start_supervised!({Engine, {animation, [paintable]}})

:ok = Engine.play_animation(animation_2)

assert_receive {:frame, ^frame_2}
end

test "can register and unregister paintables", %{paintable: paintable} do
{animation, frame} = solid_animation()
{animation_2, frame_2} = 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(animation_2)

refute_receive {:frame, ^frame_2}
end
end
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ExUnit.start()
ExUnit.start(exclude: [:skip])
4 changes: 0 additions & 4 deletions test/xebow_test.exs

This file was deleted.