Add a nodejs testing function for apply_layout

This will allow making the JS test code more similar to the C++/Rust code,
allowing replacing synthetic mouse clicks.
This commit is contained in:
Simon Hausmann 2021-04-08 16:51:51 +02:00
parent 8fb0c060d6
commit beb5d63d1b
4 changed files with 36 additions and 2 deletions

View file

@ -26,6 +26,16 @@ function load_native_lib() {
*/ */
let native = !process.env.SIXTYFPS_NODE_NATIVE_LIB ? require('../native/index.node') : load_native_lib(); let native = !process.env.SIXTYFPS_NODE_NATIVE_LIB ? require('../native/index.node') : load_native_lib();
/**
* @hidden
*/
interface Rect {
x: number,
y: number,
width: number,
height: number
}
/** /**
* @hidden * @hidden
*/ */
@ -55,6 +65,10 @@ class Component {
send_keyboard_string_sequence(s: String) { send_keyboard_string_sequence(s: String) {
this.comp.send_keyboard_string_sequence(s) this.comp.send_keyboard_string_sequence(s)
} }
apply_layout(rect: Rect) {
this.comp.apply_layout(rect)
}
} }
/** /**

View file

@ -11,7 +11,7 @@ use core::cell::RefCell;
use neon::prelude::*; use neon::prelude::*;
use rand::RngCore; use rand::RngCore;
use sixtyfps_compilerlib::langtype::Type; use sixtyfps_compilerlib::langtype::Type;
use sixtyfps_corelib::{ImageReference, SharedVector}; use sixtyfps_corelib::{graphics::Rect, ImageReference, SharedVector};
mod js_model; mod js_model;
mod persistent_context; mod persistent_context;
@ -478,6 +478,22 @@ declare_types! {
})?; })?;
Ok(JsUndefined::new().as_value(&mut cx)) Ok(JsUndefined::new().as_value(&mut cx))
} }
method apply_layout(mut cx) {
let rect = cx.argument::<JsObject>(0)?;
let x = rect.get(&mut cx, "x")?.downcast::<JsNumber>().unwrap().value();
let y = rect.get(&mut cx, "y")?.downcast::<JsNumber>().unwrap().value();
let width = rect.get(&mut cx, "width")?.downcast::<JsNumber>().unwrap().value();
let height = rect.get(&mut cx, "height")?.downcast::<JsNumber>().unwrap().value();
let this = cx.this();
let component = cx.borrow(&this, |x| x.0.as_ref().map(|c| c.clone_strong()));
let component = component.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
run_scoped(&mut cx,this.downcast().unwrap(), || {
sixtyfps_interpreter::testing::apply_layout(&component, Rect::new((x as f32, y as f32).into(), (width as f32, height as f32).into()));
Ok(())
})?;
Ok(JsUndefined::new().as_value(&mut cx))
}
} }
} }

View file

@ -868,6 +868,10 @@ pub mod testing {
&comp.inner.window(), &comp.inner.window(),
); );
} }
/// Applies the specified rectangular constraints to the component's layout.
pub fn apply_layout(comp: &super::ComponentInstance, rect: sixtyfps_corelib::graphics::Rect) {
comp.inner.borrow().as_ref().apply_layout(rect);
}
} }
#[test] #[test]

View file

@ -44,7 +44,7 @@ assert!(instance.get_fake_image_ok());
```js ```js
var instance = new sixtyfps.TestCase(); var instance = new sixtyfps.TestCase();
instance.send_mouse_click(5., 5.); instance.apply_layout({x: 0, y: 0, width: 300, height: 300});
assert(instance.fake_image_ok); assert(instance.fake_image_ok);
``` ```