private async Task CaptureWindowAsync(string prefix = "") { Log.Comment($"Capturing window from event test {eventName}EventTester"); var current = default(Window); var failed = false; try { Log.Comment($"Capturing screen {prefix} event test {eventName}EventTester"); UIExecutor.Execute(() => { current = Window.Current; }); if (current != null) { //await current.CaptureWindowAsync($"{prefix}{eventName}EventTester"); } } catch { failed = true; } if (current == null || failed) { if (current == null) { Log.Warning($"No active Window, fallback to CaptureScreen"); } else if (failed) { Log.Warning($"Previuos CaptureWindow failed, fallback to CaptureScreen"); } await CaptureScreenAsync(prefix); } }
//Registers FocusManager event handlers. public FocusEventOrderingTester() { UIExecutor.Execute(() => { this.AddEventsToFocusManager(); }); }
public async Task VerifyEventNotRaised() { string prefix = string.Empty; UIExecutor.Execute(() => { var element = this.sender as FrameworkElement; if (element != null && !string.IsNullOrEmpty(element.Name)) { prefix = element.Name + "."; } }); Verify.IsFalse(await this.WaitAsync(TimeSpan.FromMilliseconds(100)), $"Event '{prefix}{this.eventName}' was raised before timeout."); }
public async Task VerifyEventRaised() { string prefix = string.Empty; UIExecutor.Execute(() => { var element = this.sender as FrameworkElement; if (element != null && !string.IsNullOrEmpty(element.Name)) { prefix = element.Name + "."; } }); Verify.IsTrue(await this.WaitAsync(), $"Event '{prefix}{this.eventName}' was not raised before timeout."); }
public FocusEventOrderingTester(Hyperlink[] elements, StringBuilder eventOrder) { this.EventOrder = eventOrder; this.HyperlinksToTest = elements; UIExecutor.Execute(() => { for (int i = 0; i < HyperlinksToTest.Length; i++) { this.AddEventsToHyperlink(HyperlinksToTest[i]); } this.AddEventsToFocusManager(); }); }
protected EventTester(TSender sender, Type senderType, string eventName, Action <object, TEventArgs> action, EventTesterOptions options, bool setBVTflags) { this.eventName = eventName; this.eventData = new List <Tuple <object, TEventArgs> >(); this.sender = sender; this.resetEvent = new AutoResetEvent(false); this.Action = action; this.options = options; if (setBVTflags) { this.options |= EventTesterOptions.BVTEvent; } this.senderType = senderType; var delegateType = GetDelegateType(eventName); Type eventArgType = delegateType.GetTypeInfo().GetDeclaredMethod("Invoke").GetParameters()[1].ParameterType; // Check to ensure that the caller passed in the correct event args. if (typeof(TEventArgs) != eventArgType) { throw new ArgumentException($"The event arg '{typeof(TEventArgs).Name}' does not match the event arg for {typeof(TSender)}.{eventName}. Expected '{eventArgType.Name}"); } // Because we use different Event Handlers for different events (i.e. GotFocus uses RoutedEventHandler while Flyout.Click uses EventHandler<object>), // we need to convert them to a delegate and use the delegate as our handler. Action <object, TEventArgs> handler = this.OnEventFired; MethodInfo handlerInvoke = handler.GetType().GetRuntimeMethod("Invoke", new[] { typeof(object), typeof(TEventArgs) }); this.handlerInvocationDelegate = handlerInvoke.CreateDelegate(delegateType, handler); UIExecutor.Execute(() => { this.AddEvent(); }); if (this.options.HasFlag(EventTesterOptions.CaptureWindowBefore)) { this.CaptureWindowAsync("Before").Wait(this.Timeout); } if (this.options.HasFlag(EventTesterOptions.CaptureScreenBefore)) { this.CaptureScreenAsync("Before").Wait(this.Timeout); } }
public static async Task EnsureFocusAsync(UIElement element, FocusState focusState, UInt32 Attempts = 3) { bool gotFocus = false; while (Attempts > 0 && !gotFocus) { Attempts--; //TestServicesExtensions.EnsureForegroundWindow(); using (var eventTester = new EventTester <UIElement, RoutedEventArgs>(element, "GotFocus")) using (var eventTesterFocusMgr = EventTester <object, FocusManagerGotFocusEventArgs> .FromStaticEvent <FocusManager>("GotFocus")) { UIExecutor.Execute(() => { if (element.FocusState == focusState && FocusManager.GetFocusedElement(element.XamlRoot).Equals(element)) { // The element is already focused with the expected focus state Log.Comment("Focus was already set on this element"); gotFocus = true; } else { Log.Comment("Setting focus to the element..."); element.Focus(focusState); } }); await Task.Delay(100); //eventTester.WaitForNoThrow(TimeSpan.FromMilliseconds(4000)); //eventTesterFocusMgr.WaitForNoThrow(TimeSpan.FromMilliseconds(4000)); gotFocus = gotFocus == false ? eventTester.HasFired : true; } await TestServices.WindowHelper.WaitForIdle(); } //if (!gotFocus) //{ // TestServices.Utilities.CaptureScreen("FocusTestHelper"); //} Verify.IsTrue(gotFocus); }
public void Dispose() { UIExecutor.Execute(() => { if (ElementsToTest != null) { for (int i = 0; i < ElementsToTest.Length; i++) { this.RemoveEventsFromFrameworkElement(ElementsToTest[i]); } } if (HyperlinksToTest != null) { for (int i = 0; i < HyperlinksToTest.Length; i++) { this.RemoveEventsFromHyperlink(HyperlinksToTest[i]); } } this.RemoveEventsFromFocusManager(); }); }
private void Dispose(bool disposing) { if (!disposedValue) { this.isDisposing = disposing; if (disposing) { // TODO: dispose managed state (managed objects). UIExecutor.Execute(() => { this.RemoveEvent(); }); TestServices.WindowHelper.WaitForIdle(); //if (XamlTestsBase.IsBVT) //{ // Log.Comment($"Disposing Event '{this.eventName}', executions count: {this.ExecuteCount}"); //} if (this.options.HasFlag(EventTesterOptions.CaptureWindowAfter)) { this.CaptureWindowAsync("After").Wait(this.Timeout); } if (this.options.HasFlag(EventTesterOptions.CaptureScreenAfter)) { this.CaptureScreenAsync("After").Wait(this.Timeout); } resetEvent.Dispose(); this.isDisposing = false; } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } }
public FocusEventOrderingTester(FrameworkElement[] elements, StringBuilder eventOrder) { this.EventOrder = eventOrder; this.ElementsToTest = elements; FrameworkElementLosingFocusHandler = new TypedEventHandler <UIElement, LosingFocusEventArgs>((source, e) => { FrameworkElement sourceAsFrameworkElement = (FrameworkElement)source; EventOrder.Append($"[{sourceAsFrameworkElement.Name}LosingFocus{CanceledToLoggedString(e.Cancel)}{HandledToLoggedString(e.Handled)}{CorrelationIDToLoggedString(e.CorrelationId)}]"); }); FrameworkElementGettingFocusHandler = new TypedEventHandler <UIElement, GettingFocusEventArgs>((source, e) => { FrameworkElement sourceAsFrameworkElement = (FrameworkElement)source; EventOrder.Append($"[{sourceAsFrameworkElement.Name}GettingFocus{CanceledToLoggedString(e.Cancel)}{HandledToLoggedString(e.Handled)}{CorrelationIDToLoggedString(e.CorrelationId)}]"); }); FocusManagerLosingFocusHandler = new EventHandler <Windows.UI.Xaml.Input.LosingFocusEventArgs>((source, e) => { EventOrder.Append($"[FocusManagerLosingFocus{CanceledToLoggedString(e.Cancel)}{HandledToLoggedString(e.Handled)}{CorrelationIDToLoggedString(e.CorrelationId)}]"); }); FocusManagerGettingFocusHandler = new EventHandler <Windows.UI.Xaml.Input.GettingFocusEventArgs>((source, e) => { EventOrder.Append($"[FocusManagerGettingFocus{CanceledToLoggedString(e.Cancel)}{HandledToLoggedString(e.Handled)}{CorrelationIDToLoggedString(e.CorrelationId)}]"); }); UIExecutor.Execute(() => { for (int i = 0; i < ElementsToTest.Length; i++) { this.AddEventsToFrameworkElement(ElementsToTest[i]); } this.AddEventsToFocusManager(); }); }