diff --git a/AGENTS.md b/AGENTS.md index cddc4ba..40f2c67 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,16 +9,18 @@ AngleSharp.Browser adds a fully usable (visible) browser based on top of the Ang ## Architecture - `src/AngleSharp.Browser` (NuGet package): the UI-agnostic `BrowserEngine` (navigation, painting, input). Everything configurable lives in `BrowserOptions`. All DOM work is serialized and runs on AngleSharp's event loop, as AngleSharp is not thread-safe. -- Frames: `BrowserEngine.PrepareFrameAsync` lays out on the event loop; `BrowserFrame.Rasterize` (raw RGBA, overscan above/below the viewport) runs on any thread, so `BrowserView` pipelines layout and raster. Each frame's hit-test map answers cursor/link lookups without the DOM; scroll-only frames reuse the last display list unless the page has fixed/sticky boxes. -- Page scrolling is smooth and runs ahead of the DOM (`ScrollPosition` vs `ScrollTop`); the view shifts the last frame meanwhile and draws an overlay scrollbar. While a script keeps the page busy, `PrepareFrameAsync` repositions the last frame (DOM-free, like a compositor) instead of waiting for the event loop. A frame after document mutations only (not resources, time or hover: `_needsFullLayout`) asks `IDomHarness.UpdateFrame()` first, which keeps or patches the last frame when the changes are confined to boxes that can be laid out on their own (out-of-flow boxes, or in-flow blocks staying the same from outside). Until a page has a measured extent, scrolling is not clamped to it (the next frame clamps). +- Frames: `BrowserEngine.PrepareFrameAsync` lays out on the event loop (the first frame of a document only what is in view, `PrepareViewportFrame`, the next one the whole page); `BrowserFrame.Rasterize` (raw RGBA, overscan above/below the viewport) runs on any thread, so `BrowserView` pipelines layout and raster. Each frame's hit-test map answers cursor/link lookups without the DOM; scroll-only frames reuse the last display list unless the page has fixed/sticky boxes. +- Page scrolling is smooth and runs ahead of the DOM (`ScrollPosition` vs `ScrollTop`); the view shifts the last frame meanwhile and draws an overlay scrollbar. While a script keeps the page busy, `PrepareFrameAsync` repositions the last frame (DOM-free, like a compositor) instead of waiting for the event loop. A frame after document mutations only (not resources, time or hover: `_needsFullLayout`) asks `IDomHarness.UpdateFrame()` first, which keeps or patches the last frame when the changes are confined to boxes that can be laid out on their own (out-of-flow boxes, flex and grid items, or in-flow blocks staying the same from outside). Until a page has a measured extent, scrolling is not clamped to it (the next frame clamps). A root with `overflow: hidden` can't be scrolled by the user, but keeps its position. - Windows: one `BrowserEngine` (browsing context, history) per window. `WindowRequested` (target=_blank, Ctrl+click) lets the host open one via `CreateWindow()`, which shares the profile data (one synchronized cookie provider, storage and cache factories) but gets its own HTTP handler; unhandled, the page navigates in place. +- Iframes: the document of each one has its own browsing context and harness (with a viewport of the iframe's size); the page's layout leaves a `DrawSubframeCommand`, and the nested frame is prepared right after it, so layouts don't nest. The page's hit-test map delegates to the nested one, and `BrowserEngine.Subframes.cs` routes pointer coordinates, hover, focus, wheel and links. All contexts of an engine share one event loop (`FrameEventLoop` scopes cancelling), as the DOM isn't thread-safe. Script isolation is enforced at the boundary in AngleSharp.Js (`OriginPolicy`, `CrossOriginWindow`): C# code here, like the renderer, sees every document, so anything acting for a script in a frame has to act in that frame's document (see `RunScript`, `FollowHyperlink`). - `src/AngleSharp.Browser.Avalonia` (NuGet package): `BrowserView` displays frames and forwards input; with just a `Source` it creates and owns an engine while attached. Inside of this namespace `Avalonia` means the namespace itself, so `using` directives for the Avalonia framework stay above the namespace declaration. -- `src/Vision`: the sample app (Avalonia), not packaged. `App` hosts the windows and the user's `Models/BrowserSettings` (JSON in the app data directory), which apply to windows opened after saving. -- Vision's own UI beyond the toolbar (diagnostics tab, settings, about) is HTML/CSS in `Pages/` (embedded resources), shown by a `BrowserView` with a scripting-free engine. `HtmlPage` subclasses wire the DOM with .NET event handlers via `BrowserEngine.InvokeAsync`; don't add JavaScript there. `BrowserEngine.Diagnostics` collects the current page's requests, errors and console output. -- `src/AngleSharp.Browser.Tests`: NUnit tests driving the engine headlessly. `src/AngleSharp.Browser.Avalonia.Tests`: NUnit tests of the control using Avalonia's headless platform with real Skia rendering. Don't use `Assert.ThrowsAsync` with the engine (it blocks and hangs); use an awaited try/catch. +- Site information: `BrowserEngine.Site.cs` finds the page's icon while syncing the document and records the certificate of each host in the HTTP handler's validation callback (unless the host set its own), keeping the platform's verdict. +- `src/Vision`: the sample app (Avalonia), not packaged. Its icon lives in `Assets/` (`vision.ico` for Windows/Linux, `vision.png` with the macOS margins, `vision.icns`); on macOS a window's icon is the Dock icon, so the page's icon only shows in the address bar there, while other platforms also show it in the title bar. `App` hosts the windows and the user's `Models/BrowserSettings` (JSON in the app data directory), which apply to windows opened after saving. +- Vision's own UI beyond the toolbar (diagnostics tab, settings, about) is HTML/CSS in `Pages/` (embedded resources), shown by a `BrowserView` with a scripting-free engine. `HtmlPage` subclasses wire the DOM with .NET event handlers via `BrowserEngine.InvokeAsync`; don't add JavaScript there. `BrowserEngine.Diagnostics` collects the current page's requests, errors, console output and frame timings. The side panel of a window shows one page at a time, the diagnostics or the inspector, which share its width (a third of the window, unless the user resized it); `InspectorPage` builds its views on the event loop of the inspected engine and only looks at expanded rows, so its refreshes stay cheap. +- `src/AngleSharp.Browser.Tests`: NUnit tests driving the engine headlessly. `src/AngleSharp.Browser.Avalonia.Tests`: NUnit tests of the control and of Vision's panels using Avalonia's headless platform with real Skia rendering. Don't use `Assert.ThrowsAsync` with the engine (it blocks and hangs); use an awaited try/catch. - `build/`: the Fallout build, run via `build.sh`/`build.ps1`, `.github/workflows/ci.yml` calls it. The version comes from the top of `CHANGELOG.md`; a push to `main` publishes a release, one to `devel` a pre-release. - `docs/`: the documentation, published through the doclet in `src/AngleSharp.Browser.Docs`. Keep it in sync with the public API. -- `BrowserEngine` patches a few upstream gaps (User-Agent header, `noscript` style, text documents never completing, timer delays clamped because a zero-delay `setInterval` never yields before AngleSharp 1.9.0); remove them once fixed upstream. +- `BrowserEngine` patches a few upstream gaps (User-Agent header, `noscript` style, text documents never completing, timer delays clamped because a zero-delay `setInterval` never yields before AngleSharp 1.9.0, `label.control` not finding wrapped controls, event listener errors reported by an `ErrorEvent` without a type, and `NavigationHistory` reaching the protected `Document.DocumentUri` setter by reflection, as `pushState` has to change the document's address, pointer events being plain `MouseEvent`s as the `PointerEvent` of AngleSharp is not published yet, and `LegacyKeyboardEvent.Code` as it knows the key's code by itself from then on); remove them once fixed upstream. ## Most Important Commands diff --git a/CHANGELOG.md b/CHANGELOG.md index 91a7672..d8e501e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,20 @@ # 0.1.0 -Released on Monday, November 2 2026. +Released on Monday, November 9 2026. - Added `AngleSharp.Browser`, the UI-agnostic `BrowserEngine` for navigating, painting and interacting with pages - Added `AngleSharp.Browser.Avalonia`, the `BrowserView` control displaying web pages in Avalonia applications +- Added iframes, with their documents isolated by origin and sandbox, and input routed into them - Added `Vision`, a sample browser built on top of the control +- Added details of errors (type, location and stack trace), request headers and frame timings to `BrowserDiagnostics` +- Added a waterfall of the requests, the rendering timings and the stack traces of errors to the diagnostics of `Vision` +- Added an inspector to `Vision`, showing the DOM, the CSSOM and the render tree of a page +- Added the server GC to `Vision`, which shortens the pauses for garbage collection while laying out large pages +- Added precompiled code (ReadyToRun) to published versions of `Vision`, and optimizing often called code without a delay, which shortens the first frames of a page +- Added the metrics of the renderer to the frame timings of `BrowserDiagnostics` (`FrameTiming.Metrics`), and their breakdown to the performance diagnostics of `Vision` +- Added showing a page that was just loaded early on: its first frame styles and lays out just what is in view, the next one all of the page +- Added patching the frame after hovering or focusing an element, which is painted again instead of laid out when only colors, backgrounds, borders or text decoration change +- Added the page's icon (`IconUrl`, `IconUrls`, `LoadIconAsync`) and the certificate of its connection (`Certificate`, `ServerCertificate`) to `BrowserEngine` +- Added the page's icon and a site information window (the certificate and its chain) to `Vision`, which got its own icon and application menu +- Added decompressing responses (gzip, deflate, brotli), which some servers send without being asked (e.g. the CDN of the Microsoft login) +- Added `navigator.serviceWorker`: pages can register service workers, which are installed and activated, but don't run (requests go to the network) diff --git a/README.md b/README.md index db508d7..4fcb4f3 100644 --- a/README.md +++ b/README.md @@ -72,14 +72,45 @@ dotnet run --project src/Vision -- https://example.org Cookies, web storage and the HTTP cache are persisted in a profile directory (by default in the local application data folder). Use `--profile ` to pick another one or `--in-memory` to persist nothing. -The diagnostics show what a page did; they are HTML and CSS rendered by a second `BrowserView`, like the rest of Vision's own UI: +The diagnostics (F12) show what a page did: errors with their stack traces, the console output, a waterfall of the requests and the timings of the rendering. They are HTML and CSS rendered by a second `BrowserView`, like the rest of Vision's own UI: -![Vision showing Wikipedia with the diagnostics panel](https://raw.githubusercontent.com/AngleSharp/AngleSharp.Browser/main/docs/screenshots/diagnostics.png) +![Vision showing Wikipedia with the requests in the diagnostics panel](https://raw.githubusercontent.com/AngleSharp/AngleSharp.Browser/main/docs/screenshots/diagnostics.png) + +The inspector (Ctrl+Shift+I) opens in the same place and shows the page as AngleSharp sees it right now: its DOM, its CSSOM and its render tree, with the matched rules, computed style and box of any element: + +![Vision showing GitHub with the DOM in the inspector panel](https://raw.githubusercontent.com/AngleSharp/AngleSharp.Browser/main/docs/screenshots/inspector.png) | Settings | About | | --- | --- | | ![The settings of Vision](https://raw.githubusercontent.com/AngleSharp/AngleSharp.Browser/main/docs/screenshots/settings.png) | ![The about window of Vision in dark mode](https://raw.githubusercontent.com/AngleSharp/AngleSharp.Browser/main/docs/screenshots/about.png) | +## Help Us Render the Web + +Every page that looks wrong points at something we can improve, and Vision is the easiest way to find those pages. Run it with a website you care about: + +```bash +dotnet run --project src/Vision -- https://your-favorite-website.example +``` + +If the page does not render correctly, then please do one of the following: + +1. **Report it.** [Open an issue](https://github.com/AngleSharp/AngleSharp.Browser/issues/new/choose) with the address, a screenshot, and what you expected to see. +2. **Even better, fix it.** Point your coding agent at the page and let it find the cause, then send a pull request to the project responsible. + +The diagnostics (F12) show the page's failed requests, errors and console output, and the inspector (Ctrl+Shift+I) shows the DOM, CSSOM and render tree AngleSharp ended up with, which is a good place to start. The fix may belong to any part of the ecosystem: + +| Project | Responsible for | +| --- | --- | +| [AngleSharp](https://github.com/AngleSharp/AngleSharp) | HTML parsing and the DOM | +| [AngleSharp.Css](https://github.com/AngleSharp/AngleSharp.Css) | CSS parsing, the cascade and computed styles | +| [AngleSharp.Xml](https://github.com/AngleSharp/AngleSharp.Xml) | XML parsing and validation | +| [AngleSharp.Renderer](https://github.com/AngleSharp/AngleSharp.Renderer) | Layout and painting | +| [AngleSharp.Js](https://github.com/AngleSharp/AngleSharp.Js) | Running scripts and exposing the DOM to them | +| [AngleSharp.Io](https://github.com/AngleSharp/AngleSharp.Io) | Requests and other I/O | +| [AngleSharp.Browser](https://github.com/AngleSharp/AngleSharp.Browser) | Navigation, input, scrolling and Vision itself | + +Not sure which one it is? Open the issue here and we will help to find out. A small reproduction and a test make a pull request much easier to review, whoever (or whatever) wrote it. See the [contributing guide](.github/CONTRIBUTING.md) for the details. + ## .NET Foundation This project is supported by the [.NET Foundation](https://dotnetfoundation.org). diff --git a/build/Build.cs b/build/Build.cs index a9d2b1c..aa33819 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -52,8 +52,6 @@ class Build : FalloutBuild string Version { get; set; } - IReadOnlyCollection TargetFrameworks { get; set; } - protected override void OnBuildInitialized() { var parser = new ReleaseNotesParser(); @@ -88,11 +86,6 @@ protected override void OnBuildInitialized() Log.Information("Building version: {Version}", Version); TargetProjects = TargetLibNames.Select(name => Solution.GetProject(name).NotNull($"{name} could not be loaded!")).ToList(); - - TargetFrameworks = TargetProjects.SelectMany(project => project.GetTargetFrameworks()).Distinct().ToList(); - TargetFrameworks.NotNull("No TargetFramework(s) found to build for!"); - - Log.Information("Target Framework(s): {Frameworks}", TargetFrameworks); } Target Clean => _ => _ diff --git a/docs/general/02-Engine.md b/docs/general/02-Engine.md index dcfb8de..ea5d732 100644 --- a/docs/general/02-Engine.md +++ b/docs/general/02-Engine.md @@ -28,6 +28,25 @@ await engine.NavigateAsync("example.org"); Console.WriteLine(engine.CanGoBack); ``` +## Site Information + +| Member | Description | +| --- | --- | +| `IconUrls`, `IconUrl` | Where the page's icon may come from, best first: bitmaps of ``, then of ``, `/favicon.ico` of an HTTP(S) page, and declared SVG icons last. `IconUrl` is the first of them. | +| `LoadIconAsync()` | Loads the first of them that is an image, with the page's cookies and cache; `null` if there is none. Images are recognized by their content, as servers send icons with all kinds of content types. | +| `Certificate` | The `ServerCertificate` the server of the current HTTPS page presented. | +| `GetCertificate(host)` | The certificate a host presented the last time the engine connected to it. | + +A changed icon raises `StateChanged`. A `ServerCertificate` is a snapshot of what the platform verified: names, issuer, validity, fingerprints, key, the chain up to the root, and `PolicyErrors` (`IsValid`). The engine captures it in the validation callback of its HTTP handler, keeping the platform's verdict; a host setting its own callback (via `BrowserOptions.ConfigureIo`) can use `ServerCertificate.Create` to the same end. A page served from the HTTP cache without a new connection may have no certificate. + +```cs +engine.StateChanged += async (_, _) => +{ + var icon = await engine.LoadIconAsync(); + Console.WriteLine($"{engine.IconUrl}: {icon?.Length ?? 0} bytes, issued by {engine.Certificate?.IssuerName}"); +}; +``` + ## Threading AngleSharp is not thread-safe, so all DOM work is serialized and runs on AngleSharp's event loop. This has some consequences: @@ -70,7 +89,7 @@ The underlying `IBrowsingContext` is available via `Context`, e.g., to retrieve | Event | Raised when | | --- | --- | | `Invalidated` | The visual output changed and a new frame should be painted. | -| `StateChanged` | The address, title, loading state, or history state changed. | +| `StateChanged` | The address, title, icon, loading state, or history state changed. | | `HoverChanged` | The hovered link or the cursor changed. | | `ScrollChanged` | The scroll position changed. | | `WindowRequested` | The page asks for a new window. See [Configuration](05-Configuration.md#multiple-windows). | @@ -90,8 +109,16 @@ foreach (var request in engine.Diagnostics.Requests) foreach (var message in engine.Diagnostics.Messages) { - Console.WriteLine($"[{message.Kind}] {message.Text}"); + Console.WriteLine($"[{message.Kind}] {message.ErrorType} {message.Text}"); + Console.WriteLine(message.StackTrace); +} + +foreach (var frame in engine.Diagnostics.Frames) +{ + Console.WriteLine($"{frame.Kind}: layout {frame.Layout} (styles {frame.Metrics.Style}), raster {frame.Raster}"); } ``` +Requests come with their `StartedAt` and `Duration` as well as their `RequestHeaders` and `ResponseHeaders`. Errors tell as much as is known about them: `ErrorType`, `Line`, `Column` and the `StackTrace` of the script (or of the .NET exception, if no script threw). `Source` names the script, if known; AngleSharp.Js does not report that yet. `Frames` has the timings of the latest painted frames (those made by `PrepareFrameAsync` and rasterized), where `Kind` tells if the document was laid out (`Layout`), the last frame was patched (`Update`) or just moved (`Reposition`). The `Metrics` of a frame come from AngleSharp.Renderer and tell where the time of its layout went (`Setup`, `Style`, `Layout`, `HitTesting`, `Subframes`) and how much work that was (e.g., `Elements`, `Boxes`, `TrialLayouts`, `AllocatedBytes`); they are `RenderMetrics.None` for a frame that needed no layout. + The `Changed` event tells when there is something new. `Clear()` removes what has been collected so far. diff --git a/docs/general/03-Rendering.md b/docs/general/03-Rendering.md index 636dbba..5707830 100644 --- a/docs/general/03-Rendering.md +++ b/docs/general/03-Rendering.md @@ -41,6 +41,8 @@ if (frame is not null) Preparing a frame also refreshes what the engine knows for hit testing. The cursor and links are then looked up without touching the DOM, which keeps pointer moves responsive on busy pages. +The first frame of a page shows up early: only what is in the viewport is styled and laid out for it. The engine asks for the next frame right away (it raises `Invalidated`), which lays out the whole page. Until then, the page below the viewport is blank, and the scroll position is not limited to the height of the page. + ## Overscan and Scrolling A frame may be larger than the viewport: `OverscanTop` rows are painted above the viewport, and some more below it. The viewport starts at row `OverscanTop`. diff --git a/docs/general/04-Input.md b/docs/general/04-Input.md index 4c54c8a..829eee5 100644 --- a/docs/general/04-Input.md +++ b/docs/general/04-Input.md @@ -46,6 +46,24 @@ await engine.KeyDownAsync("Enter"); await engine.KeyUpAsync("Enter"); ``` +## Selects + +A `select` showing several options (`multiple` or `size`) is a list box: + +- A click selects a row. With `multiple`, Ctrl (or Cmd) toggles it and Shift selects the rows from the last one clicked. +- The up and down keys move the selection, with Shift (and `multiple`) extending it. +- The wheel scrolls it before the page. + +Any other `select` is a dropdown. Pressing it, or Enter, Space or Alt+Down while it has the focus, opens its list: + +- `IsSelectPopupOpen` tells if the list is open. Frames from `PrepareFrameAsync` show it on top of the page (the image of `PaintAsync` does not), so a host has nothing to do for it. It stays within the viewport, opening above the select without room below. +- While it is open, the list takes all keys: they move its highlight (skipping what is disabled), Enter, Space or Tab choose the highlighted row, and Escape closes it without a change. A click on a row chooses it. +- A press elsewhere or scrolling the page closes it. So do the select losing the focus or going away, and a change of the viewport. +- While it is closed, the up and down keys, Home, End, Page Up, Page Down and typing change the value right away. +- The list uses the color scheme of the select: dark if its `color-scheme` (or else the `color-scheme` meta element of the page) supports dark, and doesn't also support light unless `ColorScheme` is `dark`. `ColorScheme` alone doesn't make it dark, as in other browsers. Colors of the select and of its options win over those of the scheme. + +Choosing a row fires `input` and `change` if the value changed, then `click`. + ## Scrolling `ScrollAsync` scrolls by a distance: what is below the pointer, if that is a scroll container, or else the page. The returned task completes once the scrolling arrived. diff --git a/docs/general/05-Configuration.md b/docs/general/05-Configuration.md index a83f616..34b698d 100644 --- a/docs/general/05-Configuration.md +++ b/docs/general/05-Configuration.md @@ -54,6 +54,10 @@ var options = new BrowserOptions }; ``` +Like a browser, the handler decompresses responses (gzip, deflate and brotli; `AutomaticDecompression`), which some servers send even if not asked to. + +Pages can register service workers (`navigator.serviceWorker`, through `ServiceWorkerPlatform`). The workers are installed and activated, but their scripts don't run, so every request goes to the network. Setting `io.ServiceWorkerPlatform` to `null` in `ConfigureIo` removes the API, and another platform can run the workers. + `Configure` receives the AngleSharp configuration before the browsing context is created, so further services can be added: ```cs diff --git a/docs/general/07-Vision.md b/docs/general/07-Vision.md index 9f74b1a..19d8cf2 100644 --- a/docs/general/07-Vision.md +++ b/docs/general/07-Vision.md @@ -25,7 +25,7 @@ The profile and the settings are kept in the `Vision` folder of the local applic | File | Shows | | --- | --- | | `App.axaml.cs` | One engine per window, sharing a profile using `CreateWindow`, and handling `WindowRequested`. | -| `ViewModels/MainViewModel.cs` | A toolbar and status bar on top of the engine's state and events. | +| `ViewModels/MainViewModel.cs` | A toolbar and status bar on top of the engine's state and events, including the page's icon. | | `Views/MainWindow.axaml` | Placing `BrowserView` in a window, next to a second one for the diagnostics. | | `Models/BrowserSettings.cs` | Turning user settings into `BrowserOptions`. | -| `Pages/` | The app's own UI (diagnostics, settings, about) written as HTML and CSS, and wired up with .NET event handlers through `InvokeAsync`, in an engine with scripting disabled. | +| `Pages/` | The app's own UI (diagnostics, inspector, settings, site information, about) written as HTML and CSS, and wired up with .NET event handlers through `InvokeAsync`, in an engine with scripting disabled. | diff --git a/docs/screenshots/about.png b/docs/screenshots/about.png index 16960d7..6d007e1 100644 Binary files a/docs/screenshots/about.png and b/docs/screenshots/about.png differ diff --git a/docs/screenshots/diagnostics.png b/docs/screenshots/diagnostics.png index cc4b79a..e8afb34 100644 Binary files a/docs/screenshots/diagnostics.png and b/docs/screenshots/diagnostics.png differ diff --git a/docs/screenshots/inspector.png b/docs/screenshots/inspector.png new file mode 100644 index 0000000..ff0ed49 Binary files /dev/null and b/docs/screenshots/inspector.png differ diff --git a/docs/screenshots/page.png b/docs/screenshots/page.png index e6eafee..85c01fa 100644 Binary files a/docs/screenshots/page.png and b/docs/screenshots/page.png differ diff --git a/docs/screenshots/settings.png b/docs/screenshots/settings.png index d6d9e51..c2aef17 100644 Binary files a/docs/screenshots/settings.png and b/docs/screenshots/settings.png differ diff --git a/docs/tutorials/01-API.md b/docs/tutorials/01-API.md index 40c2fc5..52f5fc4 100644 --- a/docs/tutorials/01-API.md +++ b/docs/tutorials/01-API.md @@ -21,7 +21,7 @@ The following APIs cover most day-to-day tasks: - `ClickAsync`, `TextInputAsync`, `KeyDownAsync`, `ScrollAsync` : Interacts with the page like a user. - `Diagnostics` -: Lists the requests, errors and console output of the page. +: Lists the requests, errors, console output and frame timings of the page. - `BrowserView` : Displays an engine in Avalonia. @@ -33,6 +33,7 @@ The following APIs cover most day-to-day tasks: | --- | --- | | `Url`, `Title` | The address (including one being loaded) and the title. | | `IsLoading` | If a navigation is ongoing. | +| `IconUrl`, `IconUrls`, `Certificate` | The page's icon and the `ServerCertificate` of its connection. See [Site Information](../general/02-Engine.md#site-information). | | `CanGoBack`, `CanGoForward` | The state of the history. | | `Document`, `Context` | The current `IDocument` (use it within `InvokeAsync`) and the `IBrowsingContext`. | | `ViewportWidth`, `ViewportHeight` | The size of the viewport in CSS pixels. | @@ -59,6 +60,7 @@ The following APIs cover most day-to-day tasks: | `KeyDownAsync`, `KeyUpAsync`, `TextInputAsync` | Keyboard input. | | `ScrollAsync(deltaX, deltaY)`, `ScrollToAsync(top, animate)` | Scrolling. | | `CreateWindow()`, `CreateWindow(options)` | Creates an engine sharing the profile. | +| `LoadIconAsync()`, `GetCertificate(host)` | Loads the page's icon, or gets the certificate of a host. | All asynchronous methods take an optional `CancellationToken`. @@ -80,8 +82,9 @@ All asynchronous methods take an optional `CancellationToken`. | Member | Description | | --- | --- | -| `Requests` | A list of `NetworkEntry`: `Method`, `Url`, `StartedAt`, `Duration`, `StatusCode`, `ContentType`, `ContentLength`, `IsPending`. | -| `Messages` | A list of `DiagnosticMessage`: `Kind` (`Console` or `Error`), `Time`, `Text`. | +| `Requests` | A list of `NetworkEntry`: `Method`, `Url`, `StartedAt`, `Duration`, `StatusCode`, `ContentType`, `ContentLength`, `RequestHeaders`, `ResponseHeaders`, `IsPending`. | +| `Messages` | A list of `DiagnosticMessage`: `Kind` (`Console` or `Error`), `Time`, `Text`, and for errors `ErrorType`, `Source`, `Line`, `Column`, `StackTrace`. | +| `Frames` | A list of `FrameTiming` for the latest painted frames: `Time`, `Kind` (`Layout`, `Update` or `Reposition`), `Layout`, `Raster`, and the `Metrics` of the renderer (the time of each phase and how much work it did). | | `Changed` | Raised when something was added or cleared, on any thread. | | `Clear()` | Removes what has been collected. | diff --git a/docs/tutorials/04-Questions.md b/docs/tutorials/04-Questions.md index 6047e8c..b882aa2 100644 --- a/docs/tutorials/04-Questions.md +++ b/docs/tutorials/04-Questions.md @@ -63,3 +63,27 @@ await engine.NavigateAsync("data:text/html," + Uri.EscapeDataString("Hell ## Does the engine run on servers without a display? Yes. Painting only requires SkiaSharp, whose native assets are included. On Linux they depend on fontconfig, so make sure it and some fonts are installed. Only `AngleSharp.Browser.Avalonia` needs a UI. + +## How do I make large pages paint faster? + +Use the server GC in the application. Laying out a large page keeps a lot alive until it is done (the styles and boxes of all elements), which the default workstation GC copies on a single thread for every collection meanwhile; the server GC does it on all cores, and its heaps adapt to the size of the application. For GitHub's start page, the pauses for garbage collection went from about 150 ms to 40 ms per frame, for about 200 MB more memory: + +```xml +<PropertyGroup> + <ServerGarbageCollection>true</ServerGarbageCollection> +</PropertyGroup> +``` + +The first frames of a page are mostly spent compiling and optimizing code. A host shortens them by publishing precompiled code, and by not having the runtime wait for a quiet moment before it optimizes what is called often: + +```xml +<PropertyGroup> + <PublishReadyToRun Condition="'$(RuntimeIdentifier)' != ''">true</PublishReadyToRun> +</PropertyGroup> + +<ItemGroup> + <RuntimeHostConfigurationOption Include="System.Runtime.TieredCompilation.CallCountingDelayMs" Value="0" /> +</ItemGroup> +``` + +The styles and the style maps of large pages are computed on all cores. A host that has none to spare turns this off with the AppContext switches `AngleSharp.Css.DisableParallelism` and `AngleSharp.Renderer.DisableParallelism`. diff --git a/src/AngleSharp.Browser.Avalonia.Tests/AngleSharp.Browser.Avalonia.Tests.csproj b/src/AngleSharp.Browser.Avalonia.Tests/AngleSharp.Browser.Avalonia.Tests.csproj index 624e261..73ca673 100644 --- a/src/AngleSharp.Browser.Avalonia.Tests/AngleSharp.Browser.Avalonia.Tests.csproj +++ b/src/AngleSharp.Browser.Avalonia.Tests/AngleSharp.Browser.Avalonia.Tests.csproj @@ -16,5 +16,6 @@ <ItemGroup> <ProjectReference Include="..\AngleSharp.Browser.Avalonia\AngleSharp.Browser.Avalonia.csproj" /> + <ProjectReference Include="..\Vision\Vision.csproj" /> </ItemGroup> </Project> diff --git a/src/AngleSharp.Browser.Avalonia.Tests/BrowserViewTests.cs b/src/AngleSharp.Browser.Avalonia.Tests/BrowserViewTests.cs index c501587..35b132a 100644 --- a/src/AngleSharp.Browser.Avalonia.Tests/BrowserViewTests.cs +++ b/src/AngleSharp.Browser.Avalonia.Tests/BrowserViewTests.cs @@ -3,6 +3,7 @@ using Avalonia.Controls; using Avalonia.Headless; using Avalonia.Headless.NUnit; +using Avalonia.Input; using Avalonia.Media.Imaging; using Avalonia.Platform; using Avalonia.Styling; @@ -108,12 +109,12 @@ public async Task AnEngineOfTheHostIsKeptWhenDetached() public async Task AnEngineShowingAPageKeepsItAndUpdatesTheSource() { using var engine = new BrowserEngine(); - await engine.NavigateAsync(BluePage); var view = new BrowserView { Source = RedPage }; var window = Show(view); await WaitUntil(() => view.Engine?.Title == "Red"); var created = view.Engine!; + await engine.NavigateAsync(BluePage); view.Engine = engine; Assert.That(view.Source, Is.EqualTo(BluePage)); @@ -180,6 +181,64 @@ await WaitUntil(() => window.Close(); } + [AvaloniaTest] + public async Task ClickingADropdownShowsItsListAndChoosesARow() + { + var view = new BrowserView(); + var window = Show(view); + var engine = new BrowserEngine(new BrowserOptions { IsScriptingEnabled = false }); + view.Engine = engine; + await engine.LoadHtmlAsync("<style>html, body { margin: 0; height: 100%; background: rgb(255,0,0) } select { display: block; width: 300px; height: 20px }</style><select><option>a</option><option>b</option><option>c</option></select>"); + await WaitUntil(() => engine.HasPaintedDocument); + + // on the page, where the list goes + const int X = 280; + const int Y = 30; + await WaitUntilAsync(async () => IsRed(await GetPixelAsync(window, X, Y))); + + window.MouseDown(new global::Avalonia.Point(10, 10), MouseButton.Left); + window.MouseUp(new global::Avalonia.Point(10, 10), MouseButton.Left); + await WaitUntil(() => engine.IsSelectPopupOpen); + await WaitUntilAsync(async () => !IsRed(await GetPixelAsync(window, X, Y))); + + // the second row + window.MouseDown(new global::Avalonia.Point(10, 20 + 1 + (1.5 * 19)), MouseButton.Left); + window.MouseUp(new global::Avalonia.Point(10, 20 + 1 + (1.5 * 19)), MouseButton.Left); + await WaitUntil(() => !engine.IsSelectPopupOpen); + await WaitUntilAsync(async () => IsRed(await GetPixelAsync(window, X, Y))); + + var value = await engine.InvokeAsync(document => ((AngleSharp.Html.Dom.IHtmlSelectElement)document!.QuerySelector("select")!).Value); + Assert.That(value, Is.EqualTo("b")); + window.Close(); + engine.Dispose(); + } + + private static async Task<byte[]> GetPixelAsync(Window window, int x, int y) + { + await Dispatcher.UIThread.InvokeAsync(() => { }, DispatcherPriority.Background); + using var frame = window.CaptureRenderedFrame(); + return frame is null ? new byte[4] : GetPixel(frame, x, y); + } + + private static async Task WaitUntilAsync(Func<Task<bool>> condition) + { + var watch = System.Diagnostics.Stopwatch.StartNew(); + + while (!await condition()) + { + Assert.That(watch.ElapsedMilliseconds, Is.LessThan(10000), "Timed out."); + await Task.Delay(10); + Dispatcher.UIThread.RunJobs(); + } + } + + private static unsafe byte[] GetPixel(WriteableBitmap frame, int x, int y) + { + using var buffer = frame.Lock(); + var pixel = (byte*)buffer.Address + (y * buffer.RowBytes) + (x * 4); + return buffer.Format == PixelFormat.Bgra8888 ? [pixel[0], pixel[1], pixel[2], pixel[3]] : [pixel[2], pixel[1], pixel[0], pixel[3]]; + } + private static string CreatePage(string title, string background) => "data:text/html," + Uri.EscapeDataString($"<!doctype html><html style='background:{background}'><title>{title}"); diff --git a/src/AngleSharp.Browser.Avalonia.Tests/VisionPanelTests.cs b/src/AngleSharp.Browser.Avalonia.Tests/VisionPanelTests.cs new file mode 100644 index 0000000..f714982 --- /dev/null +++ b/src/AngleSharp.Browser.Avalonia.Tests/VisionPanelTests.cs @@ -0,0 +1,244 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using AngleSharp.Dom; +using AngleSharp.Html.Dom; +using Avalonia.Headless.NUnit; +using Avalonia.Threading; +using NUnit.Framework; +using Vision.Pages; + +namespace AngleSharp.Browser.Avalonia.Tests; + +/// +/// The panels looking at a page, each in its own engine like in Vision. +/// +[TestFixture] +public class VisionPanelTests +{ + private const string Page = """ + + T + +

Hello

x
+ """; + + private BrowserEngine _target = null!; + + [SetUp] + public void Setup() => _target = new BrowserEngine(new BrowserOptions { ProfileDirectory = null, ViewportWidth = 800, ViewportHeight = 600 }); + + [TearDown] + public void TearDown() => _target.Dispose(); + + [AvaloniaTest] + public async Task TheInspectorShowsTheCurrentDom() + { + await _target.LoadHtmlAsync(Page); + using var page = new InspectorPage("light", _target); + await page.LoadAsync(); + + await WaitUntil(page, d => Tree(d).Contains("id=\"title\"") && Tree(d).Contains("note")); + + // the document is alive: what scripts or anybody else do to it shows up + await _target.InvokeAsync(d => d!.Body!.AppendChild(d.CreateElement("article"))); + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("refresh")!).DoClick()); + + await WaitUntil(page, d => Tree(d).Contains("article")); + } + + [AvaloniaTest] + public async Task SelectingAnElementShowsItsAttributesRulesAndComputedStyle() + { + await _target.LoadHtmlAsync(Page); + using var page = new InspectorPage("light", _target); + await page.LoadAsync(); + await WaitUntil(page, d => FindRow(d, "h1") is not null); + + await page.Engine.InvokeAsync(d => ((IHtmlElement)FindRow(d, "h1")!).DoClick()); + + await WaitUntil(page, d => d!.GetElementById("details-top")!.TextContent.Contains("Matched rules")); + var details = await page.Engine.InvokeAsync(d => d!.GetElementById("details-top")!.TextContent); + var computed = await page.Engine.InvokeAsync(d => d!.GetElementById("computed")!.TextContent); + Assert.That(details, Does.Contain("big")); + Assert.That(details, Does.Contain("h1")); + Assert.That(computed, Does.Contain("color")); + } + + [AvaloniaTest] + public async Task TheInspectorShowsTheStyleSheetsAndTheirRules() + { + await _target.LoadHtmlAsync(Page); + using var page = new InspectorPage("light", _target); + await page.LoadAsync(); + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("tab-cssom")!).DoClick()); + await WaitUntil(page, d => Tree(d).Contains("3 rules")); + + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.QuerySelector("#tree .twisty")!).DoClick()); + + await WaitUntil(page, d => Tree(d).Contains("h1") && Tree(d).Contains("@media")); + } + + [AvaloniaTest] + public async Task SelectingAnyRuleShowsItsDetails() + { + await _target.LoadHtmlAsync(""" + +

x

+ """); + using var page = new InspectorPage("light", _target); + await page.LoadAsync(); + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("tab-cssom")!).DoClick()); + await WaitUntil(page, d => Tree(d).Contains("rules")); + + // expand everything, then select every row once + for (var round = 0; round < 4; round++) + { + var collapsed = await page.Engine.InvokeAsync(d => d!.QuerySelectorAll("#tree .twisty").Where(m => m.TextContent == "▸").Select(m => m.Closest(".row")!.GetAttribute("data-id")!).ToArray()); + + foreach (var id in collapsed) + { + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.QuerySelector($"#tree .row[data-id='{id}'] .twisty")!).DoClick()); + await WaitUntil(page, d => d!.QuerySelector($"#tree .row[data-id='{id}'] .twisty")?.TextContent != "▸"); + } + } + + var ids = await page.Engine.InvokeAsync(d => d!.QuerySelectorAll("#tree .row:not(.more)").Select(m => m.GetAttribute("data-id")!).ToArray()); + + foreach (var id in ids) + { + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.QuerySelector($"#tree .row[data-id='{id}']")!).DoClick()); + await WaitUntil(page, d => d!.QuerySelector($"#tree .row.selected[data-id='{id}']") is not null); + var (row, details) = await page.Engine.InvokeAsync(d => (d!.QuerySelector($"#tree .row[data-id='{id}']")!.TextContent, d.GetElementById("details-top")!.TextContent)); + Assert.That(details, Does.Not.Contain("failed"), row); + } + + Assert.That(await page.Engine.InvokeAsync(Tree), Does.Contain("(unsupported selector)")); + } + + [AvaloniaTest] + public async Task TheRenderTreeLeavesOutWhatIsNotRendered() + { + await _target.LoadHtmlAsync(Page); + using var page = new InspectorPage("light", _target); + await page.LoadAsync(); + + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("tab-render")!).DoClick()); + + await WaitUntil(page, d => Tree(d).Contains("h1#title.big")); + var tree = await page.Engine.InvokeAsync(Tree); + Assert.That(tree, Does.Not.Contain("gone")); + Assert.That(tree, Does.Not.Contain("head")); + } + + [AvaloniaTest] + public async Task TheDiagnosticsShowStackTraces() + { + await _target.LoadHtmlAsync(""); + using var page = new DiagnosticsPage("light", _target); + await page.LoadAsync(); + + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("tab-errors")!).DoClick()); + + await WaitUntil(page, d => d!.GetElementById("errors")!.TextContent.Contains("TypeError: oh no")); + var errors = await page.Engine.InvokeAsync(d => d!.GetElementById("errors")!.TextContent); + Assert.That(errors, Does.Contain("broken")); + } + + [AvaloniaTest] + public async Task TheDiagnosticsShowTheTimingsOfFrames() + { + await _target.LoadHtmlAsync("

Hello

"); + (await _target.PrepareFrameAsync())!.Rasterize(); + using var page = new DiagnosticsPage("light", _target); + await page.LoadAsync(); + + await page.Engine.InvokeAsync(d => ((IHtmlElement)d!.GetElementById("tab-performance")!).DoClick()); + + await WaitUntil(page, d => d!.QuerySelector("#performance .frame .layout") is not null); + var text = await page.Engine.InvokeAsync(d => d!.GetElementById("performance")!.TextContent); + Assert.That(text, Does.Contain("Latest layout")); + Assert.That(text, Does.Contain("trial layouts")); + } + + [AvaloniaTest] + public async Task TheSiteInformationTellsAboutAnInsecureConnection() + { + using var page = new CertificatePage("light", "http://example.org/", null, () => { }); + await page.LoadAsync(); + + var text = await page.Engine.InvokeAsync(d => d!.Body!.TextContent); + + Assert.That(text, Does.Contain("example.org")); + Assert.That(text, Does.Contain("Connection is not secure")); + } + + [AvaloniaTest] + public async Task TheSiteInformationShowsTheCertificateAndItsChain() + { + using var rootKey = System.Security.Cryptography.ECDsa.Create(); + var rootRequest = new System.Security.Cryptography.X509Certificates.CertificateRequest("CN=Test Root, O=Test Trust", rootKey, System.Security.Cryptography.HashAlgorithmName.SHA256); + rootRequest.CertificateExtensions.Add(new System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension(true, false, 0, true)); + using var root = rootRequest.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-10), DateTimeOffset.UtcNow.AddYears(5)); + using var key = System.Security.Cryptography.ECDsa.Create(); + var request = new System.Security.Cryptography.X509Certificates.CertificateRequest("CN=example.org", key, System.Security.Cryptography.HashAlgorithmName.SHA256); + using var leaf = request.Create(root, DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(30), [1, 2, 3, 4]); + using var chain = new System.Security.Cryptography.X509Certificates.X509Chain(); + chain.ChainPolicy.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck; + chain.ChainPolicy.TrustMode = System.Security.Cryptography.X509Certificates.X509ChainTrustMode.CustomRootTrust; + chain.ChainPolicy.CustomTrustStore.Add(root); + chain.Build(leaf); + var certificate = ServerCertificate.Create("example.org", leaf, chain, System.Net.Security.SslPolicyErrors.None); + + using var page = new CertificatePage("light", "https://example.org/index.html", certificate, () => { }); + await page.LoadAsync(); + var text = await page.Engine.InvokeAsync(d => d!.Body!.TextContent); + + Assert.That(text, Does.Contain("Connection is secure")); + Assert.That(text, Does.Contain("Test Trust")); + Assert.That(text, Does.Contain("Root authority")); + Assert.That(text, Does.Contain("01 02 03 04")); + Assert.That(text, Does.Contain("(in 29 days)").Or.Contain("(in 30 days)")); + } + + private static string Tree(IDocument? document) => document!.GetElementById("tree")!.TextContent; + + private static IElement? FindRow(IDocument? document, string tag) + { + foreach (var row in document!.QuerySelectorAll("#tree .row")) + { + if (row.QuerySelector(".tag")?.TextContent == tag) + { + return row; + } + } + + return null; + } + + private static async Task WaitUntil(HtmlPage page, Func condition) + { + var deadline = DateTime.UtcNow.AddSeconds(10); + + while (!await page.Engine.InvokeAsync(condition)) + { + Assert.That(DateTime.UtcNow, Is.LessThan(deadline), "Condition was not met in time."); + Dispatcher.UIThread.RunJobs(); + await Task.Delay(20); + } + } +} diff --git a/src/AngleSharp.Browser.Tests/BrowserEngineTests.cs b/src/AngleSharp.Browser.Tests/BrowserEngineTests.cs index cd28967..fa98bea 100644 --- a/src/AngleSharp.Browser.Tests/BrowserEngineTests.cs +++ b/src/AngleSharp.Browser.Tests/BrowserEngineTests.cs @@ -1,6 +1,7 @@ namespace AngleSharp.Browser.Tests; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; @@ -8,7 +9,10 @@ namespace AngleSharp.Browser.Tests; using AngleSharp.Css.Dom; using AngleSharp.Dom; using AngleSharp.Html.Dom; +using AngleSharp.Html.Dom.Events; using AngleSharp.Io; +using AngleSharp.Js; +using AngleSharp.Renderer.Rendering; using NUnit.Framework; [TestFixture] @@ -40,133 +44,1202 @@ public void NormalizeAddressCompletesUserInput(string input, string expected) Assert.That(BrowserEngine.NormalizeAddress(input), Is.EqualTo(expected)); } + private async Task<(IDocument Nested, IHtmlInlineFrameElement Frame)> LoadFrameAsync(string frameHtml, string pageStyle = "") + { + var srcdoc = System.Net.WebUtility.HtmlEncode(frameHtml); + await _engine.LoadHtmlAsync($""); + IHtmlInlineFrameElement frame = null!; + await _engine.InvokeAsync(document => frame = (IHtmlInlineFrameElement)document!.GetElementById("frame")!); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + await _engine.PrepareFrameAsync(); + return (frame.ContentDocument!, frame); + } + + [Test] + public async Task IframeShowsItsDocumentInItsBox() + { + await LoadFrameAsync(""); + + var frame = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(frame, 100, 100), Is.EqualTo(((byte)255, (byte)0, (byte)0))); + Assert.That(PixelOf(frame, 10, 100), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + Assert.That(PixelOf(frame, 100, 10), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + Assert.That(PixelOf(frame, 460, 100), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + } + + [Test] + public async Task IframeClipsWhatItsDocumentOverflowsWith() + { + await LoadFrameAsync("
"); + + var frame = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(frame, 100, 100), Is.EqualTo(((byte)0, (byte)255, (byte)0))); + Assert.That(PixelOf(frame, 500, 100), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + Assert.That(PixelOf(frame, 100, 300), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + } + + [Test] + public async Task IframeHasADefaultSize() + { + await _engine.LoadHtmlAsync(""); + var (width, height) = await _engine.InvokeAsync(document => + { + var box = document!.GetElementById("frame")!.GetBoundingClientRect(); + return (box.Width, box.Height); + }); + + Assert.That(width, Is.EqualTo(304)); + Assert.That(height, Is.EqualTo(154)); + } + + [Test] + public async Task ClickReachesTheElementInAnIframe() + { + var (nested, _) = await LoadFrameAsync(""); + var clicks = new List<(double X, double Y)>(); + await _engine.InvokeAsync(_ => nested.GetElementById("b")!.AddEventListener("click", (_, e) => clicks.Add((((MouseEvent)e).ClientX, ((MouseEvent)e).ClientY)))); + + await _engine.ClickAsync(50 + 30, 30 + 20); + + Assert.That(clicks, Is.EqualTo(new[] { (30d, 20d) })); + } + + [Test] + public async Task HoveringAnElementInAnIframeMatchesHoverThere() + { + var (nested, _) = await LoadFrameAsync("
"); + + await _engine.MovePointerAsync(50 + 10, 30 + 10); + var frame = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(frame, 60, 40), Is.EqualTo(((byte)255, (byte)0, (byte)0))); + } + + [Test] + public async Task TypingGoesToTheFieldInAnIframe() + { + var (nested, _) = await LoadFrameAsync(""); + + await _engine.ClickAsync(50 + 20, 30 + 10); + await _engine.TextInputAsync("Hello"); + + var value = await _engine.InvokeAsync(_ => ((IHtmlInputElement)nested.GetElementById("i")!).Value); + Assert.That(value, Is.EqualTo("Hello")); + } + + [Test] + public async Task WheelScrollsTheIframeUnderThePointer() + { + var (nested, _) = await LoadFrameAsync("
"); + + await _engine.MovePointerAsync(50 + 10, 30 + 10); + await _engine.ScrollAsync(0, 100); + + var (inner, page) = await _engine.InvokeAsync(_ => (nested.DocumentElement!.GetScrollTop(), _engine.ScrollTop)); + Assert.That(inner, Is.EqualTo(100)); + Assert.That(page, Is.EqualTo(0)); + } + + [Test] + public async Task ScrollingAnIframePaintsItsDocumentMoved() + { + await LoadFrameAsync("
"); + + await _engine.MovePointerAsync(50 + 10, 30 + 10); + await _engine.ScrollAsync(0, 100); + var frame = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(frame, 60, 30 + 10), Is.EqualTo(((byte)255, (byte)0, (byte)0))); + Assert.That(PixelOf(frame, 60, 30 + 60), Is.EqualTo(((byte)0, (byte)0, (byte)255))); + } + + [Test] + public async Task WheelMovesThePageOnceTheIframeCannotScroll() + { + var srcdoc = "short"; + await _engine.LoadHtmlAsync($"
"); + await _engine.PrepareFrameAsync(); + + await _engine.MovePointerAsync(10, 10); + await _engine.ScrollAsync(0, 100); + + await Eventually(() => _engine.ScrollPosition > 0); + } + + [Test] + public async Task ChangingTheDocumentOfAnIframeRepaintsThePage() + { + var (nested, _) = await LoadFrameAsync("
"); + await _engine.PrepareFrameAsync(); + + await _engine.InvokeAsync(_ => nested.GetElementById("d")!.SetAttribute("style", "width:100px;height:100px;background:#0000ff")); + + // the observer of the document in the iframe tells the page when the event loop gets to it + var stopwatch = Stopwatch.StartNew(); + BrowserFrame frame; + + do + { + await Task.Delay(20); + frame = await _engine.PrepareFrameAsync(); + } + while (PixelOf(frame, 60, 40) != ((byte)0, (byte)0, (byte)255) && stopwatch.ElapsedMilliseconds < 5000); + + Assert.That(PixelOf(frame, 60, 40), Is.EqualTo(((byte)0, (byte)0, (byte)255))); + } + + [Test] + public async Task LinkInAnIframeNavigatesTheIframe() + { + var page = Path.Combine(_directory, "other.html"); + File.WriteAllText(page, "other

other

"); + var address = new Uri(page).AbsoluteUri; + var (_, frame) = await LoadFrameAsync($"go"); + var url = _engine.Url; + + await _engine.ClickAsync(50 + 10, 30 + 10); + await Eventually(() => frame.ContentDocument?.Title == "other"); + + Assert.That(_engine.Url, Is.EqualTo(url)); + } + + [Test] + public async Task AnIframeGoingAwayLeavesTheTasksOfThePage() + { + var page = Path.Combine(_directory, "other.html"); + File.WriteAllText(page, "other"); + var address = new Uri(page).AbsoluteUri; + var (_, frame) = await LoadFrameAsync($"go"); + await _engine.EvaluateScriptAsync("window.ran = 0; setTimeout(function () { window.ran = 1; }, 300);"); + + await _engine.ClickAsync(50 + 10, 30 + 10); + await Eventually(() => frame.ContentDocument?.Title == "other"); + await _engine.InvokeAsync(_ => frame.ContentDocument?.Dispose()); + + var stopwatch = Stopwatch.StartNew(); + var ran = "0"; + + while (ran != "1" && stopwatch.ElapsedMilliseconds < 5000) + { + await Task.Delay(50); + ran = Convert.ToString(await _engine.EvaluateScriptAsync("window.ran"), System.Globalization.CultureInfo.InvariantCulture) ?? "0"; + } + + Assert.That(ran, Is.EqualTo("1")); + } + + [Test] + public async Task LinkWithTargetTopInAnIframeNavigatesThePage() + { + var page = Path.Combine(_directory, "other.html"); + File.WriteAllText(page, "other

other

"); + var address = new Uri(page).AbsoluteUri; + await LoadFrameAsync($"go"); + + await _engine.ClickAsync(50 + 10, 30 + 10); + + await Eventually(() => _engine.Title == "other"); + } + + /// + /// Serves pages for two origins (the same server by its address and by its name). + /// + private sealed class TwoOriginSite : IDisposable + { + private readonly System.Net.Sockets.TcpListener _listener = new(System.Net.IPAddress.Loopback, 0); + private readonly Dictionary _pages = []; + + public TwoOriginSite() + { + _listener.Start(); + _ = AcceptAsync(); + } + + private int Port => ((System.Net.IPEndPoint)_listener.LocalEndpoint).Port; + + public string PageOrigin => $"http://127.0.0.1:{Port}"; + + public string OtherOrigin => $"http://localhost:{Port}"; + + public void Add(string path, string body, string headers = "") => _pages[path] = (headers, body); + + public void Dispose() => _listener.Stop(); + + private async Task AcceptAsync() + { + try + { + while (true) + { + var client = await _listener.AcceptTcpClientAsync(); + _ = ServeAsync(client); + } + } + catch (Exception ex) when (ex is ObjectDisposedException or System.Net.Sockets.SocketException) + { + } + } + + private async Task ServeAsync(System.Net.Sockets.TcpClient client) + { + using (client) + { + var stream = client.GetStream(); + using var reader = new StreamReader(stream, leaveOpen: true); + var path = (await reader.ReadLineAsync())?.Split(' ')[1] ?? "/"; + + while (!string.IsNullOrEmpty(await reader.ReadLineAsync())) + { + } + + var (headers, body) = _pages.GetValueOrDefault(path.Split('?')[0], (string.Empty, string.Empty)); + var content = System.Text.Encoding.UTF8.GetBytes(body); + var head = $"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {content.Length}\r\nConnection: close\r\n{headers}\r\n"; + await stream.WriteAsync(System.Text.Encoding.ASCII.GetBytes(head)); + await stream.WriteAsync(content); + } + } + } + + private async Task<(IDocument Page, IHtmlInlineFrameElement Frame)> LoadTwoOriginsAsync(TwoOriginSite site, string frameAttributes, string frameAddress) + { + site.Add("/page", $""); + await _engine.NavigateAsync(site.PageOrigin + "/page"); + IDocument page = null!; + IHtmlInlineFrameElement frame = null!; + await _engine.InvokeAsync(document => + { + page = document!; + frame = (IHtmlInlineFrameElement)document!.GetElementById("frame")!; + }); + return (page, frame); + } + + private async Task EvalInAsync(IDocument document, string script) => + await _engine.InvokeAsync(_ => Convert.ToString(document.ExecuteScript(script), System.Globalization.CultureInfo.InvariantCulture)); + + [Test] + public async Task PageCannotReachAFrameOfAnotherOrigin() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "secret"); + var (_, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + Assert.That(Convert.ToString(await _engine.EvaluateScriptAsync("String(document.getElementById('frame').contentDocument)")), Is.EqualTo("null")); + Assert.That(Convert.ToString(await _engine.EvaluateScriptAsync("typeof document.getElementById('frame').contentWindow.document")), Is.EqualTo("undefined")); + } + + [Test] + public async Task FrameOfAnotherOriginCannotReachThePage() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "frame"); + var (_, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + Assert.That(await EvalInAsync(frame.ContentDocument!, "typeof parent.document"), Is.EqualTo("undefined")); + Assert.That(await EvalInAsync(frame.ContentDocument!, "typeof top.document"), Is.EqualTo("undefined")); + } + + [Test] + public async Task FrameOfTheSameOriginCanReachThePage() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "frame"); + var (_, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.PageOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + Assert.That(await EvalInAsync(frame.ContentDocument!, "typeof parent.document"), Is.EqualTo("object")); + } + + [Test] + public async Task EngineStillSeesAndPaintsAFrameOfAnotherOrigin() + { + using var site = new TwoOriginSite(); + site.Add("/frame", ""); + var (_, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + var painted = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(painted, 100, 100), Is.EqualTo(((byte)255, (byte)0, (byte)0))); + } + + [Test] + public async Task MessageCrossesBetweenOrigins() + { + using var site = new TwoOriginSite(); + site.Add("/frame", ""); + var (page, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + await EvalInAsync(page, "window.addEventListener('message', function (e) { window.reply = e.data + '|' + e.origin; }); document.getElementById('frame').contentWindow.postMessage({ text: 'hi' }, '*')"); + + var stopwatch = Stopwatch.StartNew(); + string? reply = "undefined"; + + while (reply == "undefined" && stopwatch.ElapsedMilliseconds < 5000) + { + await Task.Delay(20); + reply = await EvalInAsync(page, "String(window.reply)"); + } + + Assert.That(reply, Is.EqualTo("back|" + site.OtherOrigin)); + Assert.That(await EvalInAsync(frame.ContentDocument!, "window.seen"), Is.EqualTo("hi|" + site.PageOrigin)); + } + + [TestCase("X-Frame-Options: DENY\r\n")] + [TestCase("X-Frame-Options: SAMEORIGIN\r\n")] + [TestCase("Content-Security-Policy: frame-ancestors 'none'\r\n")] + public async Task FrameRefusingToBeShownStaysEmpty(string headers) + { + using var site = new TwoOriginSite(); + site.Add("/frame", "", headers); + var (_, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Task.Delay(200); + + var painted = await _engine.PrepareFrameAsync(); + + Assert.That(frame.ContentDocument, Is.Null); + Assert.That(PixelOf(painted, 100, 100), Is.EqualTo(((byte)255, (byte)255, (byte)255))); + } + + [Test] + public async Task SandboxedFrameRunsNoScripts() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "start"); + var (_, frame) = await LoadTwoOriginsAsync(site, "sandbox", site.PageOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + var title = await _engine.InvokeAsync(_ => frame.ContentDocument!.Title); + + Assert.That(title, Is.EqualTo("start")); + } + + [Test] + public async Task SandboxedFrameOfTheSameAddressIsOfAnotherOrigin() + { + using var site = new TwoOriginSite(); + site.Add("/frame", ""); + var (_, frame) = await LoadTwoOriginsAsync(site, "sandbox=\"allow-scripts\"", site.PageOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + + Assert.That(await EvalInAsync(frame.ContentDocument!, "window.seen"), Is.EqualTo("undefined")); + } + + [Test] + public async Task SandboxedFrameCannotNavigateThePage() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "go"); + site.Add("/other", "other"); + var (page, frame) = await LoadTwoOriginsAsync(site, "sandbox", site.PageOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + await _engine.PrepareFrameAsync(); + + await _engine.ClickAsync(10, 10); + await Task.Delay(200); + + Assert.That(_engine.Title, Is.Not.EqualTo("other")); + Assert.That(_engine.Url, Is.EqualTo(site.PageOrigin + "/page")); + } + + [Test] + public async Task JavaScriptLinkInAFrameRunsInTheFrame() + { + using var site = new TwoOriginSite(); + site.Add("/frame", "go"); + var (page, frame) = await LoadTwoOriginsAsync(site, string.Empty, site.OtherOrigin + "/frame"); + await Eventually(() => frame.ContentDocument?.ReadyState == DocumentReadyState.Complete); + await _engine.PrepareFrameAsync(); + + await _engine.ClickAsync(10, 10); + + Assert.That(await EvalInAsync(frame.ContentDocument!, "String(window.ranHere)"), Is.EqualTo("1")); + Assert.That(await EvalInAsync(page, "String(window.ranHere)"), Is.EqualTo("undefined")); + } + [Test] public async Task PaintProducesImageOfViewportSize() { await _engine.LoadHtmlAsync("

Hello

World

"); - var image = await _engine.PaintAsync(); + var image = await _engine.PaintAsync(); + + Assert.That(image, Is.Not.Null); + Assert.That(image!.Width, Is.EqualTo(800)); + Assert.That(image.Height, Is.EqualTo(600)); + Assert.That(image.Data[1..4], Is.EqualTo("PNG"u8.ToArray())); + } + + [Test] + public async Task ResizeChangesPaintedSize() + { + await _engine.LoadHtmlAsync("

Resize me

"); + + _engine.Resize(320, 240); + var image = await _engine.PaintAsync(); + + Assert.That(image!.Width, Is.EqualTo(320)); + Assert.That(image.Height, Is.EqualTo(240)); + } + + [Test] + public async Task NavigatingFileUpdatesTitleAndHistory() + { + var first = WritePage("first.html", "Firstnext"); + var second = WritePage("second.html", "Second

done

"); + + await _engine.NavigateAsync(first); + Assert.That(_engine.Title, Is.EqualTo("First")); + Assert.That(_engine.CanGoBack, Is.False); + + await _engine.NavigateAsync(second); + Assert.That(_engine.Title, Is.EqualTo("Second")); + Assert.That(_engine.CanGoBack, Is.True); + + await _engine.GoBackAsync(); + Assert.That(_engine.Title, Is.EqualTo("First")); + Assert.That(_engine.CanGoForward, Is.True); + + await _engine.GoForwardAsync(); + Assert.That(_engine.Title, Is.EqualTo("Second")); + Assert.That(_engine.CanGoForward, Is.False); + } + + [Test] + public async Task ClickingLinkNavigates() + { + var first = WritePage("first.html", "First

Go to the second page

"); + WritePage("second.html", "Second

done

"); + await _engine.NavigateAsync(first); + + var (x, y) = CenterOf("link"); + await _engine.ClickAsync(x, y); + + Assert.That(_engine.Title, Is.EqualTo("Second")); + Assert.That(_engine.CanGoBack, Is.True); + } + + [Test] + public async Task HoveringLinkExposesAddressAndCursor() + { + var page = WritePage("first.html", "

Go to the second page

"); + await _engine.NavigateAsync(page); + + var (x, y) = CenterOf("link"); + await _engine.MovePointerAsync(x, y); + + Assert.That(_engine.HoveredLink, Does.EndWith("second.html")); + Assert.That(_engine.Cursor, Is.EqualTo(BrowserCursor.Pointer)); + + await _engine.MovePointerAsync(790, 590); + + Assert.That(_engine.HoveredLink, Is.Null); + } + + [Test] + public async Task ClickHandlersRunAndCanPreventNavigation() + { + await _engine.LoadHtmlAsync(""" + Click here please +

before

+ + """); + + var (x, y) = CenterOf("link"); + await _engine.ClickAsync(x, y); + + Assert.That(_engine.Document!.GetElementById("out")!.TextContent, Is.EqualTo("after")); + Assert.That(_engine.CanGoBack, Is.False); + } + + [Test] + public async Task TypingIntoFocusedInputUpdatesValueAndAttribute() + { + await _engine.LoadHtmlAsync(""); + var input = (IHtmlInputElement)_engine.Document!.GetElementById("name")!; + + var (x, y) = CenterOf("name"); + await _engine.ClickAsync(x, y); + await Eventually(() => input.IsFocused); + await _engine.TextInputAsync("Hello"); + await _engine.KeyDownAsync("Backspace"); + + Assert.That(input.Value, Is.EqualTo("Hell")); + Assert.That(input.GetAttribute("value"), Is.EqualTo("Hell")); + } + + [Test] + public async Task ClickingCheckboxLabelTogglesIt() + { + await _engine.LoadHtmlAsync(""); + var check = (IHtmlInputElement)_engine.Document!.GetElementById("check")!; + + var (x, y) = CenterOf("label"); + await _engine.ClickAsync(x, y); + + Assert.That(check.IsChecked, Is.True); + Assert.That(check.HasAttribute("checked"), Is.True); + } + + [Test] + public async Task ClickingTheTextOfAWrappingLabelTogglesItsControl() + { + await _engine.LoadHtmlAsync(""); + var check = (IHtmlInputElement)_engine.Document!.GetElementById("check")!; + + var (x, y) = CenterOf("text"); + await _engine.ClickAsync(x, y); + + Assert.That(check.IsChecked, Is.True); + } + + [Test] + public async Task ClickingALabelWithAForAttributeOnlyTogglesTheNamedControl() + { + await _engine.LoadHtmlAsync(""); + var check = (IHtmlInputElement)_engine.Document!.GetElementById("check")!; + + var (x, y) = CenterOf("text"); + await _engine.ClickAsync(x, y); + + Assert.That(check.IsChecked, Is.False); + } + + private const double ListBoxRowHeight = 17.8; + + private const string ListBoxStyle = ""; + + private async Task LoadListBoxAsync(string markup) + { + await _engine.LoadHtmlAsync(ListBoxStyle + markup); + await _engine.PrepareFrameAsync(); + return (IHtmlSelectElement)_engine.Document!.QuerySelector("select")!; + } + + private Task ClickListBoxRowAsync(int row, InputModifiers modifiers = InputModifiers.None) => + _engine.ClickAsync(10, ((row + 0.5) * ListBoxRowHeight), modifiers); + + private static string[] SelectedTexts(IHtmlSelectElement select) => + select.SelectedOptions.Select(option => option.Text).ToArray(); + + [Test] + public async Task ClickingAListBoxRowSelectsOnlyIt() + { + var select = await LoadListBoxAsync(""); + + await ClickListBoxRowAsync(0); + await ClickListBoxRowAsync(2); + + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "c" })); + Assert.That(_engine.Document!.ActiveElement, Is.EqualTo(select)); + } + + [Test] + public async Task ControlClickingAListBoxRowTogglesIt() + { + var select = await LoadListBoxAsync(""); + + await ClickListBoxRowAsync(0); + await ClickListBoxRowAsync(2, InputModifiers.Control); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "a", "c" })); + + await ClickListBoxRowAsync(0, InputModifiers.Meta); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "c" })); + } + + [Test] + public async Task ShiftClickingAListBoxRowSelectsFromTheAnchor() + { + var select = await LoadListBoxAsync(""); + + await ClickListBoxRowAsync(1); + await ClickListBoxRowAsync(3, InputModifiers.Shift); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "b", "c", "d" })); + + // the anchor stays, so the range is replaced + await ClickListBoxRowAsync(0, InputModifiers.Shift); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "a", "b" })); + } + + [Test] + public async Task ModifiersDoNotExtendASingleSelectionListBox() + { + var select = await LoadListBoxAsync(""); + + await ClickListBoxRowAsync(0); + await ClickListBoxRowAsync(2, InputModifiers.Control); + + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "c" })); + } + + [Test] + public async Task ListBoxClicksFireEventsInTheOrderOfBrowsers() + { + var select = await LoadListBoxAsync(""); + var events = new List(); + + foreach (var name in new[] { "mousedown", "input", "change", "click" }) + { + select.AddEventListener(name, (_, ev) => events.Add($"{name}@{((IElement)ev.OriginalTarget!).LocalName}")); + } + + await ClickListBoxRowAsync(0); + Assert.That(events, Is.EqualTo(new[] { "mousedown@option", "input@select", "change@select", "click@option" })); + + // nothing changes, so nothing is told + events.Clear(); + await ClickListBoxRowAsync(0); + Assert.That(events, Is.EqualTo(new[] { "mousedown@option", "click@option" })); + } + + [Test] + public async Task DisabledListBoxRowsCannotBeSelected() + { + var select = await LoadListBoxAsync(""); + var events = new List(); + select.AddEventListener("click", (_, ev) => events.Add(((IElement)ev.OriginalTarget!).LocalName)); + + // the label of a group takes clicks, but selects nothing + await ClickListBoxRowAsync(0); + Assert.That(events, Is.EqualTo(new[] { "optgroup" })); + Assert.That(SelectedTexts(select), Is.Empty); + + events.Clear(); + await ClickListBoxRowAsync(2); + await ClickListBoxRowAsync(4); + Assert.That(events, Is.Empty); + Assert.That(SelectedTexts(select), Is.Empty); + + await ClickListBoxRowAsync(1); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "a" })); + } + + [Test] + public async Task ADisabledListBoxTakesNoClicks() + { + var select = await LoadListBoxAsync(""); + + await ClickListBoxRowAsync(0); + + Assert.That(SelectedTexts(select), Is.Empty); + Assert.That(_engine.Document!.ActiveElement, Is.Not.EqualTo(select)); + } + + [Test] + public async Task PressingADisabledListBoxMovesTheFocusAway() + { + await _engine.LoadHtmlAsync(ListBoxStyle + ""); + await _engine.PrepareFrameAsync(); + var field = (IHtmlInputElement)_engine.Document!.GetElementById("field")!; + var (x, y) = CenterOf("field"); + await _engine.ClickAsync(x, y); + Assert.That(field.IsFocused, Is.True); + + await ClickListBoxRowAsync(0); + + Assert.That(field.IsFocused, Is.False); + } + + [Test] + public async Task ScrollingAListBoxPaintsTheRowsFurtherDown() + { + await LoadListBoxAsync(""); + var select = _engine.Document!.QuerySelector("select")!; + select.SetScrollTop(0); + var top = await _engine.PrepareFrameAsync(); + var unselected = PixelOf(top!, 90, 4); + + select.SetScrollTop(2 * ListBoxRowHeight); + var scrolled = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(scrolled!, 90, 4), Is.Not.EqualTo(unselected)); + } + + [Test] + public async Task AListBoxScrollsToItsSelectedOption() + { + var select = await LoadListBoxAsync(""); + + Assert.That(select.GetScrollTop(), Is.EqualTo(2 * ListBoxRowHeight).Within(0.5)); + + // what is shown first now is "c" + await ClickListBoxRowAsync(0); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "c" })); + } + + [Test] + public async Task ArrowKeysMoveAndExtendAListBoxSelection() + { + var select = await LoadListBoxAsync(""); + var changes = 0; + select.AddEventListener("change", (_, _) => changes++); + + await ClickListBoxRowAsync(0); + changes = 0; + await _engine.KeyDownAsync("ArrowDown"); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "b" })); + Assert.That(changes, Is.EqualTo(1)); + + // past the disabled one, and scrolled to it + await _engine.KeyDownAsync("ArrowDown"); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "d" })); + Assert.That(select.GetScrollTop(), Is.EqualTo(2 * ListBoxRowHeight).Within(0.5)); + + await _engine.KeyDownAsync("ArrowUp", InputModifiers.Shift); + Assert.That(SelectedTexts(select), Is.EqualTo(new[] { "b", "d" })); + } + + [Test] + public async Task TheWheelScrollsAListBoxBeforeThePage() + { + await _engine.LoadHtmlAsync(ListBoxStyle + "
"); + await _engine.PrepareFrameAsync(); + var select = (IHtmlSelectElement)_engine.Document!.QuerySelector("select")!; + await _engine.MovePointerAsync(10, 5); + await _engine.PrepareFrameAsync(); + + await _engine.ScrollAsync(0, ListBoxRowHeight); + + Assert.That(select.GetScrollTop(), Is.EqualTo(ListBoxRowHeight).Within(1)); + Assert.That(_engine.ScrollTop, Is.EqualTo(0)); + + // nothing left to scroll: the page takes it + await _engine.ScrollAsync(0, 500); + await _engine.ScrollAsync(0, 40); + Assert.That(_engine.ScrollTop, Is.GreaterThan(0)); + } + + [Test] + public async Task ASelectedListBoxRowIsPainted() + { + await LoadListBoxAsync(""); + var before = await _engine.PrepareFrameAsync(); + const int X = 90; + const int Y = 4; + var unselected = PixelOf(before!, X, Y); + + await ClickListBoxRowAsync(0); + var after = await _engine.PrepareFrameAsync(); + + Assert.That(PixelOf(after!, X, Y), Is.Not.EqualTo(unselected)); + } + + private static (byte R, byte G, byte B) PixelOf(BrowserFrame frame, int x, int y) + { + var pixels = frame.Rasterize(); + var offset = (((y + frame.OverscanTop) * pixels.Width) + x) * 4; + return (pixels.Data[offset], pixels.Data[offset + 1], pixels.Data[offset + 2]); + } + + private const string DropdownStyle = ""; + + private async Task LoadDropdownAsync(string markup) + { + await _engine.LoadHtmlAsync(DropdownStyle + markup); + await _engine.PrepareFrameAsync(); + return (IHtmlSelectElement)_engine.Document!.QuerySelector("select")!; + } + + private async Task PressKeyAsync(string key, InputModifiers modifiers = InputModifiers.None) + { + await _engine.KeyDownAsync(key, modifiers); + await _engine.KeyUpAsync(key, modifiers); + } + + [Test] + public async Task ClickingADropdownOpensItAndAgainClosesIt() + { + var select = await LoadDropdownAsync(""); + + await _engine.ClickAsync(10, 10); + Assert.That(_engine.IsSelectPopupOpen, Is.True); + Assert.That(_engine.Document!.ActiveElement, Is.EqualTo(select)); + + await _engine.ClickAsync(10, 10); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + } + + [Test] + public async Task ClickingARowOfADropdownSelectsIt() + { + var select = await LoadDropdownAsync(""); + var events = new List(); + + foreach (var name in new[] { "input", "change", "click" }) + { + select.AddEventListener(name, (_, _) => events.Add(name)); + } + + await _engine.ClickAsync(10, 10); + events.Clear(); + // the rows follow below the select: the third one + await _engine.ClickAsync(10, 20 + 1 + (2.5 * PopupRowHeight)); + + Assert.That(_engine.IsSelectPopupOpen, Is.False); + Assert.That(select.Value, Is.EqualTo("c")); + Assert.That(events, Is.EqualTo(new[] { "input", "change", "click" })); + } + + // of the default font of a select: 1.2em of 13.33px and the padding of the row + private const double PopupRowHeight = 19; + + [Test] + public async Task DisabledRowsAndGroupsOfADropdownLeaveItOpen() + { + var select = await LoadDropdownAsync(""); + + await _engine.ClickAsync(10, 10); + await _engine.ClickAsync(10, 20 + 1 + (1.5 * PopupRowHeight)); + Assert.That(_engine.IsSelectPopupOpen, Is.True); + await _engine.ClickAsync(10, 20 + 1 + (2.5 * PopupRowHeight)); + Assert.That(_engine.IsSelectPopupOpen, Is.True); + Assert.That(select.Value, Is.EqualTo("a")); + + await _engine.ClickAsync(10, 20 + 1 + (3.5 * PopupRowHeight)); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + Assert.That(select.Value, Is.EqualTo("c")); + } + + [Test] + public async Task ClickingOutsideOfADropdownClosesItAndReachesThePage() + { + await LoadDropdownAsync("
"); + var clicks = 0; + _engine.Document!.GetElementById("out")!.AddEventListener("click", (_, _) => clicks++); + + await _engine.ClickAsync(10, 10); + await _engine.ClickAsync(10, 220); + + Assert.That(_engine.IsSelectPopupOpen, Is.False); + Assert.That(clicks, Is.EqualTo(1)); + Assert.That(_engine.Document!.ActiveElement, Is.Not.InstanceOf()); + } + + [Test] + public async Task ADisabledDropdownOrOnePreventedDoesNotOpen() + { + var select = await LoadDropdownAsync(""); + await _engine.ClickAsync(10, 10); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + + select = await LoadDropdownAsync(""); + select.AddEventListener("mousedown", (_, ev) => ev.Cancel()); + await _engine.ClickAsync(10, 10); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + + await LoadDropdownAsync(""); + await _engine.ClickAsync(10, 10); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + } + + [Test] + public async Task KeysMoveTheHighlightOfAnOpenDropdownUntilCommitted() + { + var select = await LoadDropdownAsync(""); + var changes = 0; + select.AddEventListener("change", (_, _) => changes++); + + await _engine.ClickAsync(10, 10); + await PressKeyAsync("ArrowDown"); + await PressKeyAsync("ArrowDown"); + Assert.That(select.Value, Is.EqualTo("a3")); + Assert.That(changes, Is.EqualTo(0)); + + // skipped what is disabled: on a4 now, then back + await PressKeyAsync("ArrowUp"); + await PressKeyAsync("Enter"); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + Assert.That(select.Value, Is.EqualTo("g0")); + Assert.That(changes, Is.EqualTo(1)); + + // Escape takes the moved highlight back + await PressKeyAsync("Enter"); + Assert.That(_engine.IsSelectPopupOpen, Is.True); + await PressKeyAsync("ArrowDown"); + await PressKeyAsync("Escape"); + Assert.That(_engine.IsSelectPopupOpen, Is.False); + Assert.That(select.Value, Is.EqualTo("g0")); + + // Tab commits and stays + await PressKeyAsync(" "); + await PressKeyAsync("End"); + await PressKeyAsync("Tab"); + Assert.That(select.Value, Is.EqualTo("a4")); + Assert.That(_engine.Document!.ActiveElement, Is.EqualTo(select)); + + await PressKeyAsync("ArrowDown", InputModifiers.Alt); + Assert.That(_engine.IsSelectPopupOpen, Is.True); + await PressKeyAsync("Escape"); + } + + [Test] + public async Task KeysChangeAClosedDropdownRightAway() + { + var select = await LoadDropdownAsync(""); + var changes = 0; + select.AddEventListener("change", (_, _) => changes++); + await _engine.ClickAsync(10, 10); + await PressKeyAsync("Escape"); + + await PressKeyAsync("ArrowDown"); + Assert.That(select.Value, Is.EqualTo("g0")); + await PressKeyAsync("ArrowDown"); + Assert.That(select.Value, Is.EqualTo("a4")); + await PressKeyAsync("ArrowUp"); + Assert.That(select.Value, Is.EqualTo("g0")); + await PressKeyAsync("End"); + Assert.That(select.Value, Is.EqualTo("a4")); + await PressKeyAsync("Home"); + Assert.That(select.Value, Is.EqualTo("a0")); + await PressKeyAsync("PageDown"); + Assert.That(select.Value, Is.EqualTo("a3")); + Assert.That(changes, Is.EqualTo(6)); + + // nowhere to go, nothing to tell + changes = 0; + await PressKeyAsync("End"); + await PressKeyAsync("ArrowDown"); + Assert.That(changes, Is.EqualTo(1)); + } + + [Test] + public async Task TypingFindsTheOptionsOfADropdown() + { + var select = await LoadDropdownAsync(""); + await _engine.ClickAsync(10, 10); + await PressKeyAsync("Escape"); + + // goes through the ones starting with a repeated letter + await PressKeyAsync("a"); + Assert.That(select.Value, Is.EqualTo("avocado")); + await PressKeyAsync("a"); + Assert.That(select.Value, Is.EqualTo("apricot")); + await Task.Delay(1100); + + // further letters narrow it down; nothing matching changes nothing + await PressKeyAsync("c"); + Assert.That(select.Value, Is.EqualTo("cherry")); + await PressKeyAsync("h"); + Assert.That(select.Value, Is.EqualTo("cherry")); + await PressKeyAsync("x"); + Assert.That(select.Value, Is.EqualTo("cherry")); + await Task.Delay(1100); + + // a disabled one isn't found + await PressKeyAsync("b"); + Assert.That(select.Value, Is.EqualTo("cherry")); + } + + [Test] + public async Task TheKeysOfAnOpenDropdownAreNotForThePage() + { + var select = await LoadDropdownAsync(""); + var keys = new List(); + select.AddEventListener("keydown", (_, _) => keys.Add("keydown")); + + await _engine.ClickAsync(10, 10); + await _engine.KeyDownAsync("ArrowDown"); + + Assert.That(keys, Is.Empty); + } + + [Test] + public async Task HoveringADropdownRowHighlightsIt() + { + var select = await LoadDropdownAsync(""); + await _engine.ClickAsync(10, 10); + await _engine.PrepareFrameAsync(); + + await _engine.MovePointerAsync(10, 20 + 1 + (2.5 * PopupRowHeight)); + await PressKeyAsync("Enter"); - Assert.That(image, Is.Not.Null); - Assert.That(image!.Width, Is.EqualTo(800)); - Assert.That(image.Height, Is.EqualTo(600)); - Assert.That(image.Data[1..4], Is.EqualTo("PNG"u8.ToArray())); + Assert.That(select.Value, Is.EqualTo("c")); } [Test] - public async Task ResizeChangesPaintedSize() + public async Task AnOpenDropdownIsPaintedOnTopOfThePage() { - await _engine.LoadHtmlAsync("

Resize me

"); + await LoadDropdownAsync("
"); + const int X = 95; + const int Y = 30; + var closed = PixelOf((await _engine.PrepareFrameAsync())!, X, Y); + Assert.That(closed, Is.EqualTo(((byte)255, (byte)0, (byte)0))); + + await _engine.ClickAsync(10, 10); + var open = PixelOf((await _engine.PrepareFrameAsync())!, X, Y); + Assert.That(open, Is.Not.EqualTo(closed)); + + await PressKeyAsync("Escape"); + Assert.That(PixelOf((await _engine.PrepareFrameAsync())!, X, Y), Is.EqualTo(closed)); + } - _engine.Resize(320, 240); - var image = await _engine.PaintAsync(); + [TestCase("", null, 255)] + [TestCase("", "dark", 255)] + [TestCase("", "light", 59)] + [TestCase("", "dark", 59)] + [TestCase("", "light", 255)] + [TestCase("", "light", 59)] + [TestCase("", "dark", 255)] + public async Task AnOpenDropdownUsesTheColorSchemeOfThePage(string head, string? preference, int expected) + { + _engine.ColorScheme = preference; + await LoadDropdownAsync(head + ""); - Assert.That(image!.Width, Is.EqualTo(320)); - Assert.That(image.Height, Is.EqualTo(240)); + await _engine.ClickAsync(10, 10); + // the end of the first row, which isn't the highlighted one + var frame = await _engine.PrepareFrameAsync(); + await PressKeyAsync("ArrowDown"); + frame = await _engine.PrepareFrameAsync(); + + Assert.That((int)PixelOf(frame!, 90, 20 + 1 + 5).R, Is.EqualTo(expected)); + _engine.ColorScheme = null; } [Test] - public async Task NavigatingFileUpdatesTitleAndHistory() + public async Task ControlsDoNotTakeTheTextColorOfThePage() { - var first = WritePage("first.html", "Firstnext"); - var second = WritePage("second.html", "Second

done

"); + var select = await LoadDropdownAsync(""); - await _engine.NavigateAsync(first); - Assert.That(_engine.Title, Is.EqualTo("First")); - Assert.That(_engine.CanGoBack, Is.False); + Assert.That(select.ComputeCurrentStyle().GetPropertyValue("color"), Is.EqualTo("rgba(0, 0, 0, 1)")); + } - await _engine.NavigateAsync(second); - Assert.That(_engine.Title, Is.EqualTo("Second")); - Assert.That(_engine.CanGoBack, Is.True); + [Test] + public async Task ClicksHitAnAbsoluteBoxWhereItIsPlacedAgainstTheViewport() + { + await _engine.LoadHtmlAsync("
"); + await _engine.PrepareFrameAsync(); + var clicks = 0; + _engine.Document!.GetElementById("b")!.AddEventListener("click", (_, _) => clicks++); - await _engine.GoBackAsync(); - Assert.That(_engine.Title, Is.EqualTo("First")); - Assert.That(_engine.CanGoForward, Is.True); + await _engine.ClickAsync(10, 10); - await _engine.GoForwardAsync(); - Assert.That(_engine.Title, Is.EqualTo("Second")); - Assert.That(_engine.CanGoForward, Is.False); + Assert.That(clicks, Is.EqualTo(1)); } [Test] - public async Task ClickingLinkNavigates() + public async Task ClicksReachWhatEscapesAClipAndWhatIsInLaterColumns() { - var first = WritePage("first.html", "First

Go to the second page

"); - WritePage("second.html", "Second

done

"); - await _engine.NavigateAsync(first); + await _engine.LoadHtmlAsync(""" + +
+
+
+ +
+ + """); + await _engine.PrepareFrameAsync(); + var clicks = new List(); + _engine.Document!.GetElementById("escaped")!.AddEventListener("click", (_, _) => clicks.Add("escaped")); + _engine.Document!.GetElementById("column")!.AddEventListener("click", (_, _) => clicks.Add("column")); - var (x, y) = CenterOf("link"); - await _engine.ClickAsync(x, y); + await _engine.ClickAsync(210, 10); + var column = _engine.Document!.GetElementById("column")!.GetBoundingClientRect(); + Assert.That(column.Left, Is.EqualTo(210)); + await _engine.ClickAsync(column.Left + 10, column.Top + 10); - Assert.That(_engine.Title, Is.EqualTo("Second")); - Assert.That(_engine.CanGoBack, Is.True); + Assert.That(clicks, Is.EqualTo(new[] { "escaped", "column" })); } [Test] - public async Task HoveringLinkExposesAddressAndCursor() + public async Task ClientRectsCanBeIndexedByScripts() { - var page = WritePage("first.html", "

Go to the second page

"); - await _engine.NavigateAsync(page); + await _engine.LoadHtmlAsync("
"); - var (x, y) = CenterOf("link"); - await _engine.MovePointerAsync(x, y); + var width = await _engine.EvaluateScriptAsync("document.getElementById('box').getClientRects()[0].width"); - Assert.That(_engine.HoveredLink, Does.EndWith("second.html")); - Assert.That(_engine.Cursor, Is.EqualTo(BrowserCursor.Pointer)); + Assert.That(Convert.ToDouble(width), Is.EqualTo(30)); + } - await _engine.MovePointerAsync(790, 590); + [Test] + public async Task OpeningADropdownAsksForAFrame() + { + await LoadDropdownAsync(""); + await _engine.PrepareFrameAsync(); + var invalidated = 0; + _engine.Invalidated += (_, _) => invalidated++; - Assert.That(_engine.HoveredLink, Is.Null); + await _engine.ClickAsync(10, 10); + + Assert.That(invalidated, Is.GreaterThan(0)); + Assert.That((await _engine.PrepareFrameAsync())!.Width, Is.GreaterThan(0)); } [Test] - public async Task ClickHandlersRunAndCanPreventNavigation() + public async Task ThePopupOfADropdownOpensAboveWithoutRoomBelow() { - await _engine.LoadHtmlAsync(""" - Click here please -

before

- - """); + await _engine.LoadHtmlAsync(DropdownStyle + "
"); + await _engine.PrepareFrameAsync(); + var select = (IHtmlSelectElement)_engine.Document!.QuerySelector("select")!; + var top = select.GetBoundingClientRect().Top; - var (x, y) = CenterOf("link"); - await _engine.ClickAsync(x, y); + await _engine.ClickAsync(10, top + 5); + // the first row is above the select now + await _engine.ClickAsync(10, top - (3.5 * PopupRowHeight) - 1); - Assert.That(_engine.Document!.GetElementById("out")!.TextContent, Is.EqualTo("after")); - Assert.That(_engine.CanGoBack, Is.False); + Assert.That(select.Value, Is.EqualTo("a")); } [Test] - public async Task TypingIntoFocusedInputUpdatesValueAndAttribute() + public async Task ScrollingThePageClosesADropdownButTheWheelOnItsListDoesNot() { - await _engine.LoadHtmlAsync(""); - var input = (IHtmlInputElement)_engine.Document!.GetElementById("name")!; + var options = string.Concat(Enumerable.Range(0, 40).Select(i => $"