Start fixing up platform tests

This commit is contained in:
Piotr Osiewicz 2023-08-22 00:30:52 +02:00
parent 7516e91a56
commit b4cba64fd6
10 changed files with 72 additions and 72 deletions

1
Cargo.lock generated
View file

@ -2270,6 +2270,7 @@ dependencies = [
"fuzzy",
"git",
"gpui",
"gpui_mac",
"indoc",
"itertools",
"language",

View file

@ -18,7 +18,8 @@ test-support = [
"util/test-support",
"workspace/test-support",
"tree-sitter-rust",
"tree-sitter-typescript"
"tree-sitter-typescript",
"gpui_mac"
]
[dependencies]
@ -44,6 +45,7 @@ theme = { path = "../theme" }
util = { path = "../util" }
sqlez = { path = "../sqlez" }
workspace = { path = "../workspace" }
gpui_mac = { path = "../gpui_mac", optional = true}
aho-corasick = "0.7"
anyhow.workspace = true
@ -79,6 +81,7 @@ project = { path = "../project", features = ["test-support"] }
settings = { path = "../settings", features = ["test-support"] }
workspace = { path = "../workspace", features = ["test-support"] }
ctor.workspace = true
env_logger.workspace = true
rand.workspace = true

View file

@ -278,48 +278,3 @@ impl DerefMut for LineWrapperHandle {
self.wrapper.as_mut().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
fonts::{Style, Weight},
platform::{test, Platform as _},
};
#[test]
fn test_select_font() {
let platform = test::platform();
let fonts = FontCache::new(platform.fonts());
let arial = fonts
.load_family(
&["Arial"],
&Features {
calt: Some(false),
..Default::default()
},
)
.unwrap();
let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap();
let arial_italic = fonts
.select_font(arial, Properties::new().style(Style::Italic))
.unwrap();
let arial_bold = fonts
.select_font(arial, Properties::new().weight(Weight::BOLD))
.unwrap();
assert_ne!(arial_regular, arial_italic);
assert_ne!(arial_regular, arial_bold);
assert_ne!(arial_italic, arial_bold);
let arial_with_calt = fonts
.load_family(
&["Arial"],
&Features {
calt: Some(true),
..Default::default()
},
)
.unwrap();
assert_ne!(arial_with_calt, arial);
}
}

View file

@ -27,6 +27,8 @@ pub mod platform;
pub use gpui_macros::{test, Element};
pub use window::{Axis, RectFExt, SizeConstraint, Vector2FExt, WindowContext};
#[cfg(any(test, feature="test-support"))]
pub mod test;
pub use anyhow;
pub use serde_json;

View file

@ -1,4 +1,6 @@
mod event;
#[cfg(any(test, feature = "test-support"))]
pub(super) mod test;
use crate::{
executor,

View file

@ -1,7 +1,7 @@
use super::{AppVersion, CursorStyle, WindowBounds};
use anyhow::{anyhow, Result};
use collections::VecDeque;
use gpui::{
use crate::{
geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
@ -64,7 +64,7 @@ impl super::ForegroundPlatform for ForegroundPlatform {
fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
fn on_quit(&self, _: Box<dyn FnMut()>) {}
fn on_reopen(&self, _: Box<dyn FnMut()>) {}
fn on_event(&self, _: Box<dyn FnMut(gpui::platform::Event) -> bool>) {}
fn on_event(&self, _: Box<dyn FnMut(super::Event) -> bool>) {}
fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
@ -93,10 +93,6 @@ impl super::ForegroundPlatform for ForegroundPlatform {
fn reveal_path(&self, _: &Path) {}
}
pub fn platform() -> Platform {
Platform::new()
}
pub struct Platform {
dispatcher: Arc<dyn super::Dispatcher>,
fonts: Arc<dyn super::FontSystem>,
@ -106,10 +102,10 @@ pub struct Platform {
}
impl Platform {
fn new() -> Self {
pub fn new(fonts: Arc<dyn super::FontSystem>) -> Self {
Self {
dispatcher: Arc::new(Dispatcher),
fonts: Arc::new(super::current::FontSystem::new()),
fonts,
current_clipboard_item: Default::default(),
cursor: Mutex::new(CursorStyle::Arrow),
active_window: Default::default(),
@ -136,11 +132,11 @@ impl super::Platform for Platform {
fn quit(&self) {}
fn screen_by_id(&self, _id: uuid::Uuid) -> Option<Rc<dyn gpui::platform::Screen>> {
fn screen_by_id(&self, _id: uuid::Uuid) -> Option<Rc<dyn super::Screen>> {
None
}
fn screens(&self) -> Vec<Rc<dyn gpui::platform::Screen>> {
fn screens(&self) -> Vec<Rc<dyn super::Screen>> {
Default::default()
}
@ -165,7 +161,7 @@ impl super::Platform for Platform {
self.active_window.lock().clone()
}
fn add_status_item(&self, handle: AnyWindowHandle) -> Box<dyn gpui::platform::Window> {
fn add_status_item(&self, handle: AnyWindowHandle) -> Box<dyn super::Window> {
Box::new(Window::new(
handle,
vec2f(24., 24.),
@ -263,7 +259,7 @@ pub struct Window {
handle: AnyWindowHandle,
pub(crate) size: Vector2F,
scale_factor: f32,
current_scene: Option<gpui::Scene>,
current_scene: Option<crate::Scene>,
event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
pub(crate) resize_handlers: Vec<Box<dyn FnMut()>>,
pub(crate) moved_handlers: Vec<Box<dyn FnMut()>>,
@ -324,11 +320,11 @@ impl super::Window for Window {
24.
}
fn appearance(&self) -> gpui::platform::Appearance {
gpui::platform::Appearance::Light
fn appearance(&self) -> super::Appearance {
super::Appearance::Light
}
fn screen(&self) -> Rc<dyn gpui::platform::Screen> {
fn screen(&self) -> Rc<dyn super::Screen> {
Rc::new(Screen)
}
@ -336,11 +332,11 @@ impl super::Window for Window {
self
}
fn set_input_handler(&mut self, _: Box<dyn gpui::platform::InputHandler>) {}
fn set_input_handler(&mut self, _: Box<dyn super::InputHandler>) {}
fn prompt(
&self,
_: gpui::platform::PromptLevel,
_: super::PromptLevel,
_: &str,
_: &[&str],
) -> oneshot::Receiver<usize> {
@ -367,13 +363,13 @@ impl super::Window for Window {
fn zoom(&self) {}
fn present_scene(&mut self, scene: gpui::Scene) {
fn present_scene(&mut self, scene: crate::Scene) {
self.current_scene = Some(scene);
}
fn toggle_full_screen(&self) {}
fn on_event(&mut self, callback: Box<dyn FnMut(gpui::platform::Event) -> bool>) {
fn on_event(&mut self, callback: Box<dyn FnMut(super::Event) -> bool>) {
self.event_handlers.push(callback);
}

View file

@ -35,6 +35,7 @@ fn init_logger() {
// static ALLOC: dhat::Alloc = dhat::Alloc;
pub fn run_test(
fonts: fn() -> std::sync::Arc<dyn crate::platform::FontSystem>,
mut num_iterations: u64,
mut starting_seed: u64,
max_retries: usize,
@ -66,10 +67,10 @@ pub fn run_test(
loop {
let result = panic::catch_unwind(|| {
let fonts = fonts();
let foreground_platform = Rc::new(platform::test::foreground_platform());
let platform = Arc::new(platform::test::platform());
let font_system = platform.fonts();
let font_cache = Arc::new(FontCache::new(font_system));
let platform = Arc::new(platform::test::Platform::new(fonts.clone()));
let font_cache = Arc::new(FontCache::new(fonts.clone()));
let mut prev_runnable_history: Option<Vec<ExecutorEvent>> = None;
for _ in 0..num_iterations {

View file

@ -668,4 +668,39 @@ mod tests {
// There's no glyph for \u{feff}
assert_eq!(layout.runs[0].glyphs[1].id, 69); // b
}
#[test]
fn test_select_font() {
let platform = test::platform(FontSystem::new());
let fonts = FontCache::new(platform.fonts());
let arial = fonts
.load_family(
&["Arial"],
&Features {
calt: Some(false),
..Default::default()
},
)
.unwrap();
let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap();
let arial_italic = fonts
.select_font(arial, Properties::new().style(Style::Italic))
.unwrap();
let arial_bold = fonts
.select_font(arial, Properties::new().weight(Weight::BOLD))
.unwrap();
assert_ne!(arial_regular, arial_italic);
assert_ne!(arial_regular, arial_bold);
assert_ne!(arial_italic, arial_bold);
let arial_with_calt = fonts
.load_family(
&["Arial"],
&Features {
calt: Some(true),
..Default::default()
},
)
.unwrap();
assert_ne!(arial_with_calt, arial);
}
}

View file

@ -10,8 +10,7 @@ mod renderer;
mod screen;
mod sprite_cache;
mod status_item;
#[cfg(test)]
mod test;
mod window;
use cocoa::{
@ -104,3 +103,9 @@ unsafe impl objc::Encode for NSRange {
unsafe fn ns_string(string: &str) -> id {
NSString::alloc(nil).init_str(string).autorelease()
}
#[doc(hidden)]
#[allow(dead_code)]
pub fn font_system() -> Arc<dyn gpui::platform::FontSystem> {
Arc::new(FontSystem::new())
}

View file

@ -157,8 +157,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
#[test]
fn #outer_fn_name() {
#inner_fn
#namespace::test::run_test(
gpui_mac::font_system,
#num_iterations as u64,
#starting_seed as u64,
#max_retries,
@ -234,8 +234,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
#[test]
fn #outer_fn_name() {
#inner_fn
#namespace::test::run_test(
gpui_mac::font_system,
#num_iterations as u64,
#starting_seed as u64,
#max_retries,