dymokomi/luced-browser
Source of release 0.3.0
0.3.0 / tests / browser_tests.luc
## The browser driven as a user drives it, through the application's input
## dispatch and rendering on a standalone frame (luce-ui's clicks test does the
## same): typing an address and Enter loads it, Enter again reloads it, Cmd+A
## in the address field selects the field, a click on a link follows it, the
## back and forward buttons walk the history, the window's title follows the
## page, the tabs open and close, a drag selects the page's text, only the
## current tab renders while the others load unseen, the tabs a crash saves
## come back, and a page loads over a loopback socket the window's loop
## watches.
from luce_ui.ui import Event, on_macos
from luce_gpu.gpu import Frame
from luce_window.input import EventKind, Key
from luce_window import window
from luce_browser_engine import webview_api as webview
from app import Browser
from luce_std import files
from luce_std import paths
from loopback_server import LoopbackServer, start_loopback_server, loopback_port, loopback_requests
import math
import time
## Nanoseconds a test waits for a page before it fails.
let patience: int = 20000000000
func click(browser: Browser, x: float, y: float) -> unit!:
browser.app.dispatch(Event(kind = EventKind.pointer_moved, x = x, y = y))
browser.app.dispatch(Event(kind = EventKind.pointer_down, x = x, y = y))
browser.app.dispatch(Event(kind = EventKind.pointer_up, x = x, y = y))
## A click in the middle of the address field.
func click_address(browser: Browser) -> unit!:
let field = browser.toolbar.address.layout().bounds()
click(browser, field.x + 20.0, field.y + field.height * 0.5)
## The platform's primary chord on `key`: Cmd on macOS, Ctrl elsewhere.
func chord(browser: Browser, key: Key) -> unit!:
browser.app.dispatch(Event(kind = EventKind.key_down, key = key, meta = on_macos(), control = not on_macos()))
func press(browser: Browser, key: Key) -> unit!:
browser.app.dispatch(Event(kind = EventKind.key_down, key = key))
browser.app.dispatch(Event(kind = EventKind.key_up, key = key))
## Types `text` into whatever has focus, then Enter.
func type_line(browser: Browser, text: str) -> unit!:
for byte in text.bytes():
browser.app.dispatch(Event(kind = EventKind.text_input, codepoint = byte))
press(browser, Key.enter)
## Renders the window once, on a standalone frame of its own (a frame takes a
## bounded number of draws).
func render(browser: Browser) -> unit!:
let frame = Frame(window.Size(1000.0, 700.0, 1000.0, 700.0, 1.0))
browser.app.render(frame.target())
frame.close()
## One turn of the window's loop, then a render.
func step(browser: Browser) -> unit!:
browser.turn(0.016)
render(browser)
## Runs the browser's turns and renders until the current tab's document at an
## address ending with `ending` has finished loading. The load is told by its
## URL, as the engine's tests tell it: a new view also reports its initial
## about:blank's.
func wait_for(browser: Browser, ending: str) -> unit!:
let start = time.now()
while true:
step(browser)
let tab = browser.tabs[browser.current]
if tab.loaded_url.ends_with(ending) and not tab.loading:
return
assert(time.now() - start <= patience, f"the page {ending} did not load")
## Runs the browser's turns for `seconds`, rendering after each (the engine's
## frames are timed: a turn alone does not bring the next one closer).
func settle(browser: Browser, seconds: float) -> unit!:
let end = time.now() + int(seconds * 1000000000.0)
while time.now() < end:
step(browser)
pub func browsing(browser: Browser, pages: str) -> unit!:
browser.app.layout(1000.0, 700.0)
render(browser)
assert(browser.app.title() == "New Tab")
# Typing an address (a file path, as sanitize_url takes one) and Enter loads it.
click_address(browser)
type_line(browser, pages + "/first.html")
wait_for(browser, "/pages/first.html")
assert(browser.tabs[0].title == "First page")
assert(browser.app.title() == "First page")
assert(browser.toolbar.address.text().ends_with("/pages/first.html"))
assert(not browser.toolbar.back.layout().is_enabled())
# Enter on the same address loads it again: the new document has no selection.
browser.commands.select_all.trigger()
assert(webview.selected_text(browser.view()).contains("Select this text"))
click_address(browser)
press(browser, Key.enter)
let start = time.now()
while webview.selected_text(browser.view()) != "" or browser.tabs[0].loading:
step(browser)
assert(time.now() - start <= patience, "Enter did not load the page again")
wait_for(browser, "/pages/first.html")
assert(browser.toolbar.address.text().ends_with("/pages/first.html"))
# Cmd/Ctrl+A in the address field, focused by a click or by Cmd/Ctrl+L,
# selects the field's text, not the page's: a focused field takes the
# editing chords before the commands do.
click_address(browser)
assert(browser.toolbar.address.layout().is_focused())
chord(browser, Key.a)
assert(webview.selected_text(browser.view()) == "", "Cmd+A in the clicked field selected the page")
browser.app.focus(browser.tabs[0].page.layout())
render(browser)
assert(browser.tabs[0].page.layout().is_focused())
# Cmd/Ctrl+L asks for the field's focus, which the next dispatch (or
# frame) applies before routing its event: Cmd/Ctrl+A right after goes to
# the field.
chord(browser, Key.l)
chord(browser, Key.a)
assert(browser.toolbar.address.layout().is_focused())
assert(webview.selected_text(browser.view()) == "", "Cmd+A in the field Cmd+L focused selected the page")
# A click on the link follows it; the window's title follows the page.
let page = browser.tabs[0].page.layout().bounds()
click(browser, page.x + 20.0, page.y + 10.0)
wait_for(browser, "/pages/second.html")
assert(browser.tabs[0].title == "Second page")
assert(browser.app.title() == "Second page")
assert(browser.toolbar.back.layout().is_enabled())
assert(not browser.toolbar.forward.layout().is_enabled())
# The back button goes back; the forward button forward again.
let back = browser.toolbar.back.layout().bounds()
click(browser, back.x + back.width * 0.5, back.y + back.height * 0.5)
wait_for(browser, "/pages/first.html")
assert(browser.app.title() == "First page")
assert(browser.toolbar.forward.layout().is_enabled())
let forward = browser.toolbar.forward.layout().bounds()
click(browser, forward.x + forward.width * 0.5, forward.y + forward.height * 0.5)
wait_for(browser, "/pages/second.html")
assert(browser.app.title() == "Second page")
assert(not browser.toolbar.forward.layout().is_enabled())
# Cmd/Ctrl+[ goes back too.
browser.app.dispatch(Event(kind = EventKind.key_down, key = Key.left_bracket, meta = true, control = true))
wait_for(browser, "/pages/first.html")
# A new tab, then closing it, leaves the first; the title follows the tab shown.
browser.commands.new_tab.trigger()
assert(browser.tabs.length == 2 and browser.current == 1)
assert(browser.app.title() == "New Tab")
browser.commands.close_tab.trigger()
assert(browser.tabs.length == 1 and browser.current == 0)
assert(browser.tabs[0].url.ends_with("/pages/first.html"))
assert(browser.app.title() == "First page")
# A drag across the paragraph selects its text; Select All the page's.
let y = page.y + 64.0
browser.app.dispatch(Event(kind = EventKind.pointer_moved, x = page.x + 1.0, y = y))
browser.app.dispatch(Event(kind = EventKind.pointer_down, x = page.x + 1.0, y = y))
settle(browser, 0.2)
browser.app.dispatch(Event(kind = EventKind.pointer_moved, x = page.x + 400.0, y = y))
settle(browser, 0.2)
browser.app.dispatch(Event(kind = EventKind.pointer_up, x = page.x + 400.0, y = y))
settle(browser, 0.2)
assert(webview.selected_text(browser.view()) == "Select this text")
browser.commands.select_all.trigger()
assert(webview.selected_text(browser.view()).contains("Second page"))
## Runs the browser's turns and renders until `ready` holds.
func wait_until(browser: Browser, ready: func() -> bool, what: str) -> unit!:
let start = time.now()
while not ready():
step(browser)
assert(time.now() - start <= patience, what)
## Tabs: only the current tab's page renders. A background tab is hidden from
## the engine, its load goes on and presents no frame, and switched to it
## paints at once; a closed tab's page ends. The tabs saved for a crash
## (save_tabs) open again (restore_tabs), the current one current again.
pub func tabs(browser: Browser, pages: str, scratch: str) -> unit!:
assert(browser.tabs.length == 1)
let first = browser.tabs[0]
browser.commands.new_tab.trigger()
let second = browser.tabs[1]
type_line(browser, pages + "/second.html")
wait_for(browser, "/pages/second.html")
assert(webview.is_visible(second.page.view) and not webview.is_visible(first.page.view), "only the new current tab is shown")
browser.select_tab(0)
assert(webview.is_visible(first.page.view) and not webview.is_visible(second.page.view), "the tab switched from is hidden")
let serial = webview.frame_serial(second.page.view)
_ = webview.load(second.page.view, pages + "/first.html")
let loaded = func () -> bool:
return second.loaded_url.ends_with("/pages/first.html") and not second.loading
wait_until(browser, loaded, "the background tab did not load")
settle(browser, 0.2)
assert(webview.frame_serial(second.page.view) == serial, "a background tab presents no frame")
browser.select_tab(1)
let painted = func () -> bool:
return webview.frame_serial(second.page.view) != serial
wait_until(browser, painted, "the tab switched to did not paint")
# The tabs a crash saves, then opened again after the second is closed.
let saved = paths.join(scratch, "tabs.txt")
browser.save_tabs(saved)
let text = files.read_text(saved)
assert(text.starts_with(" file://") and text.contains("\n* file://") and text.ends_with("/pages/first.html\n"), "the tabs file lists the tabs, the current one marked")
browser.close_tab(1)
assert(browser.tabs.length == 1 and browser.current == 0)
assert(browser.restore_tabs(saved))
assert(browser.tabs.length == 2 and browser.current == 1, "the saved tabs open again, the current one current")
wait_for(browser, "/pages/first.html")
assert(not webview.is_visible(browser.tabs[0].page.view))
browser.close_tab(1)
assert(browser.tabs.length == 1 and browser.current == 0 and webview.is_visible(first.page.view))
wait_for(browser, "/pages/first.html")
## A page over a loopback socket: the engine hands the client's socket to the
## window's loop (Application.watch) while the response is on its way, and
## takes it back once the server closed the connection.
pub func loopback(browser: Browser) -> unit!:
let server: LoopbackServer = start_loopback_server()
let address = f"http://127.0.0.1:{loopback_port(server)}/page"
browser.toolbar.address.set_text("")
click_address(browser)
type_line(browser, address)
var watched = 0
let start = time.now()
while not (browser.tabs[0].loaded_url == address and not browser.tabs[0].loading):
step(browser)
watched = math.max(watched, browser.sockets.length)
assert(time.now() - start <= patience, "the loopback page did not load")
assert(browser.app.title() == "Loopback page")
assert(loopback_requests(server) >= 1)
assert(watched >= 1, "the window's loop watched the client's socket")
while browser.sockets.length > 0:
step(browser)
assert(time.now() - start <= patience, "the closed socket stayed watched")
server.close()