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

namespace XCoder.App.UiTests;

/// <summary>工作台首页密度:欢迎区高度、卡片尺寸、每行数量与快捷/最近顺序</summary>
public class HomeLayoutTests
{
    private static Task RunOnUi(Action action) => UiSession.Run(action);

    [Fact]
    [DisplayName("首页欢迎区保持紧凑,工具卡片一行四个")]
    public Task Home_IsCompact() => RunOnUi(() =>
    {
        var oldQuick = QuickToolsStore.FilePath;
        var oldRecent = RecentToolsStore.FilePath;
        var dir = Path.Combine(Path.GetTempPath(), "xcoder-home-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(dir);

        try
        {
            QuickToolsStore.FilePath = Path.Combine(dir, "quick.tools");
            RecentToolsStore.FilePath = Path.Combine(dir, "recent.tools");
            File.WriteAllLines(QuickToolsStore.FilePath, QuickToolsStore.DefaultKeys);
            File.WriteAllLines(RecentToolsStore.FilePath, QuickToolsStore.DefaultKeys);

            var window = new ShellWindow { Width = 1200, Height = 800 };
            window.Show();
            Dispatcher.UIThread.RunJobs();
            try
            {
                var home = window.FindControl<StackPanel>("HomePanel");
                Assert.NotNull(home);
                Assert.True(home!.Children.Count >= 4);

                var hero = Assert.IsType<Border>(home.Children[0]);
                Assert.InRange(hero.Bounds.Height, 120, 190);

                var quick = Assert.IsType<StackPanel>(home.Children[1]);
                var recent = Assert.IsType<StackPanel>(home.Children[2]);
                var quickCards = Assert.IsType<WrapPanel>(quick.Children[^1]).Children;
                var recentCards = Assert.IsType<WrapPanel>(recent.Children[^1]).Children;

                Assert.Equal(8, quickCards.Count);
                Assert.Equal(4, recentCards.Count);

                var card = Assert.IsType<Border>(quickCards[0]);
                Assert.Equal(192, card.Width);
                Assert.Equal(112, card.Height);

                var cards = home.GetVisualDescendants().OfType<Border>()
                    .Where(e => e.Classes.Contains("card"))
                    .ToList();
                Assert.True(cards.Count >= 8);
                Assert.Equal(192, cards[0].Width);
                Assert.Equal(112, cards[0].Height);

                // 窗口宽 1200 时卡片按 4 个一行排布
                Assert.Single(cards.Take(4).Select(e => e.Bounds.Y).Distinct());
                Assert.NotEqual(cards[0].Bounds.Y, cards[4].Bounds.Y);
            }
            finally
            {
                window.Close();
            }
        }
        finally
        {
            QuickToolsStore.FilePath = oldQuick;
            RecentToolsStore.FilePath = oldRecent;
            try { Directory.Delete(dir, true); } catch { }
        }
    });
}