XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
using Newtonsoft.Json.Linq;

namespace XFEWarframe.Desktop.Editing;

internal static class QuestEditorVerification
{
    internal static async Task VerifyAsync(Page page, FormDialogButton add, AutoSuggestBox search,
        Func<IReadOnlyList<EditorField>> fields, Func<IReadOnlyList<EditorItem>> items, Func<bool> idle,
        Func<string?> error, Func<Task<JObject>> inventory)
    {
        var before = (await inventory())["QuestKeys"]!.DeepClone();
        page.UpdateLayout();
        if (add.Visibility != Visibility.Visible) throw new InvalidOperationException("The missing quest entry is hidden for an authorized account.");
        var opening = add.OpenAsync();
        await UntilAsync(() => add.ActiveDialog?.IsLoaded == true, "Quest creation dialog did not load.");
        var dialog = add.ActiveDialog!; dialog.UpdateLayout();
        var field = fields().Single();
        var option = field.Options.First();
        var input = DialogVerification.Children<AutoSuggestBox>(dialog).Single();
        input.Focus(FocusState.Programmatic);
        await SuggestionVerification.TypeAsync(input, option.Value.ToString());
        await SuggestionVerification.SubmitAsync(input);
        if (!JToken.DeepEquals(field.Serialize(), option.Value)) throw new InvalidOperationException("Quest search did not select the requested missing quest.");
        var button = DialogVerification.Children<Button>(dialog).Single(x => x.Name == "PrimaryButton");
        ((IInvokeProvider)new ButtonAutomationPeer(button).GetPattern(PatternInterface.Invoke)).Invoke();
        await opening.WaitAsync(TimeSpan.FromSeconds(8));
        if (error() is string failure) throw new InvalidOperationException(failure);
        var after = (await inventory())["QuestKeys"]!;
        if (!after.Any(x => x.Value<string>("ItemType") == option.Value.ToString())) throw new InvalidOperationException("Native quest addition did not persist.");
        foreach (var previous in before)
            if (!after.Any(x => JToken.DeepEquals(x, previous))) throw new InvalidOperationException("Adding a quest changed existing quest progress.");
        page.UpdateLayout(); search.Focus(FocusState.Programmatic);
        await SuggestionVerification.TypeAsync(search, "no-such-quest-" + Guid.NewGuid().ToString("N"));
        await UntilAsync(() => idle() && items().Count == 0, "Typing an unmatched search did not filter the list.");
        await SuggestionVerification.TypeAsync(search, option.Label);
        await UntilAsync(() => idle() && items().Any(x => x.Key == option.Value.ToString()), "Quest name search did not update the list automatically.");
        await SuggestionVerification.TypeAsync(search, option.Value.ToString());
        await SuggestionVerification.SubmitAsync(search);
        await UntilAsync(() => idle() && items().Count == 1 && items()[0].Key == option.Value.ToString(), "Exact quest path search did not return the selected quest.");
        await SuggestionVerification.TypeAsync(search, "");
        await UntilAsync(() => idle() && items().Count == Math.Min(40, after.Count()), "Clearing search did not restore the quest list.");
    }
    private static async Task UntilAsync(Func<bool> ready, string message)
    {
        for (var i = 0; i < 500 && !ready(); i++) await Task.Delay(10);
        if (!ready()) throw new InvalidOperationException(message);
    }
}