From a38f74d989e47cc53ea5cb23bbcdf88a665354f7 Mon Sep 17 00:00:00 2001 From: Mike Welsh Date: Wed, 5 Apr 2023 10:30:24 -0700 Subject: [PATCH] tests: Add `NavigatorTestBackend` --- Cargo.lock | 1 + tests/Cargo.toml | 1 + tests/tests/util/mod.rs | 1 + tests/tests/util/navigator.rs | 58 +++++++++++++++++++++++++++++++++++ tests/tests/util/runner.rs | 5 +-- 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/tests/util/navigator.rs diff --git a/Cargo.lock b/Cargo.lock index c1f552cb2..09a084648 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4175,6 +4175,7 @@ dependencies = [ "ruffle_video_software", "serde", "toml 0.5.11", + "url", "walkdir", ] diff --git a/tests/Cargo.toml b/tests/Cargo.toml index cc2eb8b08..9c39eec30 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -16,6 +16,7 @@ ruffle_input_format = { path = "input-format" } ruffle_video_software = { path = "../video/software", optional = true } image = { version = "0.24.5", default-features = false, features = ["png"] } regex = "1.7.1" +url = "2.3.1" [features] # Enable running image comparison tests. This is off by default, diff --git a/tests/tests/util/mod.rs b/tests/tests/util/mod.rs index 11a14b8b3..29e80c4a7 100644 --- a/tests/tests/util/mod.rs +++ b/tests/tests/util/mod.rs @@ -2,6 +2,7 @@ // https://doc.rust-lang.org/book/ch11-03-test-organization.html pub mod environment; +pub mod navigator; pub mod options; pub mod runner; pub mod test; diff --git a/tests/tests/util/navigator.rs b/tests/tests/util/navigator.rs new file mode 100644 index 000000000..a6d50190d --- /dev/null +++ b/tests/tests/util/navigator.rs @@ -0,0 +1,58 @@ +use ruffle_core::backend::navigator::{ + NavigationMethod, NavigatorBackend, NullExecutor, NullSpawner, OwnedFuture, Request, Response, +}; +use ruffle_core::indexmap::IndexMap; +use ruffle_core::loader::Error; +use std::path::{Path, PathBuf}; +use url::Url; + +pub struct NavigatorTestBackend { + spawner: NullSpawner, + relative_base_path: PathBuf, +} + +impl NavigatorTestBackend { + pub fn with_base_path(path: &Path, executor: &NullExecutor) -> Result { + Ok(Self { + spawner: executor.spawner(), + relative_base_path: path.canonicalize()?, + }) + } + + fn url_from_file_path(path: &Path) -> Result { + Url::from_file_path(path) + } +} + +impl NavigatorBackend for NavigatorTestBackend { + fn navigate_to_url( + &self, + _url: String, + _target: String, + _vars_method: Option<(NavigationMethod, IndexMap)>, + ) { + } + + fn fetch(&self, request: Request) -> OwnedFuture { + let mut path = self.relative_base_path.clone(); + path.push(request.url()); + + Box::pin(async move { + let url = Self::url_from_file_path(&path) + .map_err(|()| Error::FetchError("Invalid URL".to_string()))? + .into(); + + let body = std::fs::read(path).map_err(|e| Error::FetchError(e.to_string()))?; + + Ok(Response { url, body }) + }) + } + + fn spawn_future(&mut self, future: OwnedFuture<(), Error>) { + //self.spawner.spawn_local(future); + } + + fn pre_process_url(&self, url: Url) -> Url { + url + } +} diff --git a/tests/tests/util/runner.rs b/tests/tests/util/runner.rs index 7926f37d7..0d510577f 100644 --- a/tests/tests/util/runner.rs +++ b/tests/tests/util/runner.rs @@ -1,3 +1,4 @@ +use crate::util::navigator::NavigatorTestBackend; use crate::util::test::Test; use anyhow::{anyhow, Result}; use ruffle_core::backend::audio::{ @@ -5,7 +6,7 @@ use ruffle_core::backend::audio::{ SoundTransform, }; use ruffle_core::backend::log::LogBackend; -use ruffle_core::backend::navigator::{NullExecutor, NullNavigatorBackend}; +use ruffle_core::backend::navigator::NullExecutor; use ruffle_core::events::KeyCode; use ruffle_core::events::MouseButton as RuffleMouseButton; use ruffle_core::impl_audio_mixer_backend; @@ -84,7 +85,7 @@ pub fn run_swf( let builder = PlayerBuilder::new() .with_log(TestLogBackend::new(trace_output.clone())) - .with_navigator(NullNavigatorBackend::with_base_path(base_path, &executor)?) + .with_navigator(NavigatorTestBackend::with_base_path(base_path, &executor)?) .with_max_execution_duration(Duration::from_secs(300)) .with_viewport_dimensions( movie.width().to_pixels() as u32,