#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member namespace TerminalGuiFluentTesting; public partial class GuiTestContext { /// /// Sets the input focus to the given . /// Throws if focus did not change due to system /// constraints e.g. /// is /// /// /// /// public GuiTestContext Focus (View toFocus) { toFocus.FocusDeepest (NavigationDirection.Forward, TabBehavior.TabStop); if (!toFocus.HasFocus) { throw new ArgumentException ("Failed to set focus, FocusDeepest did not result in HasFocus becoming true. Ensure view is added and focusable"); } return WaitIteration (); } /// /// Tabs through the UI until a View matching the /// is found (of Type T) or all views are looped through (back to the beginning) /// in which case triggers hard stop and Exception /// /// /// Delegate that returns true if the passed View is the one /// you are trying to focus. Leave to focus the first view of type /// /// /// /// public GuiTestContext Focus (Func? evaluator = null) where T : View { evaluator ??= _ => true; View? t = App?.TopRunnableView; HashSet seen = new (); if (t == null) { Fail ("Application.TopRunnable was null when trying to set focus"); return this; } do { View? next = t.MostFocused; // Is view found? if (next is T v && evaluator (v)) { return this; } // No, try tab to the next (or first) EnqueueKeyEvent (Terminal.Gui.App.Application.NextTabKey); WaitIteration (); next = t.MostFocused; if (next is null) { Fail ( "Failed to tab to a view which matched the Type and evaluator constraints of the test because MostFocused became or was always null" + DescribeSeenViews (seen)); return this; } // Track the views we have seen // We have looped around to the start again if it was already there if (!seen.Add (next)) { Fail ( "Failed to tab to a view which matched the Type and evaluator constraints of the test before looping back to the original View" + DescribeSeenViews (seen)); return this; } } while (true); } private string DescribeSeenViews (HashSet seen) { return Environment.NewLine + string.Join (Environment.NewLine, seen); } }