Skip to content

Commit

Permalink
add change handlers tester, also a commented out sendGet test (it's s…
Browse files Browse the repository at this point in the history
…low and problematic), few more tests
  • Loading branch information
openshwprojects committed Nov 21, 2022
1 parent 6ab8890 commit 4626aae
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 33 deletions.
3 changes: 3 additions & 0 deletions openBeken_win32_mvsc2017.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,18 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Win32 ScriptOnly|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="src\selftest\selftest_buttonEvents.c" />
<ClCompile Include="src\selftest\selftest_changeHandlers.c" />
<ClCompile Include="src\selftest\selftest_cmd_alias.c" />
<ClCompile Include="src\selftest\selftest_cmd_channels.c" />
<ClCompile Include="src\selftest\selftest_expandConstant.c" />
<ClCompile Include="src\selftest\selftest_expressions.c" />
<ClCompile Include="src\selftest\selftest_http.c" />
<ClCompile Include="src\selftest\selftest_http_client.c" />
<ClCompile Include="src\selftest\selftest_if.c" />
<ClCompile Include="src\selftest\selftest_led.c" />
<ClCompile Include="src\selftest\selftest_lfs.c" />
<ClCompile Include="src\selftest\selftest_main.c" />
<ClCompile Include="src\selftest\selftest_repeatingEvents.c" />
<ClCompile Include="src\selftest\selftest_script.c" />
<ClCompile Include="src\selftest\selftest_tokenizer.c" />
<ClCompile Include="src\selftest\selftest_tuyaMCU.c" />
Expand Down
26 changes: 13 additions & 13 deletions src/selftest/selftest_buttonEvents.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ void Test_ButtonEvents() {
SELFTEST_ASSERT_CHANNEL(12, 0);
SELFTEST_ASSERT_CHANNEL(13, 0);
// NOTE: respect BTN_DEBOUNCE_TICKS
Sim_RunFrames(15);
Sim_RunFrames(15, false);
// user presses the button and shorts it to ground
SIM_SetSimulatedPinValue(9, false);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_CHANNEL(10, 345);
SELFTEST_ASSERT_CHANNEL(11, 0);
SELFTEST_ASSERT_CHANNEL(12, 0);
SELFTEST_ASSERT_CHANNEL(13, 0);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SIM_SetSimulatedPinValue(9, true);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_CHANNEL(10, 345);
SELFTEST_ASSERT_CHANNEL(11, 123);
SELFTEST_ASSERT_CHANNEL(12, 0);
SELFTEST_ASSERT_CHANNEL(13, 0);
// wait extra frames for the click event to fire
Sim_RunFrames(100);
Sim_RunFrames(100, false);
// the click happens with a certain delay after "onRelease" because it waits
// too see if it's a click or a double click
SELFTEST_ASSERT_CHANNEL(10, 345);
Expand All @@ -50,32 +50,32 @@ void Test_ButtonEvents() {
//
// Test OnPress and OnRelease and OnDblClick
//
Sim_RunFrames(1000);
Sim_RunFrames(1000);
Sim_RunFrames(1000, false);
Sim_RunFrames(1000, false);
SELFTEST_ASSERT_CHANNEL(10, 345);
SELFTEST_ASSERT_CHANNEL(11, 123);
SELFTEST_ASSERT_CHANNEL(12, 22);
SELFTEST_ASSERT_CHANNEL(13, 0);
// user presses the button and shorts it to ground
SIM_SetSimulatedPinValue(9, false);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_CHANNEL(10, (345 + 345));
SELFTEST_ASSERT_CHANNEL(11, 123);
SELFTEST_ASSERT_CHANNEL(12, 22);
SELFTEST_ASSERT_CHANNEL(13, 0);
// user releases it
SIM_SetSimulatedPinValue(9, true);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_CHANNEL(10, (345 + 345));
SELFTEST_ASSERT_CHANNEL(11, (123 + 123));
SELFTEST_ASSERT_CHANNEL(12, 22);
SELFTEST_ASSERT_CHANNEL(13, 0);
// within a short time, the click is repeated
// but the Click event will not get called, only DblClick
Sim_RunFrames(25);
Sim_RunFrames(25, false);
// SECOND TIME user presses the button and shorts it to ground
SIM_SetSimulatedPinValue(9, false);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
// Double Click is not like a click - it does not wait extra cycles after button release.
// It fires just after it's relased, it's quicker
// Double click will happen now:
Expand All @@ -85,12 +85,12 @@ void Test_ButtonEvents() {
SELFTEST_ASSERT_CHANNEL(13, 1201);
// user releases it
SIM_SetSimulatedPinValue(9, true);
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_CHANNEL(10, (345 + 345));
SELFTEST_ASSERT_CHANNEL(11, (123 + 123 + 123));
SELFTEST_ASSERT_CHANNEL(12, 22);
SELFTEST_ASSERT_CHANNEL(13, 1201);
Sim_RunFrames(100);
Sim_RunFrames(100, false);
SELFTEST_ASSERT_CHANNEL(10, (345 + 345));
SELFTEST_ASSERT_CHANNEL(11, (123 + 123 + 123));
SELFTEST_ASSERT_CHANNEL(12, 22);
Expand Down
42 changes: 41 additions & 1 deletion src/selftest/selftest_changeHandlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,48 @@ void Test_ChangeHandlers() {
// reset whole device
CMD_ExecuteCommand("clearAll", 0);

// this will only happens when Channel1 value changes from not equal to 0 to the one equal to 0
CMD_ExecuteCommand("addChangeHandler Channel1 == 0 addChannel 10 1111", 0);
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 0);
CMD_ExecuteCommand("setChannel 1 0",0);
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 0);
CMD_ExecuteCommand("setChannel 1 1", 0);
SELFTEST_ASSERT_CHANNEL(1, 1);
SELFTEST_ASSERT_CHANNEL(10, 0);
CMD_ExecuteCommand("setChannel 1 0", 0); // this will fire change handler first time
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 1111);
CMD_ExecuteCommand("setChannel 1 1", 0);
SELFTEST_ASSERT_CHANNEL(1, 1);
SELFTEST_ASSERT_CHANNEL(10, 1111);
CMD_ExecuteCommand("setChannel 1 0", 0); // this will fire change handler second time
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 2222);
CMD_ExecuteCommand("toggleChannel 1", 0);
SELFTEST_ASSERT_CHANNEL(1, 1);
SELFTEST_ASSERT_CHANNEL(10, 2222);
SELFTEST_ASSERT_CHANNEL(11, 0);
CMD_ExecuteCommand("toggleChannel 1", 0); // this will fire change handler next time
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 3333);
SELFTEST_ASSERT_CHANNEL(11, 0);
// let's make a change callback that will be fired from within a change callback
CMD_ExecuteCommand("addChangeHandler Channel10 != 3333 addChannel 11 22", 0);
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 3333);
SELFTEST_ASSERT_CHANNEL(11, 0);
CMD_ExecuteCommand("toggleChannel 1", 0);
SELFTEST_ASSERT_CHANNEL(1, 1);
SELFTEST_ASSERT_CHANNEL(10, 3333);
SELFTEST_ASSERT_CHANNEL(11, 0);
CMD_ExecuteCommand("toggleChannel 1", 0); // this will fire change handler next time
SELFTEST_ASSERT_CHANNEL(1, 0);
SELFTEST_ASSERT_CHANNEL(10, 4444);
SELFTEST_ASSERT_CHANNEL(11, 22);


//
}


Expand Down
20 changes: 19 additions & 1 deletion src/selftest/selftest_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ void Test_Expressions_RunTests_Basic() {
SELFTEST_ASSERT_EXPRESSION("1-1", 0);
SELFTEST_ASSERT_EXPRESSION("1+1", 2);
SELFTEST_ASSERT_EXPRESSION("-1.0-1.0", -2);

// same as above but with spaces
SELFTEST_ASSERT_EXPRESSION("-1 -1", -2);
SELFTEST_ASSERT_EXPRESSION("1 - 1", 0);
SELFTEST_ASSERT_EXPRESSION("1 + 1", 2);
SELFTEST_ASSERT_EXPRESSION(" 1 + 1 ", 2);
SELFTEST_ASSERT_EXPRESSION("-1.0 -1.0", -2);
SELFTEST_ASSERT_EXPRESSION("-1.0 - 1.0", -2);
// multiply
SELFTEST_ASSERT_EXPRESSION("5*10", 50);
SELFTEST_ASSERT_EXPRESSION("10*5", 50);
SELFTEST_ASSERT_EXPRESSION("10*0.5", 5);
SELFTEST_ASSERT_EXPRESSION("0.5*10", 5);
SELFTEST_ASSERT_EXPRESSION("0.5*0.5", 0.25);
// same as above but with spaces
SELFTEST_ASSERT_EXPRESSION("5* 10", 50);
SELFTEST_ASSERT_EXPRESSION("10 *5", 50);
SELFTEST_ASSERT_EXPRESSION("10 *0.5", 5);
SELFTEST_ASSERT_EXPRESSION("0.5 * 10", 5);
SELFTEST_ASSERT_EXPRESSION(" 0.5 * 0.5 ", 0.25);
// test spaces
SELFTEST_ASSERT_EXPRESSION("10.0", 10.0f);
SELFTEST_ASSERT_EXPRESSION(" 10.0 ", 10.0f);
Expand Down
28 changes: 26 additions & 2 deletions src/selftest/selftest_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@ void Test_HTTP_Client() {
// reset whole device
CMD_ExecuteCommand("clearAll", 0);

CMD_ExecuteCommand("SendGet http://192.168.0.103/cm?cmnd=POWER%20TOGGLE", 0);

PIN_SetPinRoleForPinIndex(9, IOR_Relay);
PIN_SetPinChannelForPinIndex(9, 1);

SELFTEST_ASSERT_CHANNEL(1, 0);

// Also nice method of testing: addRepeatingEvent 2 -1 SendGet http://192.168.0.103/cm?cmnd=POWER%20TOGGLE
///CMD_ExecuteCommand("SendGet http://192.168.0.103/cm?cmnd=POWER%20TOGGLE", 0);
//CMD_ExecuteCommand("SendGet http://192.168.0.104/cm?cmnd=POWER%20TOGGLE", 0);
//SELFTEST_ASSERT_CHANNEL(5, 666);


// The following selftest may be problematic. It does a real TCP send to our loopback 127.0.0.1 address
// It will also fail if port 80 is already in use (and not in use by us)
#if 0
// send GET to ourselves (127.0.0.1 is a localhost)
CMD_ExecuteCommand("SendGet http://127.0.0.1/cm?cmnd=POWER%20TOGGLE", 0);
Sim_RunSeconds(5, true);
SELFTEST_ASSERT_CHANNEL(1, 1);
// send GET to ourselves (127.0.0.1 is a localhost)
CMD_ExecuteCommand("SendGet http://127.0.0.1/cm?cmnd=POWER%20TOGGLE", 0);
Sim_RunSeconds(5, true);
SELFTEST_ASSERT_CHANNEL(1, 0);
// send GET to ourselves (127.0.0.1 is a localhost)
CMD_ExecuteCommand("SendGet http://127.0.0.1/cm?cmnd=POWER%20ON", 0);
Sim_RunSeconds(5, true);
SELFTEST_ASSERT_CHANNEL(1, 1);
#endif
}


Expand Down
6 changes: 3 additions & 3 deletions src/selftest/selftest_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void Test_FakeHTTPClientPacket_POST(const char *tg, const char *data);
const char *Test_GetLastHTMLReply();

// TODO: move elsewhere?
void Sim_RunMiliseconds(int ms);
void Sim_RunSeconds(float f);
void Sim_RunFrames(int n);
void Sim_RunMiliseconds(int ms, bool bApplyRealtimeWait);
void Sim_RunSeconds(float f, bool bApplyRealtimeWait);
void Sim_RunFrames(int n, bool bApplyRealtimeWait);

#endif
8 changes: 4 additions & 4 deletions src/selftest/selftest_repeatingEvents.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ void Test_RepeatingEvents() {
// NOTE: addRepeatingEvent [RepeatTime] [RepeatCount]
// This should fire once due to RepeatCount 1
CMD_ExecuteCommand("addRepeatingEvent 2 1 addChannel 10 1", 0);
Sim_RunSeconds(4.0f);
Sim_RunSeconds(4.0f, false);
SELFTEST_ASSERT_CHANNEL(10, 1);
Sim_RunSeconds(6.0f);
Sim_RunSeconds(6.0f, false);
SELFTEST_ASSERT_CHANNEL(10, 1);
// NOTE: addRepeatingEvent [RepeatTime] [RepeatCount]
// This should fire twice due to RepeatCount 2
CMD_ExecuteCommand("addRepeatingEvent 2 2 addChannel 11 1", 0);
Sim_RunSeconds(6.0f);
Sim_RunSeconds(6.0f, false);
SELFTEST_ASSERT_CHANNEL(11, 2);
Sim_RunSeconds(6.0f);
Sim_RunSeconds(6.0f, false);
SELFTEST_ASSERT_CHANNEL(11, 2);
}

Expand Down
2 changes: 1 addition & 1 deletion src/selftest/selftest_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Test_Scripting() {
SELFTEST_ASSERT_INTEGER(CMD_GetCountActiveScriptThreads(), 1);
// should execute and quit within few frames
// (we have a limit of scripting executed per frame)
Sim_RunFrames(15);
Sim_RunFrames(15, false);
SELFTEST_ASSERT_INTEGER(CMD_GetCountActiveScriptThreads(), 0);
SELFTEST_ASSERT_CHANNEL(10, 3);
SELFTEST_ASSERT_CHANNEL(11, 234);
Expand Down
8 changes: 4 additions & 4 deletions src/selftest/selftest_tuyaMCU.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ void Test_TuyaMCU_Basic() {
// This packet sets fnID 2 of type Value to 100
CMD_ExecuteCommand("tuyaMcu_fakeHex 55AA0307000802020004000000647D", 0);
// above command will just put into buffer - need at least a frame to parse it
Sim_RunFrames(1000);
Sim_RunFrames(1000, false);
// Now, channel 15 should be set to 100...
SELFTEST_ASSERT_CHANNEL(15, 100);

// This packet sets fnID 2 of type Value to 90
CMD_ExecuteCommand("tuyaMcu_fakeHex 55AA03070008020200040000005A73", 0);
// above command will just put into buffer - need at least a frame to parse it
Sim_RunFrames(1000);
Sim_RunFrames(1000,false);
// Now, channel 15 should be set to 90...
SELFTEST_ASSERT_CHANNEL(15, 90);

// This packet sets fnID 2 of type Value to 110
CMD_ExecuteCommand("tuyaMcu_fakeHex 55AA03070008020200040000006E87", 0);
// above command will just put into buffer - need at least a frame to parse it
Sim_RunFrames(1000);
Sim_RunFrames(1000, false);
// Now, channel 15 should be set to 110...
SELFTEST_ASSERT_CHANNEL(15, 110);

// This packet sets fnID 2 of type Value to 120
CMD_ExecuteCommand("tuyaMcu_fakeHex 55AA03070008020200040000007891", 0);
// above command will just put into buffer - need at least a frame to parse it
Sim_RunFrames(1000);
Sim_RunFrames(1000, false);
// Now, channel 15 should be set to 120...
SELFTEST_ASSERT_CHANNEL(15, 120);

Expand Down
15 changes: 11 additions & 4 deletions src/win_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,26 @@ void Sim_RunFrame() {
Main_OnEverySecond();
}
}
void Sim_RunMiliseconds(int ms) {
void Sim_RunMiliseconds(int ms, bool bApplyRealtimeWait) {
while (ms > 0) {
if (bApplyRealtimeWait) {
Sleep(5);
}
Sim_RunFrame();
ms -= frameTime;
}
}
void Sim_RunSeconds(float f) {
void Sim_RunSeconds(float f, bool bApplyRealtimeWait) {
int ms = f * 1000;
Sim_RunMiliseconds(ms);
Sim_RunMiliseconds(ms, bApplyRealtimeWait);
}
void Sim_RunFrames(int n) {
void Sim_RunFrames(int n, bool bApplyRealtimeWait) {
int i;

for (i = 0; i < n; i++) {
if (bApplyRealtimeWait) {
Sleep(5);
}
Sim_RunFrame();
}
}
Expand All @@ -197,6 +203,7 @@ void Win_DoUnitTests() {

Test_HTTP_Client();
Test_ExpandConstant();
Test_ChangeHandlers();
Test_RepeatingEvents();
Test_ButtonEvents();
Test_Commands_Alias();
Expand Down

0 comments on commit 4626aae

Please sign in to comment.