• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 #include <cstdlib>
19 #include <cstring>
20 #include <functional>
21 #include <string>
22 #include <unordered_set>
23 
24 #include <gtest/gtest.h>
25 
26 #define LOG_TAG "SysAudio_Test"
27 #include <log/log.h>
28 #include <media/PatchBuilder.h>
29 #include <system/audio.h>
30 
31 using namespace android;
32 
TEST(SystemAudioTest,PatchInvalid)33 TEST(SystemAudioTest, PatchInvalid) {
34     audio_patch patch{};
35     ASSERT_FALSE(audio_patch_is_valid(&patch));
36     patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1;
37     patch.num_sinks = 1;
38     ASSERT_FALSE(audio_patch_is_valid(&patch));
39     patch.num_sources = 1;
40     patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1;
41     ASSERT_FALSE(audio_patch_is_valid(&patch));
42     patch.num_sources = 0;
43     patch.num_sinks = 1;
44     ASSERT_FALSE(audio_patch_is_valid(&patch));
45 }
46 
TEST(SystemAudioTest,PatchValid)47 TEST(SystemAudioTest, PatchValid) {
48     const audio_port_config src = {
49         .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
50     // It's OK not to have sinks.
51     ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).patch()));
52     const audio_port_config sink = {
53         .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
54     ASSERT_TRUE(audio_patch_is_valid((PatchBuilder{}).addSource(src).addSink(sink).patch()));
55     ASSERT_TRUE(audio_patch_is_valid(
56                     (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
57     ASSERT_TRUE(audio_patch_is_valid(
58                     (PatchBuilder{}).addSource(src).addSink(sink).addSink(sink).patch()));
59     ASSERT_TRUE(audio_patch_is_valid(
60                     (PatchBuilder{}).addSource(src).addSource(src).
61                     addSink(sink).addSink(sink).patch()));
62 }
63 
TEST(SystemAudioTest,PatchHwAvSync)64 TEST(SystemAudioTest, PatchHwAvSync) {
65     audio_port_config device_src_cfg = {
66         .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
67     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
68     device_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
69     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_src_cfg));
70     device_src_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
71     ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_src_cfg));
72 
73     audio_port_config device_sink_cfg = {
74         .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
75     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
76     device_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
77     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
78     device_sink_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
79     ASSERT_TRUE(audio_port_config_has_hw_av_sync(&device_sink_cfg));
80 
81     audio_port_config mix_sink_cfg = {
82         .id = 1, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_MIX };
83     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
84     mix_sink_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
85     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
86     mix_sink_cfg.flags.input = AUDIO_INPUT_FLAG_HW_AV_SYNC;
87     ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_sink_cfg));
88 
89     audio_port_config mix_src_cfg = {
90         .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_MIX };
91     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
92     mix_src_cfg.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
93     ASSERT_FALSE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
94     mix_src_cfg.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
95     ASSERT_TRUE(audio_port_config_has_hw_av_sync(&mix_src_cfg));
96 }
97 
TEST(SystemAudioTest,PatchEqual)98 TEST(SystemAudioTest, PatchEqual) {
99     const audio_patch patch1{}, patch2{};
100     // Invalid patches are not equal.
101     ASSERT_FALSE(audio_patches_are_equal(&patch1, &patch2));
102     const audio_port_config src = {
103         .id = 1, .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE };
104     const audio_port_config sink = {
105         .id = 2, .role = AUDIO_PORT_ROLE_SINK, .type = AUDIO_PORT_TYPE_DEVICE };
106     ASSERT_FALSE(audio_patches_are_equal(
107                     (PatchBuilder{}).addSource(src).patch(),
108                     (PatchBuilder{}).addSource(src).addSink(sink).patch()));
109     ASSERT_TRUE(audio_patches_are_equal(
110                     (PatchBuilder{}).addSource(src).addSink(sink).patch(),
111                     (PatchBuilder{}).addSource(src).addSink(sink).patch()));
112     ASSERT_FALSE(audio_patches_are_equal(
113                     (PatchBuilder{}).addSource(src).addSink(sink).patch(),
114                     (PatchBuilder{}).addSource(src).addSource(src).addSink(sink).patch()));
115     audio_port_config sink_hw_av_sync = sink;
116     sink_hw_av_sync.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
117     sink_hw_av_sync.flags.output = AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
118     ASSERT_FALSE(audio_patches_are_equal(
119                     (PatchBuilder{}).addSource(src).addSink(sink).patch(),
120                     (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
121     ASSERT_TRUE(audio_patches_are_equal(
122                     (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch(),
123                     (PatchBuilder{}).addSource(src).addSink(sink_hw_av_sync).patch()));
124 }
125 
runAudioDeviceTypeHelperFunction(const std::unordered_set<audio_devices_t> & allDevices,const audio_devices_t targetDevices[],unsigned int targetDeviceCount,const std::string & deviceTag,bool (* device_type_helper_function)(audio_devices_t))126 void runAudioDeviceTypeHelperFunction(const std::unordered_set<audio_devices_t>& allDevices,
127                                       const audio_devices_t targetDevices[],
128                                       unsigned int targetDeviceCount,
129                                       const std::string& deviceTag,
130                                       bool (*device_type_helper_function)(audio_devices_t))
131 {
132     std::unordered_set<audio_devices_t> devices(targetDevices, targetDevices + targetDeviceCount);
133     for (auto device : allDevices) {
134         if (devices.find(device) == devices.end()) {
135             ASSERT_FALSE(device_type_helper_function(device))
136                     << std::hex << device << " should not be " << deviceTag << " device";
137         } else {
138             ASSERT_TRUE(device_type_helper_function(device))
139                     << std::hex << device << " should be " << deviceTag << " device";
140         }
141     }
142 }
143 
TEST(SystemAudioTest,AudioDeviceTypeHelperFunction)144 TEST(SystemAudioTest, AudioDeviceTypeHelperFunction) {
145     std::unordered_set<audio_devices_t> allDeviceTypes;
146     allDeviceTypes.insert(std::begin(AUDIO_DEVICE_OUT_ALL_ARRAY),
147             std::end(AUDIO_DEVICE_OUT_ALL_ARRAY));
148     allDeviceTypes.insert(std::begin(AUDIO_DEVICE_IN_ALL_ARRAY),
149             std::end(AUDIO_DEVICE_IN_ALL_ARRAY));
150 
151     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_ARRAY,
152             std::size(AUDIO_DEVICE_OUT_ALL_ARRAY), "output", audio_is_output_device);
153     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_ARRAY,
154             std::size(AUDIO_DEVICE_IN_ALL_ARRAY), "input", audio_is_input_device);
155     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY,
156             std::size(AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY), "a2dp out", audio_is_a2dp_out_device);
157     const audio_devices_t bluetoothInA2dpDevices[] = { AUDIO_DEVICE_IN_BLUETOOTH_A2DP };
158     runAudioDeviceTypeHelperFunction(allDeviceTypes, bluetoothInA2dpDevices,
159             std::size(bluetoothInA2dpDevices), "a2dp in", audio_is_a2dp_in_device);
160     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_SCO_ARRAY,
161             std::size(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), "bluetooth out sco",
162             audio_is_bluetooth_out_sco_device);
163     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_SCO_ARRAY,
164             std::size(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), "bluetooth in sco",
165             audio_is_bluetooth_in_sco_device);
166     const unsigned int scoDeviceCount = AUDIO_DEVICE_OUT_SCO_CNT + AUDIO_DEVICE_IN_SCO_CNT;
167     audio_devices_t scoDevices[scoDeviceCount];
168     std::copy(std::begin(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_OUT_ALL_SCO_ARRAY),
169               std::begin(scoDevices));
170     std::copy(std::begin(AUDIO_DEVICE_IN_ALL_SCO_ARRAY), std::end(AUDIO_DEVICE_IN_ALL_SCO_ARRAY),
171               std::begin(scoDevices) + AUDIO_DEVICE_OUT_SCO_CNT);
172     runAudioDeviceTypeHelperFunction(allDeviceTypes, scoDevices,
173             std::size(scoDevices), "bluetooth sco", audio_is_bluetooth_sco_device);
174     const audio_devices_t hearingAidOutDevices[] = { AUDIO_DEVICE_OUT_HEARING_AID };
175     runAudioDeviceTypeHelperFunction(allDeviceTypes, hearingAidOutDevices,
176             std::size(hearingAidOutDevices), "hearing aid out", audio_is_hearing_aid_out_device);
177     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_USB_ARRAY,
178             std::size(AUDIO_DEVICE_OUT_ALL_USB_ARRAY), "usb out", audio_is_usb_out_device);
179     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_USB_ARRAY,
180             std::size(AUDIO_DEVICE_IN_ALL_USB_ARRAY), "usb in", audio_is_usb_in_device);
181     const audio_devices_t remoteSubmixDevices[] = {
182             AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX };
183     runAudioDeviceTypeHelperFunction(allDeviceTypes, remoteSubmixDevices,
184             std::size(remoteSubmixDevices), "remote submix", audio_is_remote_submix_device);
185     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY,
186             std::size(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY), "digital out",
187             audio_is_digital_out_device);
188     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY,
189             std::size(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY), "digital in",
190             audio_is_digital_in_device);
191     const unsigned int digitalDeviceCount
192             = AUDIO_DEVICE_OUT_DIGITAL_CNT + AUDIO_DEVICE_IN_DIGITAL_CNT;
193     audio_devices_t digitalDevices[digitalDeviceCount];
194     std::copy(std::begin(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
195               std::end(AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY),
196               std::begin(digitalDevices));
197     std::copy(std::begin(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
198               std::end(AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY),
199               std::begin(digitalDevices) + AUDIO_DEVICE_OUT_DIGITAL_CNT);
200     runAudioDeviceTypeHelperFunction(allDeviceTypes, digitalDevices,
201               std::size(digitalDevices), "digital", audio_device_is_digital);
202     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_ALL_BLE_ARRAY,
203             std::size(AUDIO_DEVICE_OUT_ALL_BLE_ARRAY), "ble out",
204             audio_is_ble_out_device);
205     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_IN_ALL_BLE_ARRAY,
206             std::size(AUDIO_DEVICE_IN_ALL_BLE_ARRAY), "ble in",
207             audio_is_ble_in_device);
208     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY,
209             std::size(AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY), "ble unicast",
210             audio_is_ble_unicast_device);
211     runAudioDeviceTypeHelperFunction(allDeviceTypes, AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY,
212             std::size(AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY), "ble broadcast",
213             audio_is_ble_broadcast_device);
214 }
215 
216 
217 // An array whose length is AUDIO_PORT_MAX_CHANNEL_MASKS for generating audio port information.
218 static constexpr audio_channel_mask_t OUT_CHANNEL_MASKS[AUDIO_PORT_MAX_CHANNEL_MASKS] = {
219     AUDIO_CHANNEL_OUT_FRONT_LEFT,
220     AUDIO_CHANNEL_OUT_FRONT_RIGHT,
221     AUDIO_CHANNEL_OUT_FRONT_CENTER,
222     AUDIO_CHANNEL_OUT_LOW_FREQUENCY,
223     AUDIO_CHANNEL_OUT_BACK_LEFT,
224     AUDIO_CHANNEL_OUT_BACK_RIGHT,
225     AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER,
226     AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER,
227     AUDIO_CHANNEL_OUT_BACK_CENTER,
228     AUDIO_CHANNEL_OUT_SIDE_LEFT,
229     AUDIO_CHANNEL_OUT_SIDE_RIGHT,
230     AUDIO_CHANNEL_OUT_TOP_CENTER,
231     AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT,
232     AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER,
233     AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT,
234     AUDIO_CHANNEL_OUT_TOP_BACK_LEFT,
235     AUDIO_CHANNEL_OUT_TOP_BACK_CENTER,
236     AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT,
237     AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT,
238     AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT,
239     AUDIO_CHANNEL_OUT_HAPTIC_A,
240     AUDIO_CHANNEL_OUT_HAPTIC_B,
241     AUDIO_CHANNEL_OUT_MONO,
242     AUDIO_CHANNEL_OUT_STEREO,
243     AUDIO_CHANNEL_OUT_2POINT1,
244     AUDIO_CHANNEL_OUT_TRI,
245     AUDIO_CHANNEL_OUT_TRI_BACK,
246     AUDIO_CHANNEL_OUT_3POINT1,
247     AUDIO_CHANNEL_OUT_2POINT0POINT2,
248     AUDIO_CHANNEL_OUT_2POINT1POINT2,
249     AUDIO_CHANNEL_OUT_3POINT0POINT2,
250     AUDIO_CHANNEL_OUT_QUAD
251 };
252 
253 // An array whose length is AUDIO_PORT_MAX_CHANNEL_MASKS for generating audio port information.
254 static constexpr audio_channel_mask_t IN_CHANNEL_MASKS[AUDIO_PORT_MAX_CHANNEL_MASKS] = {
255     AUDIO_CHANNEL_IN_LEFT,
256     AUDIO_CHANNEL_IN_RIGHT,
257     AUDIO_CHANNEL_IN_FRONT,
258     AUDIO_CHANNEL_IN_BACK,
259     AUDIO_CHANNEL_IN_LEFT_PROCESSED,
260     AUDIO_CHANNEL_IN_RIGHT_PROCESSED,
261     AUDIO_CHANNEL_IN_FRONT_PROCESSED,
262     AUDIO_CHANNEL_IN_BACK_PROCESSED,
263     AUDIO_CHANNEL_IN_PRESSURE,
264     AUDIO_CHANNEL_IN_X_AXIS,
265     AUDIO_CHANNEL_IN_Y_AXIS,
266     AUDIO_CHANNEL_IN_Z_AXIS,
267     AUDIO_CHANNEL_IN_BACK_LEFT,
268     AUDIO_CHANNEL_IN_BACK_RIGHT,
269     AUDIO_CHANNEL_IN_CENTER,
270     AUDIO_CHANNEL_IN_LOW_FREQUENCY,
271     AUDIO_CHANNEL_IN_TOP_LEFT,
272     AUDIO_CHANNEL_IN_TOP_RIGHT,
273     AUDIO_CHANNEL_IN_VOICE_UPLINK,
274     AUDIO_CHANNEL_IN_VOICE_DNLINK,
275     AUDIO_CHANNEL_IN_MONO,
276     AUDIO_CHANNEL_IN_STEREO,
277     AUDIO_CHANNEL_IN_FRONT_BACK,
278     AUDIO_CHANNEL_IN_6,
279     AUDIO_CHANNEL_IN_2POINT0POINT2,
280     AUDIO_CHANNEL_IN_2POINT1POINT2,
281     AUDIO_CHANNEL_IN_3POINT0POINT2,
282     AUDIO_CHANNEL_IN_3POINT1POINT2,
283     AUDIO_CHANNEL_IN_5POINT1,
284     AUDIO_CHANNEL_IN_VOICE_UPLINK_MONO,
285     AUDIO_CHANNEL_IN_VOICE_DNLINK_MONO,
286     AUDIO_CHANNEL_IN_VOICE_CALL_MONO
287 };
288 
289 static constexpr unsigned int SHORT_AUDIO_DESCRIPTOR_LENGTH = 3;
290 
291 using SystemAudioPortTestParams = std::tuple<audio_port_role_t, audio_port_type_t>;
292 
293 class SystemAudioPortTest : public testing::TestWithParam<SystemAudioPortTestParams> {
294 protected:
295     const struct audio_gain_config mGainConfig = {
296             .index = 0,
297             .mode = AUDIO_GAIN_MODE_JOINT,
298             .channel_mask = AUDIO_CHANNEL_OUT_FRONT_LEFT,
299             .values = {1, 2},
300             .ramp_duration_ms = 10
301     };
302     const struct audio_gain mGain = {
303             .mode = AUDIO_GAIN_MODE_JOINT,
304             .channel_mask = AUDIO_CHANNEL_OUT_FRONT_LEFT,
305             .min_value = 10,
306             .max_value = 100,
307             .default_value = 42,
308             .step_value = 2,
309             .min_ramp_ms = 10,
310             .max_ramp_ms = 20
311     };
312     const std::string mName = "SystemAudioPortTestName";
313     const size_t mLastFormat = 0x7f;
314     const audio_input_flags_t mInputFlag = AUDIO_INPUT_FLAG_FAST;
315     const audio_output_flags_t mOutputFlag = AUDIO_OUTPUT_FLAG_FAST;
316     const audio_module_handle_t mHwModule = 1;
317     const std::string mAddress = "SystemAudioPortTestAddress";
318     const audio_devices_t mInputDeviceType = AUDIO_DEVICE_IN_BUILTIN_MIC;
319     const audio_devices_t mOutputDeviceType = AUDIO_DEVICE_OUT_SPEAKER;
320     const audio_io_handle_t mIoHandle = 1;
321     const audio_stream_type_t mStream = AUDIO_STREAM_MUSIC;
322     const audio_source_t mSource = AUDIO_SOURCE_MIC;
323     const audio_session_t mSession = AUDIO_SESSION_DEVICE;
324 
325     size_t fillFakeFormats(audio_format_t formats[], size_t numFormats);
326     void fillFakeAudioPortConfigInfo(struct audio_port_config* config);
327     void fillFakeAudioPortInfo(struct audio_port* audioPort);
328     void fillFakeAudioPortV7Info(struct audio_port_v7* portV7, bool containsExtraAudioDescriptor);
329 
330     template <typename T, typename U, typename Func>
updateFieldAndCompare(const T updatedValue,T U::* field,U * lhs,U * rhs,Func p)331     void updateFieldAndCompare(const T updatedValue, T U::*field, U* lhs, U* rhs, Func p) {
332         lhs->*field = updatedValue;
333         ASSERT_FALSE(p(lhs, rhs));
334         lhs->*field = rhs->*field;
335         ASSERT_TRUE(p(lhs, rhs));
336     }
337 
338     template <typename T, typename Func>
updateConfigFieldAndCompare(const T updatedValue,T audio_port_config::* field,struct audio_port_config * lhs,struct audio_port_config * rhs,unsigned int configMask,Func p)339     void updateConfigFieldAndCompare(const T updatedValue, T audio_port_config::*field,
340             struct audio_port_config* lhs, struct audio_port_config* rhs,
341             unsigned int configMask, Func p) {
342         lhs->*field = updatedValue;
343         ASSERT_FALSE(p(lhs, rhs));
344         unsigned int savedConfigMask = lhs->config_mask;
345         lhs->config_mask &= ~configMask;
346         rhs->config_mask = lhs->config_mask;
347         ASSERT_TRUE(p(lhs, rhs));
348         lhs->config_mask = savedConfigMask;
349         rhs->config_mask = savedConfigMask;
350         lhs->*field = rhs->*field;
351         ASSERT_TRUE(p(lhs, rhs));
352     }
353 
354     template <typename T>
fillFakeAudioPortBaseInfo(T * port)355     void fillFakeAudioPortBaseInfo(T* port) {
356         port->id = 1;
357         port->role = std::get<0>(GetParam());
358         port->type = std::get<1>(GetParam());
359         // Intentionally make a name that is not ended with '\0' to test the conversion function.
360         strncpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN);
361         port->num_gains = AUDIO_PORT_MAX_GAINS;
362         for (size_t i = 0; i < port->num_gains; ++i) {
363             port->gains[i] = mGain;
364             port->gains[i].max_ramp_ms *= (i + 1);
365         }
366         fillFakeAudioPortConfigInfo(&port->active_config);
367         switch (port->type) {
368         case AUDIO_PORT_TYPE_DEVICE:
369             port->ext.device.hw_module = mHwModule;
370             port->ext.device.type = port->role == AUDIO_PORT_ROLE_SINK ? mOutputDeviceType
371                                                                        : mInputDeviceType;
372             strncpy(port->ext.device.address, mAddress.c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
373 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
374             port->ext.device.encapsulation_modes = AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM;
375             port->ext.device.encapsulation_metadata_types =
376                     AUDIO_ENCAPSULATION_METADATA_TYPE_FRAMEWORK_TUNER;
377 #endif
378             break;
379         case AUDIO_PORT_TYPE_MIX:
380             port->ext.mix = {
381                     mHwModule,
382                     mIoHandle,
383                     AUDIO_LATENCY_NORMAL
384             };
385             break;
386         case AUDIO_PORT_TYPE_SESSION:
387             port->ext.session.session = mSession;
388             break;
389         default:
390             // Must not happen
391             FAIL() << "Unknown port type " << port->type;
392         }
393     }
394 
395     template <typename T, typename Func>
testAudioPortExtBaseEquivalent(T * lhs,T * rhs,Func p)396     void testAudioPortExtBaseEquivalent(T* lhs, T* rhs, Func p) {
397         switch (lhs->type) {
398         case AUDIO_PORT_TYPE_DEVICE:
399             lhs->ext.device.hw_module = rhs->ext.device.hw_module + 1;
400             ASSERT_FALSE(p(lhs, rhs));
401             lhs->ext.device.hw_module = rhs->ext.device.hw_module;
402             ASSERT_TRUE(p(lhs, rhs));
403             lhs->ext.device.type = AUDIO_DEVICE_NONE;
404             ASSERT_FALSE(p(lhs, rhs));
405             lhs->ext.device.type = rhs->ext.device.type;
406             ASSERT_TRUE(p(lhs, rhs));
407             memset(lhs->ext.device.address, 0, sizeof(lhs->ext.device.address));
408             ASSERT_FALSE(p(lhs, rhs));
409             strncpy(lhs->ext.device.address, rhs->ext.device.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
410             ASSERT_TRUE(p(lhs, rhs));
411             break;
412         case AUDIO_PORT_TYPE_MIX:
413             lhs->ext.mix.hw_module = rhs->ext.mix.hw_module + 1;
414             ASSERT_FALSE(p(lhs, rhs));
415             lhs->ext.mix.hw_module = rhs->ext.mix.hw_module;
416             ASSERT_TRUE(p(lhs, rhs));
417             lhs->ext.mix.handle = rhs->ext.mix.handle + 1;
418             ASSERT_FALSE(p(lhs, rhs));
419             lhs->ext.mix.handle = rhs->ext.mix.handle;
420             ASSERT_TRUE(p(lhs, rhs));
421             break;
422         case AUDIO_PORT_TYPE_SESSION:
423             lhs->ext.session.session = AUDIO_SESSION_NONE;
424             ASSERT_FALSE(p(lhs, rhs));
425             lhs->ext.session.session = rhs->ext.session.session;
426             ASSERT_TRUE(p(lhs, rhs));
427             break;
428         default:
429             break;
430         }
431     }
432 
433     template <typename T, typename Func>
testAudioPortEquivalent(T * lhs,T * rhs,Func p)434     void testAudioPortEquivalent(T* lhs, T* rhs, Func p) {
435         updateFieldAndCompare(rhs->id + 1, &T::id, lhs, rhs, p);
436         updateFieldAndCompare(AUDIO_PORT_ROLE_NONE, &T::role, lhs, rhs, p);
437         updateFieldAndCompare(AUDIO_PORT_TYPE_NONE, &T::type, lhs, rhs, p);
438         memset(lhs->name, 0, sizeof(lhs->name));
439         ASSERT_FALSE(p(lhs, rhs));
440         strncpy(lhs->name, rhs->name, AUDIO_PORT_MAX_NAME_LEN);
441         ASSERT_TRUE(p(lhs, rhs));
442         updateFieldAndCompare(rhs->num_gains + 1, &T::num_gains, lhs, rhs, p);
443 
444         lhs->gains[0] = {};
445         ASSERT_FALSE(p(lhs, rhs));
446         lhs->gains[0] = rhs->gains[0];
447         ASSERT_TRUE(p(lhs, rhs));
448 
449         testAudioPortExtBaseEquivalent(lhs, rhs, p);
450         switch (lhs->type) {
451         case AUDIO_PORT_TYPE_DEVICE:
452 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
453             lhs->ext.device.encapsulation_modes = AUDIO_ENCAPSULATION_MODE_NONE;
454             ASSERT_FALSE(p(lhs, rhs));
455             lhs->ext.device.encapsulation_modes = rhs->ext.device.encapsulation_modes;
456             ASSERT_TRUE(p(lhs, rhs));
457             lhs->ext.device.encapsulation_metadata_types = AUDIO_ENCAPSULATION_METADATA_TYPE_NONE;
458             ASSERT_FALSE(p(lhs, rhs));
459             lhs->ext.device.encapsulation_metadata_types =
460                     rhs->ext.device.encapsulation_metadata_types;
461             ASSERT_TRUE(p(lhs, rhs));
462 #endif
463             break;
464         case AUDIO_PORT_TYPE_MIX:
465             lhs->ext.mix.latency_class = AUDIO_LATENCY_LOW;
466             ASSERT_FALSE(p(lhs, rhs));
467             lhs->ext.mix.latency_class = rhs->ext.mix.latency_class;
468             ASSERT_TRUE(p(lhs, rhs));
469             break;
470         default:
471             break;
472         }
473     }
474 
475     template <typename T, typename U, typename Func>
testAudioPortCapabilityArraysEquivalent(T values[],size_t size,const T & replacedValue,U * lhs,U * rhs,Func p)476     void testAudioPortCapabilityArraysEquivalent(T values[], size_t size, const T& replacedValue,
477             U* lhs, U* rhs, Func p) {
478         ASSERT_GT(size, 1);
479         std::swap(values[0], values[1]);
480         ASSERT_TRUE(p(lhs, rhs));
481         T savedValue = values[0];
482         values[0] = replacedValue;
483         ASSERT_FALSE(p(lhs, rhs));
484         values[0] = savedValue;
485         ASSERT_TRUE(p(lhs, rhs));
486     }
487 };
488 
fillFakeAudioPortConfigInfo(struct audio_port_config * config)489 void SystemAudioPortTest::fillFakeAudioPortConfigInfo(struct audio_port_config* config) {
490     config->id = 0;
491     config->role = std::get<0>(GetParam());
492     config->type = std::get<1>(GetParam());
493     config->config_mask = AUDIO_PORT_CONFIG_ALL;
494     config->sample_rate = 48000;
495     config->channel_mask = audio_port_config_has_input_direction(config) ?
496             AUDIO_CHANNEL_IN_MONO : AUDIO_CHANNEL_OUT_MONO;
497     config->format = AUDIO_FORMAT_PCM_16_BIT;
498     config->gain = mGainConfig;
499     if (audio_port_config_has_input_direction(config)) {
500         config->flags.input = mInputFlag;
501     } else {
502         config->flags.output = mOutputFlag;
503     }
504     switch (config->type) {
505     case AUDIO_PORT_TYPE_DEVICE:
506         config->ext.device.hw_module = mHwModule;
507         config->ext.device.type =
508                 config->role == AUDIO_PORT_ROLE_SINK ? mOutputDeviceType : mInputDeviceType;
509         strncpy(config->ext.device.address, mAddress.c_str(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
510         break;
511     case AUDIO_PORT_TYPE_MIX:
512         config->ext.mix.hw_module = mHwModule;
513         config->ext.mix.handle = mIoHandle;
514         if (config->role == AUDIO_PORT_ROLE_SOURCE) {
515             config->ext.mix.usecase.stream = mStream;
516         } else {
517             config->ext.mix.usecase.source = mSource;
518         }
519         break;
520     case AUDIO_PORT_TYPE_SESSION:
521         config->ext.session.session = mSession;
522         break;
523     default:
524         // Must not happen
525         FAIL() << "Unknown port type " << config->type;
526     }
527 }
528 
fillFakeFormats(audio_format_t formats[],size_t numFormats)529 size_t SystemAudioPortTest::fillFakeFormats(audio_format_t formats[], size_t numFormats) {
530     size_t j = 0;
531     size_t format = 0x1;
532     while (j < numFormats) {
533         while (format <= mLastFormat) {
534             if (audio_is_valid_format(static_cast<audio_format_t>(format << 24))) {
535                 break;
536             }
537             format++;
538         }
539         if (format > mLastFormat) {
540             break;
541         }
542         formats[j++] = static_cast<audio_format_t>((format++) << 24);
543     }
544     return j;
545 }
546 
fillFakeAudioPortInfo(struct audio_port * audioPort)547 void SystemAudioPortTest::fillFakeAudioPortInfo(struct audio_port* audioPort) {
548     fillFakeAudioPortBaseInfo(audioPort);
549     // Use the maximum number of sample rates, formats, channel masks and gains for test
550     audioPort->num_sample_rates = AUDIO_PORT_MAX_SAMPLING_RATES;
551     for (size_t i = 0; i < audioPort->num_sample_rates; ++i) {
552         // The numbers doesn't make any difference.
553         audioPort->sample_rates[i] = (i + 1) * 1000;
554     }
555     audioPort->num_channel_masks = AUDIO_PORT_MAX_CHANNEL_MASKS;
556     auto channelMasks = audioPort->role == AUDIO_PORT_ROLE_SINK ? OUT_CHANNEL_MASKS
557                                                                 : IN_CHANNEL_MASKS;
558     std::copy(channelMasks, channelMasks+audioPort->num_channel_masks,
559             std::begin(audioPort->channel_masks));
560     audioPort->num_formats = AUDIO_PORT_MAX_FORMATS;
561     audioPort->num_formats = fillFakeFormats(audioPort->formats, audioPort->num_formats);
562 }
563 
fillFakeAudioPortV7Info(struct audio_port_v7 * portV7,bool containsExtraAudioDescriptor)564 void SystemAudioPortTest::fillFakeAudioPortV7Info(struct audio_port_v7* portV7,
565                                                   bool containsExtraAudioDescriptor) {
566     fillFakeAudioPortBaseInfo(portV7);
567     audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
568     portV7->num_audio_profiles = fillFakeFormats(formats, AUDIO_PORT_MAX_FORMATS);
569     for (size_t i = 0; i < portV7->num_audio_profiles; ++i) {
570         portV7->audio_profiles[i].format = formats[i];
571         // Use the maximum number of sample rates, formats, channel masks and gains for test
572         portV7->audio_profiles[i].num_sample_rates = AUDIO_PORT_MAX_SAMPLING_RATES;
573         for (size_t j = 0; j < portV7->audio_profiles[i].num_sample_rates; ++j) {
574             // The numbers doesn't make any difference.
575             portV7->audio_profiles[i].sample_rates[j] = rand();
576         }
577         portV7->audio_profiles[i].num_channel_masks = AUDIO_PORT_MAX_CHANNEL_MASKS;
578         auto channelMasks = portV7->role == AUDIO_PORT_ROLE_SINK ? OUT_CHANNEL_MASKS
579                                                                  : IN_CHANNEL_MASKS;
580         std::copy(channelMasks, channelMasks+portV7->audio_profiles[i].num_channel_masks,
581                 std::begin(portV7->audio_profiles[i].channel_masks));
582     }
583     if (containsExtraAudioDescriptor) {
584         portV7->num_extra_audio_descriptors = AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS;
585         for (size_t i = 0; i < portV7->num_extra_audio_descriptors; ++i) {
586             portV7->extra_audio_descriptors[i].standard = AUDIO_STANDARD_EDID;
587             portV7->extra_audio_descriptors[i].descriptor_length = SHORT_AUDIO_DESCRIPTOR_LENGTH;
588             for (unsigned int j = 0; j < SHORT_AUDIO_DESCRIPTOR_LENGTH; ++j) {
589                 portV7->extra_audio_descriptors[i].descriptor[j] = rand() % 254 + 1;
590             }
591             portV7->extra_audio_descriptors[i].encapsulation_type =
592                     AUDIO_ENCAPSULATION_TYPE_IEC61937;
593         }
594     }
595 }
596 
TEST_F(SystemAudioPortTest,AudioGainConfigEquivalentTest)597 TEST_F(SystemAudioPortTest, AudioGainConfigEquivalentTest) {
598     struct audio_gain_config lhs = mGainConfig;
599     struct audio_gain_config rhs = mGainConfig;
600     ASSERT_TRUE(audio_gain_config_are_equal(&lhs, &rhs));
601     lhs.index = rhs.index + 1;
602     ASSERT_TRUE(audio_gain_config_are_equal(&lhs, &rhs));
603     lhs.index = rhs.index;
604     lhs.values[0] = rhs.values[0] + 1;
605     ASSERT_FALSE(audio_gain_config_are_equal(&lhs, &rhs));
606     lhs.values[0] = rhs.values[0];
607     updateFieldAndCompare(rhs.ramp_duration_ms + 10, &audio_gain_config::ramp_duration_ms,
608             &lhs, &rhs, audio_gain_config_are_equal);
609     for (const audio_gain_mode_t mode : {AUDIO_GAIN_MODE_CHANNELS, AUDIO_GAIN_MODE_RAMP}) {
610         lhs.mode = mode;
611         ASSERT_FALSE(audio_gain_config_are_equal(&lhs, &rhs));
612         rhs.mode = lhs.mode;
613         ASSERT_TRUE(audio_gain_config_are_equal(&lhs, &rhs));
614         lhs.values[2] = rhs.values[2] + 1;
615         ASSERT_TRUE(audio_gain_config_are_equal(&lhs, &rhs));
616         lhs.values[2] = rhs.values[2];
617         lhs.values[0] = rhs.values[0] + 1;
618         ASSERT_FALSE(audio_gain_config_are_equal(&lhs, &rhs));
619         lhs.values[0] = rhs.values[0];
620     }
621 }
622 
TEST_F(SystemAudioPortTest,AudioGainEquivalentTest)623 TEST_F(SystemAudioPortTest, AudioGainEquivalentTest) {
624     struct audio_gain lhs = mGain;
625     struct audio_gain rhs = mGain;
626     ASSERT_TRUE(audio_gains_are_equal(&lhs, &rhs));
627 
628     lhs.mode = AUDIO_GAIN_MODE_CHANNELS;
629     ASSERT_FALSE(audio_gains_are_equal(&lhs, &rhs));
630     rhs.mode = lhs.mode;
631     ASSERT_TRUE(audio_gains_are_equal(&lhs, &rhs));
632     updateFieldAndCompare(static_cast<audio_channel_mask_t>(rhs.channel_mask << 1),
633             &audio_gain::channel_mask, &lhs, &rhs, audio_gains_are_equal);
634 
635     updateFieldAndCompare(rhs.min_value + 10, &audio_gain::min_value,
636             &lhs, &rhs, audio_gains_are_equal);
637     updateFieldAndCompare(rhs.max_value + 10, &audio_gain::max_value,
638             &lhs, &rhs, audio_gains_are_equal);
639     updateFieldAndCompare(rhs.default_value + 10, &audio_gain::default_value,
640             &lhs, &rhs, audio_gains_are_equal);
641     updateFieldAndCompare(rhs.step_value + 10, &audio_gain::step_value,
642             &lhs, &rhs, audio_gains_are_equal);
643     updateFieldAndCompare(rhs.min_ramp_ms + 10, &audio_gain::min_ramp_ms,
644             &lhs, &rhs, audio_gains_are_equal);
645     updateFieldAndCompare(rhs.max_ramp_ms + 10, &audio_gain::max_ramp_ms,
646             &lhs, &rhs, audio_gains_are_equal);
647 }
648 
TEST_P(SystemAudioPortTest,AudioPortConfigEquivalentTest)649 TEST_P(SystemAudioPortTest, AudioPortConfigEquivalentTest) {
650     struct audio_port_config lhs;
651     struct audio_port_config rhs;
652     ASSERT_NO_FATAL_FAILURE(fillFakeAudioPortConfigInfo(&lhs));
653     ASSERT_NO_FATAL_FAILURE(fillFakeAudioPortConfigInfo(&rhs));
654     ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
655 
656     updateFieldAndCompare(AUDIO_PORT_ROLE_NONE, &audio_port_config::role,
657             &lhs, &rhs, audio_port_configs_are_equal);
658     updateFieldAndCompare(AUDIO_PORT_TYPE_NONE, &audio_port_config::type,
659             &lhs, &rhs, audio_port_configs_are_equal);
660 
661     updateConfigFieldAndCompare(rhs.sample_rate * 2, &audio_port_config::sample_rate,
662             &lhs, &rhs, AUDIO_PORT_CONFIG_SAMPLE_RATE, audio_port_configs_are_equal);
663     updateConfigFieldAndCompare(AUDIO_CHANNEL_NONE, &audio_port_config::channel_mask,
664             &lhs, &rhs, AUDIO_PORT_CONFIG_CHANNEL_MASK, audio_port_configs_are_equal);
665     updateConfigFieldAndCompare(AUDIO_FORMAT_DEFAULT, &audio_port_config::format,
666             &lhs, &rhs, AUDIO_PORT_CONFIG_FORMAT, audio_port_configs_are_equal);
667     lhs.gain.ramp_duration_ms = rhs.gain.ramp_duration_ms * 2;
668     ASSERT_FALSE(audio_port_configs_are_equal(&lhs, &rhs));
669     lhs.config_mask &= ~AUDIO_PORT_CONFIG_GAIN;
670     rhs.config_mask = lhs.config_mask;
671     ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
672 
673     lhs.config_mask |= AUDIO_PORT_CONFIG_FLAGS;
674     rhs.config_mask = lhs.config_mask;
675     ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
676     if (audio_port_config_has_input_direction(&lhs)) {
677         lhs.flags.input = AUDIO_INPUT_FLAG_NONE;
678     } else {
679         lhs.flags.output = AUDIO_OUTPUT_FLAG_NONE;
680     }
681     ASSERT_FALSE(audio_port_configs_are_equal(&lhs, &rhs));
682     lhs.config_mask &= ~AUDIO_PORT_CONFIG_FLAGS;
683     rhs.config_mask = lhs.config_mask;
684     ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
685 
686     testAudioPortExtBaseEquivalent(&lhs, &rhs, audio_port_configs_are_equal);
687     if (lhs.type == AUDIO_PORT_TYPE_MIX) {
688         if (lhs.role == AUDIO_PORT_ROLE_SINK) {
689             lhs.ext.mix.usecase.source = AUDIO_SOURCE_DEFAULT;
690             ASSERT_FALSE(audio_port_configs_are_equal(&lhs, &rhs));
691             lhs.ext.mix.usecase.source = rhs.ext.mix.usecase.source;
692             ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
693         } else if (lhs.role == AUDIO_PORT_ROLE_SOURCE) {
694             lhs.ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
695             ASSERT_FALSE(audio_port_configs_are_equal(&lhs, &rhs));
696             lhs.ext.mix.usecase.stream = rhs.ext.mix.usecase.stream;
697             ASSERT_TRUE(audio_port_configs_are_equal(&lhs, &rhs));
698         }
699     }
700 }
701 
TEST_P(SystemAudioPortTest,AudioPortEquivalentTest)702 TEST_P(SystemAudioPortTest, AudioPortEquivalentTest) {
703     struct audio_port lhs;
704     ASSERT_NO_FATAL_FAILURE(fillFakeAudioPortInfo(&lhs));
705     struct audio_port rhs = lhs;
706     ASSERT_TRUE(audio_ports_are_equal(&lhs, &rhs));
707 
708     testAudioPortEquivalent(&lhs, &rhs, audio_ports_are_equal);
709 
710     testAudioPortCapabilityArraysEquivalent(lhs.formats, lhs.num_formats,
711             AUDIO_FORMAT_DEFAULT, &lhs, &rhs, audio_ports_are_equal);
712     testAudioPortCapabilityArraysEquivalent(lhs.channel_masks, lhs.num_channel_masks,
713             AUDIO_CHANNEL_NONE, &lhs, &rhs, audio_ports_are_equal);
714     testAudioPortCapabilityArraysEquivalent(lhs.sample_rates, lhs.num_sample_rates,
715             (unsigned int) 0 /*replacedValue*/, &lhs, &rhs, audio_ports_are_equal);
716 }
717 
TEST_P(SystemAudioPortTest,AudioPortV7EquivalentTest)718 TEST_P(SystemAudioPortTest, AudioPortV7EquivalentTest) {
719     struct audio_port_v7 lhs;
720     ASSERT_NO_FATAL_FAILURE(fillFakeAudioPortV7Info(&lhs, true /*containsExtraAudioDescriptor*/));
721     struct audio_port_v7 rhs = lhs;
722     ASSERT_TRUE(audio_ports_v7_are_equal(&lhs, &rhs));
723 
724     testAudioPortEquivalent(&lhs, &rhs, audio_ports_v7_are_equal);
725 
726     struct audio_profile emptyProfile = {};
727     testAudioPortCapabilityArraysEquivalent(lhs.audio_profiles, lhs.num_audio_profiles,
728             emptyProfile, &lhs, &rhs, audio_ports_v7_are_equal);
729     auto& firstProfile = lhs.audio_profiles[0];
730     testAudioPortCapabilityArraysEquivalent(firstProfile.sample_rates,
731             firstProfile.num_sample_rates, (unsigned int) 0 /*replacedValue*/,
732             &lhs, &rhs, audio_ports_v7_are_equal);
733     testAudioPortCapabilityArraysEquivalent(firstProfile.channel_masks,
734             firstProfile.num_channel_masks, AUDIO_CHANNEL_NONE,
735             &lhs, &rhs, audio_ports_v7_are_equal);
736 
737     struct audio_extra_audio_descriptor emptyDesc = {};
738     testAudioPortCapabilityArraysEquivalent(lhs.extra_audio_descriptors,
739             lhs.num_extra_audio_descriptors, emptyDesc, &lhs, &rhs, audio_ports_v7_are_equal);
740 }
741 
TEST_P(SystemAudioPortTest,AudioPortV7ConversionTest)742 TEST_P(SystemAudioPortTest, AudioPortV7ConversionTest) {
743     struct audio_port srcPort, dstPort = {};
744     struct audio_port_v7 portV7;
745     ASSERT_NO_FATAL_FAILURE(fillFakeAudioPortInfo(&srcPort));
746     audio_populate_audio_port_v7(&srcPort, &portV7);
747     ASSERT_TRUE(audio_populate_audio_port(&portV7, &dstPort));
748     ASSERT_TRUE(audio_ports_are_equal(&srcPort, &dstPort));
749 
750     struct audio_port_v7 srcPortV7, dstPortV7 = {};
751     struct audio_port audioPort;
752     ASSERT_NO_FATAL_FAILURE(
753             fillFakeAudioPortV7Info(&srcPortV7, false /*containsExtraAudioDescriptor*/));
754     ASSERT_EQ(srcPortV7.num_audio_profiles, AUDIO_PORT_MAX_AUDIO_PROFILES);
755     auto& profile = srcPortV7.audio_profiles[0];
756     ASSERT_EQ(profile.num_channel_masks, AUDIO_PORT_MAX_CHANNEL_MASKS);
757     // Set a channel mask that is not present in the list
758     profile.channel_masks[0] = AUDIO_CHANNEL_NONE;
759     ASSERT_FALSE(audio_populate_audio_port(&srcPortV7, &audioPort));
760     audio_populate_audio_port_v7(&audioPort, &dstPortV7);
761     ASSERT_EQ(dstPortV7.num_audio_profiles, AUDIO_PORT_MAX_AUDIO_PROFILES);
762     // Do not compare audio profiles' information as the audio profiles will not be the
763     // same after conversion from audio_port_v7->audio_port and audio_port->audio_port_v7
764     srcPortV7.num_audio_profiles = 0;
765     dstPortV7.num_audio_profiles = 0;
766     ASSERT_TRUE(audio_ports_v7_are_equal(&srcPortV7, &dstPortV7));
767 }
768 
TEST_P(SystemAudioPortTest,AudioPortV7ContainingExtraAudioDescriptorConversionTest)769 TEST_P(SystemAudioPortTest, AudioPortV7ContainingExtraAudioDescriptorConversionTest) {
770     struct audio_port_v7 srcPortV7, dstPortV7 = {};
771     struct audio_port audioPort;
772     ASSERT_NO_FATAL_FAILURE(
773             fillFakeAudioPortV7Info(&srcPortV7, true /*containsExtraAudioDescriptor*/));
774     ASSERT_FALSE(audio_populate_audio_port(&srcPortV7, &audioPort));
775     audio_populate_audio_port_v7(&audioPort, &dstPortV7);
776     ASSERT_FALSE(audio_ports_v7_are_equal(&srcPortV7, &dstPortV7));
777 }
778 
779 INSTANTIATE_TEST_CASE_P(SystemAudioPortTest, SystemAudioPortTest,
780         testing::Combine(
781                 testing::Values(AUDIO_PORT_ROLE_SOURCE,
782                                 AUDIO_PORT_ROLE_SINK),
783                 testing::Values(AUDIO_PORT_TYPE_DEVICE,
784                                 AUDIO_PORT_TYPE_MIX,
785                                 AUDIO_PORT_TYPE_SESSION))
786         );
787