diff --git a/.travis.yml b/.travis.yml index 27d62eeed..7d6b08759 100644 --- a/.travis.yml +++ b/.travis.yml @@ -70,7 +70,9 @@ matrix: before_install: *INSTALL_NODE_VIA_NVM install: - npm ci --verbose - script: cargo test --manifest-path crates/web-sys/Cargo.toml + script: + - cargo test --manifest-path crates/web-sys/Cargo.toml + - cargo test --manifest-path crates/web-sys/Cargo.toml --target wasm32-unknown-unknown addons: firefox: latest if: branch = master diff --git a/crates/test/src/lib.rs b/crates/test/src/lib.rs index b2ccda379..bc3495754 100644 --- a/crates/test/src/lib.rs +++ b/crates/test/src/lib.rs @@ -42,7 +42,7 @@ macro_rules! wasm_bindgen_test_configure { #[link_section = "__wasm_bindgen_test_unstable"] #[cfg(target_arch = "wasm32")] pub static __WBG_TEST_RUN_IN_BROWSER: [u8; 1] = [0x01]; - wasm_bindgen_test_configure!($($others)*); + $crate::wasm_bindgen_test_configure!($($others)*); ); () => () } diff --git a/crates/web-sys/Cargo.toml b/crates/web-sys/Cargo.toml index acbb989d9..7c30e17ad 100644 --- a/crates/web-sys/Cargo.toml +++ b/crates/web-sys/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["The wasm-bindgen Developers"] readme = "./README.md" +[lib] +doctest = false +test = false + [build-dependencies] env_logger = "0.5.10" failure = "0.1" @@ -13,5 +17,9 @@ sourcefile = "0.1" [dependencies] wasm-bindgen = { path = "../..", version = "=0.2.15" } -[dev-dependencies] -wasm-bindgen-test-project-builder = { path = "../test-project-builder", version = '=0.2.15' } +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +wasm-bindgen-test-project-builder = { path = '../test-project-builder', version = '=0.2.15' } + +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = { path = '../test', version = '=0.2.15' } +js-sys = { path = '../js-sys', version = '0.2.0' } diff --git a/crates/web-sys/build.rs b/crates/web-sys/build.rs index 567b1da84..02f9766c1 100644 --- a/crates/web-sys/build.rs +++ b/crates/web-sys/build.rs @@ -1,5 +1,4 @@ extern crate env_logger; -#[macro_use] extern crate failure; extern crate wasm_bindgen_webidl; extern crate sourcefile; diff --git a/crates/web-sys/tests/all/anchor_element.rs b/crates/web-sys/tests/all/anchor_element.rs deleted file mode 100644 index f66fef68f..000000000 --- a/crates/web-sys/tests/all/anchor_element.rs +++ /dev/null @@ -1,83 +0,0 @@ -use super::websys_project; - -#[test] -fn anchor_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_anchor_element(element: &web_sys::HtmlAnchorElement) { - assert_eq!(element.target(), "", "Shouldn't have a target"); - element.set_target("_blank"); - assert_eq!(element.target(), "_blank", "Should have a target"); - - assert_eq!(element.download(), "", "Shouldn't have a download"); - element.set_download("boop.png"); - assert_eq!(element.download(), "boop.png", "Should have a download"); - - assert_eq!(element.ping(), "", "Shouldn't have a ping"); - element.set_ping("boop"); - assert_eq!(element.ping(), "boop", "Should have a ping"); - - assert_eq!(element.rel(), "", "Shouldn't have a rel"); - element.set_rel("boop"); - assert_eq!(element.rel(), "boop", "Should have a rel"); - - assert_eq!(element.referrer_policy(), "", "Shouldn't have a referrer_policy"); - element.set_referrer_policy("origin"); - assert_eq!(element.referrer_policy(), "origin", "Should have a referrer_policy"); - - assert_eq!(element.hreflang(), "", "Shouldn't have a hreflang"); - element.set_hreflang("en-us"); - assert_eq!(element.hreflang(), "en-us", "Should have a hreflang"); - - assert_eq!(element.type_(), "", "Shouldn't have a type"); - element.set_type("text/plain"); - assert_eq!(element.type_(), "text/plain", "Should have a type"); - - assert_eq!(element.text().unwrap(), "", "Shouldn't have a text"); - element.set_text("Click me!").unwrap(); - assert_eq!(element.text().unwrap(), "Click me!", "Should have a text"); - - assert_eq!(element.coords(), "", "Shouldn't have a coords"); - element.set_coords("1,2,3"); - assert_eq!(element.coords(), "1,2,3", "Should have a coords"); - - assert_eq!(element.charset(), "", "Shouldn't have a charset"); - element.set_charset("thing"); - assert_eq!(element.charset(), "thing", "Should have a charset"); - - assert_eq!(element.name(), "", "Shouldn't have a name"); - element.set_name("thing"); - assert_eq!(element.name(), "thing", "Should have a name"); - - assert_eq!(element.rev(), "", "Shouldn't have a rev"); - element.set_rev("thing"); - assert_eq!(element.rev(), "thing", "Should have a rev"); - - assert_eq!(element.shape(), "", "Shouldn't have a shape"); - element.set_shape("thing"); - assert_eq!(element.shape(), "thing", "Should have a shape"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let anchor = document.createElement("a"); - wasm.test_anchor_element(anchor); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/body_element.rs b/crates/web-sys/tests/all/body_element.rs deleted file mode 100644 index 8d6a534b3..000000000 --- a/crates/web-sys/tests/all/body_element.rs +++ /dev/null @@ -1,56 +0,0 @@ -use super::websys_project; - -#[test] -fn body_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_body_element(element: &web_sys::HtmlBodyElement) { - assert_eq!(element.text(), "", "Shouldn't have a text"); - element.set_text("boop"); - assert_eq!(element.text(), "boop", "Should have a text"); - - // Legacy color setting - assert_eq!(element.link(), "", "Shouldn't have a link"); - element.set_link("blue"); - assert_eq!(element.link(), "blue", "Should have a link"); - - assert_eq!(element.v_link(), "", "Shouldn't have a v_link"); - element.set_v_link("purple"); - assert_eq!(element.v_link(), "purple", "Should have a v_link"); - - assert_eq!(element.a_link(), "", "Shouldn't have a a_link"); - element.set_a_link("purple"); - assert_eq!(element.a_link(), "purple", "Should have a a_link"); - - assert_eq!(element.bg_color(), "", "Shouldn't have a bg_color"); - element.set_bg_color("yellow"); - assert_eq!(element.bg_color(), "yellow", "Should have a bg_color"); - - assert_eq!(element.background(), "", "Shouldn't have a background"); - element.set_background("image"); - assert_eq!(element.background(), "image", "Should have a background"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let body = document.createElement("body"); - wasm.test_body_element(body); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/br_element.rs b/crates/web-sys/tests/all/br_element.rs deleted file mode 100644 index 546862f63..000000000 --- a/crates/web-sys/tests/all/br_element.rs +++ /dev/null @@ -1,36 +0,0 @@ -use super::websys_project; - -#[test] -fn br_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_br_element(element: &web_sys::HtmlBrElement) { - // Legacy clear method - assert_eq!(element.clear(), "", "Shouldn't have a clear"); - element.set_clear("boop"); - assert_eq!(element.clear(), "boop", "Should have a clear"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let br = document.createElement("br"); - wasm.test_br_element(br); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/button_element.rs b/crates/web-sys/tests/all/button_element.rs deleted file mode 100644 index 6182548ec..000000000 --- a/crates/web-sys/tests/all/button_element.rs +++ /dev/null @@ -1,105 +0,0 @@ -use super::websys_project; - -#[test] -fn button_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_button_element(element: &web_sys::HtmlButtonElement, location: String) { - assert!(!element.autofocus(), "Shouldn't have autofocus"); - element.set_autofocus(true); - assert!(element.autofocus(), "Should have autofocus"); - - assert!(!element.disabled(), "Shouldn't be disabled"); - element.set_disabled(true); - assert!(element.disabled(), "Should be disabled"); - - match element.form() { - None => assert!(true, "Shouldn't have a form"), - _ => assert!(false, "Shouldn't have a form"), - }; - - assert_eq!(element.form_action(), location, "Should have the pages location"); - element.set_form_action("http://boop.com/"); - assert_eq!(element.form_action(), "http://boop.com/", "Should have a form_action"); - - assert_eq!(element.form_enctype(), "", "Should have no enctype"); - element.set_form_enctype("text/plain"); - assert_eq!(element.form_enctype(), "text/plain", "Should have a plain text enctype"); - - assert_eq!(element.form_method(), "", "Should have no method"); - element.set_form_method("POST"); - assert_eq!(element.form_method(), "post", "Should have a POST method"); - - assert!(!element.form_no_validate(), "Should validate"); - element.set_form_no_validate(true); - assert!(element.form_no_validate(), "Should not validate"); - - assert_eq!(element.form_target(), "", "Should have no target"); - element.set_form_target("_blank"); - assert_eq!(element.form_target(), "_blank", "Should have a _blank target"); - - assert_eq!(element.name(), "", "Shouldn't have a name"); - element.set_name("button-name"); - assert_eq!(element.name(), "button-name", "Should have a name"); - - assert_eq!(element.type_(), "submit", "Shouldn't have a type"); - element.set_type("reset"); - assert_eq!(element.type_(), "reset", "Should have a reset type"); - - assert_eq!(element.value(), "", "Shouldn't have a value"); - element.set_value("value1"); - assert_eq!(element.value(), "value1", "Should have a value"); - - assert_eq!(element.will_validate(), false, "Shouldn't validate"); - assert_eq!(element.validation_message().unwrap(), "", "Shouldn't have a value"); - assert_eq!(element.check_validity(), true, "Should be valid"); - assert_eq!(element.report_validity(), true, "Should be valid"); - element.set_custom_validity("Boop"); // Method exists but doesn't impact validity - assert_eq!(element.check_validity(), true, "Should be valid"); - assert_eq!(element.report_validity(), true, "Should be valid"); - - assert_eq!(element.labels().length(), 0, "Should return a node list with no elements"); - } - - #[wasm_bindgen] - pub fn test_button_element_in_form(element: &web_sys::HtmlButtonElement) { - match element.form() { - None => assert!(false, "Should have a form"), - Some(form) => { - assert!(true, "Should have a form"); - assert_eq!(form.name(), "test-form", "Form should have a name of test-form"); - }, - }; - assert_eq!(element.type_(), "reset", "Should have a type"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let button = document.createElement("button"); - wasm.test_button_element(button, document.location.href); - - let button2 = document.createElement("button"); - button2.type = "reset"; - let form = document.createElement("form"); - form.name = "test-form"; - form.appendChild(button2); - wasm.test_button_element_in_form(button2); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/div_element.rs b/crates/web-sys/tests/all/div_element.rs deleted file mode 100644 index 82f739d5d..000000000 --- a/crates/web-sys/tests/all/div_element.rs +++ /dev/null @@ -1,35 +0,0 @@ -use super::websys_project; - -#[test] -fn div_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_div_element(element: &web_sys::HtmlDivElement) { - assert_eq!(element.align(), "", "Shouldn't have a align"); - element.set_align("right"); - assert_eq!(element.align(), "right", "Should have a align"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let div = document.createElement("div"); - wasm.test_div_element(div); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/element.rs b/crates/web-sys/tests/all/element.rs deleted file mode 100644 index 797813437..000000000 --- a/crates/web-sys/tests/all/element.rs +++ /dev/null @@ -1,141 +0,0 @@ -use super::websys_project; - -#[test] -fn element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_element(element: &web_sys::Element) { -/* Tests needed for: - namespace_uri -*/ - assert_eq!(element.prefix(), None, "Shouldn't have a prefix"); - assert_eq!(element.local_name(), "div", "Should have a div local name"); - assert_eq!(element.tag_name(), "div", "Should be a div tag"); - assert!(!element.has_attribute("id"), "Shouldn't have an id"); - element.set_id("beep"); - assert_eq!(element.id(), "beep", "Should have an id of 'beep'"); - - // must_use is set on this result? - assert_eq!(element.set_attribute("id", "beep").unwrap(), (), "Should set id"); - assert!(element.has_attribute("id"), "Should now have an id"); - assert_eq!(element.remove_attribute("id").unwrap(), (), "Should return nothing if removed"); - - assert_eq!(element.class_name(), "", "Shouldn't have a class name"); - element.set_class_name("test thing"); - assert_eq!(element.class_name(), "test thing", "Should have a class name"); - assert_eq!(element.get_attribute("class").unwrap(), "test thing", "Should have a class name"); - assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); -/* Tests needed for: - get_attribute_ns -*/ - -/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only) - // TODO toggle_attribute should permit a single argument when optional arguments are supported - assert!(!element.has_attribute("disabled"), "Should not be disabled"); - assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set"); - assert!(element.has_attribute("disabled"), "Should be disabled"); - assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set"); - assert!(!element.has_attribute("disabled"), "Should not be disabled"); -*/ - - assert!(!element.has_attribute("title"), "Should not have a title"); - assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); - assert!(element.has_attribute("title"), "Should have a title"); - // TODO check get_attribute here when supported - assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); - assert!(!element.has_attribute("title"), "Should not have a title"); -/* Tests needed for: - set_attribute_ns -*/ - - assert!(!element.has_attributes(), "Should not have any attributes"); - assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); - assert!(element.has_attributes(), "Should have attributes"); - assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); -/* Tests needed for: - remove_attribute_ns - has_attribure_ns - closest -*/ - - assert_eq!(element.matches(".this-is-a-thing").unwrap(), false, "Should not match selector"); - assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), false, "Should not match selector"); - element.set_class_name("this-is-a-thing"); - assert_eq!(element.matches(".this-is-a-thing").unwrap(), true, "Should match selector"); - assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), true, "Should match selector"); - assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); - -// TODO non standard moz_matches_selector should we even support? - -/* Tests needed for: - insert_adjacent_element - insert_adjacent_text - set_pointer_capture - release_pointer_capture - has_pointer_capture - set_capture - release_capture - scroll_top - set_scroll_top - scroll_left - set_scroll_left - scroll_width - scroll_height - scroll, - scroll_to - scroll_by - client_top - client_left - client_width - client_height - scroll_top_max - scroll_left_max -*/ - assert_eq!(element.inner_html(), "", "Should return no content"); - element.set_inner_html("Hey!Web!"); - assert_eq!(element.inner_html(), "Hey!Web!", "Should return HTML conent"); - assert_eq!(element.query_selector_all("strong").unwrap().length(), 1, "Should return one element"); - assert!(element.query_selector("strong").unwrap().is_some(), "Should return an element"); - element.set_inner_html(""); - assert_eq!(element.inner_html(), "", "Should return no content"); - -/* Tests needed for: - outer_html - set_outer_html - insert_adjacent_html -*/ - - assert!(element.query_selector(".none-existant").unwrap().is_none(), "Should return no results"); - assert_eq!(element.query_selector_all(".none-existant").unwrap().length(), 0, "Should return no results"); -/* Tests needed for: - slot - set_slot - request_fullscreen - request_pointer_lock -*/ - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let document = new Document(); - let el = document.createElement("div"); - wasm.test_element(el); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/head_element.rs b/crates/web-sys/tests/all/head_element.rs deleted file mode 100644 index fc42340a1..000000000 --- a/crates/web-sys/tests/all/head_element.rs +++ /dev/null @@ -1,33 +0,0 @@ -use super::websys_project; - -#[test] -fn head_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_head_element(_element: &web_sys::HtmlHeadElement) { - assert!(true, "Head doesn't have an interface"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let head = document.createElement("head"); - wasm.test_head_element(head); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/headers.rs b/crates/web-sys/tests/all/headers.rs deleted file mode 100644 index b8a7014e4..000000000 --- a/crates/web-sys/tests/all/headers.rs +++ /dev/null @@ -1,50 +0,0 @@ -use super::websys_project; - -#[test] -fn headers() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_headers(headers: &web_sys::Headers) { - assert_eq!(headers.get("foo").unwrap(), None); - assert_eq!( - headers.get("content-type").unwrap(), - Some("text/plain".to_string()), - ); - assert_eq!( - headers.get("Content-Type").unwrap(), - Some("text/plain".to_string()), - ); - assert!(headers.get("").is_err()); - assert!(headers.set("", "").is_err()); - assert!(headers.set("x", "").is_ok()); - assert_eq!(headers.get("x").unwrap(), Some(String::new())); - assert!(headers.delete("x").is_ok()); - assert_eq!(headers.get("x").unwrap(), None); - assert!(headers.append("a", "y").is_ok()); - assert!(headers.append("a", "z").is_ok()); - assert_eq!(headers.get("a").unwrap(), Some("y, z".to_string())); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let headers = new Headers({'Content-Type': 'text/plain'}); - wasm.test_headers(headers); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/history.rs b/crates/web-sys/tests/all/history.rs deleted file mode 100644 index 8af76f987..000000000 --- a/crates/web-sys/tests/all/history.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::websys_project; - -#[test] -fn history() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_history(history: &web_sys::History) { - assert_eq!(history.length().unwrap(), 2); - - assert!(history.go(1).is_ok()); - assert!(history.back().is_ok()); - assert!(history.forward().is_ok()); - assert!(history.go(-1).is_ok()); - - history.set_scroll_restoration(web_sys::ScrollRestoration::Manual).expect("failure to set scroll restoration"); - assert_eq!(history.scroll_restoration().unwrap(), web_sys::ScrollRestoration::Manual); - - history.set_scroll_restoration(web_sys::ScrollRestoration::Auto).expect("failure to set scroll restoration"); - assert_eq!(history.scroll_restoration().unwrap(), web_sys::ScrollRestoration::Auto); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - window.history.pushState({}, "I am a title", "part/of/some/url"); - wasm.test_history(window.history); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/html_element.rs b/crates/web-sys/tests/all/html_element.rs deleted file mode 100644 index ca27dbef1..000000000 --- a/crates/web-sys/tests/all/html_element.rs +++ /dev/null @@ -1,101 +0,0 @@ -use super::websys_project; - -#[test] -fn html_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_html_element(element: &web_sys::HtmlElement) { - assert_eq!(element.title(), "", "Shouldn't have a title"); - element.set_title("boop"); - assert_eq!(element.title(), "boop", "Should have a title"); - - assert_eq!(element.lang(), "", "Shouldn't have a lang"); - element.set_lang("en-us"); - assert_eq!(element.lang(), "en-us", "Should have a lang"); - - assert_eq!(element.dir(), "", "Shouldn't have a dir"); - element.set_dir("ltr"); - assert_eq!(element.dir(), "ltr", "Should have a dir"); - - assert_eq!(element.inner_text(), "", "Shouldn't have inner_text"); - element.set_inner_text("hey"); - assert_eq!(element.inner_text(), "hey", "Should have inner_text"); - - assert!(!element.hidden(), "Shouldn't be hidden"); - element.set_hidden(true); - assert!(element.hidden(), "Should be hidden"); - - // TODO add a click handler here - element.click(); - - assert_eq!(element.tab_index(), -1, "Shouldn't be tab_index"); - element.set_tab_index(1); - assert_eq!(element.tab_index(), 1, "Should be tab_index"); - - // TODO add a focus handler here - assert_eq!(element.focus().unwrap(), (), "No result"); - - // TODO add a blur handler here - assert_eq!(element.blur().unwrap(), (), "No result"); - - assert_eq!(element.access_key(), "", "Shouldn't have a access_key"); - element.set_access_key("a"); - assert_eq!(element.access_key(), "a", "Should have a access_key"); - - // TODO add test for access_key_label - - assert!(!element.draggable(), "Shouldn't be draggable"); - element.set_draggable(true); - assert!(element.draggable(), "Should be draggable"); - - assert_eq!(element.content_editable(), "inherit", "Shouldn't have a content_editable"); - element.set_content_editable("true"); - assert_eq!(element.content_editable(), "true", "Should be content_editable"); - assert!(element.is_content_editable(), "Should be content_editable"); - - // TODO verify case where menu is passed - match element.context_menu() { - None => assert!(true, "Shouldn't have a custom menu set"), - _ => assert!(false, "Shouldn't have a custom menu set") - }; - - assert!(!element.spellcheck(), "Shouldn't be spellchecked"); - element.set_spellcheck(true); - assert!(element.spellcheck(), "Should be dragspellcheckedgable"); - - // TODO verify case where we have an offset_parent - match element.offset_parent() { - None => assert!(true, "Shouldn't have an offset_parent set"), - _ => assert!(false, "Shouldn't have a offset_parent set") - }; - - // TODO verify when we have offsets - assert_eq!(element.offset_top(), 0, "Shouldn't have an offset_top yet"); - assert_eq!(element.offset_left(), 0, "Shouldn't have an offset_left yet"); - assert_eq!(element.offset_width(), 0, "Shouldn't have an offset_width yet"); - assert_eq!(element.offset_height(), 0, "Shouldn't have an offset_height yet"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let html = document.createElement("html"); - wasm.test_html_element(html); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/html_html_element.rs b/crates/web-sys/tests/all/html_html_element.rs deleted file mode 100644 index bd36c346d..000000000 --- a/crates/web-sys/tests/all/html_html_element.rs +++ /dev/null @@ -1,35 +0,0 @@ -use super::websys_project; - -#[test] -fn html_html_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_html_html_element(element: &web_sys::HtmlHtmlElement) { - assert_eq!(element.version(), "", "Shouldn't have a version"); - element.set_version("4"); - assert_eq!(element.version(), "4", "Should have a version"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let html = document.createElement("html"); - wasm.test_html_html_element(html); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/main.rs b/crates/web-sys/tests/all/main.rs index 5f1873b64..42a77768e 100644 --- a/crates/web-sys/tests/all/main.rs +++ b/crates/web-sys/tests/all/main.rs @@ -1,86 +1,9 @@ +#![cfg(not(target_arch = "wasm32"))] + extern crate wasm_bindgen_test_project_builder as project_builder; use project_builder::{project, Project}; mod event; -mod headers; -mod anchor_element; -mod body_element; -mod br_element; -mod button_element; -mod div_element; -mod html_element; -// TODO fix on taskcluster -// mod head_element; -mod html_html_element; -mod script_element; -mod style_element; -// TODO fix on taskcluster -//mod span_element; -mod response; -mod element; -mod history; -/*TODO tests for: - web_sys::HtmlFontElement, - web_sys::HtmlMenuItemElement, - web_sys::HtmlSourceElement, - web_sys::HtmlAreaElement, - web_sys::HtmlFormElement, - web_sys::HtmlMetaElement, - web_sys::HtmlAudioElement, - web_sys::HtmlFrameElement, - web_sys::HtmlMeterElement, - web_sys::HtmlBaseElement, - web_sys::HtmlFrameSetElement, - web_sys::HtmlModElement, - web_sys::HtmlTableCaptionElement, - web_sys::HtmlObjectElement, - web_sys::HtmlTableCellElement, - web_sys::HtmlHeadingElement, - web_sys::HtmlOListElement, - web_sys::HtmlTableColElement, - web_sys::HtmlHRElement, - web_sys::HtmlOptGroupElement, - web_sys::HtmlTableElement, - web_sys::HtmlCanvasElement, - web_sys::HtmlOptionElement, - web_sys::HtmlTableRowElement, - web_sys::HtmlDataElement, - web_sys::HtmlIFrameElement, - web_sys::HtmlOutputElement, - web_sys::HtmlTableSectionElement, - web_sys::HtmlDataListElement, - web_sys::HtmlImageElement, - web_sys::HtmlParagraphElement, - web_sys::HtmlTemplateElement, - web_sys::HtmlDetailsElement, - web_sys::HtmlInputElement, - web_sys::HtmlParamElement, - web_sys::HtmlTextAreaElement, - web_sys::HtmlDialogElement, - web_sys::HtmlLabelElement, - web_sys::HtmlPictureElement, - web_sys::HtmlTimeElement, - web_sys::HtmlDirectoryElement, - web_sys::HtmlLegendElement, - web_sys::HtmlPreElement, - web_sys::HtmlTitleElement, - web_sys::HtmlLIElement, - web_sys::HtmlProgressElement, - web_sys::HtmlTrackElement, - web_sys::HtmlDListElement, - web_sys::HtmlLinkElement, - web_sys::HtmlQuoteElement, - web_sys::HtmlUListElement, - web_sys::HtmlMapElement, - web_sys::HtmlVideoElement, - web_sys::HtmlEmbedElement, - web_sys::HtmlMediaElement, - web_sys::HtmlSelectElement, - web_sys::HtmlFieldSetElement, - web_sys::HtmlMenuElement, - web_sys::HtmlSlotElement, -*/ -mod xpath_result; fn websys_project() -> Project { project() diff --git a/crates/web-sys/tests/all/response.rs b/crates/web-sys/tests/all/response.rs deleted file mode 100644 index e9ae3b46f..000000000 --- a/crates/web-sys/tests/all/response.rs +++ /dev/null @@ -1,35 +0,0 @@ -use super::websys_project; - -#[test] -fn response() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_response(response: &web_sys::Response) { - assert!(!response.ok()); - assert!(!response.redirected()); - assert_eq!(response.status(), 501); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let response = new Response(null, {status: 501}); - wasm.test_response(response); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/script_element.rs b/crates/web-sys/tests/all/script_element.rs deleted file mode 100644 index f07bc3b76..000000000 --- a/crates/web-sys/tests/all/script_element.rs +++ /dev/null @@ -1,77 +0,0 @@ -use super::websys_project; - -#[test] -fn script_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_script_element(element: &web_sys::HtmlScriptElement) { - assert_eq!(element.src(), "", "Shouldn't have a src"); - element.set_src("https://example.com/script.js"); - assert_eq!(element.src(), "https://example.com/script.js", "Should have a src"); - - assert_eq!(element.type_(), "", "Shouldn't have a type"); - element.set_type("application/javascript"); - assert_eq!(element.type_(), "application/javascript", "Should have a type"); - - assert!(!element.no_module(), "Shouldn't be a nomodule"); - element.set_no_module(true); - assert!(element.no_module(), "Should be a nomodule"); - - assert_eq!(element.charset(), "", "Shouldn't have a charset"); - element.set_charset("UTF-8"); - assert_eq!(element.charset(), "UTF-8", "Should have a charset"); - - assert!(element.async(), "Should be async"); - element.set_async(false); - assert!(!element.async(), "Shouldn't be a async"); - - assert!(!element.defer(), "Shouldn't be a defer"); - element.set_defer(true); - assert!(element.defer(), "Should be a defer"); - - assert!(element.cross_origin().is_none(), "Shouldn't have a crossorigin"); - element.set_cross_origin(Some("anonymous")); - assert_eq!(element.cross_origin().unwrap(), "anonymous", "Should have a crossorigin"); - element.set_cross_origin(None); - assert!(element.cross_origin().is_none(), "Shouldn't have a crossorigin"); - - assert_eq!(element.text().unwrap(), "", "Shouldn't have text"); - assert_eq!(element.set_text("text").unwrap(), ()); - assert_eq!(element.text().unwrap(), "text", "Should have text"); - - assert_eq!(element.event(), "", "Shouldn't have an event"); - element.set_event("ev"); - assert_eq!(element.event(), "ev", "Should have an event"); - - assert_eq!(element.html_for(), "", "Shouldn't have an html_for"); - element.set_html_for("hey"); - assert_eq!(element.html_for(), "hey", "Should have an html_for"); - - assert_eq!(element.integrity(), "", "Shouldn't have an integrity"); - element.set_integrity("integrity-val"); - assert_eq!(element.integrity(), "integrity-val", "Should have an integrity"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let script = document.createElement("script"); - wasm.test_script_element(script); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/span_element.rs b/crates/web-sys/tests/all/span_element.rs deleted file mode 100644 index 502c27d19..000000000 --- a/crates/web-sys/tests/all/span_element.rs +++ /dev/null @@ -1,33 +0,0 @@ -use super::websys_project; - -#[test] -fn span_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_span_element(_element: &web_sys::HtmlSpanElement) { - assert!(true, "Span doesn't have an interface"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let span = document.createElement("span"); - wasm.test_span_element(span); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/style_element.rs b/crates/web-sys/tests/all/style_element.rs deleted file mode 100644 index 361e32e59..000000000 --- a/crates/web-sys/tests/all/style_element.rs +++ /dev/null @@ -1,43 +0,0 @@ -use super::websys_project; - -#[test] -fn style_element() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros, wasm_custom_section)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_style_element(element: &web_sys::HtmlStyleElement) { - assert!(!element.disabled(), "Should be disabled"); - element.set_disabled(true); - assert!(!element.disabled(), "Should be disabled"); // Not sure why this is but Chrome in Firefox behabe the same - - assert_eq!(element.type_(), "", "Shouldn't have a type"); - element.set_type("text/css"); - assert_eq!(element.type_(), "text/css", "Should have a type"); - - assert_eq!(element.media(), "", "Shouldn't have a media"); - element.set_media("screen, print"); - assert_eq!(element.media(), "screen, print", "Should have a media"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let style = document.createElement("style"); - wasm.test_style_element(style); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/all/xpath_result.rs b/crates/web-sys/tests/all/xpath_result.rs deleted file mode 100644 index 0fb3b9b68..000000000 --- a/crates/web-sys/tests/all/xpath_result.rs +++ /dev/null @@ -1,36 +0,0 @@ -use super::websys_project; - -#[test] -fn xpath_result() { - websys_project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - extern crate wasm_bindgen; - use wasm_bindgen::prelude::*; - extern crate web_sys; - - #[wasm_bindgen] - pub fn test_xpath_result(xpath_result: &web_sys::XPathResult) { - assert_eq!(xpath_result.result_type(), web_sys::XPathResult::UNORDERED_NODE_ITERATOR_TYPE); - assert_eq!(xpath_result.invalid_iterator_state(), false); - assert_eq!(xpath_result.iterate_next().unwrap().unwrap().text_content().unwrap(), "tomato"); - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - let xmlDoc = new DOMParser().parseFromString("tomato", "application/xml"); - let xpathResult = xmlDoc.evaluate("/root//value", xmlDoc, null, XPathResult.ANY_TYPE, null); - wasm.test_xpath_result(xpathResult); - } - "#, - ) - .test(); -} diff --git a/crates/web-sys/tests/wasm/anchor_element.rs b/crates/web-sys/tests/wasm/anchor_element.rs new file mode 100644 index 000000000..367c6fc54 --- /dev/null +++ b/crates/web-sys/tests/wasm/anchor_element.rs @@ -0,0 +1,64 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlAnchorElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_a() -> HtmlAnchorElement; +} + +#[wasm_bindgen_test] +fn test_anchor_element() { + let element = new_a(); + assert_eq!(element.target(), "", "Shouldn't have a target"); + element.set_target("_blank"); + assert_eq!(element.target(), "_blank", "Should have a target"); + + assert_eq!(element.download(), "", "Shouldn't have a download"); + element.set_download("boop.png"); + assert_eq!(element.download(), "boop.png", "Should have a download"); + + assert_eq!(element.ping(), "", "Shouldn't have a ping"); + element.set_ping("boop"); + assert_eq!(element.ping(), "boop", "Should have a ping"); + + assert_eq!(element.rel(), "", "Shouldn't have a rel"); + element.set_rel("boop"); + assert_eq!(element.rel(), "boop", "Should have a rel"); + + assert_eq!(element.referrer_policy(), "", "Shouldn't have a referrer_policy"); + element.set_referrer_policy("origin"); + assert_eq!(element.referrer_policy(), "origin", "Should have a referrer_policy"); + + assert_eq!(element.hreflang(), "", "Shouldn't have a hreflang"); + element.set_hreflang("en-us"); + assert_eq!(element.hreflang(), "en-us", "Should have a hreflang"); + + assert_eq!(element.type_(), "", "Shouldn't have a type"); + element.set_type("text/plain"); + assert_eq!(element.type_(), "text/plain", "Should have a type"); + + assert_eq!(element.text().unwrap(), "", "Shouldn't have a text"); + element.set_text("Click me!").unwrap(); + assert_eq!(element.text().unwrap(), "Click me!", "Should have a text"); + + assert_eq!(element.coords(), "", "Shouldn't have a coords"); + element.set_coords("1,2,3"); + assert_eq!(element.coords(), "1,2,3", "Should have a coords"); + + assert_eq!(element.charset(), "", "Shouldn't have a charset"); + element.set_charset("thing"); + assert_eq!(element.charset(), "thing", "Should have a charset"); + + assert_eq!(element.name(), "", "Shouldn't have a name"); + element.set_name("thing"); + assert_eq!(element.name(), "thing", "Should have a name"); + + assert_eq!(element.rev(), "", "Shouldn't have a rev"); + element.set_rev("thing"); + assert_eq!(element.rev(), "thing", "Should have a rev"); + + assert_eq!(element.shape(), "", "Shouldn't have a shape"); + element.set_shape("thing"); + assert_eq!(element.shape(), "thing", "Should have a shape"); +} diff --git a/crates/web-sys/tests/wasm/body_element.rs b/crates/web-sys/tests/wasm/body_element.rs new file mode 100644 index 000000000..abdb7487b --- /dev/null +++ b/crates/web-sys/tests/wasm/body_element.rs @@ -0,0 +1,37 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlBodyElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_body() -> HtmlBodyElement; +} + +#[wasm_bindgen_test] +fn test_body_element() { + let element = new_body(); + assert_eq!(element.text(), "", "Shouldn't have a text"); + element.set_text("boop"); + assert_eq!(element.text(), "boop", "Should have a text"); + + // Legacy color setting + assert_eq!(element.link(), "", "Shouldn't have a link"); + element.set_link("blue"); + assert_eq!(element.link(), "blue", "Should have a link"); + + assert_eq!(element.v_link(), "", "Shouldn't have a v_link"); + element.set_v_link("purple"); + assert_eq!(element.v_link(), "purple", "Should have a v_link"); + + assert_eq!(element.a_link(), "", "Shouldn't have a a_link"); + element.set_a_link("purple"); + assert_eq!(element.a_link(), "purple", "Should have a a_link"); + + assert_eq!(element.bg_color(), "", "Shouldn't have a bg_color"); + element.set_bg_color("yellow"); + assert_eq!(element.bg_color(), "yellow", "Should have a bg_color"); + + assert_eq!(element.background(), "", "Shouldn't have a background"); + element.set_background("image"); + assert_eq!(element.background(), "image", "Should have a background"); +} diff --git a/crates/web-sys/tests/wasm/br_element.rs b/crates/web-sys/tests/wasm/br_element.rs new file mode 100644 index 000000000..d21d93193 --- /dev/null +++ b/crates/web-sys/tests/wasm/br_element.rs @@ -0,0 +1,17 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlBrElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_br() -> HtmlBrElement; +} + +#[wasm_bindgen_test] +fn test_br_element() { + let element = new_br(); + // Legacy clear method + assert_eq!(element.clear(), "", "Shouldn't have a clear"); + element.set_clear("boop"); + assert_eq!(element.clear(), "boop", "Should have a clear"); +} diff --git a/crates/web-sys/tests/wasm/button_element.rs b/crates/web-sys/tests/wasm/button_element.rs new file mode 100644 index 000000000..f32a38503 --- /dev/null +++ b/crates/web-sys/tests/wasm/button_element.rs @@ -0,0 +1,104 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::{HtmlButtonElement, HtmlFormElement, Node}; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_button() -> HtmlButtonElement; + fn new_form() -> HtmlFormElement; +} + +#[wasm_bindgen] +extern { + #[wasm_bindgen(js_name = location, js_namespace = document)] + static LOCATION: Location; + + type Location; + + // FIXME: `href` should be structural in `web_sys` + #[wasm_bindgen(getter, method, structural)] + fn href(this: &Location) -> String; +} + +#[wasm_bindgen_test] +fn test_button_element() { + let element = new_button(); + let location = LOCATION.href(); + assert!(!element.autofocus(), "Shouldn't have autofocus"); + element.set_autofocus(true); + assert!(element.autofocus(), "Should have autofocus"); + + assert!(!element.disabled(), "Shouldn't be disabled"); + element.set_disabled(true); + assert!(element.disabled(), "Should be disabled"); + + match element.form() { + None => assert!(true, "Shouldn't have a form"), + _ => assert!(false, "Shouldn't have a form"), + }; + + assert_eq!(element.form_action(), location, "Should have the pages location"); + element.set_form_action("http://boop.com/"); + assert_eq!(element.form_action(), "http://boop.com/", "Should have a form_action"); + + assert_eq!(element.form_enctype(), "", "Should have no enctype"); + element.set_form_enctype("text/plain"); + assert_eq!(element.form_enctype(), "text/plain", "Should have a plain text enctype"); + + assert_eq!(element.form_method(), "", "Should have no method"); + element.set_form_method("POST"); + assert_eq!(element.form_method(), "post", "Should have a POST method"); + + assert!(!element.form_no_validate(), "Should validate"); + element.set_form_no_validate(true); + assert!(element.form_no_validate(), "Should not validate"); + + assert_eq!(element.form_target(), "", "Should have no target"); + element.set_form_target("_blank"); + assert_eq!(element.form_target(), "_blank", "Should have a _blank target"); + + assert_eq!(element.name(), "", "Shouldn't have a name"); + element.set_name("button-name"); + assert_eq!(element.name(), "button-name", "Should have a name"); + + assert_eq!(element.type_(), "submit", "Shouldn't have a type"); + element.set_type("reset"); + assert_eq!(element.type_(), "reset", "Should have a reset type"); + + assert_eq!(element.value(), "", "Shouldn't have a value"); + element.set_value("value1"); + assert_eq!(element.value(), "value1", "Should have a value"); + + assert_eq!(element.will_validate(), false, "Shouldn't validate"); + assert_eq!(element.validation_message().unwrap(), "", "Shouldn't have a value"); + assert_eq!(element.check_validity(), true, "Should be valid"); + assert_eq!(element.report_validity(), true, "Should be valid"); + element.set_custom_validity("Boop"); // Method exists but doesn't impact validity + assert_eq!(element.check_validity(), true, "Should be valid"); + assert_eq!(element.report_validity(), true, "Should be valid"); + + assert_eq!(element.labels().length(), 0, "Should return a node list with no elements"); +} + +#[wasm_bindgen_test] +fn test_button_element_in_form() { + let button = new_button(); + button.set_type("reset"); + let form = new_form(); + form.set_name("test-form"); + + // TODO: implement `Clone` for types in `web_sys` to make this easier. + let button = JsValue::from(button); + let as_node = Node::from(button.clone()); + Node::from(JsValue::from(form)).append_child(&as_node).unwrap(); + + let element = HtmlButtonElement::from(button); + match element.form() { + None => assert!(false, "Should have a form"), + Some(form) => { + assert!(true, "Should have a form"); + assert_eq!(form.name(), "test-form", "Form should have a name of test-form"); + }, + }; + assert_eq!(element.type_(), "reset", "Should have a type"); +} diff --git a/crates/web-sys/tests/wasm/div_element.rs b/crates/web-sys/tests/wasm/div_element.rs new file mode 100644 index 000000000..eaa7f265d --- /dev/null +++ b/crates/web-sys/tests/wasm/div_element.rs @@ -0,0 +1,17 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlDivElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + #[wasm_bindgen(js_name = new_div)] + fn make_div() -> HtmlDivElement; +} + +#[wasm_bindgen_test] +fn test_div_element() { + let element = make_div(); + assert_eq!(element.align(), "", "Shouldn't have a align"); + element.set_align("right"); + assert_eq!(element.align(), "right", "Should have a align"); +} diff --git a/crates/web-sys/tests/wasm/element.js b/crates/web-sys/tests/wasm/element.js new file mode 100644 index 000000000..05dadfa4c --- /dev/null +++ b/crates/web-sys/tests/wasm/element.js @@ -0,0 +1,49 @@ +export function new_div() { + return document.createElement("div"); +} + +export function new_a() { + return document.createElement("a"); +} + +export function new_body() { + return document.createElement("body"); +} + +export function new_br() { + return document.createElement("br"); +} + +export function new_button() { + return document.createElement("button"); +} + +export function new_form() { + return document.createElement("form"); +} + +export function new_head() { + return document.createElement("head"); +} + +export function new_html() { + return document.createElement("html"); +} + +export function new_script() { + return document.createElement("script"); +} + +export function new_span() { + return document.createElement("span"); +} + +export function new_style() { + return document.createElement("style"); +} + +export function new_xpath_result() { + let xmlDoc = new DOMParser().parseFromString("tomato", "application/xml"); + let xpathResult = xmlDoc.evaluate("/root//value", xmlDoc, null, XPathResult.ANY_TYPE, null); + return xpathResult; +} diff --git a/crates/web-sys/tests/wasm/element.rs b/crates/web-sys/tests/wasm/element.rs new file mode 100644 index 000000000..162e74e84 --- /dev/null +++ b/crates/web-sys/tests/wasm/element.rs @@ -0,0 +1,122 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::Element; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_div() -> Element; +} + +#[wasm_bindgen_test] +fn element() { +/* Tests needed for: + namespace_uri +*/ + let element = new_div(); + + assert_eq!(element.prefix(), None, "Shouldn't have a prefix"); + assert_eq!(element.local_name(), "div", "Should have a div local name"); + assert_eq!(element.tag_name(), "DIV", "Should be a div tag"); + assert!(!element.has_attribute("id"), "Shouldn't have an id"); + element.set_id("beep"); + assert_eq!(element.id(), "beep", "Should have an id of 'beep'"); + + // must_use is set on this result? + assert_eq!(element.set_attribute("id", "beep").unwrap(), (), "Should set id"); + assert!(element.has_attribute("id"), "Should now have an id"); + assert_eq!(element.remove_attribute("id").unwrap(), (), "Should return nothing if removed"); + + assert_eq!(element.class_name(), "", "Shouldn't have a class name"); + element.set_class_name("test thing"); + assert_eq!(element.class_name(), "test thing", "Should have a class name"); + assert_eq!(element.get_attribute("class").unwrap(), "test thing", "Should have a class name"); + assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); +/* Tests needed for: +get_attribute_ns +*/ + +/*TODO should we enable toggle_attribute tests? (Firefox Nightly + Chrome canary only) + // TODO toggle_attribute should permit a single argument when optional arguments are supported + assert!(!element.has_attribute("disabled"), "Should not be disabled"); + assert!(element.toggle_attribute("disabled", true).unwrap(), "Should return true when attribute is set"); + assert!(element.has_attribute("disabled"), "Should be disabled"); + assert!(!element.toggle_attribute("disabled", false).unwrap(), "Should return false when attribute is not set"); + assert!(!element.has_attribute("disabled"), "Should not be disabled"); +*/ + + assert!(!element.has_attribute("title"), "Should not have a title"); + assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); + assert!(element.has_attribute("title"), "Should have a title"); + // TODO check get_attribute here when supported + assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); + assert!(!element.has_attribute("title"), "Should not have a title"); +/* Tests needed for: +set_attribute_ns +*/ + + assert!(!element.has_attributes(), "Should not have any attributes"); + assert_eq!(element.set_attribute("title", "boop").unwrap(), (), "Should return nothing if set correctly"); + assert!(element.has_attributes(), "Should have attributes"); + assert_eq!(element.remove_attribute("title").unwrap(), (), "Should return nothing if removed"); +/* Tests needed for: +remove_attribute_ns +has_attribure_ns +closest +*/ + + assert_eq!(element.matches(".this-is-a-thing").unwrap(), false, "Should not match selector"); + assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), false, "Should not match selector"); + element.set_class_name("this-is-a-thing"); + assert_eq!(element.matches(".this-is-a-thing").unwrap(), true, "Should match selector"); + assert_eq!(element.webkit_matches_selector(".this-is-a-thing").unwrap(), true, "Should match selector"); + assert_eq!(element.remove_attribute("class").unwrap(), (), "Should return nothing if removed"); + +// TODO non standard moz_matches_selector should we even support? + +/* Tests needed for: +insert_adjacent_element +insert_adjacent_text +set_pointer_capture +release_pointer_capture +has_pointer_capture +set_capture +release_capture +scroll_top +set_scroll_top +scroll_left +set_scroll_left +scroll_width +scroll_height +scroll, +scroll_to +scroll_by +client_top +client_left +client_width +client_height +scroll_top_max +scroll_left_max +*/ + assert_eq!(element.inner_html(), "", "Should return no content"); + element.set_inner_html("Hey!Web!"); + assert_eq!(element.inner_html(), "Hey!Web!", "Should return HTML conent"); + assert_eq!(element.query_selector_all("strong").unwrap().length(), 1, "Should return one element"); + assert!(element.query_selector("strong").unwrap().is_some(), "Should return an element"); + element.set_inner_html(""); + assert_eq!(element.inner_html(), "", "Should return no content"); + +/* Tests needed for: +outer_html +set_outer_html +insert_adjacent_html +*/ + + assert!(element.query_selector(".none-existant").unwrap().is_none(), "Should return no results"); + assert_eq!(element.query_selector_all(".none-existant").unwrap().length(), 0, "Should return no results"); +/* Tests needed for: + slot + set_slot + request_fullscreen + request_pointer_lock +*/ +} diff --git a/crates/web-sys/tests/wasm/head_element.rs b/crates/web-sys/tests/wasm/head_element.rs new file mode 100644 index 000000000..e3e5b9e49 --- /dev/null +++ b/crates/web-sys/tests/wasm/head_element.rs @@ -0,0 +1,14 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlHeadElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_head() -> HtmlHeadElement; +} + +#[wasm_bindgen_test] +fn test_head_element() { + let _element = new_head(); + assert!(true, "Head doesn't have an interface"); +} diff --git a/crates/web-sys/tests/wasm/headers.js b/crates/web-sys/tests/wasm/headers.js new file mode 100644 index 000000000..3b71cdc66 --- /dev/null +++ b/crates/web-sys/tests/wasm/headers.js @@ -0,0 +1,3 @@ +export function new_headers() { + return new Headers({'Content-Type': 'text/plain'}); +} diff --git a/crates/web-sys/tests/wasm/headers.rs b/crates/web-sys/tests/wasm/headers.rs new file mode 100644 index 000000000..6f08554c9 --- /dev/null +++ b/crates/web-sys/tests/wasm/headers.rs @@ -0,0 +1,31 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::Headers; + +#[wasm_bindgen(module = "./tests/wasm/headers.js")] +extern { + fn new_headers() -> Headers; +} + +#[wasm_bindgen_test] +fn headers() { + let headers = new_headers(); + assert_eq!(headers.get("foo").unwrap(), None); + assert_eq!( + headers.get("content-type").unwrap(), + Some("text/plain".to_string()), + ); + assert_eq!( + headers.get("Content-Type").unwrap(), + Some("text/plain".to_string()), + ); + assert!(headers.get("").is_err()); + assert!(headers.set("", "").is_err()); + assert!(headers.set("x", "").is_ok()); + assert_eq!(headers.get("x").unwrap(), Some(String::new())); + assert!(headers.delete("x").is_ok()); + assert_eq!(headers.get("x").unwrap(), None); + assert!(headers.append("a", "y").is_ok()); + assert!(headers.append("a", "z").is_ok()); + assert_eq!(headers.get("a").unwrap(), Some("y, z".to_string())); +} diff --git a/crates/web-sys/tests/wasm/history.rs b/crates/web-sys/tests/wasm/history.rs new file mode 100644 index 000000000..fb0a78dcc --- /dev/null +++ b/crates/web-sys/tests/wasm/history.rs @@ -0,0 +1,31 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::{History, ScrollRestoration}; +use js_sys::Object; + +#[wasm_bindgen] +extern { + #[wasm_bindgen(js_name = history, js_namespace = window)] + static HISTORY: History; +} + +#[wasm_bindgen_test] +fn history() { + HISTORY.push_state( + Object::new().into(), + "I am a title", + Some("part/of/some/url"), + ).unwrap(); + assert_eq!(HISTORY.length().unwrap(), 2); + + assert!(HISTORY.go(1).is_ok()); + assert!(HISTORY.back().is_ok()); + assert!(HISTORY.forward().is_ok()); + assert!(HISTORY.go(-1).is_ok()); + + HISTORY.set_scroll_restoration(ScrollRestoration::Manual).expect("failure to set scroll restoration"); + assert_eq!(HISTORY.scroll_restoration().unwrap(), ScrollRestoration::Manual); + + HISTORY.set_scroll_restoration(ScrollRestoration::Auto).expect("failure to set scroll restoration"); + assert_eq!(HISTORY.scroll_restoration().unwrap(), ScrollRestoration::Auto); +} diff --git a/crates/web-sys/tests/wasm/html_element.rs b/crates/web-sys/tests/wasm/html_element.rs new file mode 100644 index 000000000..f61df117e --- /dev/null +++ b/crates/web-sys/tests/wasm/html_element.rs @@ -0,0 +1,82 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_html() -> HtmlElement; +} + +#[wasm_bindgen_test] +fn test_html_element() { + let element = new_html(); + assert_eq!(element.title(), "", "Shouldn't have a title"); + element.set_title("boop"); + assert_eq!(element.title(), "boop", "Should have a title"); + + assert_eq!(element.lang(), "", "Shouldn't have a lang"); + element.set_lang("en-us"); + assert_eq!(element.lang(), "en-us", "Should have a lang"); + + assert_eq!(element.dir(), "", "Shouldn't have a dir"); + element.set_dir("ltr"); + assert_eq!(element.dir(), "ltr", "Should have a dir"); + + assert_eq!(element.inner_text(), "", "Shouldn't have inner_text"); + element.set_inner_text("hey"); + assert_eq!(element.inner_text(), "hey", "Should have inner_text"); + + assert!(!element.hidden(), "Shouldn't be hidden"); + element.set_hidden(true); + assert!(element.hidden(), "Should be hidden"); + + // TODO add a click handler here + element.click(); + + assert_eq!(element.tab_index(), -1, "Shouldn't be tab_index"); + element.set_tab_index(1); + assert_eq!(element.tab_index(), 1, "Should be tab_index"); + + // TODO add a focus handler here + assert_eq!(element.focus().unwrap(), (), "No result"); + + // TODO add a blur handler here + assert_eq!(element.blur().unwrap(), (), "No result"); + + assert_eq!(element.access_key(), "", "Shouldn't have a access_key"); + element.set_access_key("a"); + assert_eq!(element.access_key(), "a", "Should have a access_key"); + + // TODO add test for access_key_label + + assert!(!element.draggable(), "Shouldn't be draggable"); + element.set_draggable(true); + assert!(element.draggable(), "Should be draggable"); + + assert_eq!(element.content_editable(), "inherit", "Shouldn't have a content_editable"); + element.set_content_editable("true"); + assert_eq!(element.content_editable(), "true", "Should be content_editable"); + assert!(element.is_content_editable(), "Should be content_editable"); + + // TODO verify case where menu is passed + match element.context_menu() { + None => assert!(true, "Shouldn't have a custom menu set"), + _ => assert!(false, "Shouldn't have a custom menu set") + }; + + assert!(!element.spellcheck(), "Shouldn't be spellchecked"); + element.set_spellcheck(true); + assert!(element.spellcheck(), "Should be dragspellcheckedgable"); + + // TODO verify case where we have an offset_parent + match element.offset_parent() { + None => assert!(true, "Shouldn't have an offset_parent set"), + _ => assert!(false, "Shouldn't have a offset_parent set") + }; + + // TODO verify when we have offsets + assert_eq!(element.offset_top(), 0, "Shouldn't have an offset_top yet"); + assert_eq!(element.offset_left(), 0, "Shouldn't have an offset_left yet"); + assert_eq!(element.offset_width(), 0, "Shouldn't have an offset_width yet"); + assert_eq!(element.offset_height(), 0, "Shouldn't have an offset_height yet"); +} diff --git a/crates/web-sys/tests/wasm/html_html_element.rs b/crates/web-sys/tests/wasm/html_html_element.rs new file mode 100644 index 000000000..5c497d511 --- /dev/null +++ b/crates/web-sys/tests/wasm/html_html_element.rs @@ -0,0 +1,17 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlHtmlElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + #[wasm_bindgen(js_name = new_html)] + fn make_html() -> HtmlHtmlElement; +} + +#[wasm_bindgen_test] +fn test_html_html_element() { + let element = make_html(); + assert_eq!(element.version(), "", "Shouldn't have a version"); + element.set_version("4"); + assert_eq!(element.version(), "4", "Should have a version"); +} diff --git a/crates/web-sys/tests/wasm/main.rs b/crates/web-sys/tests/wasm/main.rs new file mode 100644 index 000000000..7c3c7be57 --- /dev/null +++ b/crates/web-sys/tests/wasm/main.rs @@ -0,0 +1,87 @@ +#![feature(use_extern_macros)] +#![cfg(target_arch = "wasm32")] + +extern crate js_sys; +extern crate wasm_bindgen; +extern crate wasm_bindgen_test; +extern crate web_sys; + +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +pub mod anchor_element; +pub mod body_element; +pub mod br_element; +pub mod button_element; +pub mod div_element; +pub mod element; +pub mod head_element; +pub mod headers; +pub mod history; +pub mod html_element; +pub mod html_html_element; +pub mod response; +pub mod script_element; +pub mod span_element; +pub mod style_element; +/*TODO tests for: + web_sys::HtmlFontElement, + web_sys::HtmlMenuItemElement, + web_sys::HtmlSourceElement, + web_sys::HtmlAreaElement, + web_sys::HtmlFormElement, + web_sys::HtmlMetaElement, + web_sys::HtmlAudioElement, + web_sys::HtmlFrameElement, + web_sys::HtmlMeterElement, + web_sys::HtmlBaseElement, + web_sys::HtmlFrameSetElement, + web_sys::HtmlModElement, + web_sys::HtmlTableCaptionElement, + web_sys::HtmlObjectElement, + web_sys::HtmlTableCellElement, + web_sys::HtmlHeadingElement, + web_sys::HtmlOListElement, + web_sys::HtmlTableColElement, + web_sys::HtmlHRElement, + web_sys::HtmlOptGroupElement, + web_sys::HtmlTableElement, + web_sys::HtmlCanvasElement, + web_sys::HtmlOptionElement, + web_sys::HtmlTableRowElement, + web_sys::HtmlDataElement, + web_sys::HtmlIFrameElement, + web_sys::HtmlOutputElement, + web_sys::HtmlTableSectionElement, + web_sys::HtmlDataListElement, + web_sys::HtmlImageElement, + web_sys::HtmlParagraphElement, + web_sys::HtmlTemplateElement, + web_sys::HtmlDetailsElement, + web_sys::HtmlInputElement, + web_sys::HtmlParamElement, + web_sys::HtmlTextAreaElement, + web_sys::HtmlDialogElement, + web_sys::HtmlLabelElement, + web_sys::HtmlPictureElement, + web_sys::HtmlTimeElement, + web_sys::HtmlDirectoryElement, + web_sys::HtmlLegendElement, + web_sys::HtmlPreElement, + web_sys::HtmlTitleElement, + web_sys::HtmlLIElement, + web_sys::HtmlProgressElement, + web_sys::HtmlTrackElement, + web_sys::HtmlDListElement, + web_sys::HtmlLinkElement, + web_sys::HtmlQuoteElement, + web_sys::HtmlUListElement, + web_sys::HtmlMapElement, + web_sys::HtmlVideoElement, + web_sys::HtmlEmbedElement, + web_sys::HtmlMediaElement, + web_sys::HtmlSelectElement, + web_sys::HtmlFieldSetElement, + web_sys::HtmlMenuElement, + web_sys::HtmlSlotElement, +*/ +pub mod xpath_result; diff --git a/crates/web-sys/tests/wasm/response.js b/crates/web-sys/tests/wasm/response.js new file mode 100644 index 000000000..3dc3e1dc7 --- /dev/null +++ b/crates/web-sys/tests/wasm/response.js @@ -0,0 +1,3 @@ +export function new_response() { + return new Response(null, {status: 501}); +} diff --git a/crates/web-sys/tests/wasm/response.rs b/crates/web-sys/tests/wasm/response.rs new file mode 100644 index 000000000..9413db27b --- /dev/null +++ b/crates/web-sys/tests/wasm/response.rs @@ -0,0 +1,16 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::Response; + +#[wasm_bindgen(module = "./tests/wasm/response.js")] +extern { + fn new_response() -> Response; +} + +#[wasm_bindgen_test] +fn test_response() { + let response = new_response(); + assert!(!response.ok()); + assert!(!response.redirected()); + assert_eq!(response.status(), 501); +} diff --git a/crates/web-sys/tests/wasm/script_element.rs b/crates/web-sys/tests/wasm/script_element.rs new file mode 100644 index 000000000..dd9abd2b0 --- /dev/null +++ b/crates/web-sys/tests/wasm/script_element.rs @@ -0,0 +1,58 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlScriptElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_script() -> HtmlScriptElement; +} + +#[wasm_bindgen_test] +fn test_script_element() { + let element = new_script(); + assert_eq!(element.src(), "", "Shouldn't have a src"); + element.set_src("https://example.com/script.js"); + assert_eq!(element.src(), "https://example.com/script.js", "Should have a src"); + + assert_eq!(element.type_(), "", "Shouldn't have a type"); + element.set_type("application/javascript"); + assert_eq!(element.type_(), "application/javascript", "Should have a type"); + + assert!(!element.no_module(), "Shouldn't be a nomodule"); + element.set_no_module(true); + assert!(element.no_module(), "Should be a nomodule"); + + assert_eq!(element.charset(), "", "Shouldn't have a charset"); + element.set_charset("UTF-8"); + assert_eq!(element.charset(), "UTF-8", "Should have a charset"); + + assert!(element.async(), "Should be async"); + element.set_async(false); + assert!(!element.async(), "Shouldn't be a async"); + + assert!(!element.defer(), "Shouldn't be a defer"); + element.set_defer(true); + assert!(element.defer(), "Should be a defer"); + + assert!(element.cross_origin().is_none(), "Shouldn't have a crossorigin"); + element.set_cross_origin(Some("anonymous")); + assert_eq!(element.cross_origin().unwrap(), "anonymous", "Should have a crossorigin"); + element.set_cross_origin(None); + assert!(element.cross_origin().is_none(), "Shouldn't have a crossorigin"); + + assert_eq!(element.text().unwrap(), "", "Shouldn't have text"); + assert_eq!(element.set_text("text").unwrap(), ()); + assert_eq!(element.text().unwrap(), "text", "Should have text"); + + assert_eq!(element.event(), "", "Shouldn't have an event"); + element.set_event("ev"); + assert_eq!(element.event(), "ev", "Should have an event"); + + assert_eq!(element.html_for(), "", "Shouldn't have an html_for"); + element.set_html_for("hey"); + assert_eq!(element.html_for(), "hey", "Should have an html_for"); + + assert_eq!(element.integrity(), "", "Shouldn't have an integrity"); + element.set_integrity("integrity-val"); + assert_eq!(element.integrity(), "integrity-val", "Should have an integrity"); +} diff --git a/crates/web-sys/tests/wasm/span_element.rs b/crates/web-sys/tests/wasm/span_element.rs new file mode 100644 index 000000000..fc50e0e85 --- /dev/null +++ b/crates/web-sys/tests/wasm/span_element.rs @@ -0,0 +1,14 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlSpanElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_span() -> HtmlSpanElement; +} + +#[wasm_bindgen_test] +fn test_span_element() { + let _element = new_span(); + assert!(true, "Span doesn't have an interface"); +} diff --git a/crates/web-sys/tests/wasm/style_element.rs b/crates/web-sys/tests/wasm/style_element.rs new file mode 100644 index 000000000..289305f56 --- /dev/null +++ b/crates/web-sys/tests/wasm/style_element.rs @@ -0,0 +1,24 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::HtmlStyleElement; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_style() -> HtmlStyleElement; +} + +#[wasm_bindgen_test] +fn test_style_element() { + let element = new_style(); + assert!(!element.disabled(), "Should be disabled"); + element.set_disabled(true); + assert!(!element.disabled(), "Should be disabled"); // Not sure why this is but Chrome in Firefox behabe the same + + assert_eq!(element.type_(), "", "Shouldn't have a type"); + element.set_type("text/css"); + assert_eq!(element.type_(), "text/css", "Should have a type"); + + assert_eq!(element.media(), "", "Shouldn't have a media"); + element.set_media("screen, print"); + assert_eq!(element.media(), "screen, print", "Should have a media"); +} diff --git a/crates/web-sys/tests/wasm/xpath_result.rs b/crates/web-sys/tests/wasm/xpath_result.rs new file mode 100644 index 000000000..c69965de6 --- /dev/null +++ b/crates/web-sys/tests/wasm/xpath_result.rs @@ -0,0 +1,16 @@ +use wasm_bindgen_test::*; +use wasm_bindgen::prelude::*; +use web_sys::XPathResult; + +#[wasm_bindgen(module = "./tests/wasm/element.js")] +extern { + fn new_xpath_result() -> XPathResult; +} + +#[wasm_bindgen_test] +fn test_xpath_result() { + let xpath_result = new_xpath_result(); + assert_eq!(xpath_result.result_type(), XPathResult::UNORDERED_NODE_ITERATOR_TYPE); + assert_eq!(xpath_result.invalid_iterator_state(), false); + assert_eq!(xpath_result.iterate_next().unwrap().unwrap().text_content().unwrap(), "tomato"); +}