namespace XFESystem.Browser.Tests; public sealed class BrowserStylesheetMutationTests { private sealed class Transport : IWebTransport { public readonly List Requests = []; public WebResponse Get(Uri address, BrowserLimits limits) { Requests.Add(address.AbsolutePath); var (mime, body) = address.AbsolutePath switch { "/" => ("text/html", "
Content
"), "/First.css" => ("text/css", "#Box{width:100px;height:30px;background:blue}"), "/Updated.css" => ("text/css", "#Box{width:180px;height:40px;background:green}"), _ => throw new InvalidOperationException("Unexpected fixture request") }; return new(address, 200, mime, body); } } [Fact] public void RemovingLinkedStylesheetRetractsItsRulesOnNextRealLayout() { var transport = new Transport(); var browser = new BrowserService(transport); browser.Navigate("http://test/"); var box = browser.Document!.GetElementById("Box")!; var first = browser.Layout(new(600, 400)); Assert.Equal(100, first.GetBoundingClientRect(box).Width); Assert.Equal(0x0000FFu, first.GetComputedStyle(box)!.Background.Rgb); browser.ExecuteJavaScript("document.getElementById('Theme').remove()"); var removed = browser.Layout(new(600, 400)); Assert.Equal(600, removed.GetBoundingClientRect(box).Width); Assert.Equal(0, removed.GetComputedStyle(box)!.Background.Alpha); Assert.Equal(new[] { "/", "/First.css" }, transport.Requests); } [Fact] public void ReplacingTheSameLinkAtStylesheetLimitLoadsNewRulesWithoutFalseOverflow() { var transport = new Transport(); var browser = new BrowserService(transport, new() { MaximumStyleSheets = 1 }); browser.Navigate("http://test/"); var box = browser.Document!.GetElementById("Box")!; browser.ExecuteJavaScript("document.getElementById('Theme').setAttribute('href','/Updated.css')"); var updated = browser.Layout(new(600, 400)); Assert.Equal(180, updated.GetBoundingClientRect(box).Width); Assert.Equal(40, updated.GetBoundingClientRect(box).Height); Assert.Equal(0x008000u, updated.GetComputedStyle(box)!.Background.Rgb); Assert.Equal(new[] { "/", "/First.css", "/Updated.css" }, transport.Requests); Assert.DoesNotContain("stylesheet count limit", browser.RefreshDocument().Text, StringComparison.Ordinal); } }