test(renderer): cover native state and Realm behavior in Realtime

This commit is contained in:
ldm0
2026-11-22 06:28:52 +08:00
committed by Donough Liu
parent 803191ceca
commit 757994e2b2
11 changed files with 258 additions and 9 deletions
@@ -545,3 +545,47 @@ fn legacy_io_modes_keep_microtask_delivery_and_create_no_frame_or_io_tasks() {
}
});
}
#[test]
fn realtime_io_projects_entries_in_callback_realm_not_observer_realm() {
run_page_vm_large_stack_async_test("realtime-io-argument-realm", || async move {
let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default()).unwrap();
let mut page = realtime_page(&loader);
page.vm_mut().eval(
"document.body.innerHTML='<div id=target style=width:100px;height:20px></div><iframe id=frame></iframe>';window.rows=[]"
).unwrap();
materialize_child_realm_through_page_turn_for_test(&mut page, "frame").unwrap();
page.vm_mut().eval(r#"
window.child=frame.contentWindow;
window.rootIO=new IntersectionObserver(child.Function('entries','observer', `
const entry=entries[0];
parent.rows.push({
callbackRealm:window===parent.child,
observerRealm:Object.getPrototypeOf(observer)===parent.IntersectionObserver.prototype,
receiver:this===observer&&observer===parent.rootIO,
arrayRealm:Object.getPrototypeOf(entries)===Array.prototype,
entryRealm:Object.getPrototypeOf(entry)===IntersectionObserverEntry.prototype,
rectRealm:[entry.boundingClientRect,entry.intersectionRect,entry.rootBounds].every(
rect=>rect!==null&&DOMRectReadOnly.prototype.isPrototypeOf(rect)),
target:entry.target===parent.document.getElementById('target'),
count:entries.length,argc:arguments.length,width:entry.boundingClientRect.width
});
`));
rootIO.observe(target);
"#).unwrap();
run_opportunity(&mut page, &loader).await.unwrap();
assert_eq!(page.vm_mut().eval("JSON.stringify(rows)").unwrap(), "[]");
let builds = page.vm().layout_pass_observability_for_test().1;
notify(&mut page, &loader).await;
assert_eq!(
page.vm_mut().eval("JSON.stringify(rows)").unwrap(),
r#"[{"callbackRealm":true,"observerRealm":true,"receiver":true,"arrayRealm":true,"entryRealm":true,"rectRealm":true,"target":true,"count":1,"argc":2,"width":100}]"#,
"observer ownership and callback argument conversion use their distinct Realms"
);
assert_eq!(
page.vm().layout_pass_observability_for_test().1,
builds,
"delivery projects owned records without another geometry pass"
);
});
}
@@ -670,3 +670,153 @@ fn realtime_select_value_change_schedules_has_resize_like_selected_index() {
}
});
}
#[cfg(feature = "devtools")]
#[test]
fn realtime_file_input_commits_schedule_validity_resize_without_capture() {
run_page_vm_large_stack_async_test("realtime-file-input-frame", || async move {
for native in [false, true] {
let loader =
crate::network::ResourceRequestClient::new(&FetchConfig::default()).unwrap();
let mut page = realtime_page(&loader);
page.vm_mut().eval(r#"
document.body.innerHTML='<style>#target{width:100px;height:20px;padding:0;border:0}#upload:valid~#target{width:200px}</style><input id=upload type=file required><div id=target></div>';
upload.value='';
const transfer=new DataTransfer();
transfer.items.add(new File(['abc'],'chosen.txt',{type:'text/plain',lastModified:7}));
window.chosenFiles=transfer.files;
window.emptyFiles=new DataTransfer().files;
window.sizes=[];
window.observer=new ResizeObserver(entries=>sizes.push(entries[0].contentRect.width));
observer.observe(target);
"#).unwrap();
run_opportunity(&mut page, &loader).await.unwrap();
assert_eq!(
page.vm_mut().eval("JSON.stringify(sizes)").unwrap(),
"[100]"
);
assert!(page.current_realtime_rendering_opportunity().is_none());
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_none()
);
assert!(page.vm().next_timeout_deadline().is_none());
let builds = page.vm().layout_pass_observability_for_test().1;
assert!(
builds > 0,
"initial RO must establish a warm, current layout"
);
let upload = page
.vm()
.document_runtime
.get_element_by_id("upload")
.unwrap();
let chosen = crate::dom::native::SelectedFile {
relative_path: String::new(),
data: b"abc".to_vec().into(),
mime_type: "text/plain".to_owned(),
name: "chosen.txt".to_owned(),
last_modified: 7.0,
};
// No getter, rAF, screenshot or callback mutation can request or
// repair the next frame after the initial observer demand is gone.
if native {
assert!(
page.vm_mut()
.set_file_input_files(upload, vec![chosen.clone()], false)
.unwrap()
);
} else {
page.vm_mut().eval("upload.files=chosenFiles").unwrap();
}
assert_eq!(page.vm().layout_pass_observability_for_test().1, builds);
assert!(
page.current_realtime_rendering_opportunity().is_some(),
"the file-state commit must create its own demand (native={native})"
);
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_some()
);
// This helper admits only a previously requested opportunity.
run_opportunity(&mut page, &loader).await.unwrap();
assert_eq!(
page.vm_mut()
.eval("JSON.stringify([upload.files.length,sizes])")
.unwrap(),
"[1,[100,200]]",
"the frame must revalidate validity-dependent style (native={native})"
);
assert_eq!(page.vm().layout_pass_observability_for_test().1, builds + 1);
assert_eq!(
page.vm()
.layout_pass_observability_for_test()
.3
.unwrap()
.paint_operation_count,
0
);
assert!(page.current_realtime_rendering_opportunity().is_none());
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_none()
);
// These repeats use the same already committed payload and dirty
// state, not an uninitialized input's first empty assignment.
if native {
assert!(
page.vm_mut()
.set_file_input_files(upload, vec![chosen], false)
.unwrap()
);
} else {
page.vm_mut().eval("upload.files=chosenFiles").unwrap();
}
assert!(page.current_realtime_rendering_opportunity().is_none());
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_none()
);
assert_eq!(page.vm().layout_pass_observability_for_test().1, builds + 1);
if native {
assert!(
page.vm_mut()
.set_file_input_files(upload, Vec::new(), false)
.unwrap()
);
} else {
page.vm_mut().eval("upload.files=emptyFiles").unwrap();
}
assert!(page.current_realtime_rendering_opportunity().is_some());
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_some()
);
run_opportunity(&mut page, &loader).await.unwrap();
assert_eq!(
page.vm_mut()
.eval("JSON.stringify([upload.files.length,sizes])")
.unwrap(),
"[0,[100,200,100]]"
);
assert_eq!(page.vm().layout_pass_observability_for_test().1, builds + 2);
assert_eq!(
page.vm()
.layout_pass_observability_for_test()
.3
.unwrap()
.paint_operation_count,
0
);
assert!(page.current_realtime_rendering_opportunity().is_none());
assert!(
page.next_rendering_opportunity_deadline_for_test()
.is_none()
);
assert!(page.vm().next_timeout_deadline().is_none());
}
});
}
@@ -1735,6 +1735,7 @@ async fn cross_origin_child_source_changes_preserve_the_parent_execution_realm()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let url = "https://child-source-sync.test/";
let mut vm = new_storage_test_vm(url);
@@ -71,7 +71,11 @@ fn file_input_validity_style_vm(policy: LayoutPolicy) -> StandaloneScriptVmHarne
#[test]
fn input_files_web_assignment_refreshes_held_validity_style_in_the_same_call() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = file_input_validity_style_vm(policy);
let builds = vm.layout_pass_observability_for_test().1;
assert_eq!(
@@ -125,7 +129,11 @@ fn input_files_web_assignment_refreshes_held_validity_style_in_the_same_call() {
#[cfg(feature = "devtools")]
#[test]
fn input_files_native_commit_refreshes_style_before_input_and_change_callbacks() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = file_input_validity_style_vm(policy);
let upload = vm.document_runtime.get_element_by_id("upload").unwrap();
let chosen = crate::dom::native::SelectedFile {
@@ -96,7 +96,11 @@ fn selection_changes_refresh_required_select_and_container_validity_styles() {
const INVALID_COLORS: &str = r#"["rgb(1, 2, 3)","rgb(1, 2, 3)","rgb(1, 2, 3)"]"#;
const VALID_COLORS: &str = r#"["rgb(4, 5, 6)","rgb(4, 5, 6)","rgb(4, 5, 6)"]"#;
let mut observed = Vec::new();
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
for (case, change) in [
("selectedIndex", "validitySelect.selectedIndex=1"),
("value", "validitySelect.value='b'"),
@@ -15,7 +15,11 @@ fn collected_native_context_count(vm: &mut StandaloneScriptVmHarness) -> usize {
#[test]
fn retired_child_content_surfaces_do_not_root_unreachable_realms() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = new_parsed_test_vm(
"https://child-content-retention.test/",
"<!doctype html><body></body>",
@@ -49,7 +53,11 @@ fn retired_child_content_surfaces_do_not_root_unreachable_realms() {
#[test]
fn retained_child_content_surfaces_preserve_author_state_until_released() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = new_parsed_test_vm(
"https://retained-child-content.test/",
"<!doctype html><body></body>",
@@ -107,7 +115,11 @@ fn retained_child_content_surfaces_preserve_author_state_until_released() {
#[test]
fn promoted_child_content_surfaces_follow_author_reachability() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = new_parsed_test_vm(
"https://promoted-child-content.test/",
"<!doctype html><body></body>",
@@ -239,7 +251,11 @@ fn detached_content_surface_inventory(vm: &StandaloneScriptVmHarness) -> [usize;
#[test]
fn collected_child_content_surface_caches_release_entries_and_capacity() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = new_parsed_test_vm(
"https://weak-content-inventory.test/",
"<!doctype html><body></body>",
@@ -268,7 +284,11 @@ fn collected_child_content_surface_caches_release_entries_and_capacity() {
#[test]
fn child_content_surface_maintenance_preserves_author_identity() {
for policy in [LayoutPolicy::Mock, LayoutPolicy::OnDemand] {
for policy in [
LayoutPolicy::Mock,
LayoutPolicy::OnDemand,
LayoutPolicy::Realtime,
] {
let mut vm = new_parsed_test_vm(
"https://weak-content-author-identity.test/",
"<!doctype html><body></body>",
@@ -4,6 +4,7 @@ async fn beforeinput_target_change(mutation: &str, should_edit: bool) {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = native_outcome_vm(policy).await;
let script=r#"
@@ -73,6 +74,7 @@ async fn native_click_revalidates_the_target_after_focus_before_preactivation()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = native_outcome_vm(policy).await;
vm.eval(
@@ -25,6 +25,7 @@ async fn retired_form_reset_and_native_click_preserve_control_state() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = native_outcome_vm(policy).await;
assert_eq!(vm.eval(r#"(()=>{
@@ -84,6 +85,7 @@ async fn native_beforeinput_cancellation_does_not_forge_event_default_prevented(
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = native_outcome_vm(policy).await;
vm.eval(r#"
@@ -168,6 +170,7 @@ async fn native_contenteditable_edit_refuses_a_retired_document_before_mutation(
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = native_outcome_vm(policy).await;
vm.eval(r#"
@@ -708,10 +708,11 @@ fn borrowed_fetch_uses_receiver_realm_and_keeps_reaction_realm_independent() {
}
#[tokio::test(flavor = "current_thread")]
async fn window_event_callbacks_project_same_window_realms_in_both_layout_modes() {
async fn window_event_callbacks_project_same_window_realms_in_all_layout_modes() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://window-event-realms.test/");
vm.set_layout_policy(policy);
@@ -815,6 +816,7 @@ async fn borrowed_default_world_window_callback_keeps_target_window_and_event_id
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://borrowed-window-event.test/");
vm.set_layout_policy(policy);
@@ -1013,6 +1015,7 @@ async fn isolated_window_world_ids_reuse_names_across_frames_but_separate_isolat
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://window-world-identity.test/");
vm.set_layout_policy(policy);
@@ -1090,6 +1093,7 @@ async fn isolated_window_world_reuse_does_not_revive_retired_realm_authority() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://window-world-rewrite.test/");
vm.set_layout_policy(policy);
@@ -1180,6 +1184,7 @@ async fn foreign_window_callbacks_use_the_target_context_in_the_listener_world()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://foreign-window-world.test/");
vm.set_layout_policy(policy);
@@ -1290,6 +1295,7 @@ async fn implicit_window_world_members_retire_independently_and_release_the_host
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://window-world-members.test/");
vm.set_layout_policy(policy);
@@ -1431,6 +1437,7 @@ async fn isolated_window_proxies_follow_child_navigation_in_surviving_worlds() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://isolated-proxy-navigation.test/");
vm.set_layout_policy(policy);
@@ -1571,6 +1578,7 @@ async fn isolated_proxy_navigation_rechecks_origin_and_universal_access() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://isolated-proxy-origin.test/");
vm.set_layout_policy(policy);
@@ -8,6 +8,7 @@ async fn isolated_node_events_project_receivers_targets_and_share_dispatch_state
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://node-events.test/");
vm.set_layout_policy(policy);
@@ -73,6 +74,7 @@ async fn adopted_node_events_use_current_document_window_in_the_listener_world()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://adopted-node-events.test/");
vm.set_layout_policy(policy);
@@ -108,6 +110,7 @@ async fn inert_document_factories_preserve_native_execution_context_for_node_eve
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://inert-node-events.test/");
vm.set_layout_policy(policy);
@@ -203,6 +206,7 @@ async fn retired_frame_nodes_and_inert_factories_distinguish_execution_context_p
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://retired-node-events.test/");
vm.set_layout_policy(policy);
@@ -267,6 +271,7 @@ fn inert_node_execution_context_survives_creation_realm_and_wrapper_cache_retire
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://inert-node-realm-retirement.test/");
vm.set_layout_policy(policy);
@@ -344,6 +349,7 @@ fn popup_document_event_authority_follows_its_window_instead_of_parser_provenanc
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://popup-node-events.test/");
vm.set_layout_policy(policy);
@@ -17,6 +17,7 @@ async fn isolated_dom_wrappers_preserve_identity_when_adopted_between_windows()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://node-world-adoption.test/");
vm.set_layout_policy(policy);
@@ -112,6 +113,7 @@ async fn retiring_one_node_world_member_releases_only_its_native_wrapper_roots()
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://node-world-roots.test/");
vm.set_layout_policy(policy);
@@ -179,6 +181,7 @@ fn collected_isolated_worlds_release_their_optional_wrapper_cache_index() {
for policy in [
moli_page_types::LayoutPolicy::Mock,
moli_page_types::LayoutPolicy::OnDemand,
moli_page_types::LayoutPolicy::Realtime,
] {
let mut vm = new_storage_test_vm("https://node-world-cache-gc.test/");
vm.set_layout_policy(policy);