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

namespace XCoder.App.UiTests;

/// <summary>平台诊断页的交互级测试(OPS-1/OPS-2 的可视化承载)</summary>
/// <remarks>
/// 验证能力列表按当前平台声明填充、串口权限文本有结论、
/// 以及「生成命令」按钮确实按输入产出网卡配置命令。
/// </remarks>
public class PlatformToolViewTests
{
    [Fact]
    [DisplayName("加载后显示平台名、能力列表与串口权限结论")]
    public async Task Load_FillsCapabilities()
    {
        await UiSession.Run(() =>
        {
            var view = new PlatformToolView();

            var platform = view.FindControl<TextBlock>("PlatformText");
            var capabilities = view.FindControl<TextBox>("CapabilityBox");
            var serial = view.FindControl<TextBlock>("SerialText");

            Assert.NotNull(platform);
            Assert.NotNull(capabilities);
            Assert.NotNull(serial);

            Assert.False(String.IsNullOrWhiteSpace(platform!.Text));
            Assert.Contains("串口", capabilities!.Text ?? "");
            Assert.False(String.IsNullOrWhiteSpace(serial!.Text));

            // 声明的能力项数量应与当前平台一致(每行形如“✔ 串口 …”)
            var expected = PlatformCapabilities.Current.Count;
            var lines = (capabilities.Text ?? "")
                .Split('\n', StringSplitOptions.RemoveEmptyEntries)
                .Where(e => e.TrimStart().StartsWith("✔") || e.TrimStart().StartsWith("✘"))
                .Count();

            Assert.Equal(expected, lines);
        });
    }

    [Fact]
    [DisplayName("生成命令按钮按输入产出静态地址配置命令")]
    public async Task Plan_GeneratesCommand()
    {
        await UiSession.Run(() =>
        {
            var view = new PlatformToolView();

            var ip = view.FindControl<TextBox>("IpBox");
            var mask = view.FindControl<TextBox>("MaskBox");
            var gateway = view.FindControl<TextBox>("GatewayBox");
            var adapterBox = view.FindControl<ComboBox>("AdapterBox");
            var plan = view.FindControl<Button>("PlanButton");
            var planBox = view.FindControl<TextBox>("PlanBox");

            Assert.NotNull(ip);
            Assert.NotNull(mask);
            Assert.NotNull(gateway);
            Assert.NotNull(adapterBox);
            Assert.NotNull(plan);
            Assert.NotNull(planBox);

            // 真实使用中需先枚举并选中网卡,这里直接给一个适配器名
            adapterBox!.Items.Add("以太网");
            adapterBox.SelectedIndex = 0;

            ip!.Text = "192.168.1.50";
            mask!.Text = "255.255.255.0";
            gateway!.Text = "192.168.1.1";

            plan!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));

            var text = planBox!.Text ?? "";
            Assert.False(String.IsNullOrWhiteSpace(text));
            Assert.Contains("192.168.1.50", text);
        });
    }

    [Fact]
    [DisplayName("非法 IP 不生成命令并给出提示")]
    public async Task Plan_InvalidIp_ShowsMessage()
    {
        await UiSession.Run(() =>
        {
            var view = new PlatformToolView();

            var ip = view.FindControl<TextBox>("IpBox");
            var plan = view.FindControl<Button>("PlanButton");
            var planBox = view.FindControl<TextBox>("PlanBox");
            var status = view.FindControl<TextBlock>("StatusText");

            Assert.NotNull(ip);
            Assert.NotNull(plan);
            Assert.NotNull(planBox);

            ip!.Text = "999.999.999.999";
            plan!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));

            Assert.True(String.IsNullOrWhiteSpace(planBox!.Text));
            Assert.NotNull(status);
            Assert.False(String.IsNullOrWhiteSpace(status!.Text));
        });
    }
}