use blitz_script::ScriptDocument;
fn value(doc: &mut ScriptDocument, expression: &str) -> serde_json::Value {
doc.eval_json(expression).unwrap_or(serde_json::Value::Null)
}
fn page() -> ScriptDocument {
ScriptDocument::from_html(
"<html><body><canvas id='c'></canvas></body></html>",
blitz_dom::DocumentConfig::default(),
)
}
#[test]
fn get_context_is_callable() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(&mut doc, "typeof document.getElementById('c').getContext"),
"function"
);
}
#[test]
fn an_unsupported_context_is_null_rather_than_a_thrown_type_error() {
let mut doc = page();
doc.execute_scripts();
for identifier in ["webgl", "experimental-webgl", "2d", "webgl2"] {
assert_eq!(
value(
&mut doc,
&format!("document.getElementById('c').getContext('{identifier}')")
),
serde_json::Value::Null,
"getContext('{identifier}') must answer null, not throw"
);
}
}
#[test]
fn the_metal_border_feature_detect_reaches_its_own_guard() {
let mut doc = page();
doc.execute_scripts();
assert_eq!(
value(
&mut doc,
"(() => { try { \
const c = document.createElement('canvas'); \
const gl = c.getContext('webgl', { alpha: true }) ?? c.getContext('experimental-webgl'); \
if (!gl) throw new Error('metal-fx: WebGL not supported'); \
return 'got a context'; \
} catch (error) { return error.message; } })()"
),
"metal-fx: WebGL not supported"
);
}