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

namespace XCoder.App.Tests;

/// <summary>浮动窗口几何记忆测试</summary>
public class UiLayoutStoreTests
{
    private static String UseTempFile()
    {
        var path = Path.Combine(Path.GetTempPath(), "xcoder-ui-" + Guid.NewGuid().ToString("N") + ".windows");
        UiLayoutStore.FilePath = path;

        return path;
    }

    [Theory]
    [DisplayName("合法几何文本可解析")]
    [InlineData("100 200 800 600", 100, 200, 800, 600)]
    [InlineData("-1920 0 1200 800", -1920, 0, 1200, 800)]
    public void Parse_Valid(String text, Int32 x, Int32 y, Int32 w, Int32 h)
    {
        var geometry = UiLayoutStore.Parse(text);

        Assert.NotNull(geometry);
        Assert.Equal(x, geometry!.X);
        Assert.Equal(y, geometry.Y);
        Assert.Equal(w, geometry.Width);
        Assert.Equal(h, geometry.Height);
    }

    [Theory]
    [DisplayName("格式或范围非法的几何返回空")]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("1 2 3")]
    [InlineData("a b c d")]
    [InlineData("0 0 100 100")]
    [InlineData("0 0 99999 100")]
    [InlineData("99999 0 800 600")]
    public void Parse_Invalid(String? text)
    {
        Assert.Null(UiLayoutStore.Parse(text));
    }

    [Fact]
    [DisplayName("保存后可读回,重复保存覆盖同一工具")]
    public void SaveAndLoad_Roundtrip()
    {
        var path = UseTempFile();
        try
        {
            UiLayoutStore.Save("sql", new WindowGeometry(10, 20, 900, 700));
            UiLayoutStore.Save("can", new WindowGeometry(30, 40, 800, 600));
            UiLayoutStore.Save("sql", new WindowGeometry(50, 60, 1000, 720));

            var sql = UiLayoutStore.Load("sql");
            var can = UiLayoutStore.Load("can");

            Assert.NotNull(sql);
            Assert.Equal(50, sql!.X);
            Assert.Equal(1000, sql.Width);
            Assert.NotNull(can);
            Assert.Equal(800, can!.Width);
            Assert.Equal(2, File.ReadAllLines(path).Length);
            Assert.Null(UiLayoutStore.Load("no-such"));
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("非法几何不写入文件")]
    public void Save_InvalidIgnored()
    {
        var path = UseTempFile();
        try
        {
            UiLayoutStore.Save("sql", new WindowGeometry(0, 0, 10, 10));

            Assert.False(File.Exists(path) && File.ReadAllText(path).Contains("sql"));
            Assert.Null(UiLayoutStore.Load("sql"));
        }
        finally
        {
            if (File.Exists(path)) File.Delete(path);
        }
    }

    [Fact]
    [DisplayName("空键读写返回空且不抛异常")]
    public void EmptyKey()
    {
        UseTempFile();

        UiLayoutStore.Save("", new WindowGeometry(0, 0, 800, 600));
        Assert.Null(UiLayoutStore.Load(""));
        Assert.Null(UiLayoutStore.Load(null!));
    }
}