0.2.0-dev Download ZIP
112 lines ·5.4 KB Raw
| 1 | using AngleSharp.Dom; |
| 2 | using EvilEngine.Html.Tests.Support; |
| 3 | |
| 4 | namespace EvilEngine.Html.Tests.Styling; |
| 5 | |
| 6 | public sealed class RepaintTests |
| 7 | { |
| 8 | private const string Link = "<link rel=stylesheet href=tailwind.css>"; |
| 9 | |
| 10 | private static PageLayout Lay(HtmlPage page, BlockBox root) |
| 11 | { |
| 12 | return PageLayout.Create(root, 800, new FixedFont(), 600, page.Stateful()); |
| 13 | } |
| 14 | |
| 15 | private static BlockBox Build(HtmlPage page, ViewState state, IReadOnlySet<IElement>? restyled = null) |
| 16 | { |
| 17 | return page.Build(new FixedWidgets(null), new MediaEnvironment(800, 600), state, restyled); |
| 18 | } |
| 19 | |
| 20 | private static List<(double, double, double, double, Rgba, double)> Painted(PageLayout layout) |
| 21 | { |
| 22 | return |
| 23 | [ |
| 24 | .. layout.Groups().SelectMany(group => group.Rects) |
| 25 | .Where(rect => rect.Width > 0 && rect.Height > 0) |
| 26 | .Select(rect => (rect.X, rect.Y, rect.Width, rect.Height, rect.Color, rect.Coverage)) |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | private static List<TextRun> Written(PageLayout layout) |
| 31 | { |
| 32 | return [.. layout.Groups().SelectMany(group => group.Runs)]; |
| 33 | } |
| 34 | |
| 35 | private static (bool Repainted, PageLayout Layout, PageLayout Fresh) Hover(string html, string? from, string? to) |
| 36 | { |
| 37 | var page = HtmlPage.Parse(Link + html); |
| 38 | var state = new ViewState { Hover = from is null ? null : page.Document.GetElementById(from) }; |
| 39 | var before = Build(page, state); |
| 40 | var layout = Lay(page, before); |
| 41 | var snapshot = state.Snapshot(); |
| 42 | state.Hover = to is null ? null : page.Document.GetElementById(to); |
| 43 | var after = Build(page, state, page.Sheet!.Restyled(page.Document, snapshot, state)); |
| 44 | var repainted = layout.Repaint(before, after); |
| 45 | var fresh = HtmlPage.Parse(Link + html); |
| 46 | var freshState = new ViewState { Hover = to is null ? null : fresh.Document.GetElementById(to) }; |
| 47 | return (repainted, layout, Lay(fresh, Build(fresh, freshState))); |
| 48 | } |
| 49 | |
| 50 | [Theory] |
| 51 | [InlineData("<div id=a class='h-8 w-20 hover:bg-red-500'></div><div id=b class='h-4 bg-zinc-200'></div>", null, |
| 52 | "a")] |
| 53 | [InlineData("<div id=a class='h-8 w-20 hover:bg-red-500'></div><div id=b class='h-4 bg-zinc-200'></div>", "a", |
| 54 | null)] |
| 55 | [InlineData( |
| 56 | "<div id=a class='h-8 w-20 rounded-lg border border-zinc-300 bg-white hover:border-orange-500 hover:bg-zinc-100'></div>", |
| 57 | null, "a")] |
| 58 | [InlineData( |
| 59 | "<div class='flex gap-2 p-2'><div id=a class='h-8 w-20 rounded-md hover:bg-white/5'></div><div id=b class='h-8 w-20 rounded-md hover:bg-white/5'></div></div>", |
| 60 | "a", "b")] |
| 61 | [InlineData( |
| 62 | "<div class='h-20 overflow-y-auto'><div class='space-y-2'><div id=a class='h-12 rounded border border-neutral-800 hover:border-neutral-600 hover:bg-neutral-800'></div><div id=b class='h-12 rounded border border-neutral-800 hover:border-neutral-600 hover:bg-neutral-800'></div></div></div>", |
| 63 | "a", "b")] |
| 64 | [InlineData( |
| 65 | "<div class='relative h-20'><div id=a class='absolute top-2 left-4 size-8 bg-sky-300 hover:bg-sky-600'></div></div>", |
| 66 | null, "a")] |
| 67 | [InlineData("<div id=a class='group h-10 p-2'><div class='h-4 group-hover:bg-lime-400'></div></div>", null, "a")] |
| 68 | [InlineData("<a id=a class='block text-zinc-500 hover:text-black'>text <span>inside</span> and<br>after</a>", null, |
| 69 | "a")] |
| 70 | [InlineData( |
| 71 | "<a id=a class='block text-zinc-500 hover:bg-zinc-800 hover:text-white'>one</a><a id=b class='block text-zinc-500 hover:bg-zinc-800 hover:text-white'>two</a>", |
| 72 | "a", "b")] |
| 73 | [InlineData("<p><a id=a class='underline decoration-zinc-400 hover:decoration-orange-500'>link</a> text</p>", null, |
| 74 | "a")] |
| 75 | [InlineData( |
| 76 | "<div class='flex gap-2'><div id=a class='rounded border border-zinc-700 px-2 text-zinc-300 hover:border-zinc-500 hover:text-white'>card</div></div>", |
| 77 | null, "a")] |
| 78 | [InlineData( |
| 79 | "<div class='h-10 overflow-y-auto'><div id=a class='text-zinc-400 hover:text-white'>row one</div><div id=b class='text-zinc-400 hover:text-white'>row two</div></div>", |
| 80 | "a", "b")] |
| 81 | public void ARepaintPaintsWhatAFreshLayoutPaints(string html, string? from, string? to) |
| 82 | { |
| 83 | var (repainted, layout, fresh) = Hover(html, from, to); |
| 84 | |
| 85 | Assert.True(repainted); |
| 86 | Assert.Equal(Painted(fresh), Painted(layout)); |
| 87 | Assert.Equal(Written(fresh).Select(run => (run.Text, run.X, run.Baseline, run.Color, run.DecorationColor)), |
| 88 | Written(layout).Select(run => (run.Text, run.X, run.Baseline, run.Color, run.DecorationColor))); |
| 89 | } |
| 90 | |
| 91 | [Theory] |
| 92 | [InlineData("<ul class='list-disc'><li id=a class='hover:text-red-500'>item</li></ul>")] |
| 93 | [InlineData("<p><span id=a class='rounded bg-zinc-100 px-1 hover:text-red-500'>pill</span></p>")] |
| 94 | [InlineData("<div id=a class='h-8 p-1 hover:p-2'></div>")] |
| 95 | [InlineData("<div id=a class='h-8 hover:h-10 hover:bg-red-500'></div>")] |
| 96 | [InlineData("<div id=a class='h-8 hover:cursor-pointer'></div>")] |
| 97 | public void ChangesBeyondColourNeedALayout(string html) |
| 98 | { |
| 99 | Assert.False(Hover(html, null, "a").Repainted); |
| 100 | } |
| 101 | |
| 102 | [Fact] |
| 103 | public void OnlyElementsAStateRuleCanReachGetAPlaceholder() |
| 104 | { |
| 105 | var page = HtmlPage.Parse(Link + "<div id=a class='h-4 hover:bg-red-500'></div><div id=b class='h-4'></div>"); |
| 106 | var layout = Lay(page, Build(page, new ViewState())); |
| 107 | |
| 108 | Assert.Equal(["a"], page.Stateful().Select(element => element.Id!)); |
| 109 | Assert.Single(layout.Rects); |
| 110 | Assert.Equal(0, layout.Rects[0].Width); |
| 111 | } |
| 112 | } |