@@ -8,8 +8,20 @@ defmodule Scenic.Component.ButtonTest do
8
8
doctest Scenic
9
9
10
10
# alias Scenic.Component
11
+ alias Scenic.Graph
12
+ alias Scenic.Primitive
13
+ alias Scenic.ViewPort
11
14
alias Scenic.Component.Button
12
15
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
+
13
25
# ============================================================================
14
26
# info
15
27
@@ -33,10 +45,81 @@ defmodule Scenic.Component.ButtonTest do
33
45
34
46
test "init works with simple data" do
35
47
{ :ok , state } = Button . init ( "Button" , styles: % { } , id: :button_id )
36
- % Scenic. Graph{ } = state . graph
48
+ % Graph { } = state . graph
37
49
assert is_map ( state . theme )
38
50
assert state . pressed == false
39
51
assert state . align == :center
40
52
assert state . id == :button_id
41
53
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