1 /*
2  * Copyright (c) 2023 Codecoup
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef MOCKS_BAP_STREAM_EXPECTS_H_
8 #define MOCKS_BAP_STREAM_EXPECTS_H_
9 
10 #include <stdbool.h>
11 
12 #include <zephyr/bluetooth/audio/bap.h>
13 #include <zephyr/bluetooth/iso.h>
14 #include <zephyr/net_buf.h>
15 #include <zephyr/ztest_assert.h>
16 
17 #include "bap_stream.h"
18 #include "expects_util.h"
19 
20 #define expect_bt_bap_stream_ops_configured_called_once(_stream, _pref)                            \
21 do {                                                                                               \
22 	const char *func_name = "bt_bap_stream_ops.configured";                                    \
23 												   \
24 	zexpect_call_count(func_name, 1, mock_bap_stream_configured_cb_fake.call_count);           \
25 												   \
26 	if (mock_bap_stream_configured_cb_fake.call_count > 0) {                                   \
27 		IF_NOT_EMPTY(_stream, (                                                            \
28 			zexpect_equal_ptr(_stream, mock_bap_stream_configured_cb_fake.arg0_val,    \
29 					  "'%s()' was called with incorrect '%s' value",           \
30 					  func_name, "stream");))                                  \
31 												   \
32 		IF_NOT_EMPTY(_pref, (                                                              \
33 			/* TODO */                                                                 \
34 			zassert_unreachable("Not implemented");))                                  \
35 	}                                                                                          \
36 } while (0)
37 
expect_bt_bap_stream_ops_configured_not_called(void)38 static inline void expect_bt_bap_stream_ops_configured_not_called(void)
39 {
40 	const char *func_name = "bt_bap_stream_ops.configured";
41 
42 	zexpect_call_count(func_name, 0, mock_bap_stream_configured_cb_fake.call_count);
43 }
44 
expect_bt_bap_stream_ops_qos_set_called_once(struct bt_bap_stream * stream)45 static inline void expect_bt_bap_stream_ops_qos_set_called_once(struct bt_bap_stream *stream)
46 {
47 	const char *func_name = "bt_bap_stream_ops.qos_set";
48 
49 	zexpect_call_count(func_name, 1, mock_bap_stream_qos_set_cb_fake.call_count);
50 
51 	if (mock_bap_stream_qos_set_cb_fake.call_count > 0) {
52 		zexpect_equal_ptr(stream, mock_bap_stream_qos_set_cb_fake.arg0_val,
53 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
54 	}
55 }
56 
expect_bt_bap_stream_ops_qos_set_not_called(void)57 static inline void expect_bt_bap_stream_ops_qos_set_not_called(void)
58 {
59 	const char *func_name = "bt_bap_stream_ops.qos_set";
60 
61 	zexpect_call_count(func_name, 0, mock_bap_stream_qos_set_cb_fake.call_count);
62 }
63 
expect_bt_bap_stream_ops_enabled_called_once(struct bt_bap_stream * stream)64 static inline void expect_bt_bap_stream_ops_enabled_called_once(struct bt_bap_stream *stream)
65 {
66 	const char *func_name = "bt_bap_stream_ops.enabled";
67 
68 	zexpect_call_count(func_name, 1, mock_bap_stream_enabled_cb_fake.call_count);
69 
70 	if (mock_bap_stream_enabled_cb_fake.call_count > 0) {
71 		zexpect_equal_ptr(stream, mock_bap_stream_enabled_cb_fake.arg0_val,
72 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
73 	}
74 }
75 
expect_bt_bap_stream_ops_enabled_not_called(void)76 static inline void expect_bt_bap_stream_ops_enabled_not_called(void)
77 {
78 	const char *func_name = "bt_bap_stream_ops.enabled";
79 
80 	zexpect_call_count(func_name, 0, mock_bap_stream_enabled_cb_fake.call_count);
81 }
82 
expect_bt_bap_stream_ops_metadata_updated_called_once(struct bt_bap_stream * stream)83 static inline void expect_bt_bap_stream_ops_metadata_updated_called_once(
84 								struct bt_bap_stream *stream)
85 {
86 	const char *func_name = "bt_bap_stream_ops.metadata_updated";
87 
88 	zexpect_call_count(func_name, 1, mock_bap_stream_metadata_updated_cb_fake.call_count);
89 
90 	if (mock_bap_stream_metadata_updated_cb_fake.call_count > 0) {
91 		zexpect_equal_ptr(stream, mock_bap_stream_metadata_updated_cb_fake.arg0_val,
92 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
93 	}
94 }
95 
expect_bt_bap_stream_ops_metadata_updated_not_called(void)96 static inline void expect_bt_bap_stream_ops_metadata_updated_not_called(void)
97 {
98 	const char *func_name = "bt_bap_stream_ops.metadata_updated";
99 
100 	zexpect_call_count(func_name, 0, mock_bap_stream_metadata_updated_cb_fake.call_count);
101 }
102 
expect_bt_bap_stream_ops_disabled_called_once(struct bt_bap_stream * stream)103 static inline void expect_bt_bap_stream_ops_disabled_called_once(struct bt_bap_stream *stream)
104 {
105 	const char *func_name = "bt_bap_stream_ops.disabled";
106 
107 	zexpect_call_count(func_name, 1, mock_bap_stream_disabled_cb_fake.call_count);
108 
109 	if (mock_bap_stream_disabled_cb_fake.call_count > 0) {
110 		zexpect_equal_ptr(stream, mock_bap_stream_disabled_cb_fake.arg0_val,
111 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
112 	}
113 }
114 
expect_bt_bap_stream_ops_disabled_not_called(void)115 static inline void expect_bt_bap_stream_ops_disabled_not_called(void)
116 {
117 	const char *func_name = "bt_bap_stream_ops.disabled";
118 
119 	zexpect_call_count(func_name, 0, mock_bap_stream_disabled_cb_fake.call_count);
120 }
121 
expect_bt_bap_stream_ops_released_called(const struct bt_bap_stream * streams[],unsigned int count)122 static inline void expect_bt_bap_stream_ops_released_called(const struct bt_bap_stream *streams[],
123 							    unsigned int count)
124 {
125 	const char *func_name = "bt_bap_stream_ops.released";
126 
127 	zexpect_call_count(func_name, count, mock_bap_stream_released_cb_fake.call_count);
128 
129 	for (unsigned int i = 0; i < count; i++) {
130 		bool found = false;
131 
132 		for (unsigned int j = 0; j < mock_bap_stream_released_cb_fake.call_count; j++) {
133 			found = streams[i] == mock_bap_stream_released_cb_fake.arg0_history[j];
134 			if (found) {
135 				break;
136 			}
137 		}
138 
139 		zexpect_true(found, "'%s()' not called with %p stream", func_name, streams[i]);
140 	}
141 }
142 
expect_bt_bap_stream_ops_released_called_once(const struct bt_bap_stream * stream)143 static inline void expect_bt_bap_stream_ops_released_called_once(const struct bt_bap_stream *stream)
144 {
145 	expect_bt_bap_stream_ops_released_called(&stream, 1);
146 }
147 
expect_bt_bap_stream_ops_released_not_called(void)148 static inline void expect_bt_bap_stream_ops_released_not_called(void)
149 {
150 	const char *func_name = "bt_bap_stream_ops.released";
151 
152 	zexpect_equal(0, mock_bap_stream_released_cb_fake.call_count,
153 		      "'%s()' was called unexpectedly", func_name);
154 }
155 
expect_bt_bap_stream_ops_started_called_once(struct bt_bap_stream * stream)156 static inline void expect_bt_bap_stream_ops_started_called_once(struct bt_bap_stream *stream)
157 {
158 	const char *func_name = "bt_bap_stream_ops.started";
159 
160 	zexpect_call_count(func_name, 1, mock_bap_stream_started_cb_fake.call_count);
161 
162 	if (mock_bap_stream_started_cb_fake.call_count > 0) {
163 		zexpect_equal_ptr(stream, mock_bap_stream_started_cb_fake.arg0_val,
164 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
165 	}
166 }
167 
expect_bt_bap_stream_ops_started_not_called(void)168 static inline void expect_bt_bap_stream_ops_started_not_called(void)
169 {
170 	const char *func_name = "bt_bap_stream_ops.started";
171 
172 	zexpect_call_count(func_name, 0, mock_bap_stream_started_cb_fake.call_count);
173 }
174 
175 #define expect_bt_bap_stream_ops_stopped_called_once(_stream, _reason)                             \
176 do {                                                                                               \
177 	const char *func_name = "bt_bap_stream_ops.stopped";                                       \
178 												   \
179 	zexpect_call_count(func_name, 1, mock_bap_stream_stopped_cb_fake.call_count);              \
180 												   \
181 	if (mock_bap_stream_stopped_cb_fake.call_count > 0) {                                      \
182 		IF_NOT_EMPTY(_stream, (                                                            \
183 			zexpect_equal_ptr(_stream, mock_bap_stream_stopped_cb_fake.arg0_val,       \
184 					  "'%s()' was called with incorrect '%s' value",           \
185 					  func_name, "stream");))                                  \
186 												   \
187 		IF_NOT_EMPTY(_reason, (                                                            \
188 			zexpect_equal(_reason, mock_bap_stream_stopped_cb_fake.arg1_val,           \
189 				      "'%s()' was called with incorrect '%s' value",               \
190 				      func_name, "reason");))                                      \
191 	}                                                                                          \
192 } while (0)
193 
expect_bt_bap_stream_ops_stopped_not_called(void)194 static inline void expect_bt_bap_stream_ops_stopped_not_called(void)
195 {
196 	const char *func_name = "bt_bap_stream_ops.stopped";
197 
198 	zexpect_call_count(func_name, 0, mock_bap_stream_stopped_cb_fake.call_count);
199 }
200 
201 static inline void
expect_bt_bap_stream_ops_connected_called_once(const struct bt_bap_stream * stream)202 expect_bt_bap_stream_ops_connected_called_once(const struct bt_bap_stream *stream)
203 {
204 	const char *func_name = "bt_bap_stream_ops.connected";
205 
206 	zexpect_call_count(func_name, 1, mock_bap_stream_connected_cb_fake.call_count);
207 
208 	if (mock_bap_stream_connected_cb_fake.call_count > 0) {
209 		zexpect_equal_ptr(stream, mock_bap_stream_connected_cb_fake.arg0_val,
210 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
211 	}
212 }
213 
214 static inline void
expect_bt_bap_stream_ops_connected_called_twice(const struct bt_bap_stream * stream)215 expect_bt_bap_stream_ops_connected_called_twice(const struct bt_bap_stream *stream)
216 {
217 	const char *func_name = "bt_bap_stream_ops.connected";
218 
219 	zexpect_call_count(func_name, 2, mock_bap_stream_connected_cb_fake.call_count);
220 
221 	if (mock_bap_stream_connected_cb_fake.call_count > 0) {
222 		zexpect_equal_ptr(stream, mock_bap_stream_connected_cb_fake.arg0_val,
223 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
224 	}
225 }
226 
227 static inline void
expect_bt_bap_stream_ops_disconnected_called_once(const struct bt_bap_stream * stream)228 expect_bt_bap_stream_ops_disconnected_called_once(const struct bt_bap_stream *stream)
229 {
230 	const char *func_name = "bt_bap_stream_ops.disconnected";
231 
232 	zexpect_call_count(func_name, 1, mock_bap_stream_disconnected_cb_fake.call_count);
233 
234 	if (mock_bap_stream_disconnected_cb_fake.call_count > 0) {
235 		zexpect_equal_ptr(stream, mock_bap_stream_disconnected_cb_fake.arg0_val,
236 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
237 	}
238 }
239 
expect_bt_bap_stream_ops_recv_called_once(struct bt_bap_stream * stream,const struct bt_iso_recv_info * info,struct net_buf * buf)240 static inline void expect_bt_bap_stream_ops_recv_called_once(struct bt_bap_stream *stream,
241 							     const struct bt_iso_recv_info *info,
242 							     struct net_buf *buf)
243 {
244 	const char *func_name = "bt_bap_stream_ops.recv";
245 
246 	zexpect_call_count(func_name, 1, mock_bap_stream_recv_cb_fake.call_count);
247 
248 	if (mock_bap_stream_recv_cb_fake.call_count > 0) {
249 		zexpect_equal_ptr(stream, mock_bap_stream_recv_cb_fake.arg0_val,
250 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
251 	}
252 
253 	/* TODO: validate info && buf */
254 }
255 
expect_bt_bap_stream_ops_recv_not_called(void)256 static inline void expect_bt_bap_stream_ops_recv_not_called(void)
257 {
258 	const char *func_name = "bt_bap_stream_ops.recv";
259 
260 	zexpect_call_count(func_name, 0, mock_bap_stream_recv_cb_fake.call_count);
261 }
262 
expect_bt_bap_stream_ops_sent_called_once(struct bt_bap_stream * stream)263 static inline void expect_bt_bap_stream_ops_sent_called_once(struct bt_bap_stream *stream)
264 {
265 	const char *func_name = "bt_bap_stream_ops.sent";
266 
267 	zexpect_call_count(func_name, 1, mock_bap_stream_sent_cb_fake.call_count);
268 
269 	if (mock_bap_stream_sent_cb_fake.call_count > 0) {
270 		zexpect_equal_ptr(stream, mock_bap_stream_sent_cb_fake.arg0_val,
271 				  "'%s()' was called with incorrect '%s'", func_name, "stream");
272 	}
273 }
274 
expect_bt_bap_stream_ops_sent_not_called(void)275 static inline void expect_bt_bap_stream_ops_sent_not_called(void)
276 {
277 	const char *func_name = "bt_bap_stream_ops.sent";
278 
279 	zexpect_call_count(func_name, 0, mock_bap_stream_sent_cb_fake.call_count);
280 }
281 
282 #endif /* MOCKS_BAP_STREAM_EXPECTS_H_ */
283