2
2
import json # Remove
3
3
from unittest import mock
4
4
import socketio
5
+ import threading
5
6
6
7
from adafruit_circuitplayground import express
7
- from .. import debugger_communication_client
8
+ from common import debugger_communication_client
9
+ from common import constants as CONSTANTS
10
+ from adafruit_circuitplayground .constants import CPX
8
11
9
12
10
13
class TestDebuggerCommunicationClient (object ):
@@ -21,43 +24,155 @@ def test_init_connection1(self):
21
24
socketio .Client .connect .assert_called_once ()
22
25
23
26
def test_update_state (self ):
27
+ threading .Event .clear = mock .Mock ()
28
+ threading .Event .wait = mock .Mock ()
24
29
socketio .Client .emit = mock .Mock ()
25
30
socketio .Client .emit .return_value = None
26
- debugger_communication_client .update_state ({})
31
+ debugger_communication_client .update_state (
32
+ {CONSTANTS .ACTIVE_DEVICE_FIELD : CPX , CONSTANTS .STATE_FIELD : {}}
33
+ )
27
34
socketio .Client .emit .assert_called_once ()
28
35
29
36
@mock .patch .dict (
30
37
express .cpx ._Express__state ,
31
- {"button_a" : False , "button_b" : False , "switch" : True },
38
+ {
39
+ "brightness" : 1.0 ,
40
+ "button_a" : False ,
41
+ "button_b" : False ,
42
+ "pixels" : [
43
+ (0 , 0 , 0 ),
44
+ (0 , 0 , 0 ),
45
+ (0 , 0 , 0 ),
46
+ (0 , 0 , 0 ),
47
+ (0 , 0 , 0 ),
48
+ (0 , 0 , 0 ),
49
+ (0 , 0 , 0 ),
50
+ (0 , 0 , 0 ),
51
+ (0 , 0 , 0 ),
52
+ (0 , 0 , 0 ),
53
+ ],
54
+ "red_led" : False ,
55
+ "switch" : False ,
56
+ "temperature" : 0 ,
57
+ "light" : 0 ,
58
+ "motion_x" : 0 ,
59
+ "motion_y" : 0 ,
60
+ "motion_z" : 0 ,
61
+ "touch" : [False ] * 7 ,
62
+ "shake" : False ,
63
+ },
32
64
clear = True ,
33
65
)
34
66
def test_button_press (self ):
35
- data = {"button_a" : True , "button_b" : True , "switch" : True }
67
+ data = {
68
+ CONSTANTS .ACTIVE_DEVICE_FIELD : CPX ,
69
+ CONSTANTS .STATE_FIELD : {"button_a" : True , "button_b" : True , "switch" : True },
70
+ }
71
+ expected_data = {
72
+ "brightness" : 1.0 ,
73
+ "button_a" : True ,
74
+ "button_b" : True ,
75
+ "pixels" : [
76
+ (0 , 0 , 0 ),
77
+ (0 , 0 , 0 ),
78
+ (0 , 0 , 0 ),
79
+ (0 , 0 , 0 ),
80
+ (0 , 0 , 0 ),
81
+ (0 , 0 , 0 ),
82
+ (0 , 0 , 0 ),
83
+ (0 , 0 , 0 ),
84
+ (0 , 0 , 0 ),
85
+ (0 , 0 , 0 ),
86
+ ],
87
+ "red_led" : False ,
88
+ "switch" : True ,
89
+ "temperature" : 0 ,
90
+ "light" : 0 ,
91
+ "motion_x" : 0 ,
92
+ "motion_y" : 0 ,
93
+ "motion_z" : 0 ,
94
+ "touch" : [False ] * 7 ,
95
+ "shake" : False ,
96
+ }
36
97
serialized_data = json .dumps (data )
37
98
debugger_communication_client .input_changed (serialized_data )
38
- assert data == express .cpx ._Express__state
99
+ assert expected_data == express .cpx ._Express__state
39
100
40
101
@mock .patch .dict (
41
102
express .cpx ._Express__state ,
42
- {"temperature" : 0 , "light" : 0 , "motion_x" : 0 , "motion_y" : 0 , "motion_z" : 0 },
103
+ {
104
+ "brightness" : 1.0 ,
105
+ "button_a" : False ,
106
+ "button_b" : False ,
107
+ "pixels" : [
108
+ (0 , 0 , 0 ),
109
+ (0 , 0 , 0 ),
110
+ (0 , 0 , 0 ),
111
+ (0 , 0 , 0 ),
112
+ (0 , 0 , 0 ),
113
+ (0 , 0 , 0 ),
114
+ (0 , 0 , 0 ),
115
+ (0 , 0 , 0 ),
116
+ (0 , 0 , 0 ),
117
+ (0 , 0 , 0 ),
118
+ ],
119
+ "red_led" : False ,
120
+ "switch" : False ,
121
+ "temperature" : 0 ,
122
+ "light" : 0 ,
123
+ "motion_x" : 0 ,
124
+ "motion_y" : 0 ,
125
+ "motion_z" : 0 ,
126
+ "touch" : [False ] * 7 ,
127
+ "shake" : False ,
128
+ },
43
129
clear = True ,
44
130
)
45
- def test_sensor_changed (self ):
131
+ def test_input_changed (self ):
46
132
data = {
133
+ CONSTANTS .ACTIVE_DEVICE_FIELD : CPX ,
134
+ CONSTANTS .STATE_FIELD : {
135
+ "temperature" : 1 ,
136
+ "light" : 2 ,
137
+ "motion_x" : 3 ,
138
+ "motion_y" : 4 ,
139
+ "motion_z" : 5 ,
140
+ },
141
+ }
142
+ expected_data = {
143
+ "brightness" : 1.0 ,
144
+ "button_a" : False ,
145
+ "button_b" : False ,
146
+ "pixels" : [
147
+ (0 , 0 , 0 ),
148
+ (0 , 0 , 0 ),
149
+ (0 , 0 , 0 ),
150
+ (0 , 0 , 0 ),
151
+ (0 , 0 , 0 ),
152
+ (0 , 0 , 0 ),
153
+ (0 , 0 , 0 ),
154
+ (0 , 0 , 0 ),
155
+ (0 , 0 , 0 ),
156
+ (0 , 0 , 0 ),
157
+ ],
158
+ "red_led" : False ,
159
+ "switch" : False ,
47
160
"temperature" : 1 ,
48
161
"light" : 2 ,
49
162
"motion_x" : 3 ,
50
163
"motion_y" : 4 ,
51
164
"motion_z" : 5 ,
165
+ "touch" : [False ] * 7 ,
166
+ "shake" : False ,
52
167
}
53
168
serialized_data = json .dumps (data )
54
169
debugger_communication_client .input_changed (serialized_data )
55
- assert data == express .cpx ._Express__state
170
+ assert expected_data == express .cpx ._Express__state
56
171
57
172
@mock .patch ("builtins.print" )
58
173
@mock .patch .dict (express .cpx ._Express__state , {}, clear = True )
59
174
def test_update_api_state_fail (self , mocked_print ):
60
175
data = []
61
- debugger_communication_client .sensor_changed (data )
176
+ debugger_communication_client .input_changed (data )
62
177
# Exception is caught and a print is stated to stderr
63
178
mocked_print .assert_called_once ()
0 commit comments