Files
arena-tracker/crates/core/tests/common/champions.rs
T

148 lines
4.5 KiB
Rust

use super::*;
/// Champion reference data served from memory.
pub struct FakeChampions {
version: String,
champions: Vec<Champion>,
failing: bool,
no_portrait: Vec<String>,
catalog_calls: Arc<AtomicUsize>,
/// What every card portrait download delivers.
card_art: Vec<u8>,
/// The card portrait downloads fail with a network error while this is set.
card_failure: Arc<AtomicBool>,
card_calls: Arc<AtomicUsize>,
pub splash_calls: Arc<AtomicUsize>,
}
/// A small valid JPEG, tall like the loading art (40 x 60 px).
pub fn card_art() -> Vec<u8> {
let img = image::RgbImage::from_fn(40, 60, |x, y| image::Rgb([x as u8 * 6, y as u8 * 4, 90]));
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new(&mut out)
.encode(img.as_raw(), 40, 60, image::ExtendedColorType::Rgb8)
.unwrap();
out
}
/// The loading art at (59, 6) in a landscape JPEG.
pub fn splash_art() -> Vec<u8> {
let card = image::load_from_memory(&card_art()).unwrap().into_rgb8();
let mut splash = image::RgbImage::new(112, 75);
image::imageops::replace(&mut splash, &card, 59, 6);
let mut out = Vec::new();
image::codecs::jpeg::JpegEncoder::new(&mut out)
.encode_image(&splash)
.unwrap();
out
}
impl FakeChampions {
/// `champions`: (id, key, display name).
pub fn new(version: &str, champions: &[(u32, &str, &str)]) -> FakeChampions {
FakeChampions {
version: version.into(),
champions: champions
.iter()
.map(|&(id, key, name)| Champion {
id,
key: key.into(),
name: name.into(),
})
.collect(),
failing: false,
no_portrait: Vec::new(),
catalog_calls: Arc::default(),
card_art: card_art(),
card_failure: Arc::default(),
card_calls: Arc::default(),
splash_calls: Arc::default(),
}
}
/// Every card portrait download delivers these bytes.
pub fn with_card_art(mut self, bytes: Vec<u8>) -> FakeChampions {
self.card_art = bytes;
self
}
/// Set it to make the card portrait downloads fail (and clear it to let them succeed).
pub fn card_failure(&self) -> Arc<AtomicBool> {
self.card_failure.clone()
}
/// Card portrait downloads attempted.
pub fn card_counter(&self) -> Arc<AtomicUsize> {
self.card_calls.clone()
}
/// Every call fails with a network error.
pub fn failing(mut self) -> FakeChampions {
self.failing = true;
self
}
/// The portrait download of this champion key fails.
pub fn without_portrait(mut self, key: &str) -> FakeChampions {
self.no_portrait.push(key.into());
self
}
pub fn catalog_counter(&self) -> Arc<AtomicUsize> {
self.catalog_calls.clone()
}
fn check(&self) -> Result<(), SourceError> {
if self.failing {
Err(SourceError::Network("hors ligne".into()))
} else {
Ok(())
}
}
}
impl ChampionSource for FakeChampions {
fn latest_version(&self) -> Result<String, SourceError> {
self.check()?;
Ok(self.version.clone())
}
fn catalog(&self, version: &str) -> Result<ChampionCatalog, SourceError> {
self.catalog_calls.fetch_add(1, Ordering::SeqCst);
self.check()?;
Ok(ChampionCatalog {
version: version.into(),
champions: self.champions.clone(),
})
}
fn portrait(&self, _version: &str, key: &str) -> Result<Vec<u8>, SourceError> {
self.check()?;
if self.no_portrait.iter().any(|k| k == key) {
return Err(SourceError::NotFound);
}
Ok(format!("png of {key}").into_bytes())
}
fn card_portrait(&self, key: &str) -> Result<Vec<u8>, SourceError> {
self.card_calls.fetch_add(1, Ordering::SeqCst);
self.check()?;
if self.card_failure.load(Ordering::SeqCst) {
return Err(SourceError::Network("coupé".into()));
}
if self.no_portrait.iter().any(|k| k == key) {
return Err(SourceError::NotFound);
}
Ok(self.card_art.clone())
}
fn splash(&self, _: &str) -> Result<Vec<u8>, SourceError> {
self.splash_calls.fetch_add(1, Ordering::SeqCst);
self.check()?;
if self.card_failure.load(Ordering::SeqCst) {
return Err(SourceError::Network("coupé".into()));
}
Ok(splash_art())
}
}