use super::*;
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
fn square(list: &mut DrawList, x: f32) {
list.rect(Vec2::new(x, 0.0), Vec2::new(x + 10.0, 10.0), RED);
}
fn group(commands: Range<usize>, opacity: f32) -> OpacityGroup {
OpacityGroup { commands, opacity }
}
#[test]
fn a_group_is_the_commands_between_its_push_and_pop() {
let mut list = DrawList::new();
square(&mut list, 0.0);
list.push_opacity(0.5);
square(&mut list, 20.0);
square(&mut list, 40.0);
list.pop_opacity().expect("pushed above");
square(&mut list, 60.0);
assert_eq!(list.opacity_groups(), &[group(1..3, 0.5)]);
}
#[test]
fn nested_groups_lie_inside_their_parent_in_push_order() {
let mut list = DrawList::new();
list.push_opacity(0.8);
square(&mut list, 0.0);
list.push_opacity(0.5);
square(&mut list, 20.0);
list.pop_opacity().expect("pushed above");
list.push_opacity(0.25);
square(&mut list, 40.0);
list.pop_opacity().expect("pushed above");
square(&mut list, 60.0);
list.pop_opacity().expect("pushed above");
assert_eq!(
list.opacity_groups(),
&[group(0..4, 0.8), group(1..2, 0.5), group(2..3, 0.25)]
);
}
#[test]
fn an_opacity_of_one_records_nothing_but_still_pairs_with_its_pop() {
let mut list = DrawList::new();
list.push_opacity(0.5);
list.push_opacity(1.0);
square(&mut list, 0.0);
list.pop_opacity().expect("the push at one");
square(&mut list, 20.0);
list.pop_opacity().expect("the push at a half");
assert_eq!(list.opacity_groups(), &[group(0..2, 0.5)]);
assert_eq!(list.pop_opacity(), Err(OpacityUnderflow));
}
#[test]
fn the_opacity_is_clamped_and_nan_is_one() {
for (pushed, recorded) in [
(1.5, None),
(f32::INFINITY, None),
(f32::NAN, None),
(-0.5, Some(0.0)),
(f32::NEG_INFINITY, Some(0.0)),
(0.3, Some(0.3)),
] {
let mut list = DrawList::new();
list.push_opacity(pushed);
square(&mut list, 0.0);
list.pop_opacity().expect("pushed above");
let want: Vec<OpacityGroup> = recorded.map(|o| group(0..1, o)).into_iter().collect();
assert_eq!(list.opacity_groups(), want.as_slice(), "pushed {pushed}");
}
}
#[test]
fn a_pop_with_no_push_is_an_underflow() {
let mut list = DrawList::new();
assert_eq!(list.pop_opacity(), Err(OpacityUnderflow));
list.push_opacity(0.5);
square(&mut list, 0.0);
list.pop_opacity().expect("pushed above");
assert_eq!(list.pop_opacity(), Err(OpacityUnderflow));
assert_eq!(list.opacity_groups(), &[group(0..1, 0.5)]);
assert_eq!(
OpacityUnderflow.to_string(),
"pop_opacity was called with no opacity group pushed"
);
}
#[test]
fn an_open_group_runs_to_the_cut_and_a_clear_drops_it() {
let mut list = DrawList::new();
list.push_opacity(0.5);
square(&mut list, 0.0);
square(&mut list, 20.0);
assert_eq!(list.opacity_groups(), &[group(0..2, 0.5)]);
list.begin_overlay();
square(&mut list, 40.0);
assert_eq!(list.opacity_groups(), &[group(0..2, 0.5)]);
assert_eq!(
list.pop_opacity(),
Err(OpacityUnderflow),
"the cut closed it"
);
list.push_opacity(0.5);
list.clear();
assert!(list.opacity_groups().is_empty());
assert_eq!(
list.pop_opacity(),
Err(OpacityUnderflow),
"the clear closed it"
);
}
#[test]
fn a_group_expands_to_its_commands_indices_and_bounds() {
let mut list = DrawList::new();
square(&mut list, 0.0);
list.push_opacity(0.5);
square(&mut list, 20.0);
list.push_opacity(1.0);
square(&mut list, 40.0);
list.pop_opacity().expect("pushed above");
list.pop_opacity().expect("pushed above");
square(&mut list, 60.0);
let triangles = list.to_triangles_split(None, None, 1.0);
assert_eq!(triangles.indices.len(), 24, "every square still draws");
assert_eq!(
triangles.groups,
[OpacityRun {
indices: 6..18,
opacity: 0.5,
bounds: ClipRect {
min: Vec2::new(20.0, 0.0),
max: Vec2::new(50.0, 10.0),
},
}]
);
}
#[test]
fn a_groups_bounds_are_clamped_to_its_commands_clips() {
let mut list = DrawList::new();
list.push_opacity(0.5);
list.push_clip(Vec2::new(2.0, 3.0), Vec2::new(6.0, 100.0));
square(&mut list, 0.0);
list.pop_clip().expect("pushed above");
list.pop_opacity().expect("pushed above");
let triangles = list.to_triangles_split(None, None, 1.0);
assert_eq!(
triangles.groups[0].bounds,
ClipRect {
min: Vec2::new(2.0, 3.0),
max: Vec2::new(6.0, 10.0),
}
);
}
#[test]
fn a_group_at_zero_leaves_its_commands_out_of_the_triangles() {
let mut list = DrawList::new();
square(&mut list, 0.0);
list.push_opacity(0.0);
square(&mut list, 20.0);
list.push_opacity(0.5);
square(&mut list, 40.0);
list.pop_opacity().expect("pushed above");
list.pop_opacity().expect("pushed above");
square(&mut list, 60.0);
assert_eq!(list.len(), 4, "the hidden commands stay in the list");
let triangles = list.to_triangles_split(None, None, 1.0);
assert_eq!(triangles.indices.len(), 12);
let xs: Vec<f32> = triangles
.vertices
.iter()
.map(|vertex| vertex.pos.x)
.collect();
assert_eq!(xs, [0.0, 10.0, 10.0, 0.0, 60.0, 70.0, 70.0, 60.0]);
assert!(triangles.groups.is_empty());
}
#[test]
fn a_group_that_drew_nothing_is_no_run() {
let mut list = DrawList::new();
list.push_opacity(0.5);
list.pop_opacity().expect("pushed above");
list.push_opacity(0.5);
list.text(Vec2::ZERO, "no atlas, no glyphs", RED, 13.0);
list.pop_opacity().expect("pushed above");
assert_eq!(list.opacity_groups().len(), 2);
assert!(list.to_triangles_split(None, None, 1.0).groups.is_empty());
}
#[test]
fn nested_runs_lie_inside_their_parent_and_respect_the_cut() {
let mut list = DrawList::new();
list.push_opacity(0.8);
square(&mut list, 0.0);
list.push_opacity(0.5);
square(&mut list, 20.0);
list.begin_overlay();
list.push_opacity(0.25);
square(&mut list, 40.0);
list.pop_opacity().expect("pushed above");
let triangles = list.to_triangles_split(None, None, 1.0);
assert_eq!(triangles.overlay, 12);
let runs: Vec<(Range<u32>, f32)> = triangles
.groups
.iter()
.map(|run| (run.indices.clone(), run.opacity))
.collect();
assert_eq!(runs, [(0..12, 0.8), (6..12, 0.5), (12..18, 0.25)]);
}
#[test]
fn the_layer_quad_covers_the_snapped_bounds_inside_the_viewport() {
let run = OpacityRun {
indices: 0..6,
opacity: 0.25,
bounds: ClipRect {
min: Vec2::new(-3.5, 10.25),
max: Vec2::new(40.5, 300.0),
},
};
let quad = run.layer_quad((100, 200));
let corners: Vec<(Vec2, Vec2)> = quad.iter().map(|vertex| (vertex.pos, vertex.uv)).collect();
assert_eq!(
corners,
[
(Vec2::new(0.0, 200.0), Vec2::new(0.0, 1.0)),
(Vec2::new(41.0, 200.0), Vec2::new(0.41, 1.0)),
(Vec2::new(41.0, 10.0), Vec2::new(0.41, 0.05)),
(Vec2::new(0.0, 10.0), Vec2::new(0.0, 0.05)),
]
);
for vertex in quad {
assert_eq!(vertex.color, [0.25; 4]);
assert_eq!(vertex.primitive(), Some(Primitive::Texture));
assert_eq!(vertex.clip, ClipRect::NONE.lane());
}
}