#![allow(deprecated)]
use rlvgl_core::animation::{
Easing, Fade, FadeTransition, KeyFade, LoopMode, Motion, Slide, Timeline,
};
use rlvgl_core::style::Style;
use rlvgl_core::widget::{Color, Rect};
#[test]
fn fade_updates_bg_color() {
let mut style = Style {
bg_color: Color(0, 0, 0, 255),
..Default::default()
};
let start_color = style.bg_color;
let mut timeline = Timeline::new();
timeline.add_fade(Fade::new(
&mut style,
start_color,
Color(255, 0, 0, 255),
100,
));
timeline.tick(50);
assert_eq!(style.bg_color, Color(127, 0, 0, 255));
assert!(!timeline.is_empty());
timeline.tick(50);
assert_eq!(style.bg_color, Color(255, 0, 0, 255));
assert!(timeline.is_empty());
}
#[test]
fn slide_moves_rect() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 10,
y: 0,
width: 10,
height: 10,
};
let mut timeline = Timeline::new();
timeline.add_slide(Slide::new(&mut rect, start, end, 100));
timeline.tick(30);
assert_eq!(rect.x, 3);
assert!(!timeline.is_empty());
timeline.tick(70);
assert_eq!(rect.x, 10);
assert!(timeline.is_empty());
}
#[test]
fn easing_linear_identity() {
assert_eq!(Easing::Linear.apply(0.0), 0.0);
assert_eq!(Easing::Linear.apply(0.5), 0.5);
assert_eq!(Easing::Linear.apply(1.0), 1.0);
}
#[test]
fn easing_ease_in_quadratic() {
assert_eq!(Easing::EaseIn.apply(0.0), 0.0);
assert!((Easing::EaseIn.apply(0.5) - 0.25).abs() < 0.001);
assert_eq!(Easing::EaseIn.apply(1.0), 1.0);
}
#[test]
fn easing_ease_out_quadratic() {
assert_eq!(Easing::EaseOut.apply(0.0), 0.0);
assert!((Easing::EaseOut.apply(0.5) - 0.75).abs() < 0.001);
assert_eq!(Easing::EaseOut.apply(1.0), 1.0);
}
#[test]
fn easing_ease_in_out_endpoints() {
assert_eq!(Easing::EaseInOut.apply(0.0), 0.0);
assert!((Easing::EaseInOut.apply(0.5) - 0.5).abs() < 0.001);
assert_eq!(Easing::EaseInOut.apply(1.0), 1.0);
}
#[test]
fn easing_step_quantizes() {
let e = Easing::Step(4);
assert_eq!(e.apply(0.0), 0.0);
assert_eq!(e.apply(0.24), 0.0);
assert_eq!(e.apply(0.25), 0.25);
assert_eq!(e.apply(0.49), 0.25);
assert_eq!(e.apply(0.5), 0.5);
assert_eq!(e.apply(0.99), 0.75);
}
#[test]
fn easing_clamps_input() {
assert_eq!(Easing::Linear.apply(-0.5), 0.0);
assert_eq!(Easing::Linear.apply(1.5), 1.0);
}
#[test]
fn easing_bounce_endpoints() {
assert!((Easing::Bounce.apply(0.0)).abs() < 0.01);
assert!((Easing::Bounce.apply(1.0) - 1.0).abs() < 0.01);
}
#[test]
fn loop_mode_once_finishes() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 100,
y: 0,
width: 10,
height: 10,
};
let mut m = Motion::new(&mut rect, start, end, 100);
m.tick(100);
assert!(m.finished());
assert_eq!(rect.x, 100);
}
#[test]
fn loop_mode_repeat_cycles() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 100,
y: 0,
width: 10,
height: 10,
};
let mut m = Motion::new(&mut rect, start, end, 100).with_loop(LoopMode::Repeat(2));
m.tick(50);
assert!(!m.finished());
assert_eq!(rect.x, 50);
m.tick(70);
assert!(!m.finished());
assert_eq!(rect.x, 20);
m.tick(100);
assert!(m.finished());
assert_eq!(rect.x, 100); }
#[test]
fn loop_mode_repeat_infinite() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 100,
y: 0,
width: 10,
height: 10,
};
let mut m = Motion::new(&mut rect, start, end, 100).with_loop(LoopMode::Repeat(0));
m.tick(250);
assert!(!m.finished()); assert_eq!(rect.x, 50);
}
#[test]
fn loop_mode_pingpong() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 100,
y: 0,
width: 10,
height: 10,
};
let mut m = Motion::new(&mut rect, start, end, 100).with_loop(LoopMode::PingPong(1));
m.tick(50);
assert_eq!(rect.x, 50);
m.tick(70);
assert_eq!(rect.x, 80);
m.tick(100);
assert!(m.finished());
assert_eq!(rect.x, 0); }
#[test]
fn motion_with_ease_in() {
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let start = rect;
let end = Rect {
x: 100,
y: 0,
width: 10,
height: 10,
};
let mut m = Motion::new(&mut rect, start, end, 100).with_easing(Easing::EaseIn);
m.tick(50);
assert_eq!(rect.x, 25);
m.tick(50);
assert_eq!(rect.x, 100);
}
#[test]
fn fade_transition_linear() {
let mut alpha: u8 = 255;
let mut ft = FadeTransition::new(&mut alpha, 255, 0, 100);
ft.tick(50);
assert_eq!(alpha, 127);
ft.tick(50);
assert_eq!(alpha, 0);
assert!(ft.finished());
}
#[test]
fn fade_transition_with_easing_and_loop() {
let mut alpha: u8 = 0;
let mut ft = FadeTransition::new(&mut alpha, 0, 255, 100)
.with_easing(Easing::EaseIn)
.with_loop(LoopMode::Repeat(2));
ft.tick(50);
assert_eq!(alpha, 63);
assert!(!ft.finished());
}
#[test]
fn key_fade_two_keys_equivalent_to_fade() {
let mut alpha: u8 = 255;
let mut kf = KeyFade::new(&mut alpha)
.key(0, 255, Easing::Linear)
.key(100, 0, Easing::Linear);
kf.tick(50);
assert_eq!(alpha, 127);
kf.tick(50);
assert_eq!(alpha, 0);
assert!(kf.finished());
}
#[test]
fn key_fade_three_keys() {
let mut alpha: u8 = 0;
let mut kf = KeyFade::new(&mut alpha)
.key(0, 0, Easing::Linear)
.key(100, 255, Easing::Linear)
.key(200, 128, Easing::Linear);
kf.tick(50);
assert_eq!(alpha, 127);
kf.tick(100);
assert_eq!(alpha, 191);
kf.tick(50);
assert_eq!(alpha, 128);
assert!(kf.finished());
}
#[test]
fn key_fade_with_per_segment_easing() {
let mut alpha: u8 = 0;
let mut kf = KeyFade::new(&mut alpha)
.key(0, 0, Easing::EaseIn) .key(100, 255, Easing::Linear);
kf.tick(50);
assert_eq!(alpha, 63);
}
#[test]
fn key_fade_looping() {
let mut alpha: u8 = 0;
let mut kf = KeyFade::new(&mut alpha)
.key(0, 0, Easing::Linear)
.key(100, 255, Easing::Linear)
.with_loop(LoopMode::Repeat(0));
kf.tick(50);
assert_eq!(alpha, 127);
kf.tick(75);
assert_eq!(alpha, 63);
assert!(!kf.finished());
}
#[test]
fn timeline_mixes_all_types() {
let mut style = Style {
bg_color: Color(0, 0, 0, 255),
..Default::default()
};
let mut rect = Rect {
x: 0,
y: 0,
width: 10,
height: 10,
};
let mut alpha: u8 = 255;
let mut tl = Timeline::new();
tl.add_fade(Fade::new(
&mut style,
Color(0, 0, 0, 255),
Color(255, 0, 0, 255),
100,
));
tl.add_motion(Motion::new(
&mut rect,
Rect {
x: 0,
y: 0,
width: 10,
height: 10,
},
Rect {
x: 100,
y: 0,
width: 10,
height: 10,
},
100,
));
tl.add_fade_transition(FadeTransition::new(&mut alpha, 255, 0, 100));
assert!(!tl.is_empty());
tl.tick(100);
assert!(tl.is_empty());
assert_eq!(style.bg_color, Color(255, 0, 0, 255));
assert_eq!(rect.x, 100);
assert_eq!(alpha, 0);
}
#[test]
fn color_with_alpha() {
let c = Color(255, 128, 0, 200);
let c2 = c.with_alpha(128);
assert_eq!(c2.0, 255);
assert_eq!(c2.1, 128);
assert_eq!(c2.2, 0);
assert_eq!(c2.3, 100);
}
#[test]
fn color_with_alpha_full() {
let c = Color(0, 0, 0, 255);
assert_eq!(c.with_alpha(255).3, 255);
assert_eq!(c.with_alpha(0).3, 0);
}