test_state.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // #if defined(ARDUINO) && defined(UNIT_TEST)
  2. #include <FS.h>
  3. #include <Arduino.h>
  4. #include "unity.h"
  5. #include <GroupState.h>
  6. #include <GroupStateStore.h>
  7. #include <GroupStateCache.h>
  8. #include <GroupStatePersistence.h>
  9. GroupState color() {
  10. GroupState s;
  11. s.setState(MiLightStatus::ON);
  12. s.setBulbMode(BulbMode::BULB_MODE_COLOR);
  13. s.setBrightness(100);
  14. s.setHue(1);
  15. s.setSaturation(10);
  16. return s;
  17. }
  18. void test_init_state() {
  19. GroupState s;
  20. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_WHITE);
  21. TEST_ASSERT_EQUAL(s.isSetBrightness(), false);
  22. }
  23. void test_state_updates() {
  24. GroupState s = color();
  25. // Make sure values are packed and unpacked correctly
  26. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_COLOR);
  27. TEST_ASSERT_EQUAL(s.getBrightness(), 100);
  28. TEST_ASSERT_EQUAL(s.getHue(), 1);
  29. TEST_ASSERT_EQUAL(s.getSaturation(), 10);
  30. // Make sure brightnesses are tied to mode
  31. s.setBulbMode(BulbMode::BULB_MODE_WHITE);
  32. s.setBrightness(0);
  33. TEST_ASSERT_EQUAL(s.getBulbMode(), BulbMode::BULB_MODE_WHITE);
  34. TEST_ASSERT_EQUAL(s.getBrightness(), 0);
  35. s.setBulbMode(BulbMode::BULB_MODE_COLOR);
  36. TEST_ASSERT_EQUAL(s.getBrightness(), 100);
  37. }
  38. void test_cache() {
  39. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  40. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  41. GroupState s = color();
  42. s.clearDirty();
  43. s.clearMqttDirty();
  44. GroupStateCache cache(1);
  45. GroupState* storedState = cache.get(id2);
  46. TEST_ASSERT_NULL_MESSAGE(storedState, "Should not retrieve value which hasn't been stored");
  47. cache.set(id1, s);
  48. storedState = cache.get(id1);
  49. TEST_ASSERT_NOT_NULL_MESSAGE(storedState, "Should retrieve a value");
  50. TEST_ASSERT_TRUE_MESSAGE(s == *storedState, "State should be the same when retrieved");
  51. cache.set(id2, s);
  52. storedState = cache.get(id2);
  53. TEST_ASSERT_NOT_NULL_MESSAGE(storedState, "Should retrieve a value");
  54. TEST_ASSERT_TRUE_MESSAGE(s == *storedState, "State should be the same when retrieved");
  55. storedState = cache.get(id1);
  56. TEST_ASSERT_NULL_MESSAGE(storedState, "Should evict old entry from cache");
  57. }
  58. void test_persistence() {
  59. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  60. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  61. GroupStatePersistence persistence;
  62. persistence.clear(id1);
  63. persistence.clear(id2);
  64. GroupState storedState;
  65. GroupState s = color();
  66. s.clearDirty();
  67. s.clearMqttDirty();
  68. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  69. persistence.get(id1, storedState);
  70. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should start with clean state");
  71. persistence.get(id2, storedState);
  72. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should start with clean state");
  73. persistence.set(id1, s);
  74. persistence.get(id2, storedState);
  75. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Should return default for state that hasn't been stored");
  76. persistence.get(id1, storedState);
  77. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(s), "Should retrieve state from flash without modification");
  78. GroupState newState = s;
  79. newState.setBulbMode(BulbMode::BULB_MODE_WHITE);
  80. newState.setBrightness(255);
  81. persistence.set(id2, newState);
  82. persistence.get(id1, storedState);
  83. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(s), "Should retrieve unmodified state");
  84. persistence.get(id2, storedState);
  85. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(newState), "Should retrieve modified state");
  86. }
  87. void test_store() {
  88. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  89. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  90. // cache 1 item, flush immediately
  91. GroupStateStore store(1, 0);
  92. GroupStatePersistence persistence;
  93. persistence.clear(id1);
  94. persistence.clear(id2);
  95. GroupState initState = color();
  96. GroupState initState2 = color();
  97. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  98. initState2.setBrightness(255);
  99. GroupState* storedState;
  100. storedState = &store.get(id2);
  101. TEST_ASSERT_TRUE_MESSAGE(*storedState == defaultState, "Should return default for state that hasn't been stored");
  102. store.set(id1, initState);
  103. storedState = &store.get(id1);
  104. TEST_ASSERT_TRUE_MESSAGE(*storedState == initState, "Should return cached state");
  105. store.flush();
  106. storedState = &store.get(id1);
  107. TEST_ASSERT_FALSE_MESSAGE(storedState->isDirty(), "Should not be dirty after flushing");
  108. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return cached state after flushing");
  109. store.set(id2, defaultState);
  110. storedState = &store.get(id2);
  111. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(defaultState), "Should return cached state");
  112. storedState = &store.get(id1);
  113. TEST_ASSERT_TRUE_MESSAGE(storedState->isEqualIgnoreDirty(initState), "Should return persisted state");
  114. }
  115. void test_group_0() {
  116. BulbId group0Id(1, 0, REMOTE_TYPE_FUT089);
  117. BulbId id1(1, 1, REMOTE_TYPE_FUT089);
  118. BulbId id2(1, 2, REMOTE_TYPE_FUT089);
  119. // cache 1 item, flush immediately
  120. GroupStateStore store(10, 0);
  121. GroupStatePersistence persistence;
  122. persistence.clear(id1);
  123. persistence.clear(id2);
  124. GroupState initState = color();
  125. GroupState initState2 = color();
  126. GroupState defaultState = GroupState::defaultState(REMOTE_TYPE_FUT089);
  127. GroupState storedState;
  128. GroupState expectedState;
  129. GroupState group0State;
  130. initState2.setBrightness(255);
  131. group0State.setHue(100);
  132. store.set(id1, initState);
  133. store.set(id2, initState2);
  134. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState), "group0 state should be different than initState");
  135. TEST_ASSERT_FALSE_MESSAGE(group0State.isEqualIgnoreDirty(initState2), "group0 state should be different than initState2");
  136. storedState = store.get(id1);
  137. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState), "Should fetch persisted state");
  138. storedState = store.get(id2);
  139. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(initState2), "Should fetch persisted state");
  140. store.set(group0Id, group0State);
  141. storedState = store.get(id1);
  142. expectedState = initState;
  143. expectedState.setHue(group0State.getHue());
  144. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  145. storedState = store.get(id2);
  146. expectedState = initState2;
  147. expectedState.setHue(group0State.getHue());
  148. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(expectedState), "Saving group 0 should only update changed field");
  149. // Test that state for group 0 is not persisted
  150. storedState = store.get(group0Id);
  151. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(defaultState), "Group 0 state should not be stored -- should return default state");
  152. // Should persist group 0 for device types with 0 groups
  153. BulbId rgbId(1, 0, REMOTE_TYPE_RGB);
  154. GroupState rgbState = GroupState::defaultState(REMOTE_TYPE_RGB);
  155. rgbState.setHue(100);
  156. rgbState.setBrightness(100);
  157. store.set(rgbId, rgbState);
  158. store.flush();
  159. storedState = store.get(rgbId);
  160. TEST_ASSERT_TRUE_MESSAGE(storedState.isEqualIgnoreDirty(rgbState), "Should persist group 0 for device type with no groups");
  161. }
  162. // setup connects serial, runs test cases (upcoming)
  163. void setup() {
  164. delay(2000);
  165. SPIFFS.begin();
  166. Serial.begin(9600);
  167. UNITY_BEGIN();
  168. RUN_TEST(test_init_state);
  169. RUN_TEST(test_state_updates);
  170. RUN_TEST(test_cache);
  171. RUN_TEST(test_persistence);
  172. RUN_TEST(test_store);
  173. RUN_TEST(test_group_0);
  174. UNITY_END();
  175. }
  176. void loop() {
  177. // nothing to be done here.
  178. }
  179. // #endif