using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
using NewLife.XCoder.Controls;
using NewLife.XCoder.Models;
using Xunit;
namespace XCoder.App.UiTests;
/// <summary>彩色日志视图测试</summary>
public class LogViewTests
{
[Fact]
[DisplayName("彩色日志按类别显示 TX/RX/ERR 并使用设计稿颜色")]
public async Task LogView_RendersColoredKinds()
{
await UiSession.Run(() =>
{
var time = new DateTime(2026, 1, 2, 3, 4, 5, 678);
var entries = new ObservableCollection<LogEntry>
{
new(time, LogKind.Tx, "send"),
new(time, LogKind.Rx, "recv"),
new(time, LogKind.Error, "boom"),
};
var view = new LogView { Entries = entries };
var window = new Window { Width = 480, Height = 240, Content = view };
window.Show();
Dispatcher.UIThread.RunJobs();
AvaloniaHeadlessPlatform.ForceRenderTimerTick();
var texts = view.GetVisualDescendants().OfType<TextBlock>().ToArray();
Assert.Contains(texts, e => e.Text == "TX");
Assert.Contains(texts, e => e.Text == "RX");
Assert.Contains(texts, e => e.Text == "ERR");
Assert.Contains(texts, e => e.Text == "03:04:05.678");
Assert.Equal(Color.Parse("#5FD0FF"), GetColor(texts.First(e => e.Text == "TX")));
Assert.Equal(Color.Parse("#6EE7A8"), GetColor(texts.First(e => e.Text == "RX")));
Assert.Equal(Color.Parse("#FF8A8A"), GetColor(texts.First(e => e.Text == "ERR")));
entries.Add(new LogEntry(time, LogKind.Warning, "careful"));
Dispatcher.UIThread.RunJobs();
Assert.Contains(view.GetVisualDescendants().OfType<TextBlock>(), e => e.Text == "WARN");
view.Clear();
Assert.Empty(entries);
window.Close();
return Task.CompletedTask;
});
}
[Fact]
[DisplayName("控制台字体使用跨平台等宽回退链")]
public async Task ConsoleFont_UsesMonoFallbackChain()
{
await UiSession.Run(() =>
{
Assert.NotNull(Application.Current);
Assert.True(Application.Current!.Resources.TryGetResource("FontMono", ThemeVariant.Light, out var value));
var family = Assert.IsType<FontFamily>(value);
Assert.Contains("Cascadia Code", family.FamilyNames);
Assert.Contains("Consolas", family.FamilyNames);
Assert.Contains("Noto Sans Mono CJK SC", family.FamilyNames);
return Task.CompletedTask;
});
}
private static Color GetColor(TextBlock text) => Assert.IsAssignableFrom<ISolidColorBrush>(text.Foreground).Color;
}
|