-
Notifications
You must be signed in to change notification settings - Fork 19
tests: unittest: Added unittests for ble_bas #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
You can find the documentation preview for this PR here. |
bc3f68a
to
6b6e791
Compare
6b6e791
to
6dab329
Compare
6dab329
to
3faff88
Compare
a353f5d
to
7d114c2
Compare
7d114c2
to
2b0df21
Compare
No milestone required for tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good but I have an important comment.
We should not try to peek into or manually modify struct ble_bas
.
In general that's only used for initial configuration (which may not be the best idea). Afterwards the user is meant to use only the API to interact with the service. So we can't really assume that the values in there have to be set to a certain value, only that the API returns the correct value. Also, we shouldn't manipulate that struct in our tests to put the library in a given state, rather, we should simulate input to the library via BLE events, to confirm that the library is in a certain state (by using API calls).
2b0df21
to
ad6a36b
Compare
__cmock_sd_ble_gatts_hvx_ExpectAnyArgsAndReturn(NRF_ERROR_INVALID_STATE); | ||
ret = ble_bas_battery_level_update(&ble_bas, conn_handle, battery_level); | ||
TEST_ASSERT_EQUAL(-EPIPE, ret); | ||
TEST_ASSERT_EQUAL(battery_level, ble_bas.battery_level); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't test this (you are testing inside ble_bas
)
TEST_ASSERT_EQUAL(BLE_GATT_HVX_NOTIFICATION, p_hvx_params->type); | ||
TEST_ASSERT_EQUAL(0, p_hvx_params->offset); | ||
TEST_ASSERT_EQUAL(sizeof(uint8_t), *(p_hvx_params->p_len)); | ||
TEST_ASSERT_EQUAL(ble_bas.battery_level, *(uint8_t *)(p_hvx_params->p_data)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
/* Battery level hasn't change 'Nothing to do' */ | ||
ret = ble_bas_battery_level_update(&ble_bas, conn_handle, battery_level); | ||
TEST_ASSERT_EQUAL(0, ret); | ||
TEST_ASSERT_EQUAL(battery_level, ble_bas.battery_level); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
TEST_ASSERT_EQUAL(SERVICE_HANDLE, ble_bas.service_handle); | ||
TEST_ASSERT_EQUAL(VALUE_HANDLE, ble_bas.battery_level_handles.value_handle); | ||
TEST_ASSERT_EQUAL(REPORT_REF_HANDLE, ble_bas.report_ref_handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't test these like that-- but you can try to call some API that would pass these values as a parameter to a SoftDevice call to verify that they were set as expected during ble_bas_init()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example ble_bas_battery_level_notify()
passes the field battery_level_handles.value_handle
to the sd_ble_gatts_hvx()
function
ret = ble_bas_init(&ble_bas, &bas_cfg); | ||
TEST_ASSERT_EQUAL(0, ret); | ||
TEST_ASSERT_EQUAL(SERVICE_HANDLE, ble_bas.service_handle); | ||
TEST_ASSERT_EQUAL(VALUE_HANDLE, ble_bas.battery_level_handles.value_handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too, you can call the ble_bas_battery_level_notify()
function to see that the hvx
parameters have the correct handle for the value.
/* Change battery level, and ble_bas should update but not notify */ | ||
battery_level = 42; | ||
__cmock_sd_ble_gatts_value_set_ExpectAnyArgsAndReturn(NRF_SUCCESS); | ||
ret = ble_bas_battery_level_update(&ble_bas, conn_handle, battery_level); | ||
TEST_ASSERT_EQUAL(0, ret); | ||
TEST_ASSERT_EQUAL(battery_level, ble_bas.battery_level); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of accepting any arguments to sd_ble_gatts_value_set()
you should check that the implementation is passing the value that the user provided when calling ble_bas_battery_level_updated()
. The implementation may pass one value to sd_ble_gatts_value_set()
and a set different value in ble_bas.battery_level
(due to a bug).
2eba186
to
9e7f915
Compare
#include "ble_gatts.h" | ||
#include "cmock_ble_gatts.h" | ||
|
||
#define SERVICE_HANDLE 0x1234 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alignment here could be improved a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the alignment is different in my editor. It seems like tabsize is set to 4 in this PR
#define REPORT_REF_HANDLE 0xF8EE | ||
#define INVALID_HANDLE 0xFFFF | ||
|
||
BLE_BAS_DEF(ble_bas); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a newline under this line.
static uint8_t battery_level; | ||
static int evt_handler_called; | ||
static int hvx_stub_called; | ||
static struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some space in between these structs.
bas_cfg.can_notify = false; | ||
bas_init(&bas_cfg); | ||
|
||
/* Battery level hasn't change 'Nothing to do' */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* Battery level hasn't change 'Nothing to do' */ | |
/* Battery level hasn't changed 'Nothing to do' */ |
{ | ||
memset(&ble_bas, 0, sizeof(ble_bas)); | ||
evt_handler_called = false; | ||
battery_level = 55; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If 55
is a default/reference value, maybe it can be a macro?
Added unit tests for ble_bas service Signed-off-by: Martin Engesvold <[email protected]> Co-authored-by: Andreas Moltumyr <[email protected]>
9e7f915
to
101275b
Compare
Added unit tests for ble_bas service