Skip to content

Commit fd76e6b

Browse files
committed
fill out tests for Button
1 parent 0eccac2 commit fd76e6b

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

lib/scenic/component/button.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,30 @@ defmodule Scenic.Component.Button do
5757
|> text(text,
5858
fill: theme.text,
5959
translate: {width / 2, height * 0.7},
60-
text_align: :center
60+
text_align: :center,
61+
id: :title
6162
)
6263

6364
:left ->
6465
Graph.build(font: font, font_size: font_size)
6566
|> rrect({width, height, radius}, fill: theme.background, id: :btn)
66-
|> text(text, fill: theme.text, translate: {8, height * 0.7}, text_align: :left)
67+
|> text(
68+
text,
69+
fill: theme.text,
70+
translate: {8, height * 0.7},
71+
text_align: :left,
72+
id: :title
73+
)
6774

6875
:right ->
6976
Graph.build(font: font, font_size: font_size)
7077
|> rrect({width, height, radius}, fill: theme.background, id: :btn)
71-
|> text(text, fill: theme.text, translate: {width - 8, height * 0.7}, text_align: :right)
78+
|> text(text,
79+
fill: theme.text,
80+
translate: {width - 8, height * 0.7},
81+
text_align: :right,
82+
id: :title
83+
)
7284
end
7385

7486
# special case the dark and light themes to show an outline

test/scenic/component/button_test.exs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ defmodule Scenic.Component.ButtonTest do
88
doctest Scenic
99

1010
# alias Scenic.Component
11+
alias Scenic.Graph
12+
alias Scenic.Primitive
13+
alias Scenic.ViewPort
1114
alias Scenic.Component.Button
1215

16+
@state %{
17+
graph: Graph.build(),
18+
theme: Primitive.Style.Theme.preset(:primary),
19+
pressed: false,
20+
contained: false,
21+
align: :center,
22+
id: :test_id
23+
}
24+
1325
# ============================================================================
1426
# info
1527

@@ -33,10 +45,81 @@ defmodule Scenic.Component.ButtonTest do
3345

3446
test "init works with simple data" do
3547
{:ok, state} = Button.init("Button", styles: %{}, id: :button_id)
36-
%Scenic.Graph{} = state.graph
48+
%Graph{} = state.graph
3749
assert is_map(state.theme)
3850
assert state.pressed == false
3951
assert state.align == :center
4052
assert state.id == :button_id
4153
end
42-
end
54+
55+
test "init works with various alignments" do
56+
{:ok, state} = Button.init("Button", [])
57+
%Primitive{styles: %{text_align: :center}} = Graph.get!( state.graph, :title )
58+
59+
{:ok, state} = Button.init("Button", styles: %{alignment: :left}, id: :button_id)
60+
%Primitive{styles: %{text_align: :left}} = Graph.get!( state.graph, :title )
61+
62+
{:ok, state} = Button.init("Button", styles: %{alignment: :center}, id: :button_id)
63+
%Primitive{styles: %{text_align: :center}} = Graph.get!( state.graph, :title )
64+
65+
{:ok, state} = Button.init("Button", styles: %{alignment: :right}, id: :button_id)
66+
%Primitive{styles: %{text_align: :right}} = Graph.get!( state.graph, :title )
67+
end
68+
69+
# ============================================================================
70+
# handle_input
71+
72+
test "handle_input {:cursor_enter, _uid} sets contained" do
73+
{:noreply, state} = Button.handle_input( {:cursor_enter, 1}, %{}, %{@state | pressed: true} )
74+
assert state.contained
75+
# confirm the graph was pushed
76+
assert_receive( {:"$gen_cast", {:push_graph, _, _, _}} )
77+
end
78+
79+
test "handle_input {:cursor_exit, _uid} clears contained" do
80+
{:noreply, state} = Button.handle_input( {:cursor_exit, 1}, %{}, %{@state | pressed: true} )
81+
refute state.contained
82+
# confirm the graph was pushed
83+
assert_receive( {:"$gen_cast", {:push_graph, _, _, _}} )
84+
end
85+
86+
test "handle_input {:cursor_button, :press" do
87+
context = %ViewPort.Context{viewport: self()}
88+
{:noreply, state} = Button.handle_input( {:cursor_button, {:left, :press, nil, nil}}, context, %{@state | pressed: false, contained: true} )
89+
assert state.pressed
90+
91+
# confirm the input was captured
92+
assert_receive( {:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}} )
93+
# confirm the graph was pushed
94+
assert_receive( {:"$gen_cast", {:push_graph, _, _, _}} )
95+
end
96+
97+
test "handle_input {:cursor_button, :release" do
98+
context = %ViewPort.Context{viewport: self()}
99+
{:noreply, state} = Button.handle_input( {:cursor_button, {:left, :release, nil, nil}}, context, %{@state | pressed: true, contained: true} )
100+
refute state.pressed
101+
102+
# confirm the input was released
103+
assert_receive( {:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}} )
104+
105+
# confirm the graph was pushed
106+
assert_receive( {:"$gen_cast", {:push_graph, _, _, _}} )
107+
end
108+
109+
110+
test "handle_input {:cursor_button, :release sends a message if contained" do
111+
self = self()
112+
Process.put(:parent_pid, self)
113+
context = %ViewPort.Context{viewport: self()}
114+
{:noreply, _} = Button.handle_input( {:cursor_button, {:left, :release, nil, nil}}, context, %{@state | pressed: true, contained: true} )
115+
116+
# confirm the graph was pushed
117+
assert_receive( {:"$gen_cast", {:event, {:click, :test_id}, ^self}} )
118+
end
119+
120+
test "handle_input does nothing on unknown input" do
121+
context = %ViewPort.Context{viewport: self()}
122+
{:noreply, state} = Button.handle_input( :unknown, context, @state )
123+
assert state == @state
124+
end
125+
end

0 commit comments

Comments
 (0)