use super::*;
const A: CanvasId = CanvasId(1);
const B: CanvasId = CanvasId(2);
type Fronts = HashMap<CanvasId, (u64, ())>;
fn open(fronts: &mut Fronts, id: CanvasId, token: u64) {
fronts.insert(id, (token, ()));
}
#[test]
fn every_encode_ends_the_frame_while_no_replacement_is_open() {
let mut boundary = FrameBoundary::default();
let fronts = Fronts::new();
for _ in 0..3 {
boundary.begin_encode();
assert!(boundary.end_encode(&fronts));
}
}
#[test]
fn a_replacement_drawn_over_several_encodes_is_one_frame() {
let mut boundary = FrameBoundary::default();
let mut fronts = Fronts::new();
boundary.begin_encode();
open(&mut fronts, A, 1);
assert!(!boundary.end_encode(&fronts), "the first batch does not end the frame");
boundary.begin_encode();
assert!(!boundary.end_encode(&fronts), "nor do the batches in between");
boundary.begin_encode();
assert!(boundary.end_replacement(&mut fronts, A).is_some());
assert!(boundary.end_encode(&fronts));
}
#[test]
fn a_replacement_begun_and_committed_in_one_encode_ends_it() {
let mut boundary = FrameBoundary::default();
let mut fronts = Fronts::new();
boundary.begin_encode();
open(&mut fronts, A, 1);
boundary.end_replacement(&mut fronts, A);
assert!(boundary.end_encode(&fronts));
}
#[test]
fn superseding_a_replacement_ends_the_frame_and_its_successor_starts_the_next() {
let mut boundary = FrameBoundary::default();
let mut fronts = Fronts::new();
boundary.begin_encode();
open(&mut fronts, A, 1);
assert!(!boundary.end_encode(&fronts));
boundary.begin_encode();
boundary.end_replacement(&mut fronts, A);
open(&mut fronts, A, 2);
assert!(boundary.end_encode(&fronts), "the superseded batches were a frame");
boundary.begin_encode();
assert!(!boundary.end_encode(&fronts), "the successor holds the next one open");
}
#[test]
fn any_canvas_holding_a_replacement_keeps_the_frame_open() {
let mut boundary = FrameBoundary::default();
let mut fronts = Fronts::new();
boundary.begin_encode();
open(&mut fronts, A, 1);
assert!(!boundary.end_encode(&fronts));
for token in 1..4 {
boundary.begin_encode();
open(&mut fronts, B, token);
boundary.end_replacement(&mut fronts, B);
assert!(!boundary.end_encode(&fronts), "A's page is not finished");
}
boundary.begin_encode();
boundary.end_replacement(&mut fronts, A);
assert!(boundary.end_encode(&fronts));
}
#[test]
fn an_encode_that_failed_part_way_ended_nothing() {
let mut boundary = FrameBoundary::default();
let mut fronts = Fronts::new();
boundary.begin_encode();
open(&mut fronts, A, 1);
boundary.end_replacement(&mut fronts, A);
open(&mut fronts, A, 2);
boundary.begin_encode();
assert!(!boundary.end_encode(&fronts));
}