Skip to content

Conversation

MechanicV1
Copy link
Contributor

Adding unit tests to increase coverage for
ble_hrs library

@MechanicV1 MechanicV1 requested review from a team as code owners August 25, 2025 10:24
Copy link

You can find the documentation preview for this PR here.

@eivindj-nordic eivindj-nordic added this to the v0.9.0 milestone Aug 27, 2025
@lemrey
Copy link
Contributor

lemrey commented Sep 8, 2025

No milestone required for tests

@lemrey lemrey removed this from the v0.9.0 milestone Sep 8, 2025
Copy link
Contributor

@lemrey lemrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, a few comments regarding splitting the tests.
Most importantly, we should not try to peek into struct ble_hrs. 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).

Comment on lines 190 to 192
/* Try to use null for hrs struct */
err = ble_hrs_sensor_contact_detected_update(NULL, true);
TEST_ASSERT_EQUAL(-EFAULT, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a dedicated test for that test_ble_hrs_sensor_contact_supported_set_efault() so this could be removed from here.

Comment on lines 265 to 266
err = ble_hrs_init(NULL, NULL);
TEST_ASSERT_EQUAL(-EFAULT, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a dedicated test

Comment on lines +268 to +313
__cmock_sd_ble_gatts_service_add_IgnoreAndReturn(NRF_ERROR_INVALID_ADDR);
err = ble_hrs_init(&hrs, &hrs_config);
TEST_ASSERT_EQUAL(-EINVAL, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a dedicated test

Comment on lines 271 to 275
TEST_ASSERT_EQUAL_PTR(hrs_config.evt_handler, hrs.evt_handler);
TEST_ASSERT_EQUAL(BLE_CONN_HANDLE_INVALID, hrs.conn_handle);
TEST_ASSERT_EQUAL(0, hrs.rr_interval_count);
TEST_ASSERT_EQUAL(hrs_config.is_sensor_contact_supported, hrs.is_sensor_contact_supported);
TEST_ASSERT_FALSE(hrs.is_sensor_contact_detected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the function ble_hrs_init() returns an error, then we can't make any assumption to the contents of struct ble_hrs (whether they are correct or not). In general I don't think we should check what's in struct ble_hrs at all, since the user is not supposed to read or write directly into that, but use the API.

Comment on lines 232 to 233
err = ble_hrs_heart_rate_measurement_send(NULL, heart_rate_measurement);
TEST_ASSERT_EQUAL(-EFAULT, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a separate test

__cmock_sd_ble_gatts_hvx_IgnoreAndReturn(BLE_ERROR_INVALID_CONN_HANDLE);
err = ble_hrs_heart_rate_measurement_send(&hrs, heart_rate_measurement);
TEST_ASSERT_EQUAL(-ENOTCONN, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too

Comment on lines +239 to +255
__cmock_sd_ble_gatts_hvx_ExpectAndReturn(hrs.conn_handle, NULL, NRF_ERROR_INVALID_STATE);
__cmock_sd_ble_gatts_hvx_IgnoreArg_p_hvx_params();
err = ble_hrs_heart_rate_measurement_send(&hrs, heart_rate_measurement);
TEST_ASSERT_EQUAL(-EPIPE, err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these are groups in this function are separate tests

Comment on lines 163 to 166
/* Try to set sensor contact supported while in connection
* Simulate being in a connection
*/
hrs.conn_handle = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not do it like so-- here we assume that once the device enters a connection, the service correctly updates its conn_handle field. That may not be the case at all. Instead, we should dispatch a BLE_GAP_CONNECTED event and then attempt to change ble_hrs_sensor_contact_supported_set() to see if the function correctly returns an error.

Comment on lines +63 to +77
struct ble_hrs hrs = {
.evt_handler = ble_hrs_evt_handler,
.service_handle = 0,
.conn_handle = BLE_CONN_HANDLE_INVALID,
.rr_interval_count = 0,
.max_hrm_len = 0,
.is_sensor_contact_supported = false,
.is_sensor_contact_detected = false
};
int err;

/* Initialize the heart rate service. */
memset(&hrs, 0, sizeof(hrs));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First you initialize hrs to something and then you zero it, which one do you want to do?

Adding unit tests to increase coverage for
ble_hrs library

Signed-off-by: Nirmal Krishna <[email protected]>
Co-authored-by: Andreas Moltumyr <[email protected]>
#include "cmock_ble_gatts.h"

void ble_hrs_evt_handler(struct ble_hrs *hrs, const struct ble_hrs_evt *evt)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good with some tests on what is received here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests for ble_hrs_conn_params_evt should also improve the coverage.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants