2022-06-26 15:39:12 +00:00
|
|
|
use lofty::id3::v2::{ID3v2Version, TextEncoding};
|
2021-11-24 22:09:12 +00:00
|
|
|
use lofty::{Picture, PictureInformation, PictureType};
|
2021-11-23 23:54:57 +00:00
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
const ORIGINAL_IMAGE: &[u8; 53368] = include_bytes!("assets/png_640x628.png");
|
|
|
|
|
|
|
|
fn get_buf(path: &str) -> Vec<u8> {
|
|
|
|
let mut f = File::open(path).unwrap();
|
|
|
|
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
f.read_to_end(&mut buf).unwrap();
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
fn create_original_picture() -> Picture {
|
|
|
|
let mut original_pic = Picture::from_reader(&mut &ORIGINAL_IMAGE[..]).unwrap();
|
2021-11-23 23:54:57 +00:00
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
original_pic.set_description(Some(String::from("png_640x628.png")));
|
|
|
|
original_pic.set_pic_type(PictureType::CoverFront);
|
2021-11-23 23:54:57 +00:00
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
original_pic
|
2021-11-23 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-11-24 01:08:11 +00:00
|
|
|
fn id3v24_apic() {
|
2021-11-23 23:54:57 +00:00
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.apic");
|
|
|
|
|
2022-06-26 15:39:12 +00:00
|
|
|
let (pic, _) = Picture::from_apic_bytes(&*buf, ID3v2Version::V4).unwrap();
|
2021-11-23 23:54:57 +00:00
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
assert_eq!(create_original_picture(), pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_apic_bytes() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.apic");
|
|
|
|
|
|
|
|
let original_picture = create_original_picture();
|
|
|
|
|
|
|
|
let original_as_apic = original_picture
|
2022-06-26 15:39:12 +00:00
|
|
|
.as_apic_bytes(ID3v2Version::V4, TextEncoding::Latin1)
|
2021-11-24 00:29:07 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(buf, original_as_apic);
|
2021-11-23 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 01:08:11 +00:00
|
|
|
#[test]
|
|
|
|
fn id3v22_pic() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.pic");
|
|
|
|
|
2022-06-26 15:39:12 +00:00
|
|
|
let (pic, _) = Picture::from_apic_bytes(&*buf, ID3v2Version::V2).unwrap();
|
2021-11-24 01:08:11 +00:00
|
|
|
|
|
|
|
assert_eq!(create_original_picture(), pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_apic_bytes_v2() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.pic");
|
|
|
|
|
|
|
|
let original_picture = create_original_picture();
|
|
|
|
|
|
|
|
let original_as_pic = original_picture
|
2022-06-26 15:39:12 +00:00
|
|
|
.as_apic_bytes(ID3v2Version::V2, TextEncoding::Latin1)
|
2021-11-24 01:08:11 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(buf, original_as_pic);
|
|
|
|
}
|
|
|
|
|
2021-11-23 23:54:57 +00:00
|
|
|
#[test]
|
|
|
|
fn ape_binary_item() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.apev2");
|
|
|
|
|
|
|
|
let pic = Picture::from_ape_bytes("Cover Art (Front)", &*buf).unwrap();
|
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
assert_eq!(create_original_picture(), pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_ape_bytes() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.apev2");
|
|
|
|
|
|
|
|
let original_picture = create_original_picture();
|
|
|
|
|
|
|
|
let original_as_ape = original_picture.as_ape_bytes();
|
|
|
|
|
|
|
|
assert_eq!(buf, original_as_ape);
|
2021-11-23 23:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flac_metadata_block_picture() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.vorbis");
|
|
|
|
|
2022-01-03 13:48:39 +00:00
|
|
|
let (pic, _) = Picture::from_flac_bytes(&*buf, true).unwrap();
|
2021-11-23 23:54:57 +00:00
|
|
|
|
2021-11-24 00:29:07 +00:00
|
|
|
assert_eq!(create_original_picture(), pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn as_flac_bytes() {
|
|
|
|
let buf = get_buf("tests/picture/assets/png_640x628.vorbis");
|
|
|
|
|
|
|
|
let original_picture = create_original_picture();
|
|
|
|
let original_picture_information =
|
|
|
|
PictureInformation::from_png(original_picture.data()).unwrap();
|
|
|
|
|
2021-12-22 22:21:15 +00:00
|
|
|
let original_as_flac = original_picture.as_flac_bytes(original_picture_information, true);
|
2021-11-24 00:29:07 +00:00
|
|
|
|
2021-12-22 22:21:15 +00:00
|
|
|
assert_eq!(&*buf, original_as_flac);
|
2021-11-23 23:54:57 +00:00
|
|
|
}
|