Skip to content

Commit c6bca57

Browse files
committed
BLE: Improve generic gatt client tests documentation.
1 parent b689a59 commit c6bca57

File tree

6 files changed

+155
-46
lines changed

6 files changed

+155
-46
lines changed

features/FEATURE_BLE/tests/generic/GattClient/TestCharacteristicDesctiptorDiscovery.cpp

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ struct ConstructibleDiscoveredCharacteristic : public DiscoveredCharacteristic {
8484
}
8585
};
8686

87+
/**
88+
* Test fixture used for client descriptor discovery testing.
89+
*/
8790
class TestGattClientDescriptorDiscovery : public ::testing::Test {
8891
protected:
8992
TestGattClientDescriptorDiscovery() :
@@ -179,8 +182,18 @@ TEST_F(TestGattClientDescriptorDiscovery, descriptor_discovery_on_characteristic
179182
EXPECT_EQ(err, BLE_ERROR_NONE);
180183
}
181184

185+
/**
186+
* Test parameter pass into tests using TestGattClientDescriptorDiscoveryP fixture.
187+
* - first element: value handle of the characteristic.
188+
* - second element: last handle of the characteristic
189+
* - third element: expected transactions; each transaction can contain multiple
190+
* pair containing the handle of the descriptor and its UUID.
191+
*/
182192
typedef tuple<uint16_t, uint16_t, vector<vector<pair<uint16_t, UUID>>>> test_param_t;
183193

194+
/**
195+
* Parametric fixture used for descriptor discovery testing.
196+
*/
184197
class TestGattClientDescriptorDiscoveryP :
185198
public TestGattClientDescriptorDiscovery,
186199
public ::testing::WithParamInterface<test_param_t> {
@@ -224,16 +237,20 @@ struct MockFindInformationResponse : public AttFindInformationResponse {
224237
vector<pair<uint16_t, UUID>> _response;
225238
};
226239

227-
240+
/**
241+
* Helper returning a DiscoveredCharacteristic from a DiscoveryCallbackParams_t*
242+
*/
228243
static const DiscoveredCharacteristic& get_characteristic(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t* p) {
229244
return p->characteristic;
230245
}
231246

247+
/**
248+
* Helper returning a DiscoveredCharacteristicDescriptor from a DiscoveryCallbackParams_t*
249+
*/
232250
static const DiscoveredCharacteristicDescriptor& get_descriptor(const CharacteristicDescriptorDiscovery::DiscoveryCallbackParams_t* p) {
233251
return p->descriptor;
234252
}
235253

236-
237254
/*
238255
* Given a discovered characteristic with the value handle not equal to the
239256
* last handle of the characteristic.
@@ -333,6 +350,7 @@ TEST_P(TestGattClientDescriptorDiscoveryP, descriptor_discovery) {
333350
EXPECT_EQ(err, BLE_ERROR_NONE);
334351
}
335352

353+
// Instantiation of the tests cases relying on the parametric fixture
336354
INSTANTIATE_TEST_CASE_P(
337355
TestGattClientDescriptorDiscoveryP_combination,
338356
TestGattClientDescriptorDiscoveryP,
@@ -649,36 +667,3 @@ INSTANTIATE_TEST_CASE_P(
649667
)
650668
);
651669

652-
// errors: Invalid handle if:
653-
// starting handle > ending handle
654-
// stating handle == 00
655-
// ending handle > last handle on the server
656-
657-
// if no handle will be return => ATTRIBUTE NOT FOUND
658-
659-
// Complete when ATTRIBUTE NOT FOUND is returned or an attribute handle
660-
// in the response is equal to the ending handle in the request.
661-
662-
663-
// Find information response:
664-
// format & [(handle, UUID)]
665-
// format == 1 => 16 bit UUID
666-
// format == 2 => 128 bit UUID
667-
668-
669-
/*
670-
* Given a discovered characteristic with the value handle not equal to the
671-
* last handle of the characteristic.
672-
* when the client launch the discovery of the descriptor of the characteristic.
673-
* Then:
674-
* - the client invoke the pal function discover_characteristics_descriptors
675-
* with an handle range starting at characteristic value handle + 1 and ending
676-
* at the last characteristic handle.
677-
* - The pal will reply with a FindInformationResponse containing the
678-
* descriptors discovered.
679-
* When the client call terminateCharacteristicDescriptorDiscovery the termination
680-
* callback is called immediately.
681-
*/
682-
683-
684-

features/FEATURE_BLE/tests/generic/GattClient/TestDiscoverAllServices.cpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ using ::testing::AllOf;
5656
using ::testing::Property;
5757
using ::testing::InSequence;
5858

59+
/*
60+
* Parametric fixture used for service discovery.
61+
*
62+
* Parameters are modeled by a tuple where:
63+
* - [0] std::tuple<bool, bool>: pair of boolean indicating the presence of the
64+
* service callback and the characteristic callback.
65+
* - [1] UUID: UUID used for service filtering.
66+
* - [2] UUID: UUID used for characteristic filtering.
67+
* - [3] server_description_t: Discovered service layout.
68+
*/
5969
class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
6070
std::tuple<std::tuple<bool, bool>, UUID, UUID, server_description_t>
6171
> {
@@ -74,6 +84,7 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
7484
server_stub() {
7585
}
7686

87+
// setup the test environment
7788
virtual void SetUp() {
7889
gatt_client.onServiceDiscoveryTermination(
7990
makeFunctionPointer(
@@ -93,6 +104,10 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
93104
std::tie(has_service_cb, has_characteristic_cb) = cb_combination;
94105
}
95106

107+
/*
108+
* Return a closure that sends a read by group type response to a client.
109+
* @param[in] service_transactions The services to include in the response.
110+
*/
96111
auto reply_read_by_group_type(const std::vector<service_description_t>& service_transactions) {
97112
return Invoke([service_transactions, this](connection_handle_t connection, attribute_handle_t handle) -> ble_error_t {
98113
//Log::info() << "discover primary service (" << connection << "," << handle << ")" << std::endl;
@@ -123,6 +138,11 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
123138
});
124139
}
125140

141+
/*
142+
* Return a closure that sends a find by type value response to the client.
143+
* @param[in] service_transactions Services to include in the response sent
144+
* to the client.
145+
*/
126146
auto reply_find_by_type_value(const std::vector<service_description_t>& service_transactions) {
127147
return Invoke([service_transactions, this](connection_handle_t connection, attribute_handle_t handle, const UUID& uuid) -> ble_error_t {
128148
//Log::info() << "discover primary service by uuid(" << connection << "," << handle << "," << uuid << ")" << std::endl;
@@ -146,6 +166,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
146166
});
147167
}
148168

169+
/**
170+
* Convert a DiscoveredCharacteristic::Properties_t into an uint8_t
171+
*/
149172
uint8_t properties_to_byte(DiscoveredCharacteristic::Properties_t p) {
150173
return (
151174
p.broadcast() << 0 |
@@ -159,6 +182,11 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
159182
);
160183
}
161184

185+
/**
186+
* Return a closure that sends a read by type to a client.
187+
* @param[in] transaction Characteristics to include in the response send to
188+
* the client.
189+
*/
162190
auto reply_read_by_type(const std::vector<characteristic_description_t>& transaction) {
163191
return Invoke([transaction, this](connection_handle_t connection, attribute_handle_range_t range) -> ble_error_t {
164192
//Log::info() << "discover characteristic(" << connection << "," << range.begin << "," << range.end << ")" << std::endl;
@@ -188,6 +216,11 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
188216
});
189217
}
190218

219+
/*
220+
* Return a closure that send an error response to the client.
221+
* @param[in] opcode Opcode that caused the error.
222+
* @param[in] error_code Error code.
223+
*/
191224
auto reply_error(AttributeOpcode opcode, AttErrorResponse::AttributeErrorCode error_code) {
192225
return InvokeWithoutArgs([this, opcode, error_code]() -> ble_error_t {
193226
//Log::info() << "reply error: opcode = " << (uint8_t) opcode << ", error_code = " << error_code << std::endl;
@@ -199,8 +232,10 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
199232
});
200233
}
201234

202-
// helper
203-
// note sequence insured by caller
235+
/*
236+
* Set service discovery expectation when the procedure discover all services
237+
* is used.
238+
*/
204239
void set_discover_all_services_expectations() {
205240
auto services_transactions = get_services_transactions();
206241

@@ -233,6 +268,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
233268
}
234269
}
235270

271+
/*
272+
* Set client expectations when the discover services by UUID is used.
273+
*/
236274
void set_discover_services_by_uuid_expectations() {
237275
std::vector<std::vector<uint8_t>> services_response;
238276
auto services_transactions = get_services_transactions();
@@ -276,6 +314,10 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
276314
}
277315
}
278316

317+
/*
318+
* Set an expectation regarding call of the service discovery callback with
319+
* the services @p services.
320+
*/
279321
void set_services_callback_expectations(const std::vector<service_description_t>& services) {
280322
if (!has_service_cb) {
281323
return;
@@ -286,6 +328,10 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
286328
}
287329
}
288330

331+
/*
332+
* Set an expectation regarding call of the service discovery callback with
333+
* the service @p service.
334+
*/
289335
void set_service_callback_expectation(const service_description_t& service) {
290336
if (!has_service_cb) {
291337
return;
@@ -313,7 +359,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
313359
}));
314360
}
315361

316-
362+
/*
363+
* Set expectations for characteristic discovery.
364+
*/
317365
void set_discover_characteristics_expectations() {
318366
auto services = get_services_discovered();
319367

@@ -411,6 +459,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
411459
}));
412460
}
413461

462+
/*
463+
* Compute the transactions involved during the service discovery process.
464+
*/
414465
std::vector<std::vector<service_description_t>> get_services_transactions() {
415466
std::vector<service_description_t> working_set = get_services_discovered();
416467

@@ -440,6 +491,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
440491
return result;
441492
}
442493

494+
/*
495+
* Compute the list of services discovered during service discovery.
496+
*/
443497
std::vector<service_description_t> get_services_discovered() {
444498
std::vector<service_description_t> working_set;
445499

@@ -456,6 +510,9 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
456510
return working_set;
457511
}
458512

513+
/*
514+
* Compute the transactions present during the characteristic discovery process.
515+
*/
459516
std::vector<std::vector<characteristic_description_t>> get_characteristics_transactions(const service_description_t& service) {
460517
std::vector<std::vector<characteristic_description_t>> transactions;
461518
for (const auto& characteristic : service.characteristics) {
@@ -495,7 +552,8 @@ class LaunchDiscoveryNoServiceFilter : public ::testing::TestWithParam<
495552
};
496553

497554
TEST_P(LaunchDiscoveryNoServiceFilter, regular) {
498-
555+
/////////////////////////////
556+
// Install default calls
499557
ON_CALL(
500558
mock_client, discover_primary_service(_, _)
501559
).WillByDefault(Invoke([](connection_handle_t connection, attribute_handle_t handle) -> ble_error_t {
@@ -533,6 +591,8 @@ TEST_P(LaunchDiscoveryNoServiceFilter, regular) {
533591
}));
534592
}
535593

594+
/////////////////////////////
595+
// Install expectations
536596
{
537597
InSequence seq;
538598
if (service_filter == UUID()) {
@@ -548,6 +608,8 @@ TEST_P(LaunchDiscoveryNoServiceFilter, regular) {
548608
EXPECT_CALL(termination_callback, call(connection_handle));
549609
}
550610

611+
/////////////////////////////
612+
// Launch the discovery process.
551613
ble_error_t err = gatt_client.launchServiceDiscovery(
552614
connection_handle,
553615
has_service_cb ?
@@ -568,14 +630,19 @@ TEST_P(LaunchDiscoveryNoServiceFilter, regular) {
568630
INSTANTIATE_TEST_CASE_P(
569631
GattClient_launch_discovery_no_service_filter,
570632
LaunchDiscoveryNoServiceFilter,
633+
// Yield all combination of each generator
571634
::testing::Combine(
635+
// service cb generator
572636
::testing::Values(
573637
std::tuple<bool, bool>(true, false),
574638
std::tuple<bool, bool>(false, true),
575639
std::tuple<bool, bool>(true, true)
576640
),
641+
// service UUID filter generator
577642
::testing::Values(UUID(), UUID(0x1452), UUID("a3d1495f-dba7-4441-99f2-d0a20f663422")),
643+
// characteristic UUID filter generator
578644
::testing::Values(UUID(), UUID(0xBEEF), UUID("1f551ee3-aef4-4719-8c52-8b419fc4ac01")),
645+
// server layout generator.
579646
::testing::Values(
580647
server_description_t { },
581648
server_description_t {

features/FEATURE_BLE/tests/generic/GattClient/TestNoCb.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ using ble::pal::vendor::mock::MockPalGattClient;
3838

3939
using ::testing::_;
4040

41-
42-
class LaunchDiscoveryNoCb :
43-
public ::testing::TestWithParam<std::tuple<UUID, UUID>> {
41+
/*
42+
* Parametric fixture used to test service discovery when no callback is registered.
43+
* Parameters are:
44+
* - [0]: service UUID filter
45+
* - [1]: characteristic UUID filter
46+
*/
47+
class LaunchDiscoveryNoCb : public ::testing::TestWithParam<std::tuple<UUID, UUID>> {
4448
protected:
4549
typedef std::tuple<UUID, UUID> parameters_t;
4650

@@ -160,11 +164,15 @@ TEST_P(LaunchDiscoveryNoCb, shall_not_change_discovery_status) {
160164
EXPECT_TRUE(_gatt_client.isServiceDiscoveryActive());
161165
}
162166

167+
// Instantiate tests cases with the given parameters
163168
INSTANTIATE_TEST_CASE_P(
164169
GattClient_launch_discovery_no_cb,
165170
LaunchDiscoveryNoCb,
171+
// Yield combination of each generator value
166172
::testing::Combine(
173+
// service UUID filter
167174
::testing::Values(UUID(), UUID(0x1452), UUID("a3d1495f-dba7-4441-99f2-d0a20f663422")),
175+
// characteristic UUID filter
168176
::testing::Values(UUID(), UUID(0xBEEF), UUID("1f551ee3-aef4-4719-8c52-8b419fc4ac01"))
169177
)
170178
);

features/FEATURE_BLE/tests/generic/GattClient/TestRead.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ static vector<uint8_t> make_char_value(uint16_t length) {
6363
return characteristic_value;
6464
}
6565

66+
/*
67+
* fixture used to test GattClient::read.
68+
*/
6669
class TestGattClientRead : public ::testing::Test {
6770
protected:
6871
TestGattClientRead() :
@@ -273,6 +276,12 @@ TEST_F(TestGattClientRead, read_with_out_of_range_offset_shall_fail) {
273276
}
274277
}
275278

279+
/*
280+
* Parametric fixture used to test error generated during GattClient::read.
281+
* Parameters are:
282+
* - [0] The attribute error code
283+
* - [1] Expected status returned in the read callback.
284+
*/
276285
class TestGattClientReadAttributeError :
277286
public TestGattClientRead,
278287
public ::testing::WithParamInterface<tuple<AttErrorResponse::AttributeErrorCode, ble_error_t>> {
@@ -384,6 +393,7 @@ TEST_P(TestGattClientReadAttributeError, read_with_offset) {
384393
EXPECT_EQ(err, BLE_ERROR_NONE);
385394
}
386395

396+
// Instantiate test cases using TestGattClientReadAttributeError
387397
INSTANTIATE_TEST_CASE_P(
388398
TestGattClientReadAttributeError_combination,
389399
TestGattClientReadAttributeError,

0 commit comments

Comments
 (0)