public void Register_WhenCreatingHandleThrowsException_ThenFreeGlobalIsCalled() { // ARRANGE var mockedBroadcastDeviceInterfaceFactory = new Mock <IBroadcastDeviceInterfaceFactory>(); var mockedMarshallWrapper = new Mock <IMarshalWrapper>(); var mockedUnsafeNativeMethodsWrapper = new Mock <IUnsafeNativeMethodsWrapper>(); var mockedProcessHandleResultFactory = new Mock <IProcessHandleResultFactory>(); var deviceManagement = new DeviceNotifications( mockedBroadcastDeviceInterfaceFactory.Object, mockedMarshallWrapper.Object, mockedUnsafeNativeMethodsWrapper.Object, mockedProcessHandleResultFactory.Object); var handleReceivesNotifications = new IntPtr(42); var devBroadcastDeviceInterface = new BroadcastDeviceInterface(); var intPtrForBuffer = new IntPtr(111); mockedBroadcastDeviceInterfaceFactory.Setup(x => x.CreateBroadcastDeviceInterface()).Returns(devBroadcastDeviceInterface); mockedMarshallWrapper.Setup(x => x.AllocHGlobal(devBroadcastDeviceInterface.Size)).Returns(intPtrForBuffer); mockedUnsafeNativeMethodsWrapper.Setup(x => x.RegisterDeviceNotification(It.IsAny <IntPtr>(), It.IsAny <IntPtr>(), It.IsAny <uint>())); mockedProcessHandleResultFactory.Setup(x => x.Create(It.IsAny <IntPtr>())).Throws <InvalidOperationException>(); // ACT var testDelegate = new Assert.ThrowsDelegate(() => deviceManagement.Register(handleReceivesNotifications)); // ASSERT Assert.Throws <InvalidOperationException>(testDelegate); mockedMarshallWrapper.Verify(x => x.FreeHGlobal(intPtrForBuffer), Times.Once); }
public void Register_ThenStructureToPointerIsCalled() { // ARRANGE var mockedBroadcastDeviceInterfaceFactory = new Mock <IBroadcastDeviceInterfaceFactory>(); var mockedMarshallWrapper = new Mock <IMarshalWrapper>(); var mockedUnsafeNativeMethodsWrapper = new Mock <IUnsafeNativeMethodsWrapper>(); var mockedProcessHandleResultFactory = new Mock <IProcessHandleResultFactory>(); var deviceManagement = new DeviceNotifications( mockedBroadcastDeviceInterfaceFactory.Object, mockedMarshallWrapper.Object, mockedUnsafeNativeMethodsWrapper.Object, mockedProcessHandleResultFactory.Object); var handleReceivesNotifications = new IntPtr(42); var devBroadcastDeviceInterface = new BroadcastDeviceInterface(); var intPtrForBuffer = new IntPtr(111); mockedBroadcastDeviceInterfaceFactory.Setup(x => x.CreateBroadcastDeviceInterface()).Returns(devBroadcastDeviceInterface); mockedMarshallWrapper.Setup(x => x.AllocHGlobal(devBroadcastDeviceInterface.Size)).Returns(intPtrForBuffer); // ACT deviceManagement.Register(handleReceivesNotifications); // ASSERT mockedMarshallWrapper.Verify(x => x.StructureToPointer(devBroadcastDeviceInterface, intPtrForBuffer, false), Times.Once); }
public void Register_ThenCreatesBroadcastDeviceInterface() { // ARRANGE var mockedBroadcastDeviceInterfaceFactory = new Mock <IBroadcastDeviceInterfaceFactory>(); var mockedMarshallWrapper = new Mock <IMarshalWrapper>(); var mockedUnsafeNativeMethodsWrapper = new Mock <IUnsafeNativeMethodsWrapper>(); var mockedProcessHandleResultFactory = new Mock <IProcessHandleResultFactory>(); var deviceManagement = new DeviceNotifications( mockedBroadcastDeviceInterfaceFactory.Object, mockedMarshallWrapper.Object, mockedUnsafeNativeMethodsWrapper.Object, mockedProcessHandleResultFactory.Object); var handleReceivesNotifications = new IntPtr(42); var devBroadcastDeviceInterface = new BroadcastDeviceInterface(); mockedBroadcastDeviceInterfaceFactory.Setup(x => x.CreateBroadcastDeviceInterface()).Returns(devBroadcastDeviceInterface); // ACT deviceManagement.Register(handleReceivesNotifications); // ASSERT mockedBroadcastDeviceInterfaceFactory.Verify(x => x.CreateBroadcastDeviceInterface(), Times.Once); }