dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / webview / tests_webview_support.lucb
#==============================================================================================
#
# tests_webview_support - what the module's tests share: the platform, a view, a pump
#
# DESCRIPTION:
# The tests run from the repository's root (as ./test.sh does): the platform is set up
# once per process with the engine's resources (written into build/webview_res) and the
# test fonts of tests/libweb/fonts in layout-test mode, so pages render alike on every
# machine. A view's pages are the files of tests/webview.
#
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## How long a test waits for the page before it fails, in nanoseconds.
let test_webview_timeout: u64 = 20000000000
## The platform for the tests (once per process).
func test_webview_platform():
webview_initialize("build/webview_res", "tests/libweb/fonts") else trap("webview tests: the platform did not initialize")
## A view of 800x600 device pixels at device pixel ratio 1.
func test_webview_open() -> WebView*:
test_webview_platform()
return webview_create(gfx.IntSize(m_width = 800, m_height = 600), 1.0)
## The file: URL of tests/webview/NAME under the working directory.
func test_webview_file_url(name: str) -> web_url.Url:
let directory = os.cwd() catch failure: trap("webview tests: no working directory")
var builder = ak.string_builder_new()
ak.string_builder_append(&builder, directory)
ak.string_builder_append(&builder, "/tests/webview/")
ak.string_builder_append(&builder, name)
let path = ak.byte_string_from_string_view(ak.string_builder_string_view(&builder))
return web_url.create_with_file_scheme(path, ak.byte_string_empty(), ak.byte_string_empty()) else trap("webview tests: not a file URL")
## The events a view reported while a test pumped it.
struct TestWebViewLog:
var load_started: i32
var load_finished: i32
var title_changed: i32
var url_changed: i32
var frames: i32
var navigation_buttons: i32
var cursor_changed: i32
var link_hovered: i32
## The page whose load a wait is for (a file of tests/webview, "" for none), and whether its
## load_finished was taken.
var awaited_page: str = ""
var awaited_page_loaded: bool = false
## Takes the view's events into `log`.
func test_webview_take_events(view: WebView*, log: TestWebViewLog*):
while true:
match webview_take_event(view):
.none_: return
.load_started: log.load_started += 1
.load_finished:
log.load_finished += 1
if log.awaited_page.length > 0:
let url = webview_finished_load_url(view) else trap("webview tests: a load_finished without its URL")
if test_webview_url_is_page(url, log.awaited_page):
log.awaited_page_loaded = true
.title_changed: log.title_changed += 1
.url_changed: log.url_changed += 1
.frame_ready: log.frames += 1
.navigation_buttons_changed: log.navigation_buttons += 1
.cursor_changed: log.cursor_changed += 1
.link_hovered: log.link_hovered += 1
.link_unhovered: _ = 0
_: _ = 0
## One turn of an application's loop: the engine's due work, then a sleep until it next has
## work (at most 5 ms).
func test_webview_turn():
_ = webview_event_loop_process(webview_event_loop())
if let due = webview_event_loop_next_due(webview_event_loop()):
let now = time.now()
if due > now:
let wait = (due - now + 999999) // 1000000
thread.sleep(wait if wait < 5 else 5)
else:
thread.sleep(5)
## Pumps until the view reports that the load of tests/webview/PAGE finished, then until it
## presents the loaded page. As Ladybird's test-web does, the load is told by its URL (a new
## view first reports the load of its initial about:blank document, often in the same turn as
## the page's), and the rendering is synchronized with a screenshot: queueing one marks
## the page for repaint and requests a frame, and the rendering update that answers it presents
## that frame. A frame presented around the load itself proves nothing: the engine paints when
## the page changed, so the load can finish after the page's last frame, with none to follow.
func test_webview_wait_for_load(view: WebView*, log: TestWebViewLog*, page: str):
let start = time.now()
log.awaited_page = page
log.awaited_page_loaded = false
while not log.awaited_page_loaded:
test_webview_turn()
test_webview_take_events(view, log)
if time.now() - start > test_webview_timeout:
trap(f"webview tests: {page} did not finish loading ({log.load_finished} loads)")
log.awaited_page = ""
test_webview_sync_rendering(view, log)
## Pumps until the view presents its current rendering, synchronized with a screenshot as
## test-web does: queueing one marks the page for repaint and requests a frame, and the rendering
## update that answers it presents that frame. The engine paints only when the page changed, so a
## test can't wait for "the next frame" after a change that may already have been painted.
func test_webview_sync_rendering(view: WebView*, log: TestWebViewLog*):
let start = time.now()
let frames = log.frames
webview_take_screenshot(view)
while webview_screenshot(view) == none or log.frames == frames:
test_webview_turn()
test_webview_take_events(view, log)
if time.now() - start > test_webview_timeout:
trap(f"webview tests: the rendering was not presented ({log.frames - frames} frames since)")
## Pumps until the event loop has no runnable task (the start of an idle period, step 5 of its
## processing model): every task queued so far has run, such as the focus scroll a handled press
## queued. Syncing the rendering doesn't wait for those: the event loop queues an update of the
## rendering per visible page (queue_task_to_update_the_rendering; a test may hold other views
## than the one it drives) and the first one presents the frame, while the others stay queued
## ahead of what that update queued, and the next of them handles the next input.
func test_webview_wait_for_idle(view: WebView*, log: TestWebViewLog*):
let start = time.now()
let task_queue = web.html_event_loop_task_queue(web.html_main_thread_event_loop())
while web.html_task_queue_has_runnable_tasks(task_queue):
test_webview_turn()
test_webview_take_events(view, log)
if time.now() - start > test_webview_timeout:
trap("webview tests: the event loop did not run out of tasks")
## Pumps until a new frame is presented.
func test_webview_wait_for_frame(view: WebView*, log: TestWebViewLog*):
let start = time.now()
let frames = log.frames
while log.frames == frames:
test_webview_turn()
test_webview_take_events(view, log)
if time.now() - start > test_webview_timeout:
trap("webview tests: no new frame")
## Pumps for `milliseconds` (lets queued input and rendering settle).
func test_webview_settle(view: WebView*, log: TestWebViewLog*, milliseconds: u64):
let end = time.now() + milliseconds * 1000000
while time.now() < end:
test_webview_turn()
test_webview_take_events(view, log)
## The view's title as text.
func test_webview_title(view: WebView*) -> ak.String:
let title = webview_title(view)
return ak.utf16_string_base_to_utf8(&title.utf16_string_base)
## The view's URL, serialized.
func test_webview_url_text(view: WebView*) -> ak.String:
let url = webview_url(view)
return web_url.url_serialize(&url)
## Whether `url` is the file: URL of tests/webview/PAGE.
func test_webview_url_is_page(url: web_url.Url, page: str) -> bool:
let text = web_url.url_serialize(&url)
let expected = test_webview_file_url(page)
let expected_text = web_url.url_serialize(&expected)
return ak.string_eq_string_view(&text, ak.string_bytes_as_string_view(&expected_text))
## Whether the view's URL ends with `ending`.
func test_webview_url_ends_with(view: WebView*, ending: str) -> bool:
let text = test_webview_url_text(view)
return ak.string_ends_with_bytes(&text, ak.sv(ending))
## The RGBA of the presented frame at `x`, `y` (device pixels), as 0xRRGGBBAA.
func test_webview_pixel(view: WebView*, x: i32, y: i32) -> u32:
let size = webview_frame_size(view)
let pixels = new u8[(usize)size.m_width * (usize)size.m_height * 4] --- in memory.heap catch failure: trap("out of memory")
defer free(pixels) in memory.heap
assert(webview_copy_frame(view, pixels, size.m_width, size.m_height), "the view has a frame")
let at = ((usize)y * (usize)size.m_width + (usize)x) * 4
return ((u32)pixels[at] << 24) | ((u32)pixels[at + 1] << 16) | ((u32)pixels[at + 2] << 8) | (u32)pixels[at + 3]
## A click of the primary button at `x`, `y` (points).
func test_webview_click(view: WebView*, x: f64, y: f64):
let primary: u32 = 1
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_move, x, y, 0, 0, 0, 0, 0, 0)))
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_down, x, y, primary, primary, 0, 0, 0, 1)))
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_up, x, y, primary, 0, 0, 0, 0, 1)))