using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Runs only from the opt-in, isolated --verify-ui harness.</summary>
internal static class DialogVerification
{
internal static int CompletedCount { get; private set; }
internal static IEnumerable<T> Children<T>(DependencyObject root) where T : DependencyObject
{
if (root is T match) yield return match;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
foreach (var item in Children<T>(VisualTreeHelper.GetChild(root, i))) yield return item;
}
internal static async Task RunAsync(FormDialogButton launcher, Action<UIElement>? fill = null, string buttonName = "CloseButton")
{
if (launcher.Form is null) throw new InvalidOperationException("Missing dialog form.");
if (launcher.CornerRadius.TopLeft != 8 || launcher.MinHeight != 40)
throw new InvalidOperationException("The form button did not inherit the shared Fluent style.");
if (VisualTreeHelper.GetParent(launcher.Form) is not null) throw new InvalidOperationException("A form is still displayed inline.");
var operation = launcher.OpenAsync();
for (var i = 0; i < 200 && launcher.ActiveDialog?.IsLoaded != true && !operation.IsCompleted; i++) await Task.Delay(10);
if (operation.IsCompleted) await operation;
var dialog = launcher.ActiveDialog ?? throw new InvalidOperationException("The form dialog did not open.");
dialog.UpdateLayout();
fill?.Invoke(launcher.Form);
dialog.UpdateLayout();
// InfoBar also templates a CloseButton; invoke the dialog's visible command, not that hidden child.
var button = Children<Button>(dialog).LastOrDefault(x => x.Name == buttonName && x.Visibility == Visibility.Visible && x.ActualHeight > 0)
?? throw new InvalidOperationException("Missing native dialog button: " + buttonName);
if (!button.IsEnabled) throw new InvalidOperationException("The requested dialog action is disabled.");
var peer = new ButtonAutomationPeer(button);
((IInvokeProvider)peer.GetPattern(PatternInterface.Invoke)).Invoke();
var finished = await Task.WhenAny(operation, Task.Delay(5000));
if (finished != operation) { launcher.ActiveDialog?.Hide(); throw new InvalidOperationException("Dialog submission did not finish; inspect validation or nested dialog handling."); }
await operation;
if (launcher.ActiveDialog is not null || VisualTreeHelper.GetParent(launcher.Form) is not null) throw new InvalidOperationException("Closed form was not detached.");
CompletedCount++;
}
}
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Runs only from the opt-in, isolated --verify-ui harness.</summary>
internal static class DialogVerification
{
internal static int CompletedCount { get; private set; }
internal static IEnumerable<T> Children<T>(DependencyObject root) where T : DependencyObject
{
if (root is T match) yield return match;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
foreach (var item in Children<T>(VisualTreeHelper.GetChild(root, i))) yield return item;
}
internal static async Task RunAsync(FormDialogButton launcher, Action<UIElement>? fill = null, string buttonName = "CloseButton")
{
if (launcher.Form is null) throw new InvalidOperationException("Missing dialog form.");
if (launcher.CornerRadius.TopLeft != 8 || launcher.MinHeight != 40)
throw new InvalidOperationException("The form button did not inherit the shared Fluent style.");
if (VisualTreeHelper.GetParent(launcher.Form) is not null) throw new InvalidOperationException("A form is still displayed inline.");
var operation = launcher.OpenAsync();
for (var i = 0; i < 200 && launcher.ActiveDialog?.IsLoaded != true && !operation.IsCompleted; i++) await Task.Delay(10);
if (operation.IsCompleted) await operation;
var dialog = launcher.ActiveDialog ?? throw new InvalidOperationException("The form dialog did not open.");
dialog.UpdateLayout();
fill?.Invoke(launcher.Form);
dialog.UpdateLayout();
// InfoBar also templates a CloseButton; invoke the dialog's visible command, not that hidden child.
var button = Children<Button>(dialog).LastOrDefault(x => x.Name == buttonName && x.Visibility == Visibility.Visible && x.ActualHeight > 0)
?? throw new InvalidOperationException("Missing native dialog button: " + buttonName);
if (!button.IsEnabled) throw new InvalidOperationException("The requested dialog action is disabled.");
var peer = new ButtonAutomationPeer(button);
((IInvokeProvider)peer.GetPattern(PatternInterface.Invoke)).Invoke();
var finished = await Task.WhenAny(operation, Task.Delay(5000));
if (finished != operation) { launcher.ActiveDialog?.Hide(); throw new InvalidOperationException("Dialog submission did not finish; inspect validation or nested dialog handling."); }
await operation;
if (launcher.ActiveDialog is not null || VisualTreeHelper.GetParent(launcher.Form) is not null) throw new InvalidOperationException("Closed form was not detached.");
CompletedCount++;
}
}