123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- use std::collections::HashMap;
- use dioxus::html::geometry::euclid::Vector3D;
- use dioxus::prelude::*;
- use dioxus_desktop::DesktopContext;
- #[path = "./utils.rs"]
- mod utils;
- pub fn main() {
- #[cfg(not(windows))]
- utils::check_app_exits(app);
- }
- static RECEIVED_EVENTS: GlobalSignal<usize> = Signal::global(|| 0);
- fn app() -> Element {
- let desktop_context: DesktopContext = consume_context();
- let received = RECEIVED_EVENTS();
- let expected = utils::EXPECTED_EVENTS();
- use_memo(move || {
- println!("expecting {} events", utils::EXPECTED_EVENTS());
- println!("received {} events", RECEIVED_EVENTS());
- });
- if expected != 0 && received == expected {
- println!("all events received");
- desktop_context.close();
- }
- rsx! {
- div {
- test_mounted {}
- test_button {}
- test_mouse_move_div {}
- test_mouse_click_div {}
- test_mouse_dblclick_div {}
- test_mouse_down_div {}
- test_mouse_up_div {}
- test_mouse_scroll_div {}
- test_key_down_div {}
- test_key_up_div {}
- test_key_press_div {}
- test_focus_in_div {}
- test_focus_out_div {}
- test_form_input {}
- test_form_submit {}
- test_select_multiple_options {}
- test_unicode {}
- }
- }
- }
- fn test_mounted() -> Element {
- use_hook(|| utils::EXPECTED_EVENTS.with_mut(|x| *x += 1));
- let mut onmounted_triggered = use_signal(|| false);
- rsx! {
- div {
- width: "100px",
- height: "100px",
- onmounted: move |evt| async move {
- let rect = evt.get_client_rect().await.unwrap();
- println!("rect: {:?}", rect);
- assert_eq!(rect.width(), 100.0);
- assert_eq!(rect.height(), 100.0);
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- // Onmounted should only be called once
- let mut onmounted_triggered_write = onmounted_triggered.write();
- assert!(!*onmounted_triggered_write);
- *onmounted_triggered_write = true;
- }
- }
- }
- }
- fn test_button() -> Element {
- utils::mock_event(
- "button",
- r#"new MouseEvent("click", {
- view: window,
- bubbles: true,
- cancelable: true,
- button: 0,
- })"#,
- );
- rsx! {
- button {
- id: "button",
- onclick: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(event.data.held_buttons().is_empty());
- assert_eq!(
- event.data.trigger_button(),
- Some(dioxus_html::input_data::MouseButton::Primary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_move_div() -> Element {
- utils::mock_event(
- "mouse_move_div",
- r#"new MouseEvent("mousemove", {
- view: window,
- bubbles: true,
- cancelable: true,
- buttons: 2,
- })"#,
- );
- rsx! {
- div {
- id: "mouse_move_div",
- onmousemove: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(
- event
- .data
- .held_buttons()
- .contains(dioxus_html::input_data::MouseButton::Secondary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_click_div() -> Element {
- utils::mock_event(
- "mouse_click_div",
- r#"new MouseEvent("click", {
- view: window,
- bubbles: true,
- cancelable: true,
- buttons: 2,
- button: 2,
- })"#,
- );
- rsx! {
- div {
- id: "mouse_click_div",
- onclick: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(
- event
- .data
- .held_buttons()
- .contains(dioxus_html::input_data::MouseButton::Secondary),
- );
- assert_eq!(
- event.data.trigger_button(),
- Some(dioxus_html::input_data::MouseButton::Secondary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_dblclick_div() -> Element {
- utils::mock_event(
- "mouse_dblclick_div",
- r#"new MouseEvent("dblclick", {
- view: window,
- bubbles: true,
- cancelable: true,
- buttons: 1|2,
- button: 2,
- })"#,
- );
- rsx! {
- div {
- id: "mouse_dblclick_div",
- ondoubleclick: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(
- event
- .data
- .held_buttons()
- .contains(dioxus_html::input_data::MouseButton::Primary),
- );
- assert!(
- event
- .data
- .held_buttons()
- .contains(dioxus_html::input_data::MouseButton::Secondary),
- );
- assert_eq!(
- event.data.trigger_button(),
- Some(dioxus_html::input_data::MouseButton::Secondary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_down_div() -> Element {
- utils::mock_event(
- "mouse_down_div",
- r#"new MouseEvent("mousedown", {
- view: window,
- bubbles: true,
- cancelable: true,
- buttons: 2,
- button: 2,
- })"#,
- );
- rsx! {
- div {
- id: "mouse_down_div",
- onmousedown: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(
- event
- .data
- .held_buttons()
- .contains(dioxus_html::input_data::MouseButton::Secondary),
- );
- assert_eq!(
- event.data.trigger_button(),
- Some(dioxus_html::input_data::MouseButton::Secondary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_up_div() -> Element {
- utils::mock_event(
- "mouse_up_div",
- r#"new MouseEvent("mouseup", {
- view: window,
- bubbles: true,
- cancelable: true,
- buttons: 0,
- button: 0,
- })"#,
- );
- rsx! {
- div {
- id: "mouse_up_div",
- onmouseup: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert!(event.data.held_buttons().is_empty());
- assert_eq!(
- event.data.trigger_button(),
- Some(dioxus_html::input_data::MouseButton::Primary),
- );
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_mouse_scroll_div() -> Element {
- utils::mock_event(
- "wheel_div",
- r#"new WheelEvent("wheel", {
- view: window,
- deltaX: 1.0,
- deltaY: 2.0,
- deltaZ: 3.0,
- deltaMode: 0x00,
- bubbles: true,
- })"#,
- );
- rsx! {
- div {
- id: "wheel_div",
- width: "100px",
- height: "100px",
- background_color: "red",
- onwheel: move |event| {
- println!("{:?}", event.data);
- let dioxus_html::geometry::WheelDelta::Pixels(delta) = event.data.delta() else {
- panic!("Expected delta to be in pixels")
- };
- assert_eq!(delta, Vector3D::new(1.0, 2.0, 3.0));
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_key_down_div() -> Element {
- utils::mock_event(
- "key_down_div",
- r#"new KeyboardEvent("keydown", {
- key: "a",
- code: "KeyA",
- location: 0,
- repeat: true,
- keyCode: 65,
- charCode: 97,
- char: "a",
- charCode: 0,
- altKey: false,
- ctrlKey: false,
- metaKey: false,
- shiftKey: false,
- isComposing: true,
- which: 65,
- bubbles: true,
- })"#,
- );
- rsx! {
- input {
- id: "key_down_div",
- onkeydown: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert_eq!(event.data.key().to_string(), "a");
- assert_eq!(event.data.code().to_string(), "KeyA");
- assert_eq!(event.data.location(), Location::Standard);
- assert!(event.data.is_auto_repeating());
- assert!(event.data.is_composing());
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_key_up_div() -> Element {
- utils::mock_event(
- "key_up_div",
- r#"new KeyboardEvent("keyup", {
- key: "a",
- code: "KeyA",
- location: 0,
- repeat: false,
- keyCode: 65,
- charCode: 97,
- char: "a",
- charCode: 0,
- altKey: false,
- ctrlKey: false,
- metaKey: false,
- shiftKey: false,
- isComposing: false,
- which: 65,
- bubbles: true,
- })"#,
- );
- rsx! {
- input {
- id: "key_up_div",
- onkeyup: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert_eq!(event.data.key().to_string(), "a");
- assert_eq!(event.data.code().to_string(), "KeyA");
- assert_eq!(event.data.location(), Location::Standard);
- assert!(!event.data.is_auto_repeating());
- assert!(!event.data.is_composing());
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_key_press_div() -> Element {
- utils::mock_event(
- "key_press_div",
- r#"new KeyboardEvent("keypress", {
- key: "a",
- code: "KeyA",
- location: 0,
- repeat: false,
- keyCode: 65,
- charCode: 97,
- char: "a",
- charCode: 0,
- altKey: false,
- ctrlKey: false,
- metaKey: false,
- shiftKey: false,
- isComposing: false,
- which: 65,
- bubbles: true,
- })"#,
- );
- rsx! {
- input {
- id: "key_press_div",
- onkeypress: move |event| {
- println!("{:?}", event.data);
- assert!(event.data.modifiers().is_empty());
- assert_eq!(event.data.key().to_string(), "a");
- assert_eq!(event.data.code().to_string(), "KeyA");
- assert_eq!(event.data.location(), Location::Standard);
- assert!(!event.data.is_auto_repeating());
- assert!(!event.data.is_composing());
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_focus_in_div() -> Element {
- utils::mock_event(
- "focus_in_div",
- r#"new FocusEvent("focusin", {bubbles: true})"#,
- );
- rsx! {
- input {
- id: "focus_in_div",
- onfocusin: move |event| {
- println!("{:?}", event.data);
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_focus_out_div() -> Element {
- utils::mock_event(
- "focus_out_div",
- r#"new FocusEvent("focusout",{bubbles: true})"#,
- );
- rsx! {
- input {
- id: "focus_out_div",
- onfocusout: move |event| {
- println!("{:?}", event.data);
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
- fn test_form_input() -> Element {
- let mut values = use_signal(HashMap::new);
- utils::mock_event_with_extra(
- "form-username",
- r#"new Event("input", { bubbles: true, cancelable: true, composed: true })"#,
- r#"element.value = "hello";"#,
- );
- let set_username = move |ev: FormEvent| {
- values.set(ev.values());
- // The value of the input should match
- assert_eq!(ev.value(), "hello");
- // And then the value the form gives us should also match
- values.with_mut(|x| {
- assert_eq!(x.get("username").unwrap(), "hello");
- assert_eq!(x.get("full-name").unwrap(), "lorem");
- assert_eq!(x.get("password").unwrap(), "ipsum");
- assert_eq!(x.get("color").unwrap(), "red");
- });
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- };
- rsx! {
- div {
- h1 { "Form" }
- form {
- id: "form",
- oninput: move |ev| {
- values.set(ev.values());
- },
- onsubmit: move |ev| {
- println!("{:?}", ev);
- },
- input {
- r#type: "text",
- name: "username",
- id: "form-username",
- oninput: set_username
- }
- input { r#type: "text", name: "full-name", value: "lorem" }
- input { r#type: "password", name: "password", value: "ipsum" }
- input {
- r#type: "radio",
- name: "color",
- value: "red",
- checked: true
- }
- input { r#type: "radio", name: "color", value: "blue" }
- button { r#type: "submit", value: "Submit", "Submit the form" }
- }
- }
- }
- }
- fn test_form_submit() -> Element {
- let mut values = use_signal(HashMap::new);
- utils::mock_event_with_extra(
- "form-submitter",
- r#"new Event("submit", { bubbles: true, cancelable: true, composed: true })"#,
- r#"element.submit();"#,
- );
- let set_values = move |ev: FormEvent| {
- values.set(ev.values());
- values.with_mut(|x| {
- assert_eq!(x.get("username").unwrap(), "goodbye");
- assert_eq!(x.get("full-name").unwrap(), "lorem");
- assert_eq!(x.get("password").unwrap(), "ipsum");
- assert_eq!(x.get("color").unwrap(), "red");
- });
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- };
- rsx! {
- div {
- h1 { "Form" }
- form { id: "form-submitter", onsubmit: set_values,
- input {
- r#type: "text",
- name: "username",
- id: "username",
- value: "goodbye"
- }
- input { r#type: "text", name: "full-name", value: "lorem" }
- input { r#type: "password", name: "password", value: "ipsum" }
- input {
- r#type: "radio",
- name: "color",
- value: "red",
- checked: true
- }
- input { r#type: "radio", name: "color", value: "blue" }
- button { r#type: "submit", value: "Submit", "Submit the form" }
- }
- }
- }
- }
- fn test_select_multiple_options() -> Element {
- utils::mock_event_with_extra(
- "select-many",
- r#"new Event("input", { bubbles: true, cancelable: true, composed: true })"#,
- r#"
- document.getElementById('usa').selected = true;
- document.getElementById('canada').selected = true;
- document.getElementById('mexico').selected = false;
- "#,
- );
- rsx! {
- select {
- id: "select-many",
- name: "country",
- multiple: true,
- oninput: move |ev| {
- let values = ev.value();
- let values = values.split(',').collect::<Vec<_>>();
- assert_eq!(values, vec!["usa", "canada"]);
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- },
- option { id: "usa", value: "usa", "USA" }
- option { id: "canada", value: "canada", "Canada" }
- option { id: "mexico", value: "mexico", selected: true, "Mexico" }
- }
- }
- }
- fn test_unicode() -> Element {
- // emulate an oninput event with a unicode character
- utils::mock_event_with_extra(
- "unicode",
- r#"new InputEvent("input", {
- inputType: 'insertText',
- bubbles: true,
- cancelable: true,
- })"#,
- r#"
- element.value = "🦀";
- "#,
- );
- rsx! {
- input {
- id: "unicode",
- oninput: move |event| {
- println!("{:?}", event.data);
- assert_eq!(event.data.value(), "🦀");
- RECEIVED_EVENTS.with_mut(|x| *x += 1);
- }
- }
- }
- }
|