dioxus/packages/desktop/headless_tests/rendering.rs

97 lines
2.9 KiB
Rust
Raw Normal View History

2023-05-04 16:04:06 +00:00
use dioxus::prelude::*;
2024-01-18 12:07:28 +00:00
use dioxus_core::Element;
2023-05-04 16:04:06 +00:00
use dioxus_desktop::DesktopContext;
2024-01-18 12:07:28 +00:00
fn main() {
check_app_exits(check_html_renders);
}
2023-05-04 16:04:06 +00:00
pub(crate) fn check_app_exits(app: Component) {
2023-05-05 16:11:50 +00:00
use dioxus_desktop::Config;
2024-01-05 00:48:53 +00:00
use tao::window::WindowBuilder;
2023-05-04 16:04:06 +00:00
// This is a deadman's switch to ensure that the app exits
let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
let should_panic_clone = should_panic.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(100));
if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
std::process::exit(exitcode::SOFTWARE);
}
});
2023-05-05 16:11:50 +00:00
dioxus_desktop::launch_cfg(
app,
2023-12-17 17:30:17 +00:00
Config::new().with_window(WindowBuilder::new().with_visible(true)),
2023-05-05 16:11:50 +00:00
);
2023-05-04 16:04:06 +00:00
should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
}
fn use_inner_html(d: &'static str) -> Option<String> {
2024-01-14 05:12:21 +00:00
let value: Signal<Option<String>> = use_signal(|| None);
2024-01-18 12:07:28 +00:00
use_effect(|| async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
window().eval();
let html = eval_provider(&format!(
r#"let element = document.getElementById('{}');
return element.innerHTML"#,
2024-01-18 12:07:28 +00:00
id
))
.unwrap();
if let Ok(serde_json::Value::String(html)) = html.await {
println!("html: {}", html);
value.set(Some(html));
2023-05-04 16:04:06 +00:00
}
});
value.read().clone()
}
const EXPECTED_HTML: &str = r#"<div id="5" style="width: 100px; height: 100px; color: rgb(0, 0, 0);"><input type="checkbox"><h1>text</h1><div><p>hello world</p></div></div>"#;
fn check_html_renders() -> Element {
let inner_html = use_inner_html("main_div");
2023-05-04 16:04:06 +00:00
let desktop_context: DesktopContext = cx.consume_context().unwrap();
if let Some(raw_html) = inner_html {
println!("{}", raw_html);
2024-01-18 12:07:28 +00:00
let fragment = &raw_html;
let expected = EXPECTED_HTML;
// let fragment = scraper::Html::parse_fragment(&raw_html);
// println!("fragment: {}", fragment.html());
// let expected = scraper::Html::parse_fragment(EXPECTED_HTML);
// println!("expected: {}", expected.html());
assert_eq!(raw_html, EXPECTED_HTML);
2023-05-04 16:04:06 +00:00
if fragment == expected {
println!("html matches");
desktop_context.close();
}
}
let dyn_value = 0;
let dyn_element = rsx! {
div {
dangerous_inner_html: "<p>hello world</p>",
}
};
2024-01-16 19:18:46 +00:00
rsx! {
2023-05-04 16:04:06 +00:00
div {
id: "main_div",
div {
width: "100px",
height: "100px",
color: "rgb({dyn_value}, {dyn_value}, {dyn_value})",
id: 5,
input {
"type": "checkbox",
},
h1 {
"text"
}
{dyn_element}
2023-05-04 16:04:06 +00:00
}
}
}
}