Skip to content
0.2.0-dev Download ZIP
107 lines ·5.0 KB Raw
1using AngleSharp.Dom;
2using EvilEngine.Html.Tests.Support;
3
4namespace EvilEngine.Html.Tests.Styling;
5
6public sealed class ResizeTests
7{
8 private const string Link = "<link rel=stylesheet href=tailwind.css>";
9
10 private static BlockBox Build(HtmlPage page, double width, double height, IReadOnlySet<IElement>? restyled = null)
11 {
12 return page.Build(new FixedWidgets(null), new MediaEnvironment(width, height), new ViewState(), restyled);
13 }
14
15 private static PageLayout Lay(HtmlPage page, BlockBox root, double width, double height)
16 {
17 return PageLayout.Create(root, width, new FixedFont(), height, page.Stateful());
18 }
19
20 private static object Drawn(PageLayout layout)
21 {
22 return (
23 layout.Groups().SelectMany(group => group.Rects)
24 .Select(rect => (rect.X, rect.Y, rect.Width, rect.Height, rect.Color, rect.Coverage)).ToList(),
25 layout.Groups().SelectMany(group => group.Runs)
26 .Select(run => (run.Text, run.X, run.Baseline, run.Width, run.Font, run.Color, run.DecorationThickness,
27 run.UnderlineOffset)).ToList(),
28 layout.Height);
29 }
30
31 private static (HtmlPage Page, object Resized, object Fresh) Resize(string html,
32 params (double W, double H)[] sizes)
33 {
34 var page = HtmlPage.Parse(Link + html);
35 Build(page, sizes[0].W, sizes[0].H);
36 BlockBox root = null!;
37 foreach (var (width, height) in sizes[1..])
38 root = Build(page, width, height, new HashSet<IElement>());
39 var (lastWidth, lastHeight) = sizes[^1];
40 var fresh = HtmlPage.Parse(Link + html);
41 return (page, Drawn(Lay(page, root, lastWidth, lastHeight)),
42 Drawn(Lay(fresh, Build(fresh, lastWidth, lastHeight), lastWidth, lastHeight)));
43 }
44
45 [Theory]
46 [InlineData(
47 "<body class='m-0 h-screen w-screen bg-zinc-900'><div class='flex h-full'><aside class='w-40 bg-black/40 p-4'>side</aside><main class='flex-1 overflow-y-auto p-4'><div class='grid grid-cols-3 gap-2'><div class='h-20 rounded-md border border-zinc-700'>card</div><div class='h-20 rounded-md'>two</div></div></main></div></body>")]
48 [InlineData(
49 "<body class='m-0'><div class='h-[calc(100vh-3rem)] bg-zinc-200'><div class='h-1/2 bg-red-500'>half</div></div></body>")]
50 [InlineData("<body class='m-0 h-screen'><div class='[height:inherit] bg-sky-300'>inherits the height</div></body>")]
51 [InlineData(
52 "<body class='m-0 text-[2vw]'><p class='m-0'>grows <span class='text-[1.5em]'>with</span> the width</p></body>")]
53 [InlineData(
54 "<body class='m-0'><div class='[letter-spacing:1vw]'><p class='m-0'>spaced <b>out</b></p></div></body>")]
55 [InlineData(
56 "<body class='m-0'><div class='p-2'><div class='w-[50vw] min-h-[10vmin] bg-lime-400'>half wide</div></div></body>")]
57 [InlineData("<body class='m-0 [--tall:40vh]'><div class='h-(--tall) bg-orange-500'>custom</div></body>")]
58 [InlineData(
59 "<body class='m-0'><div class='flex flex-col md:flex-row'><div class='h-10 w-20 bg-red-500 md:w-40'>a</div><div class='h-10 w-20 bg-blue-500'>b</div></div></body>")]
60 [InlineData("<body class='m-0'><p class='m-0 underline decoration-[0.5vw]'>thick <span>inner</span></p></body>")]
61 [InlineData("<body class='m-0'><p class='m-0 underline underline-offset-[1vh]'>low <span>inner</span></p></body>")]
62 [InlineData(
63 "<body class='m-0 min-h-screen'><table class='h-[20vh]'><tr class='align-top'><td class='[vertical-align:inherit]'>cell</td></tr></table></body>")]
64 public void AResizedPageDrawsWhatAFreshPageDraws(string html)
65 {
66 foreach (var sizes in new[]
67 {
68 new[] { (800.0, 600.0), (1000.0, 700.0) },
69 [(1000.0, 700.0), (500.0, 900.0)],
70 [(800.0, 600.0), (801.0, 600.0), (640.0, 1100.0), (800.0, 600.0)]
71 })
72 {
73 var (_, resized, fresh) = Resize(html, sizes);
74 Assert.Equal(fresh, resized);
75 }
76 }
77
78 [Fact]
79 public void ResizingRestylesOnlyTheElementsThatReadTheViewport()
80 {
81 var (page, _, _) = Resize(
82 "<body class='m-0 h-screen w-screen'><div class='flex h-full'><aside class='w-40 p-4'>side</aside><main class='flex-1 p-4'><div class='h-[50vh]'>half</div><p>text</p></main></div></body>",
83 (800, 600), (1000, 700));
84
85 Assert.Equal(2, page.ComputedStyles);
86 }
87
88 [Fact]
89 public void AnInheritedViewportLengthRestylesEveryDescendant()
90 {
91 var (page, _, _) = Resize("<body class='m-0 text-[2vw]'><p class='m-0'>a <span>b</span></p><div>c</div></body>",
92 (800, 600), (1000, 600));
93
94 Assert.Equal(4, page.ComputedStyles);
95 }
96
97 [Fact]
98 public void AMediaQueryThatFlipsRestylesEverything()
99 {
100 var html = "<body class='m-0'><div class='md:w-40'>a</div><p>b</p></body>";
101 var (page, _, _) = Resize(html, (1000, 600), (500, 600));
102 var all = HtmlPage.Parse(Link + html);
103 Build(all, 500, 600);
104
105 Assert.Equal(all.ComputedStyles, page.ComputedStyles);
106 }
107}