XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using AngleSharp.Dom;
using AngleSharp.Html.Parser;

namespace XFESystem.Browser.Tests;

/// <summary>Exercises the official MIT-licensed 0.52.2 core CSS embedded in Browser.
/// Inline sizes represent editor-generated DOM style attributes; the engine has no Monaco branches.</summary>
public sealed class MonacoCssLayoutTests
{
    private static string OfficialCss()
    {
        var assembly = typeof(BrowserLayoutEngine).Assembly;
        string name = Assert.Single(assembly.GetManifestResourceNames(), name => name.Replace('\\', '/') == "MonacoCore/EditorCore.css");
        using var stream = assembly.GetManifestResourceStream(name);
        Assert.NotNull(stream); using var reader = new StreamReader(stream); return reader.ReadToEnd();
    }
    private static IDocument EditorPage(int containerWidth, int containerHeight) => new HtmlParser().ParseDocument($$"""
        <html><head><style>html,body{height:100%;margin:0}#Host{position:relative;width:{{containerWidth}}px;height:{{containerHeight}}px}
        #Editor{position:absolute;left:0;right:0;top:28px;bottom:0;--vscode-editor-foreground:#D4D4D4;--vscode-editor-background:#1E1E1E}
        .mtk1{color:#D4D4D4}.mtk6{color:#569CD6}</style><link id=Css rel=stylesheet></head><body>
        <div id=Host><div id=Editor class="monaco-editor vs-dark" style="font-size:14px;line-height:20px;font-family:monospace">
          <div id=Guard class=overflow-guard style="position:relative;width:100%;height:100%">
            <div class=monaco-scrollable-element style="position:absolute;left:60px;right:0;top:0;bottom:0;overflow:hidden">
              <div id=Lines class="lines-content" style="position:absolute;width:100%;height:100%">
                <div class=view-lines style="position:absolute;width:100%;height:100%">
                  <div id=Line class=view-line style="top:0;height:20px"><span><span id=Keyword class=mtk6>const</span><span class=mtk1> answer = 42;</span></span></div>
                </div>
              </div>
            </div>
            <textarea id=Input class=inputarea style="left:60px;top:0;width:1px;height:20px"></textarea>
          </div>
        </div></div></body></html>
        """);

    [Theory][InlineData(640, 400)][InlineData(930, 570)]
    public void OfficialMonacoCssComputesLiveAbsoluteViewportAndTokens(int width, int height)
    {
        var page = EditorPage(width, height); var css = OfficialCss(); Assert.Equal(66_405, css.Length);
        var result = new BrowserLayoutEngine().Layout(page, new(1200, 800), [new(css, page.GetElementById("Css"))]);
        var editor = result.GetBoundingClientRect(page.GetElementById("Editor")!);
        Assert.Equal(width, editor.Width); Assert.Equal(height - 28, editor.Height); Assert.Equal(28, editor.Y);
        var line = result.GetBoundingClientRect(page.GetElementById("Line")!);
        Assert.Equal(60, line.X, 5); Assert.Equal(28, line.Y, 5); Assert.Equal(width - 60, line.Width, 5); Assert.Equal(20, line.Height, 5);
        var keyword = page.GetElementById("Keyword")!; Assert.Equal("rgb(86, 156, 214)", result.GetComputedStyle(keyword)!.GetPropertyValue("color"));
        var command = Assert.Single(result.Commands, command => command.Text == "const"); Assert.Equal(0x569CD6u, command.Color); Assert.Equal(14, command.Font.Size);
        Assert.Equal("nowrap", result.GetComputedStyle(page.GetElementById("Lines")!.QuerySelector(".view-lines")!)!.WhiteSpace);
        Assert.Equal("absolute", result.GetComputedStyle(page.GetElementById("Input")!)!.Position);
        Assert.Equal(0, result.GetComputedStyle(page.GetElementById("Input")!)!.Color.Alpha);
        Assert.Contains(result.Commands, command => command.Kind == BrowserDrawKind.PushClip);
        Assert.True(result.Diagnostics.Count < 300); // unsupported upstream features produce bounded diagnostics, not exceptions
    }

    [Fact] public void SameMonacoDomRelayoutChangesBoxesInsteadOfUsingFixedEditorGeometry()
    {
        var page = EditorPage(640, 400); var css = OfficialCss(); var engine = new BrowserLayoutEngine();
        var sheets = new[] { new BrowserStyleSheet(css, page.GetElementById("Css")) };
        var first = engine.Layout(page, new(1200, 800), sheets);
        page.GetElementById("Host")!.SetAttribute("style", "width:777px;height:333px");
        var next = engine.Layout(page, new(1200, 800), sheets);
        Assert.Equal(640, first.GetBoundingClientRect(page.GetElementById("Editor")!).Width);
        Assert.Equal(777, next.GetBoundingClientRect(page.GetElementById("Editor")!).Width);
        Assert.Equal(305, next.GetBoundingClientRect(page.GetElementById("Editor")!).Height);
        Assert.Equal(717, next.GetBoundingClientRect(page.GetElementById("Line")!).Width);
    }
}