Skip to content

Commit 9ac4429

Browse files
committed
fill in Checkbox and RadioButton tests
1 parent 3385c9e commit 9ac4429

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed

test/scenic/component/button_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ defmodule Scenic.Component.ButtonTest do
4848
{:ok, state} = Button.init("Button", styles: %{}, id: :button_id)
4949
%Graph{} = state.graph
5050
assert is_map(state.theme)
51+
assert state.contained == false
5152
assert state.pressed == false
5253
assert state.align == :center
5354
assert state.id == :button_id
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#
2+
# Created by Boyd Multerer on September 18, 2018
3+
# Copyright © 2018 Kry10 Industries. All rights reserved.
4+
#
5+
6+
defmodule Scenic.Component.Input.CheckboxTest do
7+
use ExUnit.Case, async: true
8+
doctest Scenic
9+
10+
# alias Scenic.Component
11+
alias Scenic.Graph
12+
alias Scenic.Primitive
13+
alias Scenic.ViewPort
14+
alias Scenic.Component.Input.Checkbox
15+
16+
@state %{
17+
graph: Graph.build(),
18+
theme: Primitive.Style.Theme.preset(:primary),
19+
pressed: false,
20+
contained: false,
21+
checked: false,
22+
id: :test_id
23+
}
24+
25+
# ============================================================================
26+
# info
27+
28+
test "info works" do
29+
assert is_bitstring(Checkbox.info(:bad_data))
30+
assert Checkbox.info(:bad_data) =~ ":bad_data"
31+
end
32+
33+
# ============================================================================
34+
# verify
35+
36+
test "verify passes valid data" do
37+
assert Checkbox.verify({"Title", true}) == {:ok, {"Title", true}}
38+
assert Checkbox.verify({"Title", false}) == {:ok, {"Title", false}}
39+
end
40+
41+
test "verify fails invalid data" do
42+
assert Checkbox.verify(:banana) == :invalid_data
43+
end
44+
45+
# ============================================================================
46+
# init
47+
48+
test "init works with simple data" do
49+
{:ok, state} = Checkbox.init({"Title", false}, styles: %{}, id: :test_id)
50+
%Graph{} = state.graph
51+
assert is_map(state.theme)
52+
assert state.contained == false
53+
assert state.pressed == false
54+
assert state.checked == false
55+
assert state.id == :test_id
56+
57+
{:ok, state} = Checkbox.init({"Title", true}, styles: %{}, id: :test_id)
58+
assert state.checked == true
59+
end
60+
61+
# ============================================================================
62+
# handle_input
63+
64+
test "handle_input {:cursor_enter, _uid} sets contained" do
65+
{:noreply, state} = Checkbox.handle_input({:cursor_enter, 1}, %{}, %{@state | pressed: true})
66+
assert state.contained
67+
# confirm the graph was pushed
68+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
69+
end
70+
71+
test "handle_input {:cursor_exit, _uid} clears contained" do
72+
{:noreply, state} = Checkbox.handle_input({:cursor_exit, 1}, %{}, %{@state | pressed: true})
73+
refute state.contained
74+
# confirm the graph was pushed
75+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
76+
end
77+
78+
test "handle_input {:cursor_button, :press" do
79+
context = %ViewPort.Context{viewport: self()}
80+
81+
{:noreply, state} =
82+
Checkbox.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{
83+
@state
84+
| pressed: false,
85+
contained: true
86+
})
87+
88+
assert state.pressed
89+
90+
# confirm the input was captured
91+
assert_receive({:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}})
92+
# confirm the graph was pushed
93+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
94+
end
95+
96+
test "handle_input {:cursor_button, :release" do
97+
context = %ViewPort.Context{viewport: self()}
98+
99+
{:noreply, state} =
100+
Checkbox.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
101+
@state
102+
| pressed: true,
103+
contained: true
104+
})
105+
106+
refute state.pressed
107+
108+
# confirm the input was released
109+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
110+
111+
# confirm the graph was pushed
112+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
113+
end
114+
115+
test "handle_input {:cursor_button, :release sends a message if contained" do
116+
self = self()
117+
Process.put(:parent_pid, self)
118+
context = %ViewPort.Context{viewport: self()}
119+
120+
{:noreply, _} =
121+
Checkbox.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
122+
@state
123+
| pressed: true,
124+
contained: true
125+
})
126+
127+
# confirm the graph was pushed
128+
assert_receive({:"$gen_cast", {:event, {:value_changed, :test_id, true}, ^self}})
129+
end
130+
131+
test "handle_input does nothing on unknown input" do
132+
context = %ViewPort.Context{viewport: self()}
133+
{:noreply, state} = Checkbox.handle_input(:unknown, context, @state)
134+
assert state == @state
135+
end
136+
end
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#
2+
# Created by Boyd Multerer on September 18, 2018
3+
# Copyright © 2018 Kry10 Industries. All rights reserved.
4+
#
5+
6+
defmodule Scenic.Component.Input.RadioButtonTest do
7+
use ExUnit.Case, async: true
8+
doctest Scenic
9+
10+
# alias Scenic.Component
11+
alias Scenic.Graph
12+
alias Scenic.Primitive
13+
alias Scenic.ViewPort
14+
alias Scenic.Component.Input.RadioButton
15+
16+
@state %{
17+
graph: Graph.build(),
18+
theme: Primitive.Style.Theme.preset(:primary),
19+
pressed: false,
20+
contained: false,
21+
checked: false,
22+
id: :test_id
23+
}
24+
25+
# ============================================================================
26+
# info
27+
28+
test "info works" do
29+
assert is_bitstring(RadioButton.info(:bad_data))
30+
assert RadioButton.info(:bad_data) =~ ":bad_data"
31+
end
32+
33+
# ============================================================================
34+
# verify
35+
36+
test "verify passes valid data" do
37+
assert RadioButton.verify({"Title", :abc}) == {:ok, {"Title", :abc}}
38+
assert RadioButton.verify({"Title", :abc, true}) == {:ok, {"Title", :abc, true}}
39+
assert RadioButton.verify({"Title", :abc, false}) == {:ok, {"Title", :abc, false}}
40+
end
41+
42+
test "verify fails invalid data" do
43+
assert RadioButton.verify(:banana) == :invalid_data
44+
assert RadioButton.verify({"Title", :abc, :banana}) == :invalid_data
45+
end
46+
47+
# ============================================================================
48+
# init
49+
50+
test "init works with simple data" do
51+
{:ok, state} = RadioButton.init({"Title", :test_id}, styles: %{})
52+
%Graph{} = state.graph
53+
assert is_map(state.theme)
54+
assert state.contained == false
55+
assert state.pressed == false
56+
assert state.checked == false
57+
assert state.id == :test_id
58+
59+
{:ok, state} = RadioButton.init({"Title", :test_id, false}, styles: %{}, id: :test_id)
60+
assert state.checked == false
61+
62+
{:ok, state} = RadioButton.init({"Title", :test_id, true}, styles: %{}, id: :test_id)
63+
assert state.checked == true
64+
end
65+
66+
# ============================================================================
67+
# handle_input
68+
69+
test "handle_input {:cursor_enter, _uid} sets contained" do
70+
{:noreply, state} = RadioButton.handle_input({:cursor_enter, 1}, %{}, %{@state | pressed: true})
71+
assert state.contained
72+
# confirm the graph was pushed
73+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
74+
end
75+
76+
test "handle_input {:cursor_exit, _uid} clears contained" do
77+
{:noreply, state} = RadioButton.handle_input({:cursor_exit, 1}, %{}, %{@state | pressed: true})
78+
refute state.contained
79+
# confirm the graph was pushed
80+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
81+
end
82+
83+
test "handle_input {:cursor_button, :press" do
84+
context = %ViewPort.Context{viewport: self()}
85+
86+
{:noreply, state} =
87+
RadioButton.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{
88+
@state
89+
| pressed: false,
90+
contained: true
91+
})
92+
93+
assert state.pressed
94+
95+
# confirm the input was captured
96+
assert_receive({:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}})
97+
# confirm the graph was pushed
98+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
99+
end
100+
101+
test "handle_input {:cursor_button, :release" do
102+
context = %ViewPort.Context{viewport: self()}
103+
104+
{:noreply, state} =
105+
RadioButton.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
106+
@state
107+
| pressed: true,
108+
contained: true
109+
})
110+
111+
refute state.pressed
112+
113+
# confirm the input was released
114+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
115+
116+
# confirm the graph was pushed
117+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
118+
end
119+
120+
test "handle_input {:cursor_button, :release sends a message if contained" do
121+
self = self()
122+
Process.put(:parent_pid, self)
123+
context = %ViewPort.Context{viewport: self()}
124+
125+
{:noreply, _} =
126+
RadioButton.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
127+
@state
128+
| pressed: true,
129+
contained: true
130+
})
131+
132+
# confirm the graph was pushed
133+
assert_receive({:"$gen_cast", {:event, {:click, :test_id}, ^self}})
134+
end
135+
136+
test "handle_input does nothing on unknown input" do
137+
context = %ViewPort.Context{viewport: self()}
138+
{:noreply, state} = RadioButton.handle_input(:unknown, context, @state)
139+
assert state == @state
140+
end
141+
end

0 commit comments

Comments
 (0)