Skip to content

web: Add swf-tests package, for running the swf test suite in a browser #15243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"flv",
"web",
"web/packages/extension/safari",
"web/packages/swf-tests/runner",
"wstr",
"scanner",
"exporter",
Expand Down
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ unknown-git = "deny"
# github.com organizations to allow git sources for
github = [
"ruffle-rs",
"manuel-woelker", # https://github.com/manuel-woelker/rust-vfs
]
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion render/wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ impl WgpuRenderBackend<SwapChainTarget> {
Self::new(Arc::new(descriptors), target)
}

#[cfg(target_family = "wasm")]
pub async fn descriptors_for_offscreen_canvas(
backends: wgpu::Backends,
canvas: web_sys::OffscreenCanvas,
) -> Result<Arc<Descriptors>, Error> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
..Default::default()
});
let surface = instance.create_surface(wgpu::SurfaceTarget::OffscreenCanvas(canvas))?;
let (adapter, device, queue) = request_adapter_and_device(
backends,
&instance,
Some(&surface),
wgpu::PowerPreference::HighPerformance,
None,
)
.await?;
let descriptors = Descriptors::new(instance, adapter, device, queue);
Ok(Arc::new(descriptors))
}

/// # Safety
/// See [`wgpu::SurfaceTargetUnsafe`] variants for safety requirements.
#[cfg(not(target_family = "wasm"))]
Expand Down Expand Up @@ -134,8 +156,8 @@ impl WgpuRenderBackend<SwapChainTarget> {
}
}

#[cfg(not(target_family = "wasm"))]
impl WgpuRenderBackend<crate::target::TextureTarget> {
#[cfg(not(target_family = "wasm"))]
pub fn for_offscreen(
size: (u32, u32),
backend: wgpu::Backends,
Expand Down
1 change: 0 additions & 1 deletion render/wgpu/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ pub fn capture_image<R, F: FnOnce(&[u8], u32) -> R>(
result
}

#[cfg(not(target_family = "wasm"))]
pub fn buffer_to_image(
device: &wgpu::Device,
buffer: &wgpu::Buffer,
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ serde = "1.0.196"
toml = "0.8.9"
anyhow = "1.0.79"
async-channel = "2.1.1"
vfs = "0.10.0"
vfs = { git = "https://github.com/manuel-woelker/rust-vfs", rev = "722ecc60b76c90fd7ce4c6dac6114bc13271cb65" }
percent-encoding = "2.3.1"

[features]
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl RequiredFeatures {
pub struct PlayerOptions {
max_execution_duration: Option<Duration>,
viewport_dimensions: Option<ViewportDimensions>,
with_renderer: Option<RenderOptions>,
pub with_renderer: Option<RenderOptions>,
with_audio: bool,
with_video: bool,
runtime: PlayerRuntime,
Expand Down Expand Up @@ -371,7 +371,7 @@ impl ImageComparison {
#[derive(Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RenderOptions {
optional: bool,
pub optional: bool,
pub sample_count: u32,
pub exclude_warp: bool,
}
Expand Down
91 changes: 91 additions & 0 deletions tests/framework/src/results.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::options::{Approximations, ImageComparison};
use image::RgbaImage;

#[derive(Default)]
pub struct TestResults {
pub results: Vec<TestResult>,
}

impl TestResults {
pub fn success(&self) -> bool {
self.results.iter().all(|img| img.success())
}

pub fn add(&mut self, result: impl Into<TestResult>) {
self.results.push(result.into());
}
}

pub enum TestResult {
Trace(TraceComparisonResult),
Approximation(NumberApproximationResult),
Image(ImageComparisonResult),
InvalidTest(String),
}

impl TestResult {
pub fn success(&self) -> bool {
match self {
TestResult::Trace(result) => result.success(),
TestResult::Approximation(result) => result.success(),
TestResult::Image(result) => result.success(),
TestResult::InvalidTest(_) => false,
}
}
}

impl From<String> for TestResult {
fn from(value: String) -> Self {
TestResult::InvalidTest(value)
}
}

pub struct TraceComparisonResult {
pub expected: String,
pub actual: String,
}

impl TraceComparisonResult {
pub fn success(&self) -> bool {
self.expected == self.actual
}
}

pub struct NumberApproximationResult {
pub line: usize,
pub expected: f64,
pub actual: f64,
pub options: Approximations,
pub group: Option<u32>,
pub surrounding_text: Option<TraceComparisonResult>,
}

impl NumberApproximationResult {
pub fn success(&self) -> bool {
if let Some(surrounding_text) = &self.surrounding_text {
if !surrounding_text.success() {
return false;
}
}
// NaNs should be able to pass in an approx test.
if self.actual.is_nan() && self.expected.is_nan() {
return true;
}
self.options.compare(self.actual, self.expected)
}
}

pub struct ImageComparisonResult {
pub name: String,
pub expected: RgbaImage,
pub actual: RgbaImage,
pub difference_color: Option<RgbaImage>,
pub difference_alpha: Option<RgbaImage>,
pub options: ImageComparison,
}

impl ImageComparisonResult {
pub fn success(&self) -> bool {
self.expected == self.actual
}
}
Loading