Skip to content

Commit aa55b82

Browse files
committed
web: Add swf-tests package, for running the swf test suite in a browser
1 parent fe2ed81 commit aa55b82

40 files changed

+2825
-15
lines changed

Cargo.lock

Lines changed: 61 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"flv",
99
"web",
1010
"web/packages/extension/safari",
11+
"web/packages/swf-tests/runner",
1112
"wstr",
1213
"scanner",
1314
"exporter",

deny.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ unknown-git = "deny"
7171
# github.com organizations to allow git sources for
7272
github = [
7373
"ruffle-rs",
74+
"manuel-woelker", # https://github.com/manuel-woelker/rust-vfs
7475
]

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

render/wgpu/src/backend.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ impl WgpuRenderBackend<SwapChainTarget> {
8585
Self::new(Arc::new(descriptors), target)
8686
}
8787

88+
#[cfg(target_family = "wasm")]
89+
pub async fn descriptors_for_offscreen_canvas(
90+
backends: wgpu::Backends,
91+
canvas: web_sys::OffscreenCanvas,
92+
) -> Result<Arc<Descriptors>, Error> {
93+
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
94+
backends,
95+
..Default::default()
96+
});
97+
let surface = instance.create_surface(wgpu::SurfaceTarget::OffscreenCanvas(canvas))?;
98+
let (adapter, device, queue) = request_adapter_and_device(
99+
backends,
100+
&instance,
101+
Some(&surface),
102+
wgpu::PowerPreference::HighPerformance,
103+
None,
104+
)
105+
.await?;
106+
let descriptors = Descriptors::new(instance, adapter, device, queue);
107+
Ok(Arc::new(descriptors))
108+
}
109+
88110
/// # Safety
89111
/// See [`wgpu::SurfaceTargetUnsafe`] variants for safety requirements.
90112
#[cfg(not(target_family = "wasm"))]
@@ -134,8 +156,8 @@ impl WgpuRenderBackend<SwapChainTarget> {
134156
}
135157
}
136158

137-
#[cfg(not(target_family = "wasm"))]
138159
impl WgpuRenderBackend<crate::target::TextureTarget> {
160+
#[cfg(not(target_family = "wasm"))]
139161
pub fn for_offscreen(
140162
size: (u32, u32),
141163
backend: wgpu::Backends,

render/wgpu/src/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ pub fn capture_image<R, F: FnOnce(&[u8], u32) -> R>(
154154
result
155155
}
156156

157-
#[cfg(not(target_family = "wasm"))]
158157
pub fn buffer_to_image(
159158
device: &wgpu::Device,
160159
buffer: &wgpu::Buffer,

tests/framework/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ serde = "1.0.196"
2727
toml = "0.8.9"
2828
anyhow = "1.0.79"
2929
async-channel = "2.1.1"
30-
vfs = "0.10.0"
30+
vfs = { git = "https://github.com/manuel-woelker/rust-vfs", rev = "722ecc60b76c90fd7ce4c6dac6114bc13271cb65" }
3131
percent-encoding = "2.3.1"
3232

3333
[features]

tests/framework/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl RequiredFeatures {
146146
pub struct PlayerOptions {
147147
max_execution_duration: Option<Duration>,
148148
viewport_dimensions: Option<ViewportDimensions>,
149-
with_renderer: Option<RenderOptions>,
149+
pub with_renderer: Option<RenderOptions>,
150150
with_audio: bool,
151151
with_video: bool,
152152
runtime: PlayerRuntime,
@@ -371,7 +371,7 @@ impl ImageComparison {
371371
#[derive(Clone, Deserialize)]
372372
#[serde(default, deny_unknown_fields)]
373373
pub struct RenderOptions {
374-
optional: bool,
374+
pub optional: bool,
375375
pub sample_count: u32,
376376
pub exclude_warp: bool,
377377
}

tests/framework/src/results.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use crate::options::{Approximations, ImageComparison};
2+
use image::RgbaImage;
3+
4+
#[derive(Default)]
5+
pub struct TestResults {
6+
pub results: Vec<TestResult>,
7+
}
8+
9+
impl TestResults {
10+
pub fn success(&self) -> bool {
11+
self.results.iter().all(|img| img.success())
12+
}
13+
14+
pub fn add(&mut self, result: impl Into<TestResult>) {
15+
self.results.push(result.into());
16+
}
17+
}
18+
19+
pub enum TestResult {
20+
Trace(TraceComparisonResult),
21+
Approximation(NumberApproximationResult),
22+
Image(ImageComparisonResult),
23+
InvalidTest(String),
24+
}
25+
26+
impl TestResult {
27+
pub fn success(&self) -> bool {
28+
match self {
29+
TestResult::Trace(result) => result.success(),
30+
TestResult::Approximation(result) => result.success(),
31+
TestResult::Image(result) => result.success(),
32+
TestResult::InvalidTest(_) => false,
33+
}
34+
}
35+
}
36+
37+
impl From<String> for TestResult {
38+
fn from(value: String) -> Self {
39+
TestResult::InvalidTest(value)
40+
}
41+
}
42+
43+
pub struct TraceComparisonResult {
44+
pub expected: String,
45+
pub actual: String,
46+
}
47+
48+
impl TraceComparisonResult {
49+
pub fn success(&self) -> bool {
50+
self.expected == self.actual
51+
}
52+
}
53+
54+
pub struct NumberApproximationResult {
55+
pub line: usize,
56+
pub expected: f64,
57+
pub actual: f64,
58+
pub options: Approximations,
59+
pub group: Option<u32>,
60+
pub surrounding_text: Option<TraceComparisonResult>,
61+
}
62+
63+
impl NumberApproximationResult {
64+
pub fn success(&self) -> bool {
65+
if let Some(surrounding_text) = &self.surrounding_text {
66+
if !surrounding_text.success() {
67+
return false;
68+
}
69+
}
70+
// NaNs should be able to pass in an approx test.
71+
if self.actual.is_nan() && self.expected.is_nan() {
72+
return true;
73+
}
74+
self.options.compare(self.actual, self.expected)
75+
}
76+
}
77+
78+
pub struct ImageComparisonResult {
79+
pub name: String,
80+
pub expected: RgbaImage,
81+
pub actual: RgbaImage,
82+
pub difference_color: Option<RgbaImage>,
83+
pub difference_alpha: Option<RgbaImage>,
84+
pub options: ImageComparison,
85+
}
86+
87+
impl ImageComparisonResult {
88+
pub fn success(&self) -> bool {
89+
self.expected == self.actual
90+
}
91+
}

0 commit comments

Comments
 (0)