借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
7.92 KiB
XCoder
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Headless;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
using NewLife.XCoder.Services;
using Xunit;

namespace XCoder.App.UiTests;

/// <summary>控件皮肤:内置控件必须使用语义令牌,并随浅色/深色主题切换</summary>
public class ControlSkinTests
{
    private static Task RunOnUi(Func<Task> action) => UiSession.Run(action);

    [Fact]
    [DisplayName("控件皮肤使用语义令牌:圆角、高度与输入框底色")]
    public async Task Skin_UsesSemanticTokens()
    {
        await RunOnUi(() =>
        {
            try
            {
                ThemeService.Apply("light", false);

                Assert.NotNull(Application.Current);
                Assert.True(Application.Current!.Resources.TryGetResource("ControlCornerRadius", ThemeVariant.Light, out var radius));
                Assert.Equal(new CornerRadius(8), radius);

                Assert.True(Application.Current.Resources.TryGetResource("TextControlThemeMinHeight", ThemeVariant.Light, out var height));
                Assert.Equal(32d, height);

                Assert.True(Application.Current.Resources.TryGetResource("TextControlBackground", ThemeVariant.Light, out var background));
                var brush = Assert.IsType<SolidColorBrush>(background);
                Assert.Equal(Color.Parse("#FFFFFF"), brush.Color);
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }

    [Fact]
    [DisplayName("切换深色主题后控件皮肤同步换色")]
    public async Task Skin_FollowsThemeVariant()
    {
        await RunOnUi(() =>
        {
            try
            {
                Assert.NotNull(Application.Current);
                Assert.True(Application.Current!.Resources.TryGetResource("TextControlBackground", ThemeVariant.Light, out var light));
                var lightBrush = Assert.IsType<SolidColorBrush>(light);

                Assert.True(Application.Current.Resources.TryGetResource("TextControlBackground", ThemeVariant.Dark, out var dark));
                var darkBrush = Assert.IsType<SolidColorBrush>(dark);

                Assert.NotEqual(lightBrush.Color, darkBrush.Color);
                Assert.Equal(Color.Parse("#151E2B"), darkBrush.Color);
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }

    [Fact]
    [DisplayName("输入框底色随主题切换并保持语义令牌")]
    public async Task TextBox_Background_FollowsTheme()
    {
        await RunOnUi(() =>
        {
            try
            {
                ThemeService.Apply("light", false);
                var window = new Window { Width = 320, Height = 120 };
                var box = new TextBox();
                window.Content = box;
                window.Show();
                Dispatcher.UIThread.RunJobs();

                Assert.Equal(Color.Parse("#FFFFFF"), Assert.IsAssignableFrom<ISolidColorBrush>(box.Background).Color);

                ThemeService.Apply("dark", false);
                Dispatcher.UIThread.RunJobs();
                Assert.Equal(Color.Parse("#151E2B"), Assert.IsAssignableFrom<ISolidColorBrush>(box.Background).Color);

                window.Close();
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }

    [Fact]
    [DisplayName("复选框与单选按钮的选中态使用主色")]
    public async Task CheckControls_Checked_UseAccent()
    {
        await RunOnUi(() =>
        {
            try
            {
                ThemeService.Apply("light", false);
                var window = new Window { Width = 320, Height = 160 };
                var check = new CheckBox { Content = "check", IsChecked = true };
                var radio = new RadioButton { Content = "radio", IsChecked = true };
                window.Content = new StackPanel { Children = { check, radio } };
                window.Show();
                Dispatcher.UIThread.RunJobs();
                AvaloniaHeadlessPlatform.ForceRenderTimerTick();

                var box = check.GetVisualDescendants().OfType<Border>()
                    .FirstOrDefault(e => e.Name == "NormalRectangle");
                Assert.NotNull(box);
                Assert.Equal(Color.Parse("#0B72D9"), Assert.IsAssignableFrom<ISolidColorBrush>(box!.Background).Color);
                Assert.Equal(Color.Parse("#0B72D9"), Assert.IsAssignableFrom<ISolidColorBrush>(box.BorderBrush).Color);

                var ellipse = radio.GetVisualDescendants().OfType<Ellipse>()
                    .FirstOrDefault(e => e.Name == "CheckOuterEllipse");
                Assert.NotNull(ellipse);
                Assert.Equal(Color.Parse("#0B72D9"), Assert.IsAssignableFrom<ISolidColorBrush>(ellipse!.Fill).Color);

                window.Close();
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }

    [Fact]
    [DisplayName("控制台日志聚焦时保持深底,不闪白")]
    public async Task Console_KeepsDarkBackgroundOnFocus()
    {
        await RunOnUi(() =>
        {
            try
            {
                ThemeService.Apply("light", false);
                var window = new Window { Width = 420, Height = 240 };
                var box = new TextBox { Classes = { "console" }, Text = "log line" };
                window.Content = box;
                window.Show();
                Dispatcher.UIThread.RunJobs();

                var border = box.GetVisualDescendants().OfType<Border>()
                    .FirstOrDefault(e => e.Name == "PART_BorderElement");
                Assert.NotNull(border);

                box.Focus();
                Dispatcher.UIThread.RunJobs();
                AvaloniaHeadlessPlatform.ForceRenderTimerTick();

                Assert.Equal(Color.Parse("#0A0F16"), Assert.IsAssignableFrom<ISolidColorBrush>(border!.Background).Color);
                Assert.Equal(Color.Parse("#0B72D9"), Assert.IsAssignableFrom<ISolidColorBrush>(border.BorderBrush).Color);

                ThemeService.Apply("dark", false);
                Dispatcher.UIThread.RunJobs();
                Assert.Equal(Color.Parse("#070C13"), Assert.IsAssignableFrom<ISolidColorBrush>(border.Background).Color);

                window.Close();
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }

    [Fact]
    [DisplayName("输入框获得焦点时边框切换为主色")]
    public async Task TextBox_Focus_UsesAccentBorder()
    {
        await RunOnUi(() =>
        {
            try
            {
                ThemeService.Apply("light", false);
                var window = new Window { Width = 420, Height = 200 };
                var box = new TextBox { Text = "focus" };
                window.Content = box;
                window.Show();
                box.Focus();
                Dispatcher.UIThread.RunJobs();
                AvaloniaHeadlessPlatform.ForceRenderTimerTick();

                var border = box.GetVisualDescendants().OfType<Border>()
                    .FirstOrDefault(e => e.Name == "PART_BorderElement");
                Assert.NotNull(border);
                var brush = Assert.IsAssignableFrom<ISolidColorBrush>(border!.BorderBrush);
                Assert.Equal(Color.Parse("#0B72D9"), brush.Color);

                window.Close();
            }
            finally
            {
                ThemeService.Apply("auto", false);
            }

            return Task.CompletedTask;
        });
    }
}