test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.setHue(1);
  92. s.setSaturation(10);
  93. s.setBrightness(100);
  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. GroupStateStore store(4, 0);
  169. GroupStatePersistence persistence;
  170. persistence.clear(id1);
  171. persistence.clear(id2);
  172. GroupState initState = color();
  173. GroupState initState2 = color();
  174. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  175. initState2.setBrightness(50);
  176. GroupState* storedState;
  177. storedState = store.get(id2);
  178. TEST_ASSERT_TRUE_MESSAGE(*storedState == defaultState, "Should return default for state that hasn't been stored");
  179. store.set(id1, initState);
  180. storedState = store.get(id1);
  181. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return stored state. Will not be cached because of internal group 0 lookups");
  182. store.flush();
  183. storedState = store.get(id1);
  184. TEST_ASSERT_FALSE_MESSAGE(storedState->isDirty(), "Should not be dirty after flushing");
  185. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return cached state after flushing");
  186. store.set(id2, defaultState);
  187. storedState = store.get(id2);
  188. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(defaultState), "Should return cached state");
  189. storedState = store.get(id1);
  190. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return persisted state");
  191. }
  192. void test_group_0() {
  193. BulbId group0Id(1, 0, REMOTE_TYPE_FUT089);
  194. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  195. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  196. // cache 1 item, flush immediately
  197. GroupStateStore store(10, 0);
  198. GroupStatePersistence persistence;
  199. persistence.clear(id1);
  200. persistence.clear(id2);
  201. GroupState initState = color();
  202. GroupState initState2 = color();
  203. GroupState storedState;
  204. GroupState expectedState;
  205. GroupState group0State;
  206. initState2.setBrightness(255);
  207. group0State.setHue(100);
  208. store.set(id1, initState);
  209. store.set(id2, initState2);
  210. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState), "group0 state should be different than initState");
  211. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState2), "group0 state should be different than initState2");
  212. storedState = *store.get(id1);
  213. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState), "Should fetch persisted state");
  214. storedState = *store.get(id2);
  215. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState2), "Should fetch persisted state");
  216. store.set(group0Id, group0State);
  217. storedState = *store.get(id1);
  218. expectedState = initState;
  219. expectedState.setHue(group0State.getHue());
  220. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  221. storedState = *store.get(id2);
  222. expectedState = initState2;
  223. expectedState.setHue(group0State.getHue());
  224. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  225. // Test that state for group 0 is persisted
  226. storedState = *store.get(group0Id);
  227. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(group0State), "Group 0 state should not be stored -- should return default state");
  228. // Test that states for constituent groups are properly updated
  229. initState.setHue(0);
  230. initState2.setHue(100);
  231. initState.setBrightness(50);
  232. initState2.setBrightness(70);
  233. store.set(id1, initState);
  234. store.set(id2, initState2);
  235. storedState = *store.get(group0Id);
  236. storedState.setHue(200);
  237. TEST_ASSERT_FALSE_MESSAGE(storedState.isSetBrightness(), "Should not have a set field for group 0 brightness");
  238. store.set(group0Id, storedState);
  239. storedState = *store.get(id1);
  240. TEST_ASSERT_TRUE_MESSAGE(storedState.getBrightness() == 50, "UNSET field in group 0 update SHOULD NOT overwrite constituent group field");
  241. TEST_ASSERT_TRUE_MESSAGE(storedState.getHue() == 200, "SET field in group 0 update SHOULD overwrite constituent group field");
  242. storedState = *store.get(id2);
  243. TEST_ASSERT_TRUE_MESSAGE(storedState.getBrightness() == 70, "UNSET field in group 0 update SHOULD NOT overwrite constituent group field");
  244. TEST_ASSERT_TRUE_MESSAGE(storedState.getHue() == 200, "SET field in group 0 update SHOULD overwrite constituent group field");
  245. // Should persist group 0 for device types with 0 groups
  246. BulbId rgbId(1, 0, REMOTE_TYPE_RGB);
  247. GroupState rgbState = GroupState::defaultState(REMOTE_TYPE_RGB);
  248. rgbState.setHue(100);
  249. rgbState.setBrightness(100);
  250. store.set(rgbId, rgbState);
  251. store.flush();
  252. storedState = *store.get(rgbId);
  253. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(rgbState), "Should persist group 0 for device type with no groups");
  254. }
  255. // setup connects serial, runs test cases (upcoming)
  256. void setup() {
  257. delay(2000);
  258. SPIFFS.begin();
  259. Serial.begin(9600);
  260. UNITY_BEGIN();
  261. RUN_TEST(test_init_state);
  262. RUN_TEST(test_state_updates);
  263. RUN_TEST(test_cache);
  264. RUN_TEST(test_persistence);
  265. RUN_TEST(test_store);
  266. RUN_TEST(test_group_0);
  267. RUN_TEST(test_fut091_packet_formatter);
  268. RUN_TEST(test_fut092_packet_formatter);
  269. UNITY_END();
  270. }
  271. void loop() {
  272. // nothing to be done here.
  273. }
  274. // #endif