Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 470282d

Browse files
committed
src/adafruit_circuitplayground
1 parent e951947 commit 470282d

File tree

1 file changed

+124
-9
lines changed

1 file changed

+124
-9
lines changed

src/common/test/test_debugger_communication_client.py

Lines changed: 124 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
import json # Remove
33
from unittest import mock
44
import socketio
5+
import threading
56

67
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
811

912

1013
class TestDebuggerCommunicationClient(object):
@@ -21,43 +24,155 @@ def test_init_connection1(self):
2124
socketio.Client.connect.assert_called_once()
2225

2326
def test_update_state(self):
27+
threading.Event.clear = mock.Mock()
28+
threading.Event.wait = mock.Mock()
2429
socketio.Client.emit = mock.Mock()
2530
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+
)
2734
socketio.Client.emit.assert_called_once()
2835

2936
@mock.patch.dict(
3037
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+
},
3264
clear=True,
3365
)
3466
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+
}
3697
serialized_data = json.dumps(data)
3798
debugger_communication_client.input_changed(serialized_data)
38-
assert data == express.cpx._Express__state
99+
assert expected_data == express.cpx._Express__state
39100

40101
@mock.patch.dict(
41102
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+
},
43129
clear=True,
44130
)
45-
def test_sensor_changed(self):
131+
def test_input_changed(self):
46132
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,
47160
"temperature": 1,
48161
"light": 2,
49162
"motion_x": 3,
50163
"motion_y": 4,
51164
"motion_z": 5,
165+
"touch": [False] * 7,
166+
"shake": False,
52167
}
53168
serialized_data = json.dumps(data)
54169
debugger_communication_client.input_changed(serialized_data)
55-
assert data == express.cpx._Express__state
170+
assert expected_data == express.cpx._Express__state
56171

57172
@mock.patch("builtins.print")
58173
@mock.patch.dict(express.cpx._Express__state, {}, clear=True)
59174
def test_update_api_state_fail(self, mocked_print):
60175
data = []
61-
debugger_communication_client.sensor_changed(data)
176+
debugger_communication_client.input_changed(data)
62177
# Exception is caught and a print is stated to stderr
63178
mocked_print.assert_called_once()

0 commit comments

Comments
 (0)