dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / webview / tests_webview_input.lucb
#==============================================================================================
#
# tests_webview_input - input from the application: links, history, scrolling, selection
#
# DESCRIPTION:
# A click on a link navigates through DOM event dispatch and the anchor's activation
# behavior, and the history goes back and forward; a wheel scrolls the viewport; a drag
# selects text, which the view can copy.
#
# Copyright (c) 2026 the luce-browser authors.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
test "a click on a link navigates, and the history goes back and forward":
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("first.html"))
test_webview_wait_for_load(view, &log, "first.html")
assert(not webview_can_go_back(view), "nothing to go back to")
test_webview_click(view, 20.0, 10.0)
test_webview_wait_for_load(view, &log, "second.html")
assert(test_webview_url_ends_with(view, "/tests/webview/second.html"), "the link navigated")
let title = test_webview_title(view)
assert(ak.string_eq_string_view(&title, ak.sv("Second page")), "the second page's title")
assert(webview_can_go_back(view) and not webview_can_go_forward(view), "back is enabled")
assert(test_webview_pixel(view, 400, 300) == 0x008000ff, "the second page is painted")
webview_traverse_the_history_by_delta(view, -1)
test_webview_wait_for_load(view, &log, "first.html")
assert(test_webview_url_ends_with(view, "/tests/webview/first.html"), "back to the first page")
assert(webview_can_go_forward(view) and not webview_can_go_back(view), "forward is enabled")
webview_traverse_the_history_by_delta(view, 1)
test_webview_wait_for_load(view, &log, "second.html")
assert(test_webview_url_ends_with(view, "/tests/webview/second.html"), "forward to the second page")
## The top-level traversable's viewport scroll offset, its y in CSS pixels.
func test_webview_scroll_y(view: WebView*) -> f64:
let traversable = web.page_top_level_traversable(webview_page(view))
let offset = web.html_navigable_viewport_scroll_offset((web.HtmlNavigable*)traversable)
return gfx.css_pixels_to_double_const(&offset.m_y)
test "a wheel scrolls the page, and so do the keys":
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("first.html"))
test_webview_wait_for_load(view, &log, "first.html")
assert(test_webview_scroll_y(view) == 0.0, "the page starts at its top")
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_wheel, 400.0, 300.0, 0, 0, 0, 0, 120, 0)))
test_webview_wait_for_frame(view, &log)
assert(test_webview_scroll_y(view) == 120.0, "the wheel scrolled the viewport by its delta")
let page_down = web.KeyEvent(type_ = web.KeyEventType.key_down, key = web.UiEventsKeyCode.key_page_down, modifiers = web.UiEventsKeyModifier.mod_none, code_point = 0, repeat = false, browser_data = none)
webview_enqueue_input_event(view, web.InputEvent.key_event(page_down))
test_webview_settle(view, &log, 100)
assert(test_webview_scroll_y(view) > 120.0, "Page Down scrolled further")
test "a drag selects text, which the view reads back":
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("first.html"))
test_webview_wait_for_load(view, &log, "first.html")
let primary: u32 = 1
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_move, 1.0, 64.0, 0, 0, 0, 0, 0, 0)))
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_down, 1.0, 64.0, primary, primary, 0, 0, 0, 1)))
test_webview_settle(view, &log, 50)
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_move, 400.0, 64.0, 0, primary, 0, 0, 0, 0)))
test_webview_settle(view, &log, 50)
webview_enqueue_input_event(view, web.InputEvent.mouse_event(webview_mouse_event(view, web.MouseEventType.mouse_up, 400.0, 64.0, primary, 0, 0, 0, 0, 1)))
test_webview_settle(view, &log, 50)
let selected = webview_selected_text(view)
assert(ak.string_eq_string_view(&selected, ak.sv("Select this text")), "the paragraph's text is selected")
webview_select_all(view)
let everything = webview_selected_text(view)
assert(ak.string_eq_string_view(&everything, ak.sv("\nSecond page\nSelect this text\n\n\n\n")), "Select All selects the body's text, its whitespace too")