test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // #if defined(ARDUINO) && defined(UNIT_TEST)
  2. #include <FS.h>
  3. #include <Arduino.h>
  4. #include <GroupState.h>
  5. #include <GroupStateStore.h>
  6. #include <GroupStateCache.h>
  7. #include <GroupStatePersistence.h>
  8. #include <RgbCctPacketFormatter.h>
  9. #include <FUT091PacketFormatter.h>
  10. #include <Units.h>
  11. #include "unity.h"
  12. //================================================================================
  13. // Packet formatter
  14. //================================================================================
  15. template <typename T>
  16. void run_packet_test(uint8_t* packet, PacketFormatter* packetFormatter, const BulbId& expectedBulbId, const String& expectedKey, const T expectedValue) {
  17. GroupStateStore stateStore(10, 0);
  18. Settings settings;
  19. RgbCctPacketFormatter formatter;
  20. DynamicJsonBuffer jsonBuffer;
  21. JsonObject& result = jsonBuffer.createObject();
  22. packetFormatter->prepare(0, 0);
  23. BulbId bulbId = packetFormatter->parsePacket(packet, result);
  24. TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.deviceId, bulbId.deviceId, "Should get the expected device ID");
  25. TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.groupId, bulbId.groupId, "Should get the expected group ID");
  26. TEST_ASSERT_EQUAL_INT_MESSAGE(expectedBulbId.deviceType, bulbId.deviceType, "Should get the expected remote type");
  27. TEST_ASSERT_TRUE_MESSAGE(result.containsKey(expectedKey), "Parsed packet should be for expected command type");
  28. TEST_ASSERT_TRUE_MESSAGE(result[expectedKey] == expectedValue, "Parsed packet should have expected value");
  29. }
  30. void test_fut092_packet_formatter() {
  31. RgbCctPacketFormatter packetFormatter;
  32. uint8_t onPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x66, 0xCA, 0x54, 0x66, 0xD2};
  33. run_packet_test(
  34. onPacket,
  35. &packetFormatter,
  36. BulbId(1, 1, REMOTE_TYPE_RGB_CCT),
  37. "state",
  38. "OFF"
  39. );
  40. uint8_t minColorTempPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x64, 0x3C, 0x47, 0x66, 0x31};
  41. run_packet_test(
  42. minColorTempPacket,
  43. &packetFormatter,
  44. BulbId(1, 1, REMOTE_TYPE_RGB_CCT),
  45. "color_temp",
  46. COLOR_TEMP_MIN_MIREDS
  47. );
  48. uint8_t maxColorTempPacket[] = {0x00, 0xDB, 0xE1, 0x24, 0x64, 0x94, 0x62, 0x66, 0x88};
  49. run_packet_test(
  50. maxColorTempPacket,
  51. &packetFormatter,
  52. BulbId(1, 1, REMOTE_TYPE_RGB_CCT),
  53. "color_temp",
  54. COLOR_TEMP_MAX_MIREDS
  55. );
  56. }
  57. void test_fut091_packet_formatter() {
  58. FUT091PacketFormatter packetFormatter;
  59. uint8_t onPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x66, 0xCA, 0xBA, 0x66, 0xB5};
  60. run_packet_test(
  61. onPacket,
  62. &packetFormatter,
  63. BulbId(1, 1, REMOTE_TYPE_FUT091),
  64. "state",
  65. "OFF"
  66. );
  67. uint8_t minColorTempPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x64, 0x8D, 0xB9, 0x66, 0x71};
  68. run_packet_test(
  69. minColorTempPacket,
  70. &packetFormatter,
  71. BulbId(1, 1, REMOTE_TYPE_FUT091),
  72. "color_temp",
  73. COLOR_TEMP_MIN_MIREDS
  74. );
  75. uint8_t maxColorTempPacket[] = {0x00, 0xDC, 0xE1, 0x24, 0x64, 0x55, 0xB7, 0x66, 0x27};
  76. run_packet_test(
  77. maxColorTempPacket,
  78. &packetFormatter,
  79. BulbId(1, 1, REMOTE_TYPE_FUT091),
  80. "color_temp",
  81. COLOR_TEMP_MAX_MIREDS
  82. );
  83. }
  84. //================================================================================
  85. // Group State
  86. //================================================================================
  87. GroupState color() {
  88. GroupState s;
  89. s.setState(MiLightStatus::ON);
  90. s.setBulbMode(BulbMode::BULB_MODE_COLOR);
  91. s.setBrightness(100);
  92. s.setHue(1);
  93. s.setSaturation(10);
  94. return s;
  95. }
  96. void test_init_state() {
  97. GroupState s;
  98. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_WHITE);
  99. TEST_ASSERT_EQUAL(s.isSetBrightness(), false);
  100. }
  101. void test_state_updates() {
  102. GroupState s = color();
  103. // Make sure values are packed and unpacked correctly
  104. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_COLOR);
  105. TEST_ASSERT_EQUAL(s.getBrightness(), 100);
  106. TEST_ASSERT_EQUAL(s.getHue(), 1);
  107. TEST_ASSERT_EQUAL(s.getSaturation(), 10);
  108. // Make sure brightnesses are tied to mode
  109. s.setBulbMode(BulbMode::BULB_MODE_WHITE);
  110. s.setBrightness(0);
  111. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_WHITE);
  112. TEST_ASSERT_EQUAL(s.getBrightness(), 0);
  113. s.setBulbMode(BulbMode::BULB_MODE_COLOR);
  114. TEST_ASSERT_EQUAL(s.getBrightness(), 100);
  115. }
  116. void test_cache() {
  117. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  118. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  119. GroupState s = color();
  120. s.clearDirty();
  121. s.clearMqttDirty();
  122. GroupStateCache cache(1);
  123. GroupState* storedState = cache.get(id2);
  124. TEST_ASSERT_NULL_MESSAGE(storedState, "Should not retrieve value which hasn't been stored");
  125. cache.set(id1, s);
  126. storedState = cache.get(id1);
  127. TEST_ASSERT_NOT_NULL_MESSAGE(storedState, "Should retrieve a value");
  128. TEST_ASSERT_TRUE_MESSAGE(s == *storedState, "State should be the same when retrieved");
  129. cache.set(id2, s);
  130. storedState = cache.get(id2);
  131. TEST_ASSERT_NOT_NULL_MESSAGE(storedState, "Should retrieve a value");
  132. TEST_ASSERT_TRUE_MESSAGE(s == *storedState, "State should be the same when retrieved");
  133. storedState = cache.get(id1);
  134. TEST_ASSERT_NULL_MESSAGE(storedState, "Should evict old entry from cache");
  135. }
  136. void test_persistence() {
  137. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  138. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  139. GroupStatePersistence persistence;
  140. persistence.clear(id1);
  141. persistence.clear(id2);
  142. GroupState storedState;
  143. GroupState s = color();
  144. s.clearDirty();
  145. s.clearMqttDirty();
  146. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  147. persistence.get(id1, storedState);
  148. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should start with clean state");
  149. persistence.get(id2, storedState);
  150. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should start with clean state");
  151. persistence.set(id1, s);
  152. persistence.get(id2, storedState);
  153. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should return default for state that hasn't been stored");
  154. persistence.get(id1, storedState);
  155. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(s), "Should retrieve state from flash without modification");
  156. GroupState newState = s;
  157. newState.setBulbMode(BulbMode::BULB_MODE_WHITE);
  158. newState.setBrightness(255);
  159. persistence.set(id2, newState);
  160. persistence.get(id1, storedState);
  161. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(s), "Should retrieve unmodified state");
  162. persistence.get(id2, storedState);
  163. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(newState), "Should retrieve modified state");
  164. }
  165. void test_store() {
  166. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  167. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  168. // cache 1 item, flush immediately
  169. GroupStateStore store(1, 0);
  170. GroupStatePersistence persistence;
  171. persistence.clear(id1);
  172. persistence.clear(id2);
  173. GroupState initState = color();
  174. GroupState initState2 = color();
  175. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  176. initState2.setBrightness(255);
  177. GroupState* storedState;
  178. storedState = store.get(id2);
  179. TEST_ASSERT_TRUE_MESSAGE(*storedState == defaultState, "Should return default for state that hasn't been stored");
  180. store.set(id1, initState);
  181. storedState = store.get(id1);
  182. TEST_ASSERT_TRUE_MESSAGE(*storedState == initState, "Should return cached state");
  183. store.flush();
  184. storedState = store.get(id1);
  185. TEST_ASSERT_FALSE_MESSAGE(storedState->isDirty(), "Should not be dirty after flushing");
  186. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return cached state after flushing");
  187. store.set(id2, defaultState);
  188. storedState = store.get(id2);
  189. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(defaultState), "Should return cached state");
  190. storedState = store.get(id1);
  191. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return persisted state");
  192. }
  193. void test_group_0() {
  194. BulbId group0Id(1, 0, REMOTE_TYPE_FUT089);
  195. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  196. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  197. // cache 1 item, flush immediately
  198. GroupStateStore store(10, 0);
  199. GroupStatePersistence persistence;
  200. persistence.clear(id1);
  201. persistence.clear(id2);
  202. GroupState initState = color();
  203. GroupState initState2 = color();
  204. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  205. GroupState storedState;
  206. GroupState expectedState;
  207. GroupState group0State;
  208. initState2.setBrightness(255);
  209. group0State.setHue(100);
  210. store.set(id1, initState);
  211. store.set(id2, initState2);
  212. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState), "group0 state should be different than initState");
  213. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState2), "group0 state should be different than initState2");
  214. storedState = *store.get(id1);
  215. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState), "Should fetch persisted state");
  216. storedState = *store.get(id2);
  217. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState2), "Should fetch persisted state");
  218. store.set(group0Id, group0State);
  219. storedState = *store.get(id1);
  220. expectedState = initState;
  221. expectedState.setHue(group0State.getHue());
  222. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  223. storedState = *store.get(id2);
  224. expectedState = initState2;
  225. expectedState.setHue(group0State.getHue());
  226. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  227. // Test that state for group 0 is not persisted
  228. storedState = *store.get(group0Id);
  229. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Group 0 state should not be stored -- should return default state");
  230. // Test that states for constituent groups are properly updated
  231. initState.setHue(0);
  232. initState2.setHue(100);
  233. initState.setBrightness(50);
  234. initState2.setBrightness(70);
  235. store.set(id1, initState);
  236. store.set(id2, initState2);
  237. storedState = *store.get(group0Id);
  238. storedState.setHue(200);
  239. TEST_ASSERT_FALSE_MESSAGE(storedState.isSetBrightness(), "Should not have a set field for group 0 brightness");
  240. store.set(group0Id, storedState);
  241. storedState = *store.get(id1);
  242. TEST_ASSERT_TRUE_MESSAGE(storedState.getBrightness() == 50, "UNSET field in group 0 update SHOULD NOT overwrite constituent group field");
  243. TEST_ASSERT_TRUE_MESSAGE(storedState.getHue() == 200, "SET field in group 0 update SHOULD overwrite constituent group field");
  244. storedState = *store.get(id2);
  245. TEST_ASSERT_TRUE_MESSAGE(storedState.getBrightness() == 70, "UNSET field in group 0 update SHOULD NOT overwrite constituent group field");
  246. TEST_ASSERT_TRUE_MESSAGE(storedState.getHue() == 200, "SET field in group 0 update SHOULD overwrite constituent group field");
  247. // Should persist group 0 for device types with 0 groups
  248. BulbId rgbId(1, 0, REMOTE_TYPE_RGB);
  249. GroupState rgbState = GroupState::defaultState(REMOTE_TYPE_RGB);
  250. rgbState.setHue(100);
  251. rgbState.setBrightness(100);
  252. store.set(rgbId, rgbState);
  253. store.flush();
  254. storedState = *store.get(rgbId);
  255. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(rgbState), "Should persist group 0 for device type with no groups");
  256. }
  257. // setup connects serial, runs test cases (upcoming)
  258. void setup() {
  259. delay(2000);
  260. SPIFFS.begin();
  261. Serial.begin(9600);
  262. UNITY_BEGIN();
  263. RUN_TEST(test_init_state);
  264. RUN_TEST(test_state_updates);
  265. RUN_TEST(test_cache);
  266. RUN_TEST(test_persistence);
  267. RUN_TEST(test_store);
  268. RUN_TEST(test_group_0);
  269. RUN_TEST(test_fut091_packet_formatter);
  270. RUN_TEST(test_fut092_packet_formatter);
  271. UNITY_END();
  272. }
  273. void loop() {
  274. // nothing to be done here.
  275. }
  276. // #endif