using AngleSharp.Dom; using EvilEngine.Html.Tests.Support; namespace EvilEngine.Html.Tests.Styling; public sealed class ResizeTests { private const string Link = ""; private static BlockBox Build(HtmlPage page, double width, double height, IReadOnlySet? restyled = null) { return page.Build(new FixedWidgets(null), new MediaEnvironment(width, height), new ViewState(), restyled); } private static PageLayout Lay(HtmlPage page, BlockBox root, double width, double height) { return PageLayout.Create(root, width, new FixedFont(), height, page.Stateful()); } private static object Drawn(PageLayout layout) { return ( layout.Groups().SelectMany(group => group.Rects) .Select(rect => (rect.X, rect.Y, rect.Width, rect.Height, rect.Color, rect.Coverage)).ToList(), layout.Groups().SelectMany(group => group.Runs) .Select(run => (run.Text, run.X, run.Baseline, run.Width, run.Font, run.Color, run.DecorationThickness, run.UnderlineOffset)).ToList(), layout.Height); } private static (HtmlPage Page, object Resized, object Fresh) Resize(string html, params (double W, double H)[] sizes) { var page = HtmlPage.Parse(Link + html); Build(page, sizes[0].W, sizes[0].H); BlockBox root = null!; foreach (var (width, height) in sizes[1..]) root = Build(page, width, height, new HashSet()); var (lastWidth, lastHeight) = sizes[^1]; var fresh = HtmlPage.Parse(Link + html); return (page, Drawn(Lay(page, root, lastWidth, lastHeight)), Drawn(Lay(fresh, Build(fresh, lastWidth, lastHeight), lastWidth, lastHeight))); } [Theory] [InlineData( "
card
two
")] [InlineData( "
half
")] [InlineData("
inherits the height
")] [InlineData( "

grows with the width

")] [InlineData( "

spaced out

")] [InlineData( "
half wide
")] [InlineData("
custom
")] [InlineData( "
a
b
")] [InlineData("

thick inner

")] [InlineData("

low inner

")] [InlineData( "
cell
")] public void AResizedPageDrawsWhatAFreshPageDraws(string html) { foreach (var sizes in new[] { new[] { (800.0, 600.0), (1000.0, 700.0) }, [(1000.0, 700.0), (500.0, 900.0)], [(800.0, 600.0), (801.0, 600.0), (640.0, 1100.0), (800.0, 600.0)] }) { var (_, resized, fresh) = Resize(html, sizes); Assert.Equal(fresh, resized); } } [Fact] public void ResizingRestylesOnlyTheElementsThatReadTheViewport() { var (page, _, _) = Resize( "
half

text

", (800, 600), (1000, 700)); Assert.Equal(2, page.ComputedStyles); } [Fact] public void AnInheritedViewportLengthRestylesEveryDescendant() { var (page, _, _) = Resize("

a b

c
", (800, 600), (1000, 600)); Assert.Equal(4, page.ComputedStyles); } [Fact] public void AMediaQueryThatFlipsRestylesEverything() { var html = "
a

b

"; var (page, _, _) = Resize(html, (1000, 600), (500, 600)); var all = HtmlPage.Parse(Link + html); Build(all, 500, 600); Assert.Equal(all.ComputedStyles, page.ComputedStyles); } }