furi_record_test.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "flipper.h"
  4. #include "flipper_v2.h"
  5. #include "log.h"
  6. #include "minunit.h"
  7. void test_furi_create_open() {
  8. // 1. Create record
  9. uint8_t test_data = 0;
  10. mu_check(furi_create("test/holding", (void*)&test_data));
  11. // 2. Open it
  12. void* record = furi_open("test/holding");
  13. mu_assert_pointers_eq(record, &test_data);
  14. }
  15. /*
  16. TEST: non-existent data
  17. 1. Try to open non-existent record
  18. 2. Check for NULL handler
  19. 3. Try to write/read, get error
  20. TODO: implement this test
  21. */
  22. bool test_furi_nonexistent_data() {
  23. return true;
  24. }
  25. /*
  26. TEST: mute algorithm
  27. 1. Create "parent" application:
  28. 1. Create pipe record
  29. 2. Open watch handler: no_mute=false, solo=false, subscribe to data.
  30. 2. Open handler A: no_mute=false, solo=false, NULL subscriber. Subscribe to state.
  31. Try to write data to A and check subscriber.
  32. 3. Open handler B: no_mute=true, solo=true, NULL subscriber.
  33. Check A state cb get FlipperRecordStateMute.
  34. Try to write data to A and check that subscriber get no data. (muted)
  35. Try to write data to B and check that subscriber get data.
  36. TODO: test 3 not pass beacuse state callback not implemented
  37. 4. Open hadler C: no_mute=false, solo=true, NULL subscriber.
  38. Try to write data to A and check that subscriber get no data. (muted)
  39. Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
  40. Try to write data to C and check that subscriber get data.
  41. 5. Open handler D: no_mute=false, solo=false, NULL subscriber.
  42. Try to write data to A and check that subscriber get no data. (muted)
  43. Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
  44. Try to write data to C and check that subscriber get data. (not muted because D open without solo)
  45. Try to write data to D and check that subscriber get data.
  46. 6. Close C, close B.
  47. Check A state cb get FlipperRecordStateUnmute
  48. Try to write data to A and check that subscriber get data. (unmuted)
  49. Try to write data to D and check that subscriber get data.
  50. TODO: test 6 not pass beacuse cleanup is not implemented
  51. TODO: test 6 not pass because mute algorithm is unfinished.
  52. 7. Exit "parent application"
  53. Check A state cb get FlipperRecordStateDeleted
  54. TODO: test 7 not pass beacuse cleanup is not implemented
  55. */
  56. static uint8_t mute_last_value = 0;
  57. static FlipperRecordState mute_last_state = 255;
  58. void mute_record_cb(const void* value, size_t size, void* ctx) {
  59. // hold value to static var
  60. mute_last_value = *((uint8_t*)value);
  61. }
  62. void mute_record_state_cb(FlipperRecordState state, void* ctx) {
  63. mute_last_state = state;
  64. }
  65. void furi_mute_parent_app(void* p) {
  66. // 1. Create pipe record
  67. if(!furi_create_deprecated("test/mute", NULL, 0)) {
  68. printf("cannot create record\n");
  69. furiac_exit(NULL);
  70. }
  71. // 2. Open watch handler: solo=false, no_mute=false, subscribe to data
  72. FuriRecordSubscriber* watch_handler =
  73. furi_open_deprecated("test/mute", false, false, mute_record_cb, NULL, NULL);
  74. if(watch_handler == NULL) {
  75. printf("cannot open watch handler\n");
  76. furiac_exit(NULL);
  77. }
  78. while(1) {
  79. // TODO we don't have thread sleep
  80. delay(100000);
  81. }
  82. }
  83. bool test_furi_mute_algorithm() {
  84. // 1. Create "parent" application:
  85. FuriApp* parent_app = furiac_start(furi_mute_parent_app, "parent app", NULL);
  86. delay(2); // wait creating record
  87. // 2. Open handler A: solo=false, no_mute=false, NULL subscriber. Subscribe to state.
  88. FuriRecordSubscriber* handler_a =
  89. furi_open_deprecated("test/mute", false, false, NULL, mute_record_state_cb, NULL);
  90. if(handler_a == NULL) {
  91. printf("cannot open handler A\n");
  92. return false;
  93. }
  94. uint8_t test_counter = 1;
  95. // Try to write data to A and check subscriber
  96. if(!furi_write(handler_a, &test_counter, sizeof(uint8_t))) {
  97. printf("write to A failed\n");
  98. return false;
  99. }
  100. if(mute_last_value != test_counter) {
  101. printf("value A mismatch: %d vs %d\n", mute_last_value, test_counter);
  102. return false;
  103. }
  104. // 3. Open handler B: solo=true, no_mute=true, NULL subscriber.
  105. FuriRecordSubscriber* handler_b =
  106. furi_open_deprecated("test/mute", true, true, NULL, NULL, NULL);
  107. if(handler_b == NULL) {
  108. printf("cannot open handler B\n");
  109. return false;
  110. }
  111. // Check A state cb get FlipperRecordStateMute.
  112. if(mute_last_state != FlipperRecordStateMute) {
  113. printf("A state is not FlipperRecordStateMute: %d\n", mute_last_state);
  114. return false;
  115. }
  116. test_counter = 2;
  117. // Try to write data to A and check that subscriber get no data. (muted)
  118. if(furi_write(handler_a, &test_counter, sizeof(uint8_t))) {
  119. printf("A not muted\n");
  120. return false;
  121. }
  122. if(mute_last_value == test_counter) {
  123. printf("value A must be muted\n");
  124. return false;
  125. }
  126. test_counter = 3;
  127. // Try to write data to B and check that subscriber get data.
  128. if(!furi_write(handler_b, &test_counter, sizeof(uint8_t))) {
  129. printf("write to B failed\n");
  130. return false;
  131. }
  132. if(mute_last_value != test_counter) {
  133. printf("value B mismatch: %d vs %d\n", mute_last_value, test_counter);
  134. return false;
  135. }
  136. // 4. Open hadler C: solo=true, no_mute=false, NULL subscriber.
  137. FuriRecordSubscriber* handler_c =
  138. furi_open_deprecated("test/mute", true, false, NULL, NULL, NULL);
  139. if(handler_c == NULL) {
  140. printf("cannot open handler C\n");
  141. return false;
  142. }
  143. // TODO: Try to write data to A and check that subscriber get no data. (muted)
  144. // TODO: Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
  145. // TODO: Try to write data to C and check that subscriber get data.
  146. // 5. Open handler D: solo=false, no_mute=false, NULL subscriber.
  147. FuriRecordSubscriber* handler_d =
  148. furi_open_deprecated("test/mute", false, false, NULL, NULL, NULL);
  149. if(handler_d == NULL) {
  150. printf("cannot open handler D\n");
  151. return false;
  152. }
  153. // TODO: Try to write data to A and check that subscriber get no data. (muted)
  154. // TODO: Try to write data to B and check that subscriber get data. (not muted because open with no_mute)
  155. // TODO: Try to write data to C and check that subscriber get data. (not muted because D open without solo)
  156. // TODO: Try to write data to D and check that subscriber get data.
  157. // 6. Close C, close B.
  158. // TODO: Check A state cb get FlipperRecordStateUnmute
  159. // TODO: Try to write data to A and check that subscriber get data. (unmuted)
  160. // TODO: Try to write data to D and check that subscriber get data.
  161. // 7. Exit "parent application"
  162. if(!furiac_kill(parent_app)) {
  163. printf("kill parent_app fail\n");
  164. return false;
  165. }
  166. // TODO: Check A state cb get FlipperRecordStateDeleted
  167. return true;
  168. }