#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::print_stderr,
reason = "unit tests"
)]
use super::*;
use crate::{DeviceHotplug, DeviceKind};
#[test]
fn open_hotplug_mic_loopback_poll_or_skip() {
let _guard = crate::windows::HARDWARE_TEST_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let mut hotplug =
match WindowsDeviceHotplug::open(&[DeviceKind::Microphone, DeviceKind::Loopback]) {
Ok(h) => h,
Err(e) => {
eprintln!("skip: WindowsDeviceHotplug::open ({e:?})");
return;
}
};
for _ in 0..3 {
match hotplug.poll_event() {
Ok(Some(event)) => eprintln!("hotplug: real event observed during poll: {event:?}"),
Ok(None) => {}
Err(e) => {
eprintln!("skip: poll_event ({e:?})");
return;
}
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
hotplug.close().expect("close");
}
#[test]
fn mmdevice_enumerator_does_not_implement_iagileobject_or_skip() {
use windows::Win32::Media::Audio::{IMMDeviceEnumerator, MMDeviceEnumerator};
use windows::Win32::System::Com::{
CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx, IAgileObject,
};
use windows::core::Interface;
let _guard = crate::windows::HARDWARE_TEST_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let hr = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };
if hr.is_err() {
eprintln!("skip: CoInitializeEx ({hr:?})");
return;
}
let _com = crate::windows_audio::ComGuard;
let enumerator: windows_core::Result<IMMDeviceEnumerator> =
unsafe { CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_INPROC_SERVER) };
let enumerator = match enumerator {
Ok(e) => e,
Err(e) => {
eprintln!("skip: CoCreateInstance(MMDeviceEnumerator) ({e:?})");
return;
}
};
let agile: windows_core::Result<IAgileObject> = enumerator.cast();
assert!(
agile.is_err(),
"the real MMDeviceEnumerator on this machine now implements IAgileObject \
({agile:?}) — the environment changed since this test was written; re-open the \
`WindowsDeviceHotplug: Send` question in \
mediaway-device-ffi/adr/0002-callback-event-delivery.md §8 and hotplug.rs's module \
doc instead of just updating this assertion"
);
}