Move settings::test helper into zed::test

This commit is contained in:
Antonio Scandurra 2021-10-05 11:00:33 +02:00
parent 5105596918
commit 0022c6b828
3 changed files with 45 additions and 42 deletions

View file

@ -31,7 +31,8 @@ fn main() {
app.platform().fonts().add_fonts(&embedded_fonts).unwrap(); app.platform().fonts().add_fonts(&embedded_fonts).unwrap();
let themes = settings::ThemeRegistry::new(Assets, app.font_cache()); let themes = settings::ThemeRegistry::new(Assets, app.font_cache());
let (settings_tx, settings) = settings::channel(&app.font_cache(), &themes).unwrap(); let (settings_tx, settings) =
settings::channel("Inconsolata", &app.font_cache(), &themes).unwrap();
let languages = Arc::new(language::build_language_registry()); let languages = Arc::new(language::build_language_registry());
languages.set_theme(&settings.borrow().theme.editor.syntax); languages.set_theme(&settings.borrow().theme.editor.syntax);

View file

@ -14,39 +14,13 @@ pub struct Settings {
} }
impl Settings { impl Settings {
#[cfg(any(test, feature = "test-support"))] pub fn new(
pub fn test(cx: &gpui::AppContext) -> Self { buffer_font_family: &str,
use crate::assets::Assets; font_cache: &FontCache,
use gpui::AssetSource; theme: Arc<Theme>,
) -> Result<Self> {
lazy_static::lazy_static! {
static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
static ref FONTS: Vec<Arc<Vec<u8>>> = Assets
.list("fonts")
.into_iter()
.map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
.collect();
}
cx.platform().fonts().add_fonts(&FONTS).unwrap();
let mut theme_guard = DEFAULT_THEME.lock();
let theme = if let Some(theme) = theme_guard.as_ref() {
theme.clone()
} else {
let theme = ThemeRegistry::new(Assets, cx.font_cache().clone())
.get(DEFAULT_THEME_NAME)
.expect("failed to load default theme in tests");
*theme_guard = Some(theme.clone());
theme
};
Self::new(cx.font_cache(), theme).unwrap()
}
pub fn new(font_cache: &FontCache, theme: Arc<Theme>) -> Result<Self> {
Ok(Self { Ok(Self {
buffer_font_family: font_cache.load_family(&["Inconsolata"])?, buffer_font_family: font_cache.load_family(&[buffer_font_family])?,
buffer_font_size: 16., buffer_font_size: 16.,
tab_size: 4, tab_size: 4,
theme, theme,
@ -59,12 +33,8 @@ impl Settings {
} }
} }
#[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &gpui::AppContext) -> (watch::Sender<Settings>, watch::Receiver<Settings>) {
watch::channel_with(Settings::test(cx))
}
pub fn channel( pub fn channel(
buffer_font_family: &str,
font_cache: &FontCache, font_cache: &FontCache,
themes: &ThemeRegistry, themes: &ThemeRegistry,
) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> { ) -> Result<(watch::Sender<Settings>, watch::Receiver<Settings>)> {
@ -74,5 +44,9 @@ pub fn channel(
panic!("failed to deserialize default theme: {:?}", err) panic!("failed to deserialize default theme: {:?}", err)
} }
}; };
Ok(watch::channel_with(Settings::new(font_cache, theme)?)) Ok(watch::channel_with(Settings::new(
buffer_font_family,
font_cache,
theme,
)?))
} }

View file

@ -1,13 +1,15 @@
use crate::{ use crate::{
assets::Assets, assets::Assets,
language, language,
settings::{self, ThemeRegistry}, settings::Settings,
theme::{Theme, ThemeRegistry, DEFAULT_THEME_NAME},
AppState, AppState,
}; };
use buffer::LanguageRegistry; use buffer::LanguageRegistry;
use client::{http::ServerResponse, test::FakeHttpClient, ChannelList, Client, UserStore}; use client::{http::ServerResponse, test::FakeHttpClient, ChannelList, Client, UserStore};
use gpui::MutableAppContext; use gpui::{AssetSource, MutableAppContext};
use parking_lot::Mutex; use parking_lot::Mutex;
use postage::watch;
use project::fs::FakeFs; use project::fs::FakeFs;
use std::sync::Arc; use std::sync::Arc;
@ -18,7 +20,7 @@ fn init_logger() {
} }
pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> { pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
let (settings_tx, settings) = settings::test(cx); let (settings_tx, settings) = watch::channel_with(build_settings(cx));
let mut languages = LanguageRegistry::new(); let mut languages = LanguageRegistry::new();
languages.add(Arc::new(language::rust())); languages.add(Arc::new(language::rust()));
let themes = ThemeRegistry::new(Assets, cx.font_cache().clone()); let themes = ThemeRegistry::new(Assets, cx.font_cache().clone());
@ -36,3 +38,29 @@ pub fn test_app_state(cx: &mut MutableAppContext) -> Arc<AppState> {
fs: Arc::new(FakeFs::new()), fs: Arc::new(FakeFs::new()),
}) })
} }
fn build_settings(cx: &gpui::AppContext) -> Settings {
lazy_static::lazy_static! {
static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
static ref FONTS: Vec<Arc<Vec<u8>>> = Assets
.list("fonts")
.into_iter()
.map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
.collect();
}
cx.platform().fonts().add_fonts(&FONTS).unwrap();
let mut theme_guard = DEFAULT_THEME.lock();
let theme = if let Some(theme) = theme_guard.as_ref() {
theme.clone()
} else {
let theme = ThemeRegistry::new(Assets, cx.font_cache().clone())
.get(DEFAULT_THEME_NAME)
.expect("failed to load default theme in tests");
*theme_guard = Some(theme.clone());
theme
};
Settings::new("Inconsolata", cx.font_cache(), theme).unwrap()
}