Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / webview / tests_webview_pages.lucb

#==============================================================================================
#
#   tests_webview_pages - pages real sites are made of: media, fragment links
#
#   DESCRIPTION:
#       A page with a <video> and an <audio> finishes loading and paints the video's poster:
#       without demuxers (web's LibMedia seam) each media resource is an unsupported format,
#       which fails the element's resource selection (a src: MEDIA_ERR_SRC_NOT_SUPPORTED; the
#       last <source>: waiting for another), shows the poster and stops delaying the load event. A click on a fragment link navigates within
#       the document (Navigation's same-document update) and scrolls to the fragment.
#
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## The media element with the id `id` in the view's document.
func test_webview_media_element(view: WebView*, id: str) -> web.HtmlMediaElement*:
    let traversable = web.page_top_level_traversable(webview_page(view))
    let document = web.html_navigable_active_document((web.HtmlNavigable*)traversable) else trap("webview tests: no document")
    let element = web.dom_element_by_id_map_get(web.dom_document_element_by_id(document), ak.fly_string_from_str(id), (web.DomNode*)document) else trap(f"webview tests: no #{id}")
    return web.as_html_media_element(element)

test "a page with video and audio loads, its media fail as unsupported formats, the poster is painted":
    test_webview_platform()
    let allocator: memory.Allocator = webview_heap()
    with allocator:
        let view = test_webview_open()
        defer webview_destroy(view)
        var log = TestWebViewLog()
        webview_load(view, test_webview_file_url("media.html"))
        test_webview_wait_for_load(view, &log, "media.html")
        # A src attribute that fails: the dedicated media source failure steps.
        let video = test_webview_media_element(view, "video")
        let error_ = web.html_media_element_error(video) else trap("webview tests: the video has no error")
        assert(web.html_media_error_code(error_) == web.HtmlMediaErrorCode.src_not_supported, "the video's resource is an unsupported format")
        assert(web.html_media_element_network_state(video) == web.HtmlMediaElementNetworkState.no_source, "the video has no source left")
        # <source> children that all fail: the element waits for another (no error, no source).
        let audio = test_webview_media_element(view, "audio")
        assert(web.html_media_element_error(audio) == none, "a failed <source> sets no error")
        assert(web.html_media_element_network_state(audio) == web.HtmlMediaElementNetworkState.no_source, "the audio has no source left")
        # The poster (Assets/120.png, dark blue) fills the video's box.
        assert(test_webview_pixel(view, 60, 60) == 0x000056ff, "the poster is painted")

test "a click on a fragment link navigates within the document and scrolls to the fragment":
    test_webview_platform()
    let allocator: memory.Allocator = webview_heap()
    with allocator:
        let view = test_webview_open()
        defer webview_destroy(view)
        var log = TestWebViewLog()
        webview_load(view, test_webview_file_url("fragment.html"))
        test_webview_wait_for_load(view, &log, "fragment.html")
        assert(test_webview_pixel(view, 150, 30) == 0x0000ffff, "the link is at the top")

        # The press and the release come in turns of their own, as a user's do: the press focuses the
        # link, whose focus scroll (a queued task) must run before the release activates it, or it
        # would scroll back to the link after the fragment scroll. Syncing the rendering has the
        # press handled; the event loop then runs out of tasks, the focus scroll among them.
        let primary: u32 = 1
        webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_move, 20.0, 10.0, 0, 0, 0, 0, 0, 0)))
        webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_down, 20.0, 10.0, primary, primary, 0, 0, 0, 1)))
        test_webview_sync_rendering(view, &log)
        test_webview_wait_for_idle(view, &log)
        webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_up, 20.0, 10.0, primary, 0, 0, 0, 0, 1)))
        let start = time.now()
        while not test_webview_url_ends_with(view, "/tests/webview/fragment.html#below"):
            test_webview_turn()
            test_webview_take_events(view, &log)
            if time.now() - start > test_webview_timeout:
                trap("webview tests: the fragment link did not navigate")
        # The scroll to the fragment follows the URL change; sync the rendering until it shows.
        let scrolled = time.now()
        test_webview_sync_rendering(view, &log)
        while test_webview_pixel(view, 400, 300) != 0x008000ff:
            if time.now() - scrolled > test_webview_timeout:
                trap("webview tests: the view did not scroll to the fragment")
            test_webview_sync_rendering(view, &log)