647 lines
15 KiB
MoonBit
647 lines
15 KiB
MoonBit
// High-level test harnesses, ported from gpui/src/app/test_context.rs
|
|
// (`TestAppContext` and `VisualTestContext`).
|
|
|
|
///|
|
|
priv enum EmptyView {
|
|
EmptyView
|
|
}
|
|
|
|
///|
|
|
impl @window.Render for EmptyView with fn render(_self, _window, _cx) {
|
|
@window.empty()
|
|
}
|
|
|
|
///|
|
|
/// Owns a `TestPlatform` and `Application`, pumping tasks and scheduled
|
|
/// frames together for deterministic integration tests (GPUI:
|
|
/// `TestAppContext`).
|
|
pub struct TestAppContext {
|
|
platform : TestPlatform
|
|
application : @window.Application
|
|
priv mut skip_drawing : Bool
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::new(
|
|
default_bounds? : @geometry.Bounds[@geometry.Pixels],
|
|
default_scale_factor~ : Double = 1.0,
|
|
) -> TestAppContext {
|
|
let platform = TestPlatform::new(default_bounds?, default_scale_factor~)
|
|
TestAppContext::with_platform(platform)
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::with_platform(platform : TestPlatform) -> TestAppContext {
|
|
let p : &@platform.Platform = platform
|
|
let application = @window.Application::with_platform(p)
|
|
{ platform, application, skip_drawing: false, }
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::app(self : TestAppContext) -> @app.App {
|
|
self.application.app()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::application(
|
|
self : TestAppContext,
|
|
) -> @window.Application {
|
|
self.application
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::platform(self : TestAppContext) -> TestPlatform {
|
|
self.platform
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::dispatcher(
|
|
self : TestAppContext,
|
|
) -> @executor.TestDispatcher {
|
|
self.platform.test_dispatcher()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::foreground_executor(
|
|
self : TestAppContext,
|
|
) -> @executor.Executor {
|
|
self.application.app().foreground_executor()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::background_executor(
|
|
self : TestAppContext,
|
|
) -> @executor.Executor {
|
|
self.application.app().background_executor()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::windows(
|
|
self : TestAppContext,
|
|
) -> Array[@window.AnyWindowHandle] {
|
|
self.application.windows()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::active_window(
|
|
self : TestAppContext,
|
|
) -> @window.AnyWindowHandle? {
|
|
self.application.active_window()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::test_window(
|
|
self : TestAppContext,
|
|
handle : @window.AnyWindowHandle,
|
|
) -> TestWindow {
|
|
match self.platform.window(handle.window_id()) {
|
|
Some(win) => win
|
|
None => abort("no TestWindow for \{handle.window_id()}")
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Disables (or re-enables) automatic frame pumping in `run_until_parked`
|
|
/// (GPUI: `TestAppContext::skip_drawing`).
|
|
pub fn TestAppContext::skip_drawing(
|
|
self : TestAppContext,
|
|
skip~ : Bool = true,
|
|
) -> Unit {
|
|
self.skip_drawing = skip
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::pump_frames(self : TestAppContext) -> Int32 {
|
|
self.platform.pump_scheduled_frames()
|
|
}
|
|
|
|
///|
|
|
/// Drains all ready tasks and pumps scheduled frames until neither tasks nor
|
|
/// scheduled frames remain (GPUI: `TestAppContext::run_until_parked`). Aborts
|
|
/// after 100 frame pump passes to catch runaway redraw loops.
|
|
pub fn TestAppContext::run_until_parked(self : TestAppContext) -> Unit {
|
|
let max_passes = 100
|
|
let mut passes = 0
|
|
while true {
|
|
self.platform.test_dispatcher().run_until_parked()
|
|
if self.skip_drawing {
|
|
break
|
|
}
|
|
let pumped = self.platform.pump_scheduled_frames()
|
|
if pumped == 0 {
|
|
break
|
|
}
|
|
passes = passes + 1
|
|
if passes >= max_passes {
|
|
abort(
|
|
"TestAppContext::run_until_parked: runaway redraw loop (> \{max_passes} frame pumps)",
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Advances the virtual clock by `ms`, running timers, tasks and scheduled
|
|
/// frames (GPUI: `TestAppContext::advance_clock`).
|
|
pub fn TestAppContext::advance_clock(self : TestAppContext, ms : Int32) -> Unit {
|
|
self.run_until_parked()
|
|
self.platform.test_dispatcher().advance_clock(ms)
|
|
self.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn[R] TestAppContext::update(
|
|
self : TestAppContext,
|
|
f : (@app.App) -> R,
|
|
) -> R {
|
|
let result = self.application.app().update(f)
|
|
self.run_until_parked()
|
|
result
|
|
}
|
|
|
|
///|
|
|
pub fn[R] TestAppContext::read(self : TestAppContext, f : (@app.App) -> R) -> R {
|
|
f(self.application.app())
|
|
}
|
|
|
|
///|
|
|
pub fn[T] TestAppContext::new_entity(
|
|
self : TestAppContext,
|
|
build : (@app.Context[T]) -> T,
|
|
) -> @app.Entity[T] {
|
|
self.update(fn(app) { app.new_entity(build) })
|
|
}
|
|
|
|
///|
|
|
pub fn[T, R] TestAppContext::update_entity(
|
|
self : TestAppContext,
|
|
entity : @app.Entity[T],
|
|
f : (T, @app.Context[T]) -> R,
|
|
) -> R {
|
|
self.update(fn(app) { app.update_entity(entity, f) })
|
|
}
|
|
|
|
///|
|
|
pub fn[T] TestAppContext::read_entity(
|
|
self : TestAppContext,
|
|
entity : @app.Entity[T],
|
|
) -> T {
|
|
self.application.app().read_entity(entity)
|
|
}
|
|
|
|
///|
|
|
pub fn[R] TestAppContext::spawn(
|
|
self : TestAppContext,
|
|
f : async (@app.AsyncApp) -> R,
|
|
) -> @executor.Task[R] {
|
|
self.application.app().spawn(f)
|
|
}
|
|
|
|
///|
|
|
pub fn[G] TestAppContext::set_global(
|
|
self : TestAppContext,
|
|
key : @app.GlobalKey[G],
|
|
value : G,
|
|
) -> Unit {
|
|
self.update(fn(app) { app.set_global(key, value) })
|
|
}
|
|
|
|
///|
|
|
pub fn[G] TestAppContext::read_global(
|
|
self : TestAppContext,
|
|
key : @app.GlobalKey[G],
|
|
) -> G {
|
|
self.application.app().global(key)
|
|
}
|
|
|
|
///|
|
|
pub fn[G] TestAppContext::has_global(
|
|
self : TestAppContext,
|
|
key : @app.GlobalKey[G],
|
|
) -> Bool {
|
|
self.application.app().has_global(key)
|
|
}
|
|
|
|
///|
|
|
pub fn[G, R] TestAppContext::update_global(
|
|
self : TestAppContext,
|
|
key : @app.GlobalKey[G],
|
|
f : (G, @app.App) -> R,
|
|
) -> R {
|
|
self.update(fn(app) { app.update_global(key, f) })
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::write_to_clipboard(
|
|
self : TestAppContext,
|
|
item : @platform.ClipboardItem,
|
|
) -> Unit {
|
|
self.application.write_to_clipboard(item)
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::read_from_clipboard(
|
|
self : TestAppContext,
|
|
) -> @platform.ClipboardItem? {
|
|
self.application.read_from_clipboard()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::cursor_style(
|
|
self : TestAppContext,
|
|
) -> @style.CursorStyle {
|
|
self.platform.cursor_style()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::opened_url(self : TestAppContext) -> String? {
|
|
self.platform.opened_url()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::refresh(self : TestAppContext) -> Unit {
|
|
self.application.refresh_windows()
|
|
self.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::quit(self : TestAppContext) -> Unit {
|
|
self.application.quit()
|
|
}
|
|
|
|
///|
|
|
pub fn[V : @window.Render] TestAppContext::open_window(
|
|
self : TestAppContext,
|
|
options? : @platform.WindowOptions,
|
|
build_root : (@window.Window, @app.App) -> @app.Entity[V],
|
|
) -> @window.WindowHandle[V] {
|
|
let opts = options.unwrap_or(Default::default())
|
|
let handle = self.application.open_window(opts, build_root)
|
|
self.run_until_parked()
|
|
handle
|
|
}
|
|
|
|
///|
|
|
pub fn[V : @window.Render] TestAppContext::add_window(
|
|
self : TestAppContext,
|
|
size? : @geometry.Size[@geometry.Pixels],
|
|
scale_factor? : Double,
|
|
title? : String,
|
|
build_view : (@window.Window, @app.Context[V]) -> V,
|
|
) -> @window.WindowHandle[V] {
|
|
let options : @platform.WindowOptions = match size {
|
|
Some(s) => @platform.WindowOptions::with_size(s, scale_factor?, title?)
|
|
None => { ..Default::default(), scale_factor, title, }
|
|
}
|
|
self.open_window(options~, fn(window, app) {
|
|
app.new_entity(fn(cx) { build_view(window, cx) })
|
|
})
|
|
}
|
|
|
|
///|
|
|
pub fn[V : @window.Render] TestAppContext::add_window_view(
|
|
self : TestAppContext,
|
|
size? : @geometry.Size[@geometry.Pixels],
|
|
scale_factor? : Double,
|
|
title? : String,
|
|
build_view : (@window.Window, @app.Context[V]) -> V,
|
|
) -> (@app.Entity[V], VisualTestContext) {
|
|
let handle = self.add_window(size?, scale_factor?, title?, build_view)
|
|
let vcx = VisualTestContext::from_window(handle.to_any(), self)
|
|
(handle.entity(), vcx)
|
|
}
|
|
|
|
///|
|
|
pub fn TestAppContext::add_empty_window(
|
|
self : TestAppContext,
|
|
size? : @geometry.Size[@geometry.Pixels],
|
|
scale_factor? : Double,
|
|
) -> VisualTestContext {
|
|
let (_, vcx) = self.add_window_view(size?, scale_factor?, fn(_, _) {
|
|
EmptyView::EmptyView
|
|
})
|
|
vcx
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// VisualTestContext
|
|
// ---------------------------------------------------------------------------
|
|
|
|
///|
|
|
/// Window-scoped test driver combining a `TestAppContext` with one
|
|
/// `AnyWindowHandle` (GPUI: `VisualTestContext`).
|
|
pub struct VisualTestContext {
|
|
cx : TestAppContext
|
|
window_handle : @window.AnyWindowHandle
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::from_window(
|
|
window_handle : @window.AnyWindowHandle,
|
|
cx : TestAppContext,
|
|
) -> VisualTestContext {
|
|
{ cx, window_handle, }
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::window_handle(
|
|
self : VisualTestContext,
|
|
) -> @window.AnyWindowHandle {
|
|
self.window_handle
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::window_id(
|
|
self : VisualTestContext,
|
|
) -> @platform.WindowId {
|
|
self.window_handle.window_id()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::app(self : VisualTestContext) -> @app.App {
|
|
self.cx.app()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::test_window(self : VisualTestContext) -> TestWindow {
|
|
self.cx.test_window(self.window_handle)
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::window(self : VisualTestContext) -> @window.Window {
|
|
match self.cx.application.window(self.window_id()) {
|
|
Some(win) => win
|
|
None => abort("window \{self.window_id()} is closed")
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::is_closed(self : VisualTestContext) -> Bool {
|
|
self.test_window().is_closed()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::window_title(self : VisualTestContext) -> String? {
|
|
self.test_window().title()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::is_active(self : VisualTestContext) -> Bool {
|
|
@platform.PlatformWindow::is_active(self.test_window())
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::is_hovered(self : VisualTestContext) -> Bool {
|
|
@platform.PlatformWindow::is_hovered(self.test_window())
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::bounds(
|
|
self : VisualTestContext,
|
|
) -> @geometry.Bounds[@geometry.Pixels] {
|
|
@platform.PlatformWindow::bounds(self.test_window())
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::scale_factor(self : VisualTestContext) -> Double {
|
|
@platform.PlatformWindow::scale_factor(self.test_window())
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::cursor_style(
|
|
self : VisualTestContext,
|
|
) -> @style.CursorStyle {
|
|
self.cx.cursor_style()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::write_to_clipboard(
|
|
self : VisualTestContext,
|
|
item : @platform.ClipboardItem,
|
|
) -> Unit {
|
|
self.cx.write_to_clipboard(item)
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::read_from_clipboard(
|
|
self : VisualTestContext,
|
|
) -> @platform.ClipboardItem? {
|
|
self.cx.read_from_clipboard()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::run_until_parked(self : VisualTestContext) -> Unit {
|
|
self.cx.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::advance_clock(
|
|
self : VisualTestContext,
|
|
ms : Int32,
|
|
) -> Unit {
|
|
self.cx.advance_clock(ms)
|
|
}
|
|
|
|
///|
|
|
pub fn[R] VisualTestContext::update(
|
|
self : VisualTestContext,
|
|
f : (@window.Window, @app.App) -> R,
|
|
) -> R {
|
|
let window = self.window()
|
|
let result = self.app().update(fn(app) { f(window, app) })
|
|
self.cx.run_until_parked()
|
|
result
|
|
}
|
|
|
|
///|
|
|
pub fn[R] VisualTestContext::read(
|
|
self : VisualTestContext,
|
|
f : (@app.App) -> R,
|
|
) -> R {
|
|
self.cx.read(f)
|
|
}
|
|
|
|
///|
|
|
pub fn[T, R] VisualTestContext::update_entity(
|
|
self : VisualTestContext,
|
|
entity : @app.Entity[T],
|
|
f : (T, @app.Context[T]) -> R,
|
|
) -> R {
|
|
self.cx.update_entity(entity, f)
|
|
}
|
|
|
|
///|
|
|
pub fn[T] VisualTestContext::new_entity(
|
|
self : VisualTestContext,
|
|
build : (@app.Context[T]) -> T,
|
|
) -> @app.Entity[T] {
|
|
self.cx.new_entity(build)
|
|
}
|
|
|
|
///|
|
|
pub fn[V : @window.Render] VisualTestContext::replace_root_view(
|
|
self : VisualTestContext,
|
|
build : (@window.Window, @app.Context[V]) -> V,
|
|
) -> @app.Entity[V] {
|
|
self.update(fn(window, _) { window.replace_root_view(build) })
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::draw(self : VisualTestContext) -> @scene.Scene {
|
|
let scene = self.window().draw()
|
|
self.cx.run_until_parked()
|
|
scene
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::scene(self : VisualTestContext) -> @scene.Scene {
|
|
match self.test_window().last_scene() {
|
|
Some(scene) => scene
|
|
None => self.window().scene()
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::drawn_scenes(
|
|
self : VisualTestContext,
|
|
) -> Array[@scene.Scene] {
|
|
self.test_window().drawn_scenes()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::draw_count(self : VisualTestContext) -> Int32 {
|
|
self.test_window().draw_count()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_input(
|
|
self : VisualTestContext,
|
|
event : @input.PlatformInput,
|
|
) -> @input.DispatchEventResult {
|
|
let result = self.test_window().simulate_input(event)
|
|
self.cx.run_until_parked()
|
|
result
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_mouse_move(
|
|
self : VisualTestContext,
|
|
position : @geometry.Point[@geometry.Pixels],
|
|
button? : @input.MouseButton,
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(
|
|
MouseMove({ position, pressed_button: button, modifiers, }),
|
|
)
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_mouse_down(
|
|
self : VisualTestContext,
|
|
position : @geometry.Point[@geometry.Pixels],
|
|
button~ : @input.MouseButton = Left,
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
click_count~ : Int32 = 1,
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(
|
|
MouseDown({ position, button, modifiers, click_count, first_mouse: false, }),
|
|
)
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_mouse_up(
|
|
self : VisualTestContext,
|
|
position : @geometry.Point[@geometry.Pixels],
|
|
button~ : @input.MouseButton = Left,
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
click_count~ : Int32 = 1,
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(MouseUp({ position, button, modifiers, click_count, }))
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_click(
|
|
self : VisualTestContext,
|
|
position : @geometry.Point[@geometry.Pixels],
|
|
button~ : @input.MouseButton = Left,
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
) -> Unit {
|
|
ignore(self.simulate_mouse_down(position, button~, modifiers~))
|
|
ignore(self.simulate_mouse_up(position, button~, modifiers~))
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_scroll_wheel(
|
|
self : VisualTestContext,
|
|
position : @geometry.Point[@geometry.Pixels],
|
|
delta : @input.ScrollDelta,
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(ScrollWheel({ position, delta, modifiers, }))
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_mouse_exit(
|
|
self : VisualTestContext,
|
|
position~ : @geometry.Point[@geometry.Pixels] = @geometry.point(
|
|
@geometry.px(0.0),
|
|
@geometry.px(0.0),
|
|
),
|
|
modifiers~ : @input.Modifiers = Default::default(),
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(
|
|
MouseExited({ position, pressed_button: None, modifiers, }),
|
|
)
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_modifiers_change(
|
|
self : VisualTestContext,
|
|
modifiers : @input.Modifiers,
|
|
) -> @input.DispatchEventResult {
|
|
self.simulate_input(ModifiersChanged({ modifiers, }))
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_resize(
|
|
self : VisualTestContext,
|
|
size : @geometry.Size[@geometry.Pixels],
|
|
) -> Unit {
|
|
self.test_window().simulate_resize(size)
|
|
self.cx.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_scale_factor_change(
|
|
self : VisualTestContext,
|
|
scale_factor : Double,
|
|
) -> Unit {
|
|
self.test_window().simulate_scale_factor_change(scale_factor)
|
|
self.cx.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_active_status_change(
|
|
self : VisualTestContext,
|
|
active : Bool,
|
|
) -> Unit {
|
|
self.test_window().simulate_active_status_change(active)
|
|
self.cx.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_hover_status_change(
|
|
self : VisualTestContext,
|
|
hovered : Bool,
|
|
) -> Unit {
|
|
self.test_window().simulate_hover_status_change(hovered)
|
|
self.cx.run_until_parked()
|
|
}
|
|
|
|
///|
|
|
pub fn VisualTestContext::simulate_close(self : VisualTestContext) -> Bool {
|
|
let closed = self.test_window().simulate_close_requested()
|
|
self.cx.run_until_parked()
|
|
closed
|
|
}
|