Skip to content

Commit 4652593

Browse files
committed
tests: Bluetooth: ASCS: Modify unittests to native_sim
Modify the unit tests to use the native_sim board instead of the unit_testing board. The native_sim board allows us to use the kernel in a more regular way, while still being able to treat the tests as unittest. To support mocking we cannot rely on the default Kconfig options and values. The required Kconfig options to satisfy dependencies are overridden in the Kconfig file and the necessary Kconfig files are sourced. We cannot enable e.g. CONFIG_BT as that would attempt to pull in a lot of files we do not want in this test. This commit also removes unused includes in the CMakelists, and cleans up the code. Since the kernel is actually running, we also need to support that e.g. k_work items are actually scheduled, and that we have to wait for them to finish. test_drain_syswq has been implemented as a way to drain the system workqueue before we check for callbacks or perform subsequent actions. Signed-off-by: Emil Gydesen <[email protected]>
1 parent b8541c5 commit 4652593

File tree

11 files changed

+215
-60
lines changed

11 files changed

+215
-60
lines changed

tests/bluetooth/audio/ascs/CMakeLists.txt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@
22

33
cmake_minimum_required(VERSION 3.20.0)
44

5-
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
66

77
project(bluetooth_ascs)
88

9-
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/ascs/uut uut)
9+
target_include_directories(app PRIVATE
10+
${ZEPHYR_BASE}/subsys/bluetooth/
1011

11-
target_link_libraries(testbinary PRIVATE uut)
12+
${ZEPHYR_BASE}/tests/bluetooth/audio/ascs/include
13+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/include
14+
)
1215

13-
target_include_directories(testbinary PRIVATE include)
16+
target_sources(app PRIVATE
17+
# Test source files
18+
src/main.c
19+
src/test_ase_control_params.c
20+
src/test_ase_register.c
21+
src/test_ase_state_transition_invalid.c
22+
src/test_ase_state_transition.c
23+
src/test_common.c
1424

15-
target_sources(testbinary
16-
PRIVATE
17-
src/main.c
18-
src/test_ase_control_params.c
19-
src/test_ase_register.c
20-
src/test_ase_state_transition_invalid.c
21-
src/test_ase_state_transition.c
22-
src/test_common.c
25+
# UUT files
26+
uut/bap_unicast_client.c
27+
uut/bap_unicast_server.c
28+
29+
# Stack source file
30+
${ZEPHYR_BASE}/subsys/bluetooth/audio/ascs.c
31+
${ZEPHYR_BASE}/subsys/bluetooth/audio/audio.c
32+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_iso.c
33+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_stream.c
34+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_unicast_server.c
35+
${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c
36+
37+
# Mock files
38+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/bap_stream.c
39+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/conn.c
40+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/gatt.c
41+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/iso.c
42+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/pacs.c
2343
)

tests/bluetooth/audio/ascs/Kconfig

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config BT_ATT_PREPARE_COUNT
5+
int
6+
default 1
7+
8+
config BT_AUDIO
9+
bool
10+
default y
11+
12+
config BT_BONDABLE
13+
bool
14+
default y
15+
16+
config BT_BUF_ACL_RX_SIZE
17+
int
18+
default 69
19+
20+
config BT_BUF_ACL_TX_COUNT
21+
int
22+
default 5
23+
24+
config BT_BUF_EVT_RX_COUNT
25+
int
26+
default 10
27+
28+
config BT_CONN
29+
bool
30+
default y
31+
32+
config BT_MAX_PAIRED
33+
int
34+
default 1
35+
36+
config BT_GATT_DYNAMIC_DB
37+
bool
38+
default y
39+
40+
config BT_ISO_PERIPHERAL
41+
bool
42+
default y
43+
44+
config BT_ISO_MAX_CHAN
45+
int
46+
default 10
47+
48+
config BT_L2CAP_TX_MTU
49+
int
50+
default 65
51+
52+
config BT_LOG
53+
bool
54+
default y
55+
56+
config BT_MAX_CONN
57+
int
58+
default 1
59+
60+
config BT_PERIPHERAL
61+
bool
62+
default y
63+
64+
config BT_SMP
65+
bool
66+
default y
67+
68+
# Include Zephyr's Kconfig.
69+
source "Kconfig"
70+
source "subsys/bluetooth/audio/Kconfig"
71+
source "subsys/bluetooth/Kconfig.logging"

tests/bluetooth/audio/ascs/include/test_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct test_ase_cp_chrc_value_hdr {
4040
void test_mocks_init(void);
4141
void test_mocks_cleanup(void);
4242
void test_mocks_reset(void);
43+
void test_drain_syswq(void);
4344

4445
/* Initialize connection object for test */
4546
void test_conn_init(struct bt_conn *conn);

tests/bluetooth/audio/ascs/prj.conf

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
CONFIG_ZTEST=y
22

3-
CONFIG_BT=y
4-
CONFIG_BT_SMP=y
5-
CONFIG_BT_MAX_CONN=1
6-
CONFIG_BT_PERIPHERAL=y
7-
CONFIG_BT_ISO_PERIPHERAL=y
8-
CONFIG_BT_ISO_MAX_CHAN=1
9-
CONFIG_BT_GATT_DYNAMIC_DB=y
10-
CONFIG_BT_AUDIO=y
3+
CONFIG_NET_BUF=y
4+
115
CONFIG_BT_ASCS=y
126
CONFIG_BT_PAC_SNK=y
137
CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT=2
@@ -16,9 +10,6 @@ CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT=2
1610
CONFIG_BT_ASCS_MAX_ACTIVE_ASES=1
1711
CONFIG_BT_BAP_UNICAST_SERVER=y
1812

19-
# Mandatory to support at least 1 for ASCS
20-
CONFIG_BT_ATT_PREPARE_COUNT=1
21-
2213
CONFIG_LOG=y
2314
CONFIG_BT_ASCS_LOG_LEVEL_DBG=y
2415
CONFIG_BT_BAP_ISO_LOG_LEVEL_DBG=y

tests/bluetooth/audio/ascs/src/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ ZTEST_F(ascs_test_suite, test_release_ase_on_callback_unregister)
201201
/* Unregister the callbacks - which will clean up the ASCS */
202202
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
203203

204+
test_drain_syswq(); /* Ensure that state transitions are completed */
205+
204206
/* Expected to notify the upper layers */
205207
expect_bt_bap_unicast_server_cb_release_called_once(stream);
206208
expect_bt_bap_stream_ops_released_called_once(stream);
@@ -434,6 +436,8 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_streaming_state)
434436
/* Mock CIS disconnection */
435437
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_TIMEOUT);
436438

439+
test_drain_syswq(); /* Ensure that state transitions are completed */
440+
437441
/* Expected to notify the upper layers */
438442
expect_bt_bap_stream_ops_qos_set_called_once(stream);
439443
expect_bt_bap_stream_ops_disabled_called_once(stream);
@@ -478,6 +482,8 @@ static void test_cis_link_loss_in_disabling_state(struct ascs_test_suite_fixture
478482
/* Mock CIS disconnection */
479483
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_TIMEOUT);
480484

485+
test_drain_syswq(); /* Ensure that state transitions are completed */
486+
481487
/* Expected to notify the upper layers */
482488
expect_bt_bap_stream_ops_qos_set_called_once(stream);
483489
expect_bt_bap_stream_ops_disabled_not_called();
@@ -526,6 +532,10 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state)
526532
/* Mock CIS disconnection */
527533
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_TIMEOUT);
528534

535+
test_drain_syswq(); /* Ensure that state transitions are completed */
536+
537+
k_work_queue_drain(&k_sys_work_q, false);
538+
529539
/* Expected no change in ASE state */
530540
expect_bt_bap_stream_ops_qos_set_not_called();
531541
expect_bt_bap_stream_ops_released_not_called();
@@ -534,6 +544,10 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state)
534544
err = bt_bap_stream_disable(stream);
535545
zassert_equal(0, err, "Failed to disable stream: err %d", err);
536546

547+
test_drain_syswq(); /* Ensure that state transitions are completed */
548+
549+
k_work_queue_drain(&k_sys_work_q, false);
550+
537551
if (IS_ENABLED(CONFIG_BT_ASCS_ASE_SNK)) {
538552
expect_bt_bap_stream_ops_qos_set_called_once(stream);
539553
expect_bt_bap_stream_ops_disabled_called_once(stream);
@@ -573,6 +587,10 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state_client_retries)
573587
/* Mock CIS disconnection */
574588
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_FAIL_TO_ESTAB);
575589

590+
test_drain_syswq(); /* Ensure that state transitions are completed */
591+
592+
k_work_queue_drain(&k_sys_work_q, false);
593+
576594
/* Expected to not notify the upper layers */
577595
expect_bt_bap_stream_ops_qos_set_not_called();
578596
expect_bt_bap_stream_ops_released_not_called();
@@ -588,6 +606,8 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state_client_retries)
588606
zassert_equal(0, err, "bt_bap_stream_start err %d", err);
589607
}
590608

609+
test_drain_syswq(); /* Ensure that state transitions are completed */
610+
591611
expect_bt_bap_stream_ops_connected_called_twice(stream);
592612
expect_bt_bap_stream_ops_started_called_once(stream);
593613
}

0 commit comments

Comments
 (0)