|
| 1 | +//! Unit tests for `windows_core::StaticComObject` |
| 2 | +
|
| 3 | +use std::sync::atomic::{AtomicU32, Ordering::SeqCst}; |
| 4 | +use windows_core::{ |
| 5 | + implement, interface, ComObject, IUnknown, IUnknownImpl, IUnknown_Vtbl, InterfaceRef, |
| 6 | + StaticComObject, |
| 7 | +}; |
| 8 | + |
| 9 | +#[interface("818f2fd1-d479-4398-b286-a93c4c7904d1")] |
| 10 | +unsafe trait INumberFactory: IUnknown { |
| 11 | + fn next(&self) -> u32; |
| 12 | + |
| 13 | + fn add(&self, x: u32, y: u32) -> u32; |
| 14 | +} |
| 15 | + |
| 16 | +#[implement(INumberFactory)] |
| 17 | +struct MyFactory { |
| 18 | + x: AtomicU32, |
| 19 | +} |
| 20 | + |
| 21 | +impl INumberFactory_Impl for MyFactory_Impl { |
| 22 | + unsafe fn next(&self) -> u32 { |
| 23 | + self.x.fetch_add(1, SeqCst) |
| 24 | + } |
| 25 | + |
| 26 | + unsafe fn add(&self, x: u32, y: u32) -> u32 { |
| 27 | + x + y |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +static NUMBER_FACTORY_INSTANCE: StaticComObject<MyFactory> = MyFactory { |
| 32 | + x: AtomicU32::new(100), |
| 33 | +} |
| 34 | +.into_static(); |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn as_interface() { |
| 38 | + let factory_outer: &MyFactory_Impl = NUMBER_FACTORY_INSTANCE.get(); |
| 39 | + let ifactory: InterfaceRef<INumberFactory> = factory_outer.as_interface::<INumberFactory>(); |
| 40 | + |
| 41 | + // Produce the next number. We don't verify the value since tests are multi-threaded. |
| 42 | + // This just demonstrates that you can have shared state with interior mutability (such as |
| 43 | + // atomics) in a static COM object. |
| 44 | + let n = unsafe { ifactory.next() }; |
| 45 | + println!("n = {n:?}"); |
| 46 | + |
| 47 | + assert_eq!(unsafe { ifactory.add(333, 444) }, 777); |
| 48 | +} |
| 49 | + |
| 50 | +// This tests that we can safely AddRef/Release a StaticComObject. |
| 51 | +#[test] |
| 52 | +fn to_interface() { |
| 53 | + let factory_outer: &MyFactory_Impl = NUMBER_FACTORY_INSTANCE.get(); |
| 54 | + let ifactory: INumberFactory = factory_outer.to_interface::<INumberFactory>(); |
| 55 | + assert_eq!(unsafe { ifactory.add(333, 444) }, 777); |
| 56 | + drop(ifactory); |
| 57 | +} |
| 58 | + |
| 59 | +#[test] |
| 60 | +fn to_object() { |
| 61 | + let factory_outer: &MyFactory_Impl = NUMBER_FACTORY_INSTANCE.get(); |
| 62 | + let factory_object: ComObject<MyFactory> = factory_outer.to_object(); |
| 63 | + assert_eq!(unsafe { factory_object.add(333, 444) }, 777); |
| 64 | +} |
| 65 | + |
| 66 | +// This tests the behavior when dropping a StaticComObject. Since static variables are never |
| 67 | +// dropped, this isn't relevant to normal usage. However, if app code constructs a StaticComObject |
| 68 | +// in local variables (not statics) and them drops them, then we still need well-defined behavior. |
| 69 | +// Basically, we are testing that the refererence-count field does not panic when being dropped |
| 70 | +// with a non-zero reference count. |
| 71 | +#[test] |
| 72 | +fn drop_half_constructed() { |
| 73 | + let _static_com_object: StaticComObject<MyFactory> = MyFactory { |
| 74 | + x: AtomicU32::new(0), |
| 75 | + } |
| 76 | + .into_static(); |
| 77 | +} |
0 commit comments