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