use crate::support;
use wasapi::*;
#[test]
fn cable_is_present() {
support::init_com();
let enumerator = DeviceEnumerator::new().unwrap();
let render = support::cable_endpoints(&enumerator, &Direction::Render);
let capture = support::cable_endpoints(&enumerator, &Direction::Capture);
println!(
"found {} render and {} capture endpoints of \"{}\"",
render.len(),
capture.len(),
support::CABLE_INTERFACE_NAME
);
if render.is_empty() || capture.is_empty() {
support::skip("VB-Cable is not installed");
}
}
#[test]
fn endpoint_inventory() {
support::init_com();
let enumerator = DeviceEnumerator::new().unwrap();
let describe_format = |format: Result<WaveFormat, WasapiError>| match format {
Ok(f) => format!(
"{} ch, {} Hz, {}/{} bits, {:?}",
f.get_nchannels(),
f.get_samplespersec(),
f.get_validbitspersample(),
f.get_bitspersample(),
f.get_subformat()
),
Err(e) => format!("<{e}>"),
};
for direction in [Direction::Render, Direction::Capture] {
let collection = enumerator.get_device_collection(&direction).unwrap();
println!(
"\n{direction}: {} devices",
collection.get_nbr_devices().unwrap()
);
for device in (&collection).into_iter().flatten() {
println!(" {}", support::describe(&device));
println!(
" device {} | mix {}",
describe_format(device.get_device_format()),
describe_format(device.get_iaudioclient().and_then(|c| c.get_mixformat()))
);
match device.get_data_ranges() {
Ok(ranges) if ranges.is_empty() => println!(" ranges: none declared"),
Ok(ranges) => println!(" ranges: {ranges:?}"),
Err(e) => println!(" ranges: <{e}>"),
}
}
}
}
#[test]
fn collection_agrees_with_iterator() {
let Some(fx) = support::fixture() else { return };
for direction in [Direction::Render, Direction::Capture] {
let collection = fx.enumerator.get_device_collection(&direction).unwrap();
let count = collection.get_nbr_devices().unwrap();
assert_eq!(collection.get_direction(), direction);
let by_index: Vec<String> = (0..count)
.map(|n| collection.get_device_at_index(n).unwrap().get_id().unwrap())
.collect();
let by_iterator: Vec<String> = (&collection)
.into_iter()
.map(|device| device.unwrap().get_id().unwrap())
.collect();
assert_eq!(by_index, by_iterator, "{direction} devices");
assert_eq!(by_index.len(), count as usize);
for device in (&collection).into_iter().flatten() {
assert_eq!(device.get_direction(), direction);
assert_eq!(device.get_state().unwrap(), DeviceState::Active);
}
assert!(collection.get_device_at_index(count).is_err());
}
}
#[test]
fn device_lookup_round_trips() {
let Some(fx) = support::fixture() else { return };
for device in [&fx.cable.render, &fx.cable.capture] {
let id = device.get_id().unwrap();
let name = device.get_friendlyname().unwrap();
let by_id = fx.enumerator.get_device(&id).unwrap();
assert_eq!(by_id.get_id().unwrap(), id);
assert_eq!(by_id.get_friendlyname().unwrap(), name);
assert_eq!(by_id.get_direction(), device.get_direction());
let collection = fx
.enumerator
.get_device_collection(&device.get_direction())
.unwrap();
let by_name = collection.get_device_with_name(&name).unwrap();
assert_eq!(by_name.get_id().unwrap(), id);
}
let collection = fx
.enumerator
.get_device_collection(&Direction::Render)
.unwrap();
assert!(matches!(
collection.get_device_with_name("no such device"),
Err(WasapiError::DeviceNotFound(_))
));
}
#[test]
fn notification_registration_round_trip() {
let Some(fx) = support::fixture() else { return };
let register = || {
fx.enumerator
.register_notification_callback(DeviceEventCallbacks::new())
.unwrap()
};
let (first, second) = (register(), register());
drop(first);
drop(second);
drop(register());
}