using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Automation.Peers; using Microsoft.UI.Xaml.Automation.Provider; using Microsoft.UI.Xaml.Media; using XFEWorkflow.Services; namespace XFEWorkflow.Views; public sealed partial class AppShellPage { private static async Task WaitForAccountAsync(Func condition) { for (var i = 0; i < 80; i++) { if (condition()) return; await Task.Delay(50); } throw new InvalidOperationException("用户界面状态未按预期变化"); } private static T AccountElement(DependencyObject root, string name) where T : FrameworkElement => UiSmokeTest.Descendants(root).OfType().FirstOrDefault(x => x.Name == name || AutomationProperties.GetAutomationId(x) == name) ?? throw new InvalidOperationException($"未找到账号控件 {name}:" + string.Join(", ", UiSmokeTest.Descendants(root).OfType().Select(x => x.Name + "/" + (x as ContentControl)?.Content))); private static async Task ActivateAccountButtonAsync(Button button) { try { await WaitForAccountAsync(() => button.IsLoaded && button.IsEnabled && button.ActualWidth > 0); } catch (InvalidOperationException) { throw new InvalidOperationException($"账号按钮不可用:{button.Name}, loaded={button.IsLoaded}, enabled={button.IsEnabled}, visibility={button.Visibility}, size={button.ActualWidth}x{button.ActualHeight}, popup={Current!.accountMenuOpen}, foreground={UiSmokeTest.GetForegroundWindow()}"); } // Invoke WinUI's native automation peer, so this account-only suite // does not send keys to whichever application currently has focus. var peer = FrameworkElementAutomationPeer.FromElement(button) ?? new ButtonAutomationPeer(button); ((IInvokeProvider)peer.GetPattern(PatternInterface.Invoke)).Invoke(); await Task.Delay(100); } private async Task CaptureAccountPopupAsync(string path) { var panel = (Panel)UserAccountMenu.Content; var background = panel.Background; try { // RenderTargetBitmap cannot capture a system backdrop. Use the // matching theme's base color for the visual-tree snapshot. panel.Background = new SolidColorBrush(ActualTheme == ElementTheme.Dark ? Microsoft.UI.ColorHelper.FromArgb(255, 32, 32, 32) : Microsoft.UI.ColorHelper.FromArgb(255, 243, 243, 243)); await UiSmokeTest.CaptureElementAsync(UserAccountMenu, path); } finally { panel.Background = background; } } private async Task OpenAccountPopupAsync() { App.MainWindow.Tray!.Show(); await ActivateAccountButtonAsync(HeaderAccountButton); await WaitForAccountAsync(() => accountMenuOpen && UserAccountMenu.IsLoaded && UserAccountMenu.ActualWidth > 0); } internal async Task SmokeAccountMenuAsync(string output, string url) { var services = AppServices.Current; var previousAddress = services.State.SpaceAddress; var previousTheme = RequestedTheme; try { services.State.SpaceAddress = url + "space/v1/"; services.Space.Reconnect(); await services.Space.LogoutAsync(); ViewModel.NavigationViewService.NavigateTo(); SetNavigationOpenForSmoke(false); RequestedTheme = ElementTheme.Dark; await Task.Delay(150); var page = navigationFrame.Content; await OpenAccountPopupAsync(); if (navigationFrame.Content != page) throw new InvalidOperationException("标题栏用户点击直接跳转页面,没有显示弹窗"); if (ViewModel.SignedOutVisibility != Visibility.Visible || AccountElement