using System.Text.Json; using LogiBoardToolBox.Controls; using LogiBoardToolBox.Core.Devices; using LogiBoardToolBox.Core.Onboard; using LogiBoardToolBox.Profiles; using LogiBoardToolBox.ViewModels; using LogiBoardToolBox.Views; using Microsoft.UI.Xaml.Automation; namespace LogiBoardToolBox.Utilities; internal static partial class UiVerification { private static async Task VerifyLiveDpiAsync(MainPage page, WorkspaceHost tabs, Func capture, Action check) { var index = Array.IndexOf(args, "--live-dpi-replay") + 1; if (index >= args.Length) throw new ArgumentException("需要诊断回放目录。"); var folder = args[index]; var snapshot = await OnboardMemory.LoadBackupAsync(Path.Combine(folder, "snapshot.json")); var sensor = JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Combine(folder, "sensor.json")), ProfileJsonContext.Default.SensorPerformance)!; var reports = (await File.ReadAllLinesAsync(Path.Combine(folder, "switch-reports.txt"))).Select(Convert.FromHexString).ToArray(); var vm = page.ViewModel; vm.LoadAdvancedDpiDiagnosticSnapshot(snapshot, sensor, "Superlight2c"); var replay = await vm.AttachDpiDiagnosticReplayAsync(); try { tabs.SelectedIndex = 2; var editing = vm.SelectedDpiStage; var requests = replay.Requests; check("readback-current-dpi-differs-from-default", vm.DpiStages[0].IsCurrent && vm.DefaultDpi == 3 && vm.PerformanceSummary.Contains("750 × 750"), vm.PerformanceSummary); await capture("2c-current-750"); foreach (var (firstReport, stage, dpi) in new[] { (0, 1, 800), (2, 2, 900), (4, 0, 750) }) { replay.PostReport(reports[firstReport]); replay.PostReport(reports[firstReport + 1]); await Until(() => vm.DpiStages[stage].IsCurrent && vm.PerformanceSummary.Contains($"{dpi} × {dpi}"), $"live DPI {dpi}"); check($"live-dpi-{dpi}", vm.DpiStages.Count(s => s.IsCurrent) == 1 && ReferenceEquals(editing, vm.SelectedDpiStage), vm.PerformanceSummary); if (dpi != 750) await capture($"2c-current-{dpi}"); } await vm.FlushImmediateSaveAsync(); check("dpi-events-have-no-device-writes-or-requests", replay.WriteStarts == 0 && replay.Requests == requests && vm.DefaultDpi == 3 && vm.ShiftDpi == 1, "Captured notifications update presentation without changing the edited row, DPI settings or device."); tabs.SelectedIndex = 0; await capture("2c-side-buttons"); vm.SelectedButton = vm.StandardBindings[2]; vm.ResetSelectedButtonCommand.Execute(null); await vm.FlushImmediateSaveAsync(); check("2c-reset-middle-button-readback", !vm.HasError && vm.StandardBindings[2].Original.SequenceEqual(ButtonBinding.Mouse(3)), vm.Status); vm.AssignPaletteItemCommand.Execute(new AssignmentItem("DPI 循环", "actions", "special", "dpi-cycle", "")); await vm.FlushImmediateSaveAsync(); check("2c-dpi-cycle-written-to-middle-button", !vm.HasError && vm.StandardBindings[2].Original.SequenceEqual(ButtonBinding.Special(5)) && replay.Sectors.All(p => p.Value.SequenceEqual(snapshot.Sectors[p.Key])), "Remapping restores the exact captured DPI-cycle binding and leaves the other bytes intact."); } finally { vm.DetachDiagnosticReplay(); } } private static async Task VerifyMouseButtonPresentationAsync(MainPage page, WorkspaceHost tabs, Func capture, Action check) { var vm = page.ViewModel; var map = Tree(page).OfType().Single(); int[] MappedButtons() => Tree(map).OfType