From 5a5bc63d7ee37ea78adfa7c958d45b1595bf6c61 Mon Sep 17 00:00:00 2001 From: Chun-Min Chang Date: Tue, 5 Oct 2021 09:59:34 -0700 Subject: [PATCH] Revise custom group id test (#139) * Add comments explaining the purpose of the test * Revise test for the custom group This solves https://github.com/mozilla/cubeb-coreaudio-rs/issues/138. When the Blackhole device is installed, both its input and output data source are the same. As a result, the hash-map whose key is data source value has collision when the hash-map is used to store both input and output device's group ids. To avoid the collision, we should use different hash-map for input device and output device to store the group id values. --- src/backend/tests/api.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/backend/tests/api.rs b/src/backend/tests/api.rs index f5b2433..370ad48 100644 --- a/src/backend/tests/api.rs +++ b/src/backend/tests/api.rs @@ -1238,50 +1238,55 @@ fn test_get_device_group_id() { fn test_get_same_group_id_for_builtin_device_pairs() { use std::collections::HashMap; + // These device sources have custom group id. See `get_custom_group_id`. const IMIC: u32 = 0x696D_6963; // "imic" const ISPK: u32 = 0x6973_706B; // "ispk" const EMIC: u32 = 0x656D_6963; // "emic" const HDPN: u32 = 0x6864_706E; // "hdpn" let pairs = [(IMIC, ISPK), (EMIC, HDPN)]; - // TODO: group_ids will have collision if one input device and one output device - // has same source value. - let mut group_ids = HashMap::::new(); + let mut input_group_ids = HashMap::::new(); let input_devices = test_get_devices_in_scope(Scope::Input); for device in input_devices.iter() { match get_device_source(*device, DeviceType::INPUT) { Ok(source) => match get_device_group_id(*device, DeviceType::INPUT) { - Ok(id) => assert!(group_ids + Ok(id) => assert!(input_group_ids .insert(source, id.into_string().unwrap()) .is_none()), - Err(e) => assert!(group_ids.insert(source, format!("Error {}", e)).is_none()), + Err(e) => assert!(input_group_ids + .insert(source, format!("Error {}", e)) + .is_none()), }, _ => {} // do nothing when failing to get source. } } + + let mut output_group_ids = HashMap::::new(); let output_devices = test_get_devices_in_scope(Scope::Output); for device in output_devices.iter() { match get_device_source(*device, DeviceType::OUTPUT) { Ok(source) => match get_device_group_id(*device, DeviceType::OUTPUT) { - Ok(id) => assert!(group_ids + Ok(id) => assert!(output_group_ids .insert(source, id.into_string().unwrap()) .is_none()), - Err(e) => assert!(group_ids.insert(source, format!("Error {}", e)).is_none()), + Err(e) => assert!(output_group_ids + .insert(source, format!("Error {}", e)) + .is_none()), }, _ => {} // do nothing when failing to get source. } } - for (dev1, dev2) in pairs.iter() { - let id1 = group_ids.get(dev1); - let id2 = group_ids.get(dev2); + for (input, output) in pairs.iter() { + let input_group_id = input_group_ids.get(input); + let output_group_id = output_group_ids.get(output); - if id1.is_some() && id2.is_some() { - assert_eq!(id1, id2); + if input_group_id.is_some() && output_group_id.is_some() { + assert_eq!(input_group_id, output_group_id); } - group_ids.remove(dev1); - group_ids.remove(dev2); + input_group_ids.remove(input); + output_group_ids.remove(output); } }