diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/print.rs b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/print.rs index adc99c3d401..a69557e8115 100644 --- a/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/print.rs +++ b/moli-renderer-v8/src/runtime/page_vm/tests/rendering_update/print.rs @@ -1,7 +1,156 @@ use super::*; use crate::runtime::{ - RendererCaptureScreenshotRequest, RendererPrintToPdfReply, RendererPrintToPdfRequest, + RendererCaptureScreenshotReply, RendererCaptureScreenshotRequest, RendererPrintToPdfReply, + RendererPrintToPdfRequest, RendererScreenshotPurpose, }; +use moli_page_types::LayoutPolicy; + +#[tokio::test(flavor = "current_thread")] +async fn native_pdf_embeds_a_loaded_font_under_its_print_declaration() { + run_page_vm_async_test(async move { + use base64::Engine; + + let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default())?; + loader.set_optional_resource_fetch_mask(crate::protocol_types::OptionalResourceFetchMask::FONT); + let data = base64::engine::general_purpose::STANDARD.encode(include_bytes!( + "../../../../../../moli-layout/tests/fixtures/moli-ahem.woff2" + )); + for policy in [LayoutPolicy::OnDemand, LayoutPolicy::Realtime] { + let mut page = test_page_vm_with_loader_and_document_url( + &loader, + Vec::new(), + Url::parse("https://cached-print-sources.test/")?, + ); + page.layout_configuration.policy = policy; + page.vm_mut().set_layout_policy(policy); + let loaded = page.vm_mut().eval(&format!(r#" + document.head.innerHTML = ''; + document.body.innerHTML = '
Cached text
'; + window.screenFonts = document.fonts; + window.screenFace = [...screenFonts].find(face => face.family === 'ScreenOnly'); + screenFace.load(); + JSON.stringify([screenFonts.size, screenFace.status]); + "#))?; + assert_eq!(loaded, r#"[1,"loaded"]"#); + let fonts = page.vm_mut().document_web_font_counts_for_test(); + let publishes = page.vm().layout_snapshot_cache_observability_for_test().2; + + let reply = page.print_to_pdf(RendererPrintToPdfRequest::default())?; + let RendererPrintToPdfReply::Printed(bytes) = reply else { + anyhow::bail!("expected PDF for {policy:?}, got {reply:?}"); + }; + let pdf = lopdf::Document::load_mem(&bytes)?; + assert!(pdf.extract_text(&[1])?.contains("Cached text")); + assert!( + pdf.objects.values().any(|object| { + object.as_dict().is_ok_and(|dict| { + dict.get(b"BaseFont") + .and_then(lopdf::Object::as_name) + .is_ok_and(|name| String::from_utf8_lossy(name).contains("MoliAhem")) + }) + }), + "{policy:?} must embed the font loaded through the screen declaration" + ); + assert_eq!(page.vm().layout_snapshot_cache_observability_for_test().2, publishes); + assert_eq!(page.vm_mut().document_web_font_counts_for_test(), fonts); + assert_eq!( + page.vm_mut().eval( + "JSON.stringify([document.fonts === screenFonts, screenFonts.size, \ + screenFonts.has(screenFace), screenFace.status, matchMedia('print').matches])" + )?, + r#"[true,1,true,"loaded",false]"# + ); + } + Ok::<_, anyhow::Error>(()) + }) + .await + .expect("printing should reuse the loaded font without changing the screen FontFaceSet"); +} + +#[test] +fn print_truncates_deep_frames_without_publishing_partial_screen_layout() { + run_page_vm_large_stack_async_test("bounded-print-frame-coverage", || async move { + let loader = crate::network::ResourceRequestClient::new(&FetchConfig::default()).unwrap(); + for policy in [LayoutPolicy::OnDemand, LayoutPolicy::Realtime] { + for depth in [31, 32] { + let (mut page, _, _) = page_vm_with_bound_task_sources_and_owner_wake( + &loader, + Url::parse("https://bounded-print.test/").unwrap(), + ); + page.layout_configuration.policy = policy; + page.vm_mut().set_layout_policy(policy); + let count = page + .vm_mut() + .eval(&format!( + r#"(() => {{ + let d = document; + d.body.innerHTML = 'Layer 0
'; + for (let i = 1; i <= {depth}; ++i) {{ + const frame = d.createElement('iframe'); + frame.style.cssText = 'width:400px;height:100px;border:0'; + d.body.appendChild(frame); + d = frame.contentDocument; + d.body.innerHTML = 'Layer ' + i + '
'; + }} + let count = 0; + for (d = document; d; ++count) {{ + const frame = d.querySelector('iframe'); + d = frame && frame.contentDocument; + }} + return count; + }})()"#, + )) + .unwrap(); + assert_eq!(count, (depth + 1).to_string()); + let strict_screen_exceeds_depth = policy.is_realtime() && depth == 32; + let screen_width = page.vm_mut().eval("document.body.offsetWidth"); + if strict_screen_exceeds_depth { + assert!( + screen_width + .unwrap_err() + .to_string() + .contains("embedded frame coverage") + ); + } else { + assert!(screen_width.unwrap().parse::