Skip to content

Commit

Permalink
feat: Allow passing state context in freya-testing (#981)
Browse files Browse the repository at this point in the history
* feat: Allow passing state context in freya-testing

* fix

* fix core tests
  • Loading branch information
marc2332 authored Oct 13, 2024
1 parent 9388a40 commit 1c9d579
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/render/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ mod test {
use itertools::sorted;

fn run_compositor(
utils: &TestingHandler,
utils: &TestingHandler<()>,
compositor: &mut Compositor,
) -> (Layers, Layers, usize) {
let sdom = utils.sdom();
Expand Down
6 changes: 3 additions & 3 deletions crates/hooks/src/use_init_native_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mod test {

let mut utils = launch_test_with_config(
use_focus_app,
TestingConfig {
TestingConfig::<()> {
size: (100.0, 100.0).into(),
..TestingConfig::default()
},
Expand Down Expand Up @@ -173,7 +173,7 @@ mod test {

let mut utils = launch_test_with_config(
use_focus_app,
TestingConfig {
TestingConfig::<()> {
size: (100.0, 100.0).into(),
..TestingConfig::default()
},
Expand Down Expand Up @@ -233,7 +233,7 @@ mod test {

let mut utils = launch_test_with_config(
use_focus_app,
TestingConfig {
TestingConfig::<()> {
size: (100.0, 100.0).into(),
..TestingConfig::default()
},
Expand Down
2 changes: 1 addition & 1 deletion crates/hooks/src/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod test {

let mut utils = launch_test_with_config(
use_node_app,
TestingConfig {
TestingConfig::<()> {
size: (500.0, 800.0).into(),
..TestingConfig::default()
},
Expand Down
4 changes: 2 additions & 2 deletions crates/hooks/tests/use_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn track_focus() {

let mut utils = launch_test_with_config(
use_focus_app,
TestingConfig {
TestingConfig::<()> {
size: (100.0, 100.0).into(),
..TestingConfig::default()
},
Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn block_focus() {

let mut utils = launch_test_with_config(
use_focus_app,
TestingConfig {
TestingConfig::<()> {
size: (100.0, 100.0).into(),
..TestingConfig::default()
},
Expand Down
2 changes: 1 addition & 1 deletion crates/hooks/tests/use_platform_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn window_size() {

let mut utils = launch_test_with_config(
use_animation_app,
TestingConfig {
TestingConfig::<()> {
size: (333.0, 190.0).into(),
..TestingConfig::default()
},
Expand Down
10 changes: 6 additions & 4 deletions crates/testing/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ use std::time::Duration;
use torin::geometry::Size2D;

/// Configuration for [`crate::test_handler::TestingHandler`].
#[derive(Clone, Copy)]
pub struct TestingConfig {
#[derive(Clone)]
pub struct TestingConfig<T: 'static + Clone> {
pub vdom_timeout: Duration,
pub size: Size2D,
pub event_loop_ticker: bool,
pub state: Option<T>,
}

impl Default for TestingConfig {
impl<T: 'static + Clone> Default for TestingConfig<T> {
fn default() -> Self {
Self {
vdom_timeout: Duration::from_millis(16),
size: Size2D::from((500.0, 500.0)),
event_loop_ticker: true,
state: None,
}
}
}

impl TestingConfig {
impl<T: 'static + Clone> TestingConfig<T> {
pub fn new() -> Self {
TestingConfig::default()
}
Expand Down
7 changes: 5 additions & 2 deletions crates/testing/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ use crate::{
/// Run a Component in a headless testing environment.
///
/// Default size is `500x500`.
pub fn launch_test(root: AppComponent) -> TestingHandler {
pub fn launch_test(root: AppComponent) -> TestingHandler<()> {
launch_test_with_config(root, TestingConfig::default())
}

/// Run a Component in a headless testing environment
pub fn launch_test_with_config(root: AppComponent, config: TestingConfig) -> TestingHandler {
pub fn launch_test_with_config<T: 'static + Clone>(
root: AppComponent,
config: TestingConfig<T>,
) -> TestingHandler<T> {
let vdom = with_accessibility(root);
let fdom = FreyaDOM::default();
let sdom = SafeDOM::new(fdom);
Expand Down
2 changes: 1 addition & 1 deletion crates/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
//!
//! let mut utils = launch_test_with_config(
//! our_component,
//! TestingConfig {
//! TestingConfig::<()> {
//! size: (500.0, 800.0).into(),
//! ..TestingConfig::default()
//! },
Expand Down
12 changes: 8 additions & 4 deletions crates/testing/src/test_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::{
};

/// Manages the lifecycle of your tests.
pub struct TestingHandler {
pub struct TestingHandler<T: 'static + Clone> {
pub(crate) vdom: VirtualDom,
pub(crate) utils: TestUtils,
pub(crate) event_emitter: EventEmitter,
Expand All @@ -68,12 +68,12 @@ pub struct TestingHandler {
pub(crate) font_collection: FontCollection,
pub(crate) font_mgr: FontMgr,
pub(crate) accessibility_tree: SharedAccessibilityTree,
pub(crate) config: TestingConfig,
pub(crate) config: TestingConfig<T>,
pub(crate) ticker_sender: broadcast::Sender<()>,
pub(crate) cursor_icon: CursorIcon,
}

impl TestingHandler {
impl<T: 'static + Clone> TestingHandler<T> {
/// Init the DOM.
pub(crate) fn init_dom(&mut self) {
self.provide_vdom_contexts();
Expand All @@ -83,7 +83,7 @@ impl TestingHandler {
}

/// Get a mutable reference to the current [`TestingConfig`].
pub fn config(&mut self) -> &mut TestingConfig {
pub fn config(&mut self) -> &mut TestingConfig<T> {
&mut self.config
}

Expand All @@ -102,6 +102,10 @@ impl TestingHandler {
};
self.vdom
.insert_any_root_context(Box::new(accessibility_generator));

if let Some(state) = self.config.state.clone() {
self.vdom.insert_any_root_context(Box::new(state));
}
}

/// Wait and apply new changes
Expand Down

0 comments on commit 1c9d579

Please sign in to comment.