M7: test platform — Platform traits, Application, TestPlatform, VisualTestContext (#7)

This commit was merged in pull request #7.
This commit is contained in:
nandi committed 2026-11-24 23:54:56 +00:00
1 parent fbe4c081e6
commit 73c67825b2
25 files changed
+3592 -83

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
import {
"experiments/gpui/geometry",
"experiments/gpui/scene",
"experiments/gpui/style",
"experiments/gpui/input",
"experiments/gpui/executor",
"experiments/gpui/app",
"experiments/gpui/platform",
"experiments/gpui/window",
}
// experiments/gpui/executor uses MoonBit's experimental async intrinsics.
options(
"allow-experimental-packages": true,
)
import {
"experiments/gpui/color",
} for "test"
+147
View File
@@ -0,0 +1,147 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/gpui/platform/test"
import {
"experiments/gpui/app",
"experiments/gpui/executor",
"experiments/gpui/geometry",
"experiments/gpui/input",
"experiments/gpui/platform",
"experiments/gpui/scene",
"experiments/gpui/style",
"experiments/gpui/window",
}
// Values
// Errors
// Types and methods
pub struct TestAppContext {
platform : TestPlatform
application : @window.Application
// private fields
}
pub fn TestAppContext::active_window(Self) -> @window.AnyWindowHandle?
pub fn TestAppContext::add_empty_window(Self, size? : @geometry.Size[@geometry.Pixels], scale_factor? : Double) -> VisualTestContext
pub fn[V : @window.Render] TestAppContext::add_window(Self, size? : @geometry.Size[@geometry.Pixels], scale_factor? : Double, title? : String, (@window.Window, @app.Context[V]) -> V) -> @window.WindowHandle[V]
pub fn[V : @window.Render] TestAppContext::add_window_view(Self, size? : @geometry.Size[@geometry.Pixels], scale_factor? : Double, title? : String, (@window.Window, @app.Context[V]) -> V) -> (@app.Entity[V], VisualTestContext)
pub fn TestAppContext::advance_clock(Self, Int32) -> Unit
pub fn TestAppContext::app(Self) -> @app.App
pub fn TestAppContext::application(Self) -> @window.Application
pub fn TestAppContext::background_executor(Self) -> @executor.Executor
pub fn TestAppContext::cursor_style(Self) -> @style.CursorStyle
pub fn TestAppContext::dispatcher(Self) -> @executor.TestDispatcher
pub fn TestAppContext::foreground_executor(Self) -> @executor.Executor
pub fn[G] TestAppContext::has_global(Self, @app.GlobalKey[G]) -> Bool
pub fn TestAppContext::new(default_bounds? : @geometry.Bounds[@geometry.Pixels], default_scale_factor? : Double) -> Self
pub fn[T] TestAppContext::new_entity(Self, (@app.Context[T]) -> T) -> @app.Entity[T]
pub fn[V : @window.Render] TestAppContext::open_window(Self, options? : @platform.WindowOptions, (@window.Window, @app.App) -> @app.Entity[V]) -> @window.WindowHandle[V]
pub fn TestAppContext::opened_url(Self) -> String?
pub fn TestAppContext::platform(Self) -> TestPlatform
pub fn TestAppContext::pump_frames(Self) -> Int32
pub fn TestAppContext::quit(Self) -> Unit
pub fn[R] TestAppContext::read(Self, (@app.App) -> R) -> R
pub fn[T] TestAppContext::read_entity(Self, @app.Entity[T]) -> T
pub fn TestAppContext::read_from_clipboard(Self) -> @platform.ClipboardItem?
pub fn[G] TestAppContext::read_global(Self, @app.GlobalKey[G]) -> G
pub fn TestAppContext::refresh(Self) -> Unit
pub fn TestAppContext::run_until_parked(Self) -> Unit
pub fn[G] TestAppContext::set_global(Self, @app.GlobalKey[G], G) -> Unit
pub fn TestAppContext::skip_drawing(Self, skip? : Bool) -> Unit
pub fn[R] TestAppContext::spawn(Self, async (@app.AsyncApp) -> R) -> @executor.Task[R]
pub fn TestAppContext::test_window(Self, @window.AnyWindowHandle) -> TestWindow
pub fn[R] TestAppContext::update(Self, (@app.App) -> R) -> R
pub fn[T, R] TestAppContext::update_entity(Self, @app.Entity[T], (T, @app.Context[T]) -> R) -> R
pub fn[G, R] TestAppContext::update_global(Self, @app.GlobalKey[G], (G, @app.App) -> R) -> R
pub fn TestAppContext::windows(Self) -> Array[@window.AnyWindowHandle]
pub fn TestAppContext::with_platform(TestPlatform) -> Self
pub fn TestAppContext::write_to_clipboard(Self, @platform.ClipboardItem) -> Unit
pub struct TestPlatform {
// private fields
}
pub fn TestPlatform::cursor_style(Self) -> @style.CursorStyle
pub fn TestPlatform::did_quit(Self) -> Bool
pub fn TestPlatform::did_run(Self) -> Bool
pub fn TestPlatform::new(dispatcher? : @executor.TestDispatcher, default_bounds? : @geometry.Bounds[@geometry.Pixels], default_scale_factor? : Double) -> Self
pub fn TestPlatform::opened_url(Self) -> String?
pub fn TestPlatform::opened_urls(Self) -> Array[String]
pub fn TestPlatform::pump_scheduled_frames(Self) -> Int32
pub fn TestPlatform::set_active_window(Self, TestWindow?) -> Unit
pub fn TestPlatform::test_dispatcher(Self) -> @executor.TestDispatcher
pub fn TestPlatform::window(Self, @platform.WindowId) -> TestWindow?
pub fn TestPlatform::windows(Self) -> Array[TestWindow]
pub impl @platform.Platform for TestPlatform
pub struct TestWindow {
// private fields
}
pub fn TestWindow::clear_drawn_scenes(Self) -> Unit
pub fn TestWindow::draw_count(Self) -> Int32
pub fn TestWindow::drawn_scenes(Self) -> Array[@scene.Scene]
pub fn TestWindow::frame_schedule_count(Self) -> Int32
pub fn TestWindow::frame_scheduled(Self) -> Bool
pub fn TestWindow::is_closed(Self) -> Bool
pub fn TestWindow::last_scene(Self) -> @scene.Scene?
pub fn TestWindow::simulate_active_status_change(Self, Bool) -> Unit
pub fn TestWindow::simulate_close(Self) -> Unit
pub fn TestWindow::simulate_close_requested(Self) -> Bool
pub fn TestWindow::simulate_frame_request(Self) -> Unit
pub fn TestWindow::simulate_hover_status_change(Self, Bool) -> Unit
pub fn TestWindow::simulate_input(Self, @input.PlatformInput) -> @input.DispatchEventResult
pub fn TestWindow::simulate_resize(Self, @geometry.Size[@geometry.Pixels]) -> Unit
pub fn TestWindow::simulate_scale_factor_change(Self, Double) -> Unit
pub fn TestWindow::simulate_scheduled_frame(Self) -> Bool
pub fn TestWindow::simulate_should_close(Self) -> Bool
pub fn TestWindow::title(Self) -> String?
pub fn TestWindow::window_id(Self) -> @platform.WindowId
pub impl @platform.PlatformWindow for TestWindow
pub struct VisualTestContext {
cx : TestAppContext
window_handle : @window.AnyWindowHandle
}
pub fn VisualTestContext::advance_clock(Self, Int32) -> Unit
pub fn VisualTestContext::app(Self) -> @app.App
pub fn VisualTestContext::bounds(Self) -> @geometry.Bounds[@geometry.Pixels]
pub fn VisualTestContext::cursor_style(Self) -> @style.CursorStyle
pub fn VisualTestContext::draw(Self) -> @scene.Scene
pub fn VisualTestContext::draw_count(Self) -> Int32
pub fn VisualTestContext::drawn_scenes(Self) -> Array[@scene.Scene]
pub fn VisualTestContext::from_window(@window.AnyWindowHandle, TestAppContext) -> Self
pub fn VisualTestContext::is_active(Self) -> Bool
pub fn VisualTestContext::is_closed(Self) -> Bool
pub fn VisualTestContext::is_hovered(Self) -> Bool
pub fn[T] VisualTestContext::new_entity(Self, (@app.Context[T]) -> T) -> @app.Entity[T]
pub fn[R] VisualTestContext::read(Self, (@app.App) -> R) -> R
pub fn VisualTestContext::read_from_clipboard(Self) -> @platform.ClipboardItem?
pub fn[V : @window.Render] VisualTestContext::replace_root_view(Self, (@window.Window, @app.Context[V]) -> V) -> @app.Entity[V]
pub fn VisualTestContext::run_until_parked(Self) -> Unit
pub fn VisualTestContext::scale_factor(Self) -> Double
pub fn VisualTestContext::scene(Self) -> @scene.Scene
pub fn VisualTestContext::simulate_active_status_change(Self, Bool) -> Unit
pub fn VisualTestContext::simulate_click(Self, @geometry.Point[@geometry.Pixels], button? : @input.MouseButton, modifiers? : @input.Modifiers) -> Unit
pub fn VisualTestContext::simulate_close(Self) -> Bool
pub fn VisualTestContext::simulate_hover_status_change(Self, Bool) -> Unit
pub fn VisualTestContext::simulate_input(Self, @input.PlatformInput) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_modifiers_change(Self, @input.Modifiers) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_mouse_down(Self, @geometry.Point[@geometry.Pixels], button? : @input.MouseButton, modifiers? : @input.Modifiers, click_count? : Int32) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_mouse_exit(Self, position? : @geometry.Point[@geometry.Pixels], modifiers? : @input.Modifiers) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_mouse_move(Self, @geometry.Point[@geometry.Pixels], button? : @input.MouseButton, modifiers? : @input.Modifiers) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_mouse_up(Self, @geometry.Point[@geometry.Pixels], button? : @input.MouseButton, modifiers? : @input.Modifiers, click_count? : Int32) -> @input.DispatchEventResult
pub fn VisualTestContext::simulate_resize(Self, @geometry.Size[@geometry.Pixels]) -> Unit
pub fn VisualTestContext::simulate_scale_factor_change(Self, Double) -> Unit
pub fn VisualTestContext::simulate_scroll_wheel(Self, @geometry.Point[@geometry.Pixels], @input.ScrollDelta, modifiers? : @input.Modifiers) -> @input.DispatchEventResult
pub fn VisualTestContext::test_window(Self) -> TestWindow
pub fn[R] VisualTestContext::update(Self, (@window.Window, @app.App) -> R) -> R
pub fn[T, R] VisualTestContext::update_entity(Self, @app.Entity[T], (T, @app.Context[T]) -> R) -> R
pub fn VisualTestContext::window(Self) -> @window.Window
pub fn VisualTestContext::window_handle(Self) -> @window.AnyWindowHandle
pub fn VisualTestContext::window_id(Self) -> @platform.WindowId
pub fn VisualTestContext::window_title(Self) -> String?
pub fn VisualTestContext::write_to_clipboard(Self, @platform.ClipboardItem) -> Unit
// Type aliases
// Traits
+224
View File
@@ -0,0 +1,224 @@
// Headless test platform, ported from gpui/src/platform/test/platform.rs.
///|
let default_test_bounds : @geometry.Bounds[@geometry.Pixels] = @geometry.bounds(
@geometry.point(@geometry.px(0.0), @geometry.px(0.0)),
@geometry.size(@geometry.px(1024.0), @geometry.px(768.0)),
)
///|
/// A headless `Platform` backed by `TestDispatcher` and `TestWindow`s
/// (GPUI: `TestPlatform`).
pub struct TestPlatform {
priv dispatcher : @executor.TestDispatcher
priv default_bounds : @geometry.Bounds[@geometry.Pixels]
priv default_scale_factor : Double
priv windows : Map[@platform.WindowId, TestWindow]
priv mut active_window : TestWindow?
priv mut active_cursor : @style.CursorStyle
priv mut clipboard : @platform.ClipboardItem?
priv opened_urls : Array[String]
priv mut run_called : Bool
priv mut quit_called : Bool
}
///|
pub fn TestPlatform::new(
dispatcher? : @executor.TestDispatcher,
default_bounds? : @geometry.Bounds[@geometry.Pixels],
default_scale_factor~ : Double = 1.0,
) -> TestPlatform {
{
dispatcher: dispatcher.unwrap_or(@executor.TestDispatcher::new()),
default_bounds: default_bounds.unwrap_or(default_test_bounds),
default_scale_factor,
windows: {},
active_window: None,
active_cursor: Arrow,
clipboard: None,
opened_urls: [],
run_called: false,
quit_called: false,
}
}
///|
pub impl @platform.Platform for TestPlatform with fn dispatcher(self) {
let d : &@executor.Dispatcher = self.dispatcher
d
}
///|
pub impl @platform.Platform for TestPlatform with fn default_window_bounds(self) {
self.default_bounds
}
///|
pub impl @platform.Platform for TestPlatform with fn open_window(
self,
id,
params,
) {
let window = TestWindow::new(id, self, params, self.default_scale_factor)
self.windows[id] = window
if params.focus {
let previous = self.active_window
self.active_window = Some(window)
if previous is Some(prev) && prev.id != id {
@executor.Dispatcher::dispatch(self.dispatcher, fn() {
if !prev.closed {
prev.simulate_active_status_change(false)
}
})
}
}
let pw : &@platform.PlatformWindow = window
pw
}
///|
pub impl @platform.Platform for TestPlatform with fn run(
self,
on_finish_launching,
) {
self.run_called = true
on_finish_launching()
}
///|
pub impl @platform.Platform for TestPlatform with fn quit(self) {
self.quit_called = true
}
///|
pub impl @platform.Platform for TestPlatform with fn active_window(self) {
match self.active_window {
Some(win) if !win.closed => Some(win.id)
_ => None
}
}
///|
pub impl @platform.Platform for TestPlatform with fn set_cursor_style(
self,
style,
) {
self.active_cursor = style
}
///|
pub impl @platform.Platform for TestPlatform with fn read_from_clipboard(self) {
self.clipboard
}
///|
pub impl @platform.Platform for TestPlatform with fn write_to_clipboard(
self,
item,
) {
self.clipboard = Some(item)
}
///|
pub impl @platform.Platform for TestPlatform with fn open_url(self, url) {
self.opened_urls.push(url)
}
// ---------------------------------------------------------------------------
// Inspection and simulation helpers
// ---------------------------------------------------------------------------
///|
pub fn TestPlatform::test_dispatcher(
self : TestPlatform,
) -> @executor.TestDispatcher {
self.dispatcher
}
///|
pub fn TestPlatform::window(
self : TestPlatform,
id : @platform.WindowId,
) -> TestWindow? {
self.windows.get(id)
}
///|
pub fn TestPlatform::windows(self : TestPlatform) -> Array[TestWindow] {
let out = []
for _, win in self.windows {
out.push(win)
}
out
}
///|
pub fn TestPlatform::cursor_style(self : TestPlatform) -> @style.CursorStyle {
self.active_cursor
}
///|
pub fn TestPlatform::opened_url(self : TestPlatform) -> String? {
self.opened_urls.last()
}
///|
pub fn TestPlatform::opened_urls(self : TestPlatform) -> Array[String] {
self.opened_urls
}
///|
pub fn TestPlatform::did_run(self : TestPlatform) -> Bool {
self.run_called
}
///|
pub fn TestPlatform::did_quit(self : TestPlatform) -> Bool {
self.quit_called
}
///|
/// Transfers active window status, notifying the previous and new windows on
/// the dispatcher (GPUI: `TestPlatform::set_active_window`).
pub fn TestPlatform::set_active_window(
self : TestPlatform,
window : TestWindow?,
) -> Unit {
let previous = self.active_window
self.active_window = window
let same = match (previous, window) {
(Some(prev), Some(curr)) => prev.id == curr.id
(None, None) => true
_ => false
}
if same {
return
}
@executor.Dispatcher::dispatch(self.dispatcher, fn() {
if previous is Some(prev) && !prev.closed {
prev.simulate_active_status_change(false)
}
if window is Some(win) && !win.closed {
win.simulate_active_status_change(true)
}
})
}
///|
/// Delivers one scheduled frame for every open window that currently has a
/// frame scheduled, returning how many frames ran.
pub fn TestPlatform::pump_scheduled_frames(self : TestPlatform) -> Int32 {
let open = []
for _, win in self.windows {
if !win.closed && win.frame_scheduled {
open.push(win)
}
}
let mut pumped : Int32 = 0
for win in open {
if win.simulate_scheduled_frame() {
pumped = pumped + 1
}
}
pumped
}
+646
View File
@@ -0,0 +1,646 @@
// 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
}
+498
View File
@@ -0,0 +1,498 @@
///|
fn px(v : Double) -> @geometry.Pixels {
@geometry.px(v)
}
///|
fn pt(x : Double, y : Double) -> @geometry.Point[@geometry.Pixels] {
@geometry.point(px(x), px(y))
}
///|
fn sz(w : Double, h : Double) -> @geometry.Size[@geometry.Pixels] {
@geometry.size(px(w), px(h))
}
///|
fn sp(v : Double) -> @geometry.ScaledPixels {
@geometry.ScaledPixels(v)
}
///|
/// A minimal element that paints a solid quad across its layout bounds and
/// optionally handles mouse events and hover.
priv struct BoxElement {
color : @color.Rgba
hover_color : @color.Rgba?
on_down : ((@input.MouseDownEvent, @window.Window, @app.App) -> Unit)?
on_scroll : ((@input.ScrollWheelEvent, @window.Window, @app.App) -> Unit)?
mut hitbox : @window.Hitbox?
}
///|
fn box_el(
color : @color.Rgba,
hover_color? : @color.Rgba,
on_down? : (@input.MouseDownEvent, @window.Window, @app.App) -> Unit,
on_scroll? : (@input.ScrollWheelEvent, @window.Window, @app.App) -> Unit,
) -> @window.AnyElement {
@window.AnyElement::new({
color,
hover_color,
on_down,
on_scroll,
hitbox: None,
})
}
///|
impl @window.Element for BoxElement with fn request_layout(_self, window, _app) {
window.request_layout(Default::default(), [])
}
///|
impl @window.Element for BoxElement with fn prepaint(self, bounds, window, _app) {
if self.hover_color is Some(_) ||
self.on_down is Some(_) ||
self.on_scroll is Some(_) {
self.hitbox = Some(window.insert_hitbox(bounds, Normal))
}
}
///|
impl @window.Element for BoxElement with fn paint(self, bounds, window, _app) {
let hovered = match self.hitbox {
Some(hitbox) => hitbox.is_hovered(window)
None => false
}
let bg = match (self.hover_color, hovered) {
(Some(hc), true) => hc
_ => self.color
}
if self.hitbox is Some(hitbox) {
if self.on_down is Some(cb) {
window.on_mouse_event(fn(event, phase, win, app) {
if phase == Bubble && event is MouseDown(down) && hitbox.is_hovered(win) {
cb(down, win, app)
}
})
}
if self.on_scroll is Some(cb) {
window.on_mouse_event(fn(event, phase, win, app) {
if phase == Bubble &&
event is ScrollWheel(scroll) &&
hitbox.is_hovered(win) {
cb(scroll, win, app)
}
})
}
}
window.paint_quad(@window.fill(bounds, bg.to_background()))
}
///|
struct TestView {
mut count : Int32
hover_bg : @color.Rgba?
prevent : Bool
stop : Bool
log : Array[String]
}
///|
impl @window.Render for TestView with fn render(self, _window, cx) {
box_el(
@color.rgb(0x112233),
hover_color?=self.hover_bg,
on_down=@window.listener(cx, fn(view, down, window, cx) {
view.count = view.count + 1
let btn = if down.button == Left { "Left" } else { "Other" }
view.log.push("down:\{btn}:\{down.modifiers.shift}")
if view.prevent {
window.prevent_default()
}
if view.stop {
window.stop_propagation()
}
cx.notify()
}),
on_scroll=@window.listener(cx, fn(view, scroll, _window, _cx) {
view.log.push("scroll:\{scroll.position.x.to_double()}")
}),
)
}
///|
enum PlainView {
PlainView
}
///|
impl @window.Render for PlainView with fn render(_self, _window, _cx) {
box_el(@color.rgb(0xabcdef))
}
///|
enum RunawayView {
RunawayView
}
///|
impl @window.Render for RunawayView with fn render(_self, window, _cx) {
window.refresh()
box_el(@color.rgb(0x000000))
}
///|
test "Application run, quit, and TestPlatform flags" {
let platform = @test.TestPlatform::new()
let app = @window.Application::with_platform(platform)
assert_false(platform.did_run())
assert_false(platform.did_quit())
let launched = Ref(false)
let found_app = Ref(false)
app.run(fn(a) {
launched.val = true
found_app.val = @window.application_for(a.app()) is Some(_)
a.quit()
})
assert_true(launched.val)
assert_true(found_app.val)
assert_true(platform.did_run())
assert_true(platform.did_quit())
}
///|
test "explicit frame scheduling on TestPlatform and TestWindow" {
let platform = @test.TestPlatform::new()
let app = @window.Application::with_platform(platform)
let view : Ref[@app.Entity[TestView]?] = Ref(None)
let unrelated : Ref[@app.Entity[PlainView]?] = Ref(None)
let handle = app.open_window(
@platform.WindowOptions::with_size(sz(300.0, 200.0), title="Manual"),
fn(_window, cx_app) {
unrelated.val = Some(cx_app.new_entity(fn(_) { PlainView }))
let v : @app.Entity[TestView] = cx_app.new_entity(fn(_) {
{ count: 0, hover_bg: None, prevent: false, stop: false, log: [], }
})
view.val = Some(v)
v
},
)
let tw = platform.window(handle.window_id()).unwrap()
assert_true(tw.frame_scheduled())
assert_eq(tw.draw_count(), 0)
assert_true(tw.simulate_scheduled_frame())
assert_false(tw.frame_scheduled())
assert_eq(tw.draw_count(), 1)
assert_false(tw.simulate_scheduled_frame())
// Unrelated entity notification must not schedule a frame.
unrelated.val.unwrap().update(app.app(), fn(_, cx) { cx.notify() })
assert_false(tw.frame_scheduled())
// Notifying rendered view re-schedules a frame.
view.val
.unwrap()
.update(app.app(), fn(v, cx) {
v.count = 1
cx.notify()
})
assert_true(tw.frame_scheduled())
assert_true(tw.simulate_scheduled_frame())
assert_eq(tw.draw_count(), 2)
}
///|
test "resize, scale factor, and programmatic Window::resize avoid recursion" {
let cx = @test.TestAppContext::new()
let vcx = cx.add_empty_window(size=sz(200.0, 100.0))
ignore(vcx.replace_root_view(fn(_, _) { PlainView }))
let before = vcx.draw_count()
vcx.simulate_resize(sz(320.0, 180.0))
assert_eq(vcx.draw_count(), before + 1)
assert_eq(vcx.bounds().size, sz(320.0, 180.0))
assert_eq(
vcx.scene().quads[0].bounds,
@geometry.bounds(
@geometry.point(sp(0.0), sp(0.0)),
@geometry.size(sp(320.0), sp(180.0)),
),
)
vcx.simulate_scale_factor_change(2.0)
assert_eq(vcx.scale_factor(), 2.0)
assert_eq(
vcx.scene().quads[0].bounds,
@geometry.bounds(
@geometry.point(sp(0.0), sp(0.0)),
@geometry.size(sp(640.0), sp(360.0)),
),
)
// Programmatic resize updates platform window bounds and schedules redraw without loop.
vcx.update(fn(win, _) { win.resize(sz(400.0, 200.0)) })
assert_eq(vcx.bounds().size, sz(400.0, 200.0))
assert_eq(vcx.scale_factor(), 2.0)
assert_eq(
vcx.scene().quads[0].bounds,
@geometry.bounds(
@geometry.point(sp(0.0), sp(0.0)),
@geometry.size(sp(800.0), sp(400.0)),
),
)
vcx.update(fn(win, _) { win.resize(sz(400.0, 200.0), scale_factor=1.5) })
assert_eq(vcx.window().scale_factor(), 1.5)
assert_eq(
vcx.scene().quads[0].bounds,
@geometry.bounds(
@geometry.point(sp(0.0), sp(0.0)),
@geometry.size(sp(600.0), sp(300.0)),
),
)
}
///|
test "title, cursor style, clipboard, and open_url" {
let cx = @test.TestAppContext::new()
let (_, vcx) = cx.add_window_view(size=sz(100.0, 100.0), title="Initial", fn(
_,
_,
) {
PlainView
})
assert_eq(vcx.window_title(), Some("Initial"))
vcx.update(fn(win, _) {
win.set_window_title("Updated")
win.set_cursor_style(PointingHand)
win.write_to_clipboard(
@platform.ClipboardItem::new_string_with_metadata("abc", "text"),
)
})
assert_eq(vcx.window_title(), Some("Updated"))
assert_eq(vcx.cursor_style(), @style.CursorStyle::PointingHand)
assert_eq(vcx.read_from_clipboard().unwrap().text(), Some("abc"))
assert_eq(cx.read_from_clipboard().unwrap().metadata(), Some("text"))
cx.application().open_url("https://example.com")
assert_eq(cx.opened_url(), Some("https://example.com"))
}
///|
test "multi-window activation and focus transfer" {
let cx = @test.TestAppContext::new()
let log : Array[String] = []
let (_, vcx1) = cx.add_window_view(size=sz(100.0, 100.0), fn(win, _) {
win.on_window_active_change(fn(w, _) {
log.push("w1:\{w.is_window_active()}")
})
PlainView
})
assert_true(vcx1.is_active())
assert_eq(cx.active_window(), Some(vcx1.window_handle()))
let (_, vcx2) = cx.add_window_view(size=sz(100.0, 100.0), fn(win, _) {
win.on_window_active_change(fn(w, _) {
log.push("w2:\{w.is_window_active()}")
})
PlainView
})
assert_false(vcx1.is_active())
assert_true(vcx2.is_active())
assert_eq(cx.active_window(), Some(vcx2.window_handle()))
assert_eq(log, ["w1:false"])
vcx1.update(fn(win, _) { win.activate_window() })
assert_true(vcx1.is_active())
assert_false(vcx2.is_active())
assert_eq(cx.active_window(), Some(vcx1.window_handle()))
assert_eq(log, ["w1:false", "w2:false", "w1:true"])
}
///|
test "MouseExited, hover status change, ModifiersChanged, and DispatchEventResult" {
let cx = @test.TestAppContext::new()
let (view, vcx) = cx.add_window_view(size=sz(100.0, 100.0), fn(_, _) {
{
count: 0,
hover_bg: Some(@color.rgb(0x00ff00)),
prevent: true,
stop: true,
log: [],
}
})
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x112233).to_background(),
)
ignore(vcx.simulate_mouse_move(pt(50.0, 50.0)))
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x00ff00).to_background(),
)
// MouseExited clears hovered hitboxes and marks window dirty -> redraws unhovered.
ignore(vcx.simulate_mouse_exit(position=pt(200.0, 200.0)))
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x112233).to_background(),
)
// Re-hover then lose window hover via platform hover status change.
ignore(vcx.simulate_mouse_move(pt(50.0, 50.0)))
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x00ff00).to_background(),
)
vcx.simulate_hover_status_change(false)
assert_false(vcx.is_hovered())
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x112233).to_background(),
)
// ModifiersChanged updates window modifiers and dirties/redraws on change.
let draws_before_mod = vcx.draw_count()
let shift_mods : @input.Modifiers = { ..Default::default(), shift: true, }
ignore(vcx.simulate_modifiers_change(shift_mods))
assert_eq(vcx.window().modifiers(), shift_mods)
assert_eq(vcx.draw_count(), draws_before_mod + 1)
// stop_propagation + prevent_default reflected in DispatchEventResult.
let result = vcx.simulate_mouse_down(pt(50.0, 50.0), modifiers=shift_mods)
assert_false(result.propagate)
assert_true(result.default_prevented)
assert_eq(cx.read_entity(view).log, ["down:Left:true"])
// ScrollWheel delivery.
ignore(
vcx.simulate_scroll_wheel(
pt(25.0, 25.0),
@input.ScrollDelta::Pixels(pt(0.0, -10.0)),
),
)
assert_eq(cx.read_entity(view).log, ["down:Left:true", "scroll:25"])
}
///|
test "window close veto, on_close, root release, and idempotence" {
let cx = @test.TestAppContext::new()
let can_close = Ref(false)
let events : Array[String] = []
let handle = cx.add_window(size=sz(100.0, 100.0), fn(win, ctx) {
ctx.on_release(fn(_, _) { events.push("released") }).detach()
win.on_should_close(fn(_, _) { can_close.val })
win.on_close(fn(_) { events.push("on_close") })
PlainView
})
let vcx = @test.VisualTestContext::from_window(handle.to_any(), cx)
assert_eq(cx.windows().length(), 1)
// Vetoed close leaves window open and view alive.
assert_false(vcx.simulate_close())
assert_false(vcx.is_closed())
assert_false(handle.entity().is_released())
assert_eq(events, [])
// Allowed close runs on_close, releases root view, removes window from Application.
can_close.val = true
assert_true(vcx.simulate_close())
assert_true(vcx.is_closed())
assert_eq(cx.windows().length(), 0)
assert_true(handle.entity().is_released())
assert_eq(events, ["on_close", "released"])
assert_eq(handle.update(cx.application(), fn(_, _, _) { 1 }), None)
// Idempotent repeat close calls do nothing extra.
vcx.test_window().simulate_close()
assert_eq(events, ["on_close", "released"])
}
///|
test "async task and virtual clock drive automatic redraws" {
let cx = @test.TestAppContext::new()
let (view, vcx) = cx.add_window_view(size=sz(120.0, 80.0), fn(_, ctx) {
let weak : @app.WeakEntity[TestView] = ctx.weak_entity()
ctx
.spawn(async fn(_, async_app) {
async_app.background_executor().sleep(50)
ignore(
weak.update(async_app.app, fn(v : TestView, cx) {
v.count = v.count + 10
cx.notify()
}),
)
})
.detach()
{ count: 1, hover_bg: None, prevent: false, stop: false, log: [], }
})
assert_eq(vcx.draw_count(), 1)
assert_eq(cx.read_entity(view).count, 1)
cx.advance_clock(50)
assert_eq(cx.read_entity(view).count, 11)
assert_eq(vcx.draw_count(), 2)
}
///|
test "replace_root_view and skip_drawing" {
let cx = @test.TestAppContext::new()
let released = Ref(false)
let (first, vcx) = cx.add_window_view(size=sz(100.0, 100.0), fn(_, ctx) {
ctx.on_release(fn(_, _) { released.val = true }).detach()
PlainView
})
assert_false(released.val)
let second = vcx.replace_root_view(fn(_, _) {
{ count: 42, hover_bg: None, prevent: false, stop: false, log: [], }
})
assert_true(first.is_released())
assert_true(released.val)
assert_eq(cx.read_entity(second).count, 42)
assert_eq(
vcx.scene().quads[0].background,
@color.rgb(0x112233).to_background(),
)
// skip_drawing prevents automatic frame pumping until re-enabled.
cx.skip_drawing()
let count_before = vcx.draw_count()
cx.update_entity(second, fn(v, ctx) {
v.count = 43
ctx.notify()
})
assert_eq(vcx.draw_count(), count_before)
assert_true(vcx.test_window().frame_scheduled())
cx.skip_drawing(skip=false)
cx.run_until_parked()
assert_eq(vcx.draw_count(), count_before + 1)
}
///|
test "panic on drawing a closed window" {
let cx = @test.TestAppContext::new()
let vcx = cx.add_empty_window(size=sz(50.0, 50.0))
let win = vcx.window()
win.close()
ignore(win.draw())
}
///|
test "panic on dispatching input on a closed window" {
let cx = @test.TestAppContext::new()
let vcx = cx.add_empty_window(size=sz(50.0, 50.0))
let win = vcx.window()
win.close()
ignore(
win.dispatch_input(
MouseMove({ ..Default::default(), position: pt(5.0, 5.0), }),
),
)
}
///|
test "panic on runaway redraw loop" {
let cx = @test.TestAppContext::new()
ignore(cx.add_window(size=sz(50.0, 50.0), fn(_, _) { RunawayView }))
}
+429
View File
@@ -0,0 +1,429 @@
// Headless test window, ported from gpui/src/platform/test/window.rs.
///|
/// A headless `PlatformWindow` for tests: records presented `Scene`s and
/// provides simulation hooks for frames, input, resizing, focus and closing
/// (GPUI: `TestWindow`).
pub struct TestWindow {
priv id : @platform.WindowId
priv platform : TestPlatform
priv mut bounds : @geometry.Bounds[@geometry.Pixels]
priv mut scale_factor : Double
priv mut title : String?
priv mut active : Bool
priv mut hovered : Bool
priv mut mouse_position : @geometry.Point[@geometry.Pixels]
priv mut modifiers : @input.Modifiers
priv mut closed : Bool
priv mut frame_scheduled : Bool
priv mut frame_schedule_count : Int32
priv drawn_scenes : Array[@scene.Scene]
priv mut request_frame_callback : (() -> Unit)?
priv mut input_callback : ((@input.PlatformInput) -> @input.DispatchEventResult)?
priv mut resize_callback : ((@geometry.Size[@geometry.Pixels], Double) -> Unit)?
priv mut active_status_change_callback : ((Bool) -> Unit)?
priv mut hover_status_change_callback : ((Bool) -> Unit)?
priv mut should_close_callback : (() -> Bool)?
priv mut close_callback : (() -> Unit)?
}
///|
fn TestWindow::new(
id : @platform.WindowId,
platform : TestPlatform,
params : @platform.WindowParams,
default_scale_factor : Double,
) -> TestWindow {
{
id,
platform,
bounds: params.bounds,
scale_factor: params.scale_factor.unwrap_or(default_scale_factor),
title: params.title,
active: params.focus,
hovered: false,
mouse_position: @geometry.point(@geometry.px(0.0), @geometry.px(0.0)),
modifiers: Default::default(),
closed: false,
frame_scheduled: false,
frame_schedule_count: 0,
drawn_scenes: [],
request_frame_callback: None,
input_callback: None,
resize_callback: None,
active_status_change_callback: None,
hover_status_change_callback: None,
should_close_callback: None,
close_callback: None,
}
}
///|
fn TestWindow::clear_callbacks(self : TestWindow) -> Unit {
self.request_frame_callback = None
self.input_callback = None
self.resize_callback = None
self.active_status_change_callback = None
self.hover_status_change_callback = None
self.should_close_callback = None
self.close_callback = None
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn bounds(self) {
self.bounds
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn content_size(self) {
self.bounds.size
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn scale_factor(self) {
self.scale_factor
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn mouse_position(self) {
self.mouse_position
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn modifiers(self) {
self.modifiers
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn is_active(self) {
self.active
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn is_hovered(self) {
self.hovered
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn set_title(self, title) {
self.title = Some(title)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn activate(self) {
if !self.closed {
self.platform.set_active_window(Some(self))
}
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn resize(self, size) {
self.bounds = { ..self.bounds, size, }
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn draw(self, scene) {
self.drawn_scenes.push(scene)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn schedule_frame(self) {
if !self.closed {
self.frame_scheduled = true
self.frame_schedule_count = self.frame_schedule_count + 1
}
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn close(self) {
if self.closed {
return
}
self.closed = true
self.frame_scheduled = false
self.clear_callbacks()
if self.platform.active_window is Some(active) && active.id == self.id {
self.platform.active_window = None
}
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_request_frame(
self,
callback,
) {
self.request_frame_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_input(
self,
callback,
) {
self.input_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_resize(
self,
callback,
) {
self.resize_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_active_status_change(
self,
callback,
) {
self.active_status_change_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_hover_status_change(
self,
callback,
) {
self.hover_status_change_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_should_close(
self,
callback,
) {
self.should_close_callback = Some(callback)
}
///|
pub impl @platform.PlatformWindow for TestWindow with fn on_close(
self,
callback,
) {
self.close_callback = Some(callback)
}
// ---------------------------------------------------------------------------
// Inspection helpers
// ---------------------------------------------------------------------------
///|
pub fn TestWindow::window_id(self : TestWindow) -> @platform.WindowId {
self.id
}
///|
pub fn TestWindow::title(self : TestWindow) -> String? {
self.title
}
///|
pub fn TestWindow::is_closed(self : TestWindow) -> Bool {
self.closed
}
///|
pub fn TestWindow::frame_scheduled(self : TestWindow) -> Bool {
self.frame_scheduled
}
///|
pub fn TestWindow::frame_schedule_count(self : TestWindow) -> Int32 {
self.frame_schedule_count
}
///|
pub fn TestWindow::drawn_scenes(self : TestWindow) -> Array[@scene.Scene] {
self.drawn_scenes
}
///|
pub fn TestWindow::draw_count(self : TestWindow) -> Int32 {
self.drawn_scenes.length()
}
///|
pub fn TestWindow::last_scene(self : TestWindow) -> @scene.Scene? {
self.drawn_scenes.last()
}
///|
pub fn TestWindow::clear_drawn_scenes(self : TestWindow) -> Unit {
self.drawn_scenes.clear()
}
// ---------------------------------------------------------------------------
// Simulation hooks
// ---------------------------------------------------------------------------
///|
/// Delivers one frame if one was scheduled; returns whether a frame ran
/// (GPUI: `TestWindow::simulate_scheduled_frame`).
pub fn TestWindow::simulate_scheduled_frame(self : TestWindow) -> Bool {
if self.closed || !self.frame_scheduled {
return false
}
self.frame_scheduled = false
match self.request_frame_callback {
Some(callback) => {
callback()
true
}
None => {
self.frame_scheduled = true
false
}
}
}
///|
/// Unconditionally invokes the registered frame callback, as a display-link
/// tick would (GPUI: `TestWindow::simulate_frame_request`).
pub fn TestWindow::simulate_frame_request(self : TestWindow) -> Unit {
if self.closed {
return
}
self.frame_scheduled = false
if self.request_frame_callback is Some(callback) {
callback()
}
}
///|
/// Simulates a `PlatformInput` event arriving from the OS/browser
/// (GPUI: `TestWindow::simulate_input`).
pub fn TestWindow::simulate_input(
self : TestWindow,
event : @input.PlatformInput,
) -> @input.DispatchEventResult {
if self.closed {
return { propagate: true, default_prevented: false, }
}
self.modifiers = event.modifiers()
match event {
MouseDown(e) => self.mouse_position = e.position
MouseUp(e) => self.mouse_position = e.position
MouseMove(e) => self.mouse_position = e.position
ScrollWheel(e) => self.mouse_position = e.position
MouseExited(e) => self.mouse_position = e.position
ModifiersChanged(_) => ()
}
match self.input_callback {
Some(callback) => callback(event)
None => { propagate: true, default_prevented: false, }
}
}
///|
/// Simulates a user/OS resize of the window (GPUI: `TestWindow::simulate_resize`).
pub fn TestWindow::simulate_resize(
self : TestWindow,
size : @geometry.Size[@geometry.Pixels],
) -> Unit {
if self.closed {
return
}
self.bounds = { ..self.bounds, size, }
if self.resize_callback is Some(callback) {
callback(size, self.scale_factor)
}
}
///|
/// Simulates a display scale-factor change
/// (GPUI: `TestWindow::simulate_scale_factor_change`).
pub fn TestWindow::simulate_scale_factor_change(
self : TestWindow,
scale_factor : Double,
) -> Unit {
if self.closed {
return
}
self.scale_factor = scale_factor
if self.resize_callback is Some(callback) {
callback(self.bounds.size, scale_factor)
}
}
///|
/// Simulates a window focus/activation change
/// (GPUI: `TestWindow::simulate_active_status_change`).
pub fn TestWindow::simulate_active_status_change(
self : TestWindow,
active : Bool,
) -> Unit {
if self.closed {
return
}
self.active = active
if active {
self.platform.active_window = Some(self)
} else if self.platform.active_window is Some(curr) && curr.id == self.id {
self.platform.active_window = None
}
if self.active_status_change_callback is Some(callback) {
callback(active)
}
}
///|
/// Simulates the pointer entering or leaving the window surface.
pub fn TestWindow::simulate_hover_status_change(
self : TestWindow,
hovered : Bool,
) -> Unit {
if self.closed {
return
}
self.hovered = hovered
if self.hover_status_change_callback is Some(callback) {
callback(hovered)
}
}
///|
/// Queries whether a close should proceed. Returns `true` if no handler is
/// registered.
pub fn TestWindow::simulate_should_close(self : TestWindow) -> Bool {
if self.closed {
return true
}
match self.should_close_callback {
Some(callback) => callback()
None => true
}
}
///|
/// Simulates the OS closing this window unconditionally and invoking the
/// registered `on_close` callback (GPUI: `TestWindow::simulate_close`).
pub fn TestWindow::simulate_close(self : TestWindow) -> Unit {
if self.closed {
return
}
let callback = self.close_callback
self.closed = true
self.frame_scheduled = false
self.clear_callbacks()
if self.platform.active_window is Some(curr) && curr.id == self.id {
self.platform.active_window = None
}
if callback is Some(callback) {
callback()
}
}
///|
/// Simulates a user-initiated close request: asks `on_should_close`, closes
/// if allowed, and returns whether the window closed.
pub fn TestWindow::simulate_close_requested(self : TestWindow) -> Bool {
if self.closed {
return false
}
if self.simulate_should_close() {
self.simulate_close()
true
} else {
false
}
}