use crate::{
fyrox::{
core::{algebra::Vector2, info, pool::Handle, Uuid},
engine::ApplicationLoopController,
gui::{test::UserInterfaceTestingExtension, Control, UiNode},
},
plugin::EditorPlugin,
settings::Settings,
test::macros::Macro,
Editor, StartupData,
};
use std::path::PathBuf;
pub fn run_editor_test(name: &str, plugin: impl EditorPlugin) {
let path = PathBuf::from(format!("./AutomatedTests/{name}TestData"));
if path.exists() {
std::fs::remove_dir_all(&path).unwrap();
}
std::fs::create_dir_all(&path).unwrap();
let path = path.canonicalize().unwrap();
info!("Running {name}. Working dir: {}", path.display());
let mut settings = Settings::default();
settings.windows.window_position = Default::default();
settings.windows.window_size = Vector2::repeat(1000.0);
let mut editor = Editor::new_with_settings(
Some(StartupData {
working_directory: path.clone(),
scenes: vec![],
named_objects: false,
}),
settings,
);
editor.add_editor_plugin(plugin);
editor.run_headless()
}
pub trait EditorTestingExtension {
fn click(&mut self, position: Vector2<f32>);
fn click_at(&mut self, name: Uuid) -> Handle<UiNode>;
fn click_at_text(&mut self, uuid: Uuid, text: &str);
fn find(&self, uuid: Uuid) -> Option<&UiNode>;
fn find_of<T: Control>(&self, uuid: Uuid) -> Option<&T>;
fn is_visible(&self, uuid: Uuid) -> bool {
if let Some(node) = self.find(uuid) {
node.is_globally_visible()
} else {
panic!("Widget {uuid} does not exist!")
}
}
}
impl EditorTestingExtension for Editor {
fn click(&mut self, position: Vector2<f32>) {
self.engine.user_interfaces.first_mut().click(position)
}
fn click_at(&mut self, uuid: Uuid) -> Handle<UiNode> {
self.engine.user_interfaces.first_mut().click_at(uuid)
}
fn click_at_text(&mut self, uuid: Uuid, text: &str) {
self.engine
.user_interfaces
.first_mut()
.click_at_text(uuid, text)
}
fn find(&self, uuid: Uuid) -> Option<&UiNode> {
self.engine.user_interfaces.first().find_by_uuid(uuid)
}
fn find_of<T: Control>(&self, uuid: Uuid) -> Option<&T> {
self.engine.user_interfaces.first().find_by_uuid_of(uuid)
}
}
pub struct TestPlugin {
test_macro: Macro,
}
impl TestPlugin {
pub fn new(test_macro: Macro) -> Self {
Self { test_macro }
}
}
impl EditorPlugin for TestPlugin {
fn on_post_update(&mut self, editor: &mut Editor, loop_controller: ApplicationLoopController) {
self.test_macro.execute_next(editor, loop_controller)
}
}