Skip to content

Commit

Permalink
fix(fms): 🐛 invalid embed of image assets (#132)
Browse files Browse the repository at this point in the history
* fix(fms): 🐛 invalid embed of image assets

* test: 💍 add a regression test
  • Loading branch information
theashraf authored Apr 12, 2024
1 parent dfaf166 commit 8e64f64
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 49 deletions.
2 changes: 2 additions & 0 deletions dotlottie-fms/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub fn get_animation(bytes: &Vec<u8>, animation_id: &str) -> Result<String, DotL
assets[i]["u"] = "".into();
assets[i]["p"] =
format!("data:image/{};base64,{}", image_ext, image_data_base64).into();
// explicitly indicate that the image asset is inlined
assets[i]["e"] = 1.into();
}
}
}
Expand Down
142 changes: 93 additions & 49 deletions dotlottie-fms/src/tests/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,118 @@
#[cfg(test)]
#[test]
fn get_animation_test() {
use std::{fs::File, io::Read};
mod tests {
#[test]
fn get_animation_test() {
use std::{fs::File, io::Read};

use crate::get_animation;
use crate::get_animation;

let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);
let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);

let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();
let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();

animation_file.read_to_end(&mut buffer).unwrap();
animation_file.read_to_end(&mut buffer).unwrap();

let animation = get_animation(&buffer, "anger").unwrap();
let animation = get_animation(&buffer, "anger").unwrap();

assert_eq!(animation.contains("ADBE Vector Graphic - Stroke"), true);
}
assert_eq!(animation.contains("ADBE Vector Graphic - Stroke"), true);
}

#[test]
fn get_animations_test() {
use std::{fs::File, io::Read};
#[test]
fn get_animations_test() {
use std::{fs::File, io::Read};

let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);
let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);

let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();
let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();

animation_file.read_to_end(&mut buffer).unwrap();
animation_file.read_to_end(&mut buffer).unwrap();

let animation = crate::get_animations(&buffer).unwrap();
let animation = crate::get_animations(&buffer).unwrap();

assert_eq!(animation.len(), 62);
assert_eq!(animation.len(), 62);

assert_eq!(animation[0].id, "anger");
assert_eq!(animation[5].id, "confused");
}
assert_eq!(animation[0].id, "anger");
assert_eq!(animation[5].id, "confused");
}

#[test]
fn get_manifest_test() {
use std::{fs::File, io::Read};

let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);

let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();

animation_file.read_to_end(&mut buffer).unwrap();

let manifest = crate::get_manifest(&buffer).unwrap();

// First and last animations
let first_animation_lock = manifest.animations;

let first_animation = first_animation_lock.first().unwrap();

#[test]
fn get_manifest_test() {
use std::{fs::File, io::Read};
assert_eq!(first_animation.id == "anger", true);

let file_path = format!(
"{}{}",
env!("CARGO_MANIFEST_DIR"),
"/src/tests/resources/emoji-collection.lottie"
);
let last_animation = first_animation_lock.last().unwrap();

let mut animation_file = File::open(file_path).unwrap();
let mut buffer = Vec::new();
assert_eq!(last_animation.id == "yummy", true);
}

animation_file.read_to_end(&mut buffer).unwrap();
#[test]
fn get_animation_with_image_assets_test() {
let dotlottie_bytes = &include_bytes!("../resources/bull.lottie").to_vec();
let animation_name = "animation_1";

let manifest = crate::get_manifest(&buffer).unwrap();
let lottie_string = crate::get_animation(&dotlottie_bytes, animation_name)
.expect("Failed to get animation from lottie bytes");

// First and last animations
let first_animation_lock = manifest.animations;
let lottie_json =
jzon::parse(&lottie_string).expect("Failed to parse lottie string to JSON");

let first_animation = first_animation_lock.first().unwrap();
let assets = lottie_json["assets"]
.as_array()
.expect("Expected assets to be an array");

assert_eq!(first_animation.id == "anger", true);
for asset in assets {
assert_eq!(
asset["e"]
.as_i64()
.expect("Expected embed property to be an integer"),
1,
"Expected asset to be embedded with 'e' set to 1"
);

let last_animation = first_animation_lock.last().unwrap();
assert!(
asset["u"]
.as_str()
.expect("Expected asset URL ('u') to be a string")
.is_empty(),
"Expected asset URL ('u') to be empty"
);

assert_eq!(last_animation.id == "yummy", true);
assert!(
asset["p"]
.as_str()
.expect("Expected asset path ('p') to be a string")
.starts_with("data:image/"),
"Expected asset path ('p') to be a data URL starting with 'data:image/'"
);
}
}
}

0 comments on commit 8e64f64

Please sign in to comment.