Render PRD sites in real viewport

This commit is contained in:
2026-05-08 13:20:43 -04:00
parent 2fb6b15e2d
commit 8e12d6690b
8 changed files with 477 additions and 118 deletions
+131 -28
View File
@@ -1,64 +1,167 @@
#![cfg(feature = "servo-engine")]
use std::{error::Error, process::Command};
use std::{
error::Error,
io,
process::{Child, Command, Output, Stdio},
thread,
time::{Duration, Instant},
};
const WIDTH: u64 = 640;
const HEIGHT: u64 = 480;
const PRD_SITE_COMPATIBILITY_URLS: &[&str] = &["https://example.com", "https://servo.org"];
const MINIMUM_CONTENT_PIXELS: u64 = 1_000;
const SIDECAR_TIMEOUT: Duration = Duration::from_secs(25);
const SIDECAR_POLL_INTERVAL: Duration = Duration::from_millis(20);
const PRD_SITE_COMPATIBILITY_CASES: &[PrdSiteCompatibilityCase] = &[
PrdSiteCompatibilityCase { url: "https://example.com", title_fragment: "Example Domain" },
PrdSiteCompatibilityCase { url: "https://servo.org", title_fragment: "Servo" },
];
const PRD_SITE_COMPATIBILITY_SIZES: &[FrameSize] = &[
FrameSize { width: 640, height: 480 },
FrameSize { width: 934, height: 657 },
FrameSize { width: 1614, height: 980 },
];
struct PrdSiteCompatibilityCase {
url: &'static str,
title_fragment: &'static str,
}
#[derive(Clone, Copy)]
struct FrameSize {
width: u64,
height: u64,
}
#[test]
fn sidecar_snapshots_prd_sites_to_rgba_files() -> Result<(), Box<dyn Error>> {
for site_url in PRD_SITE_COMPATIBILITY_URLS {
snapshot_prd_site(site_url)?;
for case in PRD_SITE_COMPATIBILITY_CASES {
for size in PRD_SITE_COMPATIBILITY_SIZES {
snapshot_prd_site(case, *size)?;
}
}
Ok(())
}
fn snapshot_prd_site(site_url: &str) -> Result<(), Box<dyn Error>> {
let site_name = site_url
fn snapshot_prd_site(
case: &PrdSiteCompatibilityCase,
size: FrameSize,
) -> Result<(), Box<dyn Error>> {
let site_name = case
.url
.chars()
.map(|character| if character.is_ascii_alphanumeric() { character } else { '-' })
.collect::<String>();
let output_path = std::env::temp_dir()
.join(format!("ely-servo-sidecar-{}-{site_name}.rgba", std::process::id()));
let output_path = std::env::temp_dir().join(format!(
"ely-servo-sidecar-{}-{site_name}-{}x{}.rgba",
std::process::id(),
size.width,
size.height
));
if output_path.exists() {
std::fs::remove_file(&output_path)?;
}
let output = Command::new(env!("CARGO_BIN_EXE_ely_servo_sidecar"))
.arg("snapshot")
.arg("--url")
.arg(site_url)
.arg("--rgba-out")
.arg(&output_path)
.arg("--width")
.arg(WIDTH.to_string())
.arg("--height")
.arg(HEIGHT.to_string())
.output()?;
let output = run_sidecar_snapshot(case.url, &output_path, size)?;
assert!(
output.status.success(),
"{site_url}\nstatus: {:?}\nstdout: {}\nstderr: {}",
"{} {}x{}\nstatus: {:?}\nstdout: {}\nstderr: {}",
case.url,
size.width,
size.height,
output.status.code(),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
let report: serde_json::Value = serde_json::from_slice(&output.stdout)?;
assert_eq!(report_field_as_u64(&report, "width")?, WIDTH, "{site_url}");
assert_eq!(report_field_as_u64(&report, "height")?, HEIGHT, "{site_url}");
assert_eq!(report_field_as_u64(&report, "rgba_byte_count")?, WIDTH * HEIGHT * 4, "{site_url}");
assert!(report_field_as_u64(&report, "non_white_pixel_count")? > 0, "{site_url}");
assert!(report_field_as_u64(&report, "sample_hash")? > 0, "{site_url}");
assert_eq!(std::fs::metadata(&output_path)?.len(), WIDTH * HEIGHT * 4);
assert_eq!(report_field_as_u64(&report, "width")?, size.width, "{}", case.url);
assert_eq!(report_field_as_u64(&report, "height")?, size.height, "{}", case.url);
assert_eq!(
report_field_as_u64(&report, "rgba_byte_count")?,
size.width * size.height * 4,
"{}",
case.url
);
assert_report_text_contains(&report, "loaded_url", case.url)?;
assert_report_text_contains(&report, "title", case.title_fragment)?;
assert!(report_field_as_u64(&report, "non_white_pixel_count")? > 0, "{}", case.url);
assert!(
report_field_as_u64(&report, "content_pixel_count")? >= MINIMUM_CONTENT_PIXELS,
"{}",
case.url
);
assert!(report_field_as_u64(&report, "sample_hash")? > 0, "{}", case.url);
assert_eq!(std::fs::metadata(&output_path)?.len(), size.width * size.height * 4);
std::fs::remove_file(&output_path)?;
Ok(())
}
fn run_sidecar_snapshot(
site_url: &str,
output_path: &std::path::Path,
size: FrameSize,
) -> Result<Output, Box<dyn Error>> {
let mut child = Command::new(env!("CARGO_BIN_EXE_ely_servo_sidecar"))
.arg("snapshot")
.arg("--url")
.arg(site_url)
.arg("--rgba-out")
.arg(output_path)
.arg("--width")
.arg(size.width.to_string())
.arg("--height")
.arg(size.height.to_string())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let started_at = Instant::now();
loop {
if child.try_wait()?.is_some() {
return child.wait_with_output().map_err(Into::into);
}
if started_at.elapsed() >= SIDECAR_TIMEOUT {
terminate_child(child)?;
return Err(format!(
"timed out rendering {site_url} at {}x{}",
size.width, size.height
)
.into());
}
thread::sleep(SIDECAR_POLL_INTERVAL);
}
}
fn terminate_child(mut child: Child) -> Result<(), Box<dyn Error>> {
match child.kill() {
Ok(()) => {
let _output = child.wait_with_output()?;
Ok(())
}
Err(error) if error.kind() == io::ErrorKind::InvalidInput => Ok(()),
Err(error) => Err(error.into()),
}
}
fn assert_report_text_contains(
report: &serde_json::Value,
field: &'static str,
fragment: &str,
) -> Result<(), Box<dyn Error>> {
let value = report
.get(field)
.and_then(serde_json::Value::as_str)
.ok_or_else(|| format!("missing text report field: {field}"))?;
assert!(value.contains(fragment), "{field}: {value}");
Ok(())
}
fn report_field_as_u64(
report: &serde_json::Value,
field: &'static str,
+25 -8
View File
@@ -7,7 +7,16 @@ use ely_servo_host::{
NavigationRequest, ServoHost, ServoHostError, ServoSurfaceSize, SoftwareServoHost, WebViewState,
};
const PRD_SITE_COMPATIBILITY_URLS: &[&str] = &["https://example.com", "https://servo.org"];
const MINIMUM_CONTENT_PIXELS: u64 = 1_000;
const PRD_SITE_COMPATIBILITY_CASES: &[PrdSiteCompatibilityCase] = &[
PrdSiteCompatibilityCase { url: "https://example.com", title_fragment: "Example Domain" },
PrdSiteCompatibilityCase { url: "https://servo.org", title_fragment: "Servo" },
];
struct PrdSiteCompatibilityCase {
url: &'static str,
title_fragment: &'static str,
}
#[test]
fn manages_real_servo_webview_lifecycle() -> Result<(), Box<dyn Error>> {
@@ -35,22 +44,28 @@ fn manages_real_servo_webview_lifecycle() -> Result<(), Box<dyn Error>> {
snapshot.url().is_some_and(|value| value.starts_with("data:text/html,")),
"snapshot: {snapshot:?}"
);
assert_rendered_frame_has_content(&host, "data:text/html")?;
assert_rendered_frame_has_content(&host, "data:text/html", 1)?;
let mut previous_frame_hash = Some(host.last_rendered_frame()?.sample_hash());
for site_url in PRD_SITE_COMPATIBILITY_URLS {
for site in PRD_SITE_COMPATIBILITY_CASES {
let tab_id = TabId::new();
let url = UrlText::parse(*site_url)?;
let url = UrlText::parse(site.url)?;
host.navigate(NavigationRequest { webview_id: webview_id.clone(), tab_id, url })?;
let snapshot = wait_for_rendered_webview(&mut host, &webview_id, previous_frame_hash)?;
assert_eq!(snapshot.state(), &WebViewState::Complete, "{site_url}: {snapshot:?}");
assert_eq!(snapshot.state(), &WebViewState::Complete, "{}: {snapshot:?}", site.url);
assert!(
snapshot.url().is_some_and(|value| value.starts_with(site_url)),
"{site_url}: {snapshot:?}"
snapshot.url().is_some_and(|value| value.starts_with(site.url)),
"{}: {snapshot:?}",
site.url
);
assert_rendered_frame_has_content(&host, site_url)?;
assert!(
snapshot.title().is_some_and(|value| value.contains(site.title_fragment)),
"{}: {snapshot:?}",
site.url
);
assert_rendered_frame_has_content(&host, site.url, MINIMUM_CONTENT_PIXELS)?;
previous_frame_hash = Some(host.last_rendered_frame()?.sample_hash());
}
@@ -96,6 +111,7 @@ fn wait_for_rendered_webview(
fn assert_rendered_frame_has_content(
host: &SoftwareServoHost,
label: &str,
minimum_content_pixels: u64,
) -> Result<(), Box<dyn Error>> {
let frame = host.last_rendered_frame()?;
@@ -103,6 +119,7 @@ fn assert_rendered_frame_has_content(
assert_eq!(frame.height(), 480, "{label}: {frame:?}");
assert!(frame.opaque_pixel_count() > 0, "{label}: {frame:?}");
assert!(frame.non_white_pixel_count() > 0, "{label}: {frame:?}");
assert!(frame.content_pixel_count() >= minimum_content_pixels, "{label}: {frame:?}");
assert_ne!(frame.sample_hash(), 0, "{label}: {frame:?}");
Ok(())
}