Implement tests for `wrap_line`.

These tests depend on the particulars of our default device font, Noto Sans. If this test proves to be fragile we may need to create a testing font with a locked width and kerning table...
This commit is contained in:
David Wendt 2020-05-23 16:38:54 -04:00
parent cb47e0bae8
commit 18e93b3d11
2 changed files with 125 additions and 2 deletions

View File

@ -282,3 +282,126 @@ impl FontDescriptor {
self.is_italic self.is_italic
} }
} }
#[cfg(test)]
mod tests {
use crate::backend::render::{NullRenderer, RenderBackend};
use crate::font::Font;
use crate::player::{Player, DEVICE_FONT_TAG};
use gc_arena::{rootless_arena, MutationContext};
use swf::Twips;
fn with_device_font<F>(callback: F)
where
F: for<'gc> FnOnce(MutationContext<'gc, '_>, Font<'gc>),
{
rootless_arena(|mc| {
let mut renderer: Box<dyn RenderBackend> = Box::new(NullRenderer::new());
let device_font = Player::load_device_font(mc, DEVICE_FONT_TAG, &mut renderer).unwrap();
callback(mc, device_font);
})
}
#[test]
fn wrap_line_no_breakpoint() {
with_device_font(|_mc, df| {
let string = "abcdefghijklmnopqrstuv";
let breakpoint = df.wrap_line(
&string,
Twips::from_pixels(12.0),
Twips::from_pixels(200.0),
Twips::from_pixels(0.0),
);
assert_eq!(None, breakpoint);
});
}
#[test]
fn wrap_line_breakpoint_every_word() {
with_device_font(|_mc, df| {
let string = "abcd efgh ijkl mnop";
let mut last_bp = 0;
let breakpoint = df.wrap_line(
&string,
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(Some(4), breakpoint);
last_bp += breakpoint.unwrap() + 1;
let breakpoint2 = df.wrap_line(
&string[last_bp..],
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(Some(4), breakpoint2);
last_bp += breakpoint2.unwrap() + 1;
let breakpoint3 = df.wrap_line(
&string[last_bp..],
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(Some(4), breakpoint3);
last_bp += breakpoint3.unwrap() + 1;
let breakpoint4 = df.wrap_line(
&string[last_bp..],
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(None, breakpoint4);
});
}
#[test]
fn wrap_line_breakpoint_irregular_sized_words() {
with_device_font(|_mc, df| {
let string = "abcd i j kl mnop q rstuv";
let mut last_bp = 0;
let breakpoint = df.wrap_line(
&string,
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(Some(11), breakpoint);
last_bp += breakpoint.unwrap() + 1;
let breakpoint2 = df.wrap_line(
&string[last_bp..],
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(Some(6), breakpoint2);
last_bp += breakpoint2.unwrap() + 1;
let breakpoint3 = df.wrap_line(
&string[last_bp..],
Twips::from_pixels(12.0),
Twips::from_pixels(30.0),
Twips::from_pixels(0.0),
);
assert_eq!(None, breakpoint3);
});
}
}

View File

@ -23,7 +23,7 @@ use std::convert::TryFrom;
use std::ops::DerefMut; use std::ops::DerefMut;
use std::sync::{Arc, Mutex, Weak}; use std::sync::{Arc, Mutex, Weak};
static DEVICE_FONT_TAG: &[u8] = include_bytes!("../assets/noto-sans-definefont3.bin"); pub static DEVICE_FONT_TAG: &[u8] = include_bytes!("../assets/noto-sans-definefont3.bin");
/// The newest known Flash Player version, serves as a default to /// The newest known Flash Player version, serves as a default to
/// `player_version`. /// `player_version`.
@ -910,7 +910,7 @@ impl Player {
/// Loads font data from the given buffer. /// Loads font data from the given buffer.
/// The buffer should be the `DefineFont3` info for the tag. /// The buffer should be the `DefineFont3` info for the tag.
/// The tag header should not be included. /// The tag header should not be included.
fn load_device_font<'gc>( pub fn load_device_font<'gc>(
gc_context: gc_arena::MutationContext<'gc, '_>, gc_context: gc_arena::MutationContext<'gc, '_>,
data: &[u8], data: &[u8],
renderer: &mut Renderer, renderer: &mut Renderer,