tests: Add `NavigatorTestBackend`

This commit is contained in:
Mike Welsh 2023-04-05 10:30:24 -07:00
parent 3716422b37
commit a38f74d989
5 changed files with 64 additions and 2 deletions

1
Cargo.lock generated
View File

@ -4175,6 +4175,7 @@ dependencies = [
"ruffle_video_software",
"serde",
"toml 0.5.11",
"url",
"walkdir",
]

View File

@ -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,

View File

@ -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;

View File

@ -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<Self, std::io::Error> {
Ok(Self {
spawner: executor.spawner(),
relative_base_path: path.canonicalize()?,
})
}
fn url_from_file_path(path: &Path) -> Result<Url, ()> {
Url::from_file_path(path)
}
}
impl NavigatorBackend for NavigatorTestBackend {
fn navigate_to_url(
&self,
_url: String,
_target: String,
_vars_method: Option<(NavigationMethod, IndexMap<String, String>)>,
) {
}
fn fetch(&self, request: Request) -> OwnedFuture<Response, Error> {
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
}
}

View File

@ -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,