Skip to content

Commit 82ff5f7

Browse files
committed
filling out component tests
1 parent 9ac4429 commit 82ff5f7

File tree

8 files changed

+356
-22
lines changed

8 files changed

+356
-22
lines changed

lib/scenic/component/input/dropdown.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Scenic.Component.Input.Dropdown do
1111
alias Scenic.Primitive.Style.Theme
1212
import Scenic.Primitives
1313

14-
# import IEx
14+
import IEx
1515

1616
@default_width 160
1717
@default_height 30
@@ -43,12 +43,12 @@ defmodule Scenic.Component.Input.Dropdown do
4343

4444
# --------------------------------------------------------
4545
def verify({items, initial} = data) when is_list(items) do
46-
Enum.all?(items, &verify_item(&1)) &&
47-
Enum.find_value(items, false, fn {_, id} -> id == initial end)
48-
|> case do
49-
true -> {:ok, data}
50-
_ -> :invalid_data
51-
end
46+
(Enum.all?(items, &verify_item(&1)) &&
47+
Enum.find_value(items, false, fn {_, id} -> id == initial end))
48+
|> case do
49+
true -> {:ok, data}
50+
_ -> :invalid_data
51+
end
5252
end
5353

5454
def verify(_), do: :invalid_data
@@ -269,7 +269,7 @@ defmodule Scenic.Component.Input.Dropdown do
269269
# push to the viewport
270270
|> push_graph()
271271

272-
{:continue, %{state | down: false, graph: graph}}
272+
{:noreply, %{state | down: false, graph: graph}}
273273
end
274274

275275
# --------------------------------------------------------

lib/scenic/component/input/radio_button.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ defmodule Scenic.Component.Input.RadioButton do
6767
{:ok, state}
6868
end
6969

70-
# --------------------------------------------------------
71-
def handle_cast({:set_value, new_value}, state) do
72-
state = Map.put(state, :checked, new_value)
73-
graph = update_graph(state)
74-
{:noreply, %{state | graph: graph}}
75-
end
70+
# # --------------------------------------------------------
71+
# def handle_cast({:set_value, new_value}, state) do
72+
# state = Map.put(state, :checked, new_value)
73+
# graph = update_graph(state)
74+
# {:noreply, %{state | graph: graph}}
75+
# end
7676

7777
# --------------------------------------------------------
7878
def handle_cast({:set_to_msg, set_id}, %{id: id} = state) do

lib/scenic/component/input/radio_group.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ defmodule Scenic.Component.Input.RadioGroup do
9090
{:ok, state}
9191
end
9292

93-
# --------------------------------------------------------
94-
def handle_cast({:set_value, new_value}, state) do
95-
{:noreply, %{state | value: new_value}}
96-
end
93+
# # --------------------------------------------------------
94+
# def handle_cast({:set_value, new_value}, state) do
95+
# {:noreply, %{state | value: new_value}}
96+
# end
9797

9898
# ============================================================================
9999

lib/scenic/component/input/text_field.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ defmodule Scenic.Component.Input.TextField do
8585

8686
# theme is passed in as an inherited style
8787
theme =
88-
(styles[:theme] || Theme.preset_caret(:dark))
88+
(styles[:theme] || Theme.preset(:dark))
8989
|> Theme.normalize()
9090

9191
# get the text_field specific styles

test/scenic/component/input/checkbox_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule Scenic.Component.Input.CheckboxTest do
124124
contained: true
125125
})
126126

127-
# confirm the graph was pushed
127+
# confirm the event was sent
128128
assert_receive({:"$gen_cast", {:event, {:value_changed, :test_id, true}, ^self}})
129129
end
130130

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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.DropdownTest 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.Dropdown
15+
16+
@items [{"a", 1}, {"b", 2}]
17+
@initial_item 2
18+
@data {@items, @initial_item}
19+
20+
@state %{
21+
graph: Graph.build(),
22+
selected_id: @initial_item,
23+
theme: Primitive.Style.Theme.preset(:primary),
24+
id: :test_id,
25+
down: false,
26+
hover_id: nil,
27+
items: @items,
28+
drop_time: 0,
29+
rotate_caret: 0
30+
}
31+
32+
@button_id :__dropbox_btn__
33+
34+
# ============================================================================
35+
# info
36+
37+
test "info works" do
38+
assert is_bitstring(Dropdown.info(:bad_data))
39+
assert Dropdown.info(:bad_data) =~ ":bad_data"
40+
end
41+
42+
# ============================================================================
43+
# verify
44+
45+
test "verify passes valid data" do
46+
assert Dropdown.verify(@data) == {:ok, @data}
47+
end
48+
49+
test "verify fails invalid data" do
50+
assert Dropdown.verify(:banana) == :invalid_data
51+
52+
# invalid item in list
53+
data = {[{:a, 1}, {"b", 2}], 2}
54+
assert Dropdown.verify(data) == :invalid_data
55+
56+
# selected is not in list
57+
data = {[{"a", 1}, {"b", 2}], 3}
58+
assert Dropdown.verify(data) == :invalid_data
59+
end
60+
61+
# ============================================================================
62+
# init
63+
64+
test "init works with simple data" do
65+
{:ok, state} = Dropdown.init(@data, styles: %{}, id: :test_id)
66+
%Graph{} = state.graph
67+
assert state.selected_id == @initial_item
68+
assert is_map(state.theme)
69+
assert state.down == false
70+
assert state.hover_id == nil
71+
assert state.items == @items
72+
assert state.id == :test_id
73+
end
74+
75+
# ============================================================================
76+
# handle_input - up
77+
78+
test "handle_input {:cursor_enter, _uid} - up" do
79+
{:noreply, state} =
80+
Dropdown.handle_input({:cursor_enter, 1}, %{id: 123}, %{@state | down: false})
81+
82+
assert state.hover_id == 123
83+
end
84+
85+
test "handle_input {:cursor_exit, _uid} - up" do
86+
{:noreply, state} =
87+
Dropdown.handle_input({:cursor_exit, 1}, %{id: 123}, %{@state | down: false})
88+
89+
assert state.hover_id == nil
90+
end
91+
92+
test "handle_input {:cursor_button, :press - up" do
93+
context = %ViewPort.Context{viewport: self(), id: @button_id}
94+
95+
{:noreply, state} =
96+
Dropdown.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{
97+
@state
98+
| down: false
99+
})
100+
assert state.down == true
101+
assert (is_integer( state.drop_time ) && state.drop_time > 0)
102+
103+
# confirm the input was captured
104+
assert_receive({:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}})
105+
106+
# confirm the graph was pushed
107+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
108+
end
109+
110+
# ============================================================================
111+
# handle_input - down
112+
113+
test "handle_input {:cursor_enter, _uid} - down" do
114+
context = %ViewPort.Context{viewport: self(), id: 1}
115+
116+
{:noreply, state} =
117+
Dropdown.handle_input({:cursor_enter, 1}, context, %{@state | down: true})
118+
119+
assert state.hover_id == 1
120+
121+
# confirm the graph was pushed
122+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
123+
end
124+
125+
test "handle_input {:cursor_exit, _uid} - down" do
126+
context = %ViewPort.Context{viewport: self(), id: 1}
127+
128+
{:noreply, state} =
129+
Dropdown.handle_input({:cursor_exit, 1}, context, %{@state | down: true})
130+
131+
assert state.hover_id == nil
132+
133+
# confirm the graph was pushed
134+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
135+
end
136+
137+
# mouse down outside menu
138+
test "handle_input {:cursor_button, :press nil - down" do
139+
context = %ViewPort.Context{viewport: self(), id: nil}
140+
141+
{:noreply, state} =
142+
Dropdown.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{
143+
@state
144+
| down: true
145+
})
146+
assert state.down == false
147+
148+
# confirm the input was released
149+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
150+
151+
# confirm the graph was pushed
152+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
153+
end
154+
155+
# mouse down inside button - slow
156+
test "handle_input {:cursor_button, :press button - down - slow - should close" do
157+
context = %ViewPort.Context{viewport: self(), id: @button_id}
158+
159+
{:noreply, state} =
160+
Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
161+
@state
162+
| down: true
163+
})
164+
assert state.down == false
165+
166+
# confirm the input was released
167+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
168+
169+
# confirm the graph was pushed
170+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
171+
end
172+
173+
# mouse down inside button - fast
174+
test "handle_input {:cursor_button, :press button - down - fast - should stay down" do
175+
context = %ViewPort.Context{viewport: self(), id: @button_id}
176+
177+
{:noreply, state} =
178+
Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
179+
@state
180+
| down: true,
181+
drop_time: :os.system_time(:milli_seconds)
182+
})
183+
assert state.down == true
184+
185+
# confirm the input was not released
186+
refute_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
187+
188+
# confirm the graph was not pushed
189+
refute_receive({:"$gen_cast", {:push_graph, _, _, _}})
190+
end
191+
192+
# mouse released outside dropdown space
193+
test "handle_input {:cursor_button, :release button - outside menu" do
194+
context = %ViewPort.Context{viewport: self(), id: nil}
195+
196+
{:noreply, state} =
197+
Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
198+
@state
199+
| down: true,
200+
drop_time: :os.system_time(:milli_seconds)
201+
})
202+
assert state.down == false
203+
assert state.selected_id == @initial_item
204+
205+
# confirm the input was released
206+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
207+
208+
# confirm the value change was not sent
209+
refute_receive({:"$gen_cast", {:event, {:value_changed, _, _}, _}})
210+
211+
# confirm the graph was pushed
212+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
213+
end
214+
215+
# mouse released inside dropdown space
216+
test "handle_input {:cursor_button, :release button - inside menu" do
217+
self = self()
218+
Process.put(:parent_pid, self)
219+
context = %ViewPort.Context{viewport: self, id: 1}
220+
221+
{:noreply, state} =
222+
Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{
223+
@state
224+
| down: true
225+
})
226+
assert state.down == false
227+
assert state.selected_id == 1
228+
229+
# confirm the input was released
230+
assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}})
231+
232+
# confirm the value change was not sent
233+
assert_receive({:"$gen_cast", {:event, {:value_changed, :test_id, 1}, ^self}})
234+
235+
# confirm the graph was pushed
236+
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
237+
end
238+
239+
test "handle_input does nothing on unknown input" do
240+
context = %ViewPort.Context{viewport: self()}
241+
{:noreply, state} = Dropdown.handle_input(:unknown, context, @state)
242+
assert state == @state
243+
end
244+
245+
end

test/scenic/component/input/radio_button_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ defmodule Scenic.Component.Input.RadioButtonTest do
6767
# handle_input
6868

6969
test "handle_input {:cursor_enter, _uid} sets contained" do
70-
{:noreply, state} = RadioButton.handle_input({:cursor_enter, 1}, %{}, %{@state | pressed: true})
70+
{:noreply, state} =
71+
RadioButton.handle_input({:cursor_enter, 1}, %{}, %{@state | pressed: true})
72+
7173
assert state.contained
7274
# confirm the graph was pushed
7375
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})
7476
end
7577

7678
test "handle_input {:cursor_exit, _uid} clears contained" do
77-
{:noreply, state} = RadioButton.handle_input({:cursor_exit, 1}, %{}, %{@state | pressed: true})
79+
{:noreply, state} =
80+
RadioButton.handle_input({:cursor_exit, 1}, %{}, %{@state | pressed: true})
81+
7882
refute state.contained
7983
# confirm the graph was pushed
8084
assert_receive({:"$gen_cast", {:push_graph, _, _, _}})

0 commit comments

Comments
 (0)