/// <summary> /// Specify which control or form we should use for the relative position. /// </summary> public void UseOn(ReflectionTester control) { if (mouseControl == null) { Win32.GetCursorPos(out originalPosition); } if (control == null) { throw new ArgumentNullException("control"); } mouseControl = new MouseControl(control); PositioXunit = GraphicsUnit.Pixel; // Block any user input while we are active. if (!restoreUserInput) { if (!Win32.BlockInput(true)) { //throw new Win32Exception(); } restoreUserInput = true; } }
/// <summary> /// Implements the IDisposable interface. This restores user input. /// It should eventually return the keyboard to its pre-test state. /// </summary> /// <remarks> /// If you are using the Keyboard controller through the base XunitFormTest /// class, then you should not need to call this method or use finally or using /// blocks. The base class handles this for you.</remarks> public void Dispose() { if (keyboardControl != null) { if (restoreUserInput) { //if this next line returns false, I used to throw an exception... Win32.BlockInput(false); restoreUserInput = false; } } }
/// <summary> /// Releases the resources used by the <see cref="MouseController"/>. /// </summary> /// <remarks> /// <b>Dispose</b> releases any pressed mouse keys, restores the /// mouse <see cref="Position"/> and enables user input. /// </remarks> public void Dispose() { if (mouseControl != null) { // If we do not have a control, then an exception will be thrown. try { // Release any pressed mouse buttons. MouseButtons pressedButtons = Control.MouseButtons; if (pressedButtons != MouseButtons.None) { Release(pressedButtons); } // Release any modifier keys. Keys keys = Control.ModifierKeys; if (keys != Keys.None) { Release(keys); } } catch (AmbiguousNameException) { } catch (NoSuchControlException) { } finally { // Restore the mouse position Win32.SetCursorPos(originalPosition.x, originalPosition.y); // Enable user input. if (restoreUserInput) { if (!Win32.BlockInput(false)) { //throw new Win32Exception(); } restoreUserInput = false; } } } }
/// <summary> /// Initializes the KeyboardController, blocks user input, and sets /// the focus on the specified control. /// </summary> /// <param name="control">The ControlTester to use the keyboard on.</param> public void UseOn(ReflectionTester control) { if (control == null) { throw new ArgumentNullException("control"); } Control c = control.TheObject as Control; FormsAssert.IsTrue(c != null, "Keyboard control requires tester of Control"); sendKeys = sendKeysFactory.Create(c.Handle); keyboardControl = new KeyboardControl(control); if (!restoreUserInput) { //if this next line returns false, I used to throw an exception... Win32.BlockInput(true); restoreUserInput = true; } }