-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 play() after set_frame() resets the animation (#135)
- Loading branch information
Showing
2 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
mod test_utils; | ||
|
||
use crate::test_utils::{HEIGHT, WIDTH}; | ||
use dotlottie_player_core::{Config, DotLottiePlayer}; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_play_fail_when_animation_is_not_loaded() { | ||
let player = DotLottiePlayer::new(Config::default()); | ||
|
||
assert!( | ||
!player.play(), | ||
"Expected play to fail when animation is not loaded" | ||
); | ||
|
||
assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT)); | ||
|
||
assert!( | ||
player.play(), | ||
"Expected play to succeed when animation is loaded" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_play_while_playing() { | ||
let player = DotLottiePlayer::new(Config::default()); | ||
|
||
assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT)); | ||
|
||
assert!(player.play()); | ||
|
||
assert!(player.is_playing(), "Expected player to be playing"); | ||
|
||
assert!(!player.play(), "Expected play to fail when already playing"); | ||
} | ||
|
||
#[test] | ||
fn test_play_after_pause() { | ||
let player = DotLottiePlayer::new(Config { | ||
use_frame_interpolation: false, | ||
..Config::default() | ||
}); | ||
|
||
assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT)); | ||
|
||
assert!(player.play()); | ||
|
||
let mid_frame = player.total_frames() / 2.0; | ||
|
||
while player.current_frame() < mid_frame { | ||
let next_frame = player.request_frame(); | ||
|
||
if player.set_frame(next_frame) { | ||
player.render(); | ||
} | ||
} | ||
|
||
assert!(player.pause(), "Expected pause to succeed"); | ||
|
||
let paused_at = player.current_frame(); | ||
|
||
assert!(player.play(), "Expected play to succeed after pause"); | ||
|
||
let mut rendered_frames = vec![]; | ||
|
||
while !player.is_complete() { | ||
let next_frame = player.request_frame(); | ||
|
||
if player.set_frame(next_frame) { | ||
player.render(); | ||
|
||
rendered_frames.push(player.current_frame()); | ||
} | ||
} | ||
|
||
assert!( | ||
(rendered_frames[0] - paused_at).abs() <= 1.0, | ||
"Expected first rendered frame to be the same as the frame we paused at" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_play_after_complete() { | ||
let player = DotLottiePlayer::new(Config { | ||
use_frame_interpolation: false, | ||
..Config::default() | ||
}); | ||
|
||
assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT)); | ||
|
||
assert!(player.play()); | ||
|
||
while !player.is_complete() { | ||
let next_frame = player.request_frame(); | ||
|
||
if player.set_frame(next_frame) { | ||
player.render(); | ||
} | ||
} | ||
|
||
assert!(player.is_complete(), "Expected player to be complete"); | ||
|
||
assert!(!player.is_playing(), "Expected player to not be playing"); | ||
|
||
assert!( | ||
player.current_frame() == player.total_frames(), | ||
"Expected current frame to be total frames" | ||
); | ||
|
||
assert!(player.play(), "Expected play to succeed after complete"); | ||
|
||
assert_eq!( | ||
player.current_frame(), | ||
0.0, | ||
"Expected current frame to be 0 after play" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_play_after_setting_frame() { | ||
let player = DotLottiePlayer::new(Config { | ||
use_frame_interpolation: false, | ||
..Config::default() | ||
}); | ||
|
||
assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT)); | ||
|
||
let mid_frame = player.total_frames() / 2.0; | ||
|
||
assert!(player.set_frame(mid_frame)); | ||
|
||
assert_eq!( | ||
player.current_frame(), | ||
mid_frame, | ||
"Expected current frame to be mid frame" | ||
); | ||
|
||
assert!(player.play()); | ||
|
||
assert_eq!( | ||
player.current_frame(), | ||
mid_frame, | ||
"Expected current frame to be mid frame" | ||
); | ||
|
||
let mut rendered_frames = vec![]; | ||
|
||
while !player.is_complete() { | ||
let next_frame = player.request_frame(); | ||
|
||
if player.set_frame(next_frame) && player.render() { | ||
rendered_frames.push(player.current_frame()); | ||
} | ||
} | ||
|
||
assert_eq!((rendered_frames[0] - mid_frame).abs() <= 1.0, true); | ||
} | ||
} |