2023-02-25 20:39:52 +01:00
|
|
|
use crate::cleanup::remove_compressed_test_file;
|
2025-02-21 13:35:34 +01:00
|
|
|
use caesium::parameters::CSParameters;
|
|
|
|
use std::{fs::File, sync::Once};
|
2023-02-25 20:39:52 +01:00
|
|
|
|
|
|
|
mod cleanup;
|
|
|
|
|
2021-10-23 17:18:52 +02:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
|
|
|
|
pub fn initialize(file: &str) {
|
|
|
|
INIT.call_once(|| {
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(file);
|
2021-10-23 17:18:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_20() {
|
|
|
|
let output = "tests/samples/output/compressed_20.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
params.webp.quality = 20;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_50() {
|
|
|
|
let output = "tests/samples/output/compressed_50.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
params.webp.quality = 50;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_80() {
|
|
|
|
let output = "tests/samples/output/compressed_80.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
params.webp.quality = 80;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_100() {
|
|
|
|
let output = "tests/samples/output/compressed_100.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
params.webp.quality = 100;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn optimize() {
|
|
|
|
let output = "tests/samples/output/optimized.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2021-10-23 17:18:52 +02:00
|
|
|
params.optimize = true;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2021-10-23 17:18:52 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2025-02-21 13:35:34 +01:00
|
|
|
fn downscale_to_size() {
|
|
|
|
let max_output_size = 100_000;
|
|
|
|
let output = "tests/samples/output/downscale_to_size.webp";
|
2022-02-12 14:10:12 +01:00
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2022-02-12 14:10:12 +01:00
|
|
|
params.width = 150;
|
|
|
|
params.height = 100;
|
2025-02-21 13:35:34 +01:00
|
|
|
caesium::compress_to_size(
|
2022-10-02 15:02:15 +02:00
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2025-02-21 13:35:34 +01:00
|
|
|
&mut params,
|
|
|
|
max_output_size,
|
|
|
|
false,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2022-02-12 14:10:12 +01:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
|
|
|
assert!(File::open(output).unwrap().metadata().unwrap().len() < max_output_size as u64);
|
2022-02-12 14:10:12 +01:00
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (150, 100));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2022-02-12 14:10:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn downscale_optimize() {
|
|
|
|
let output = "tests/samples/output/downscale_optimized.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2022-02-12 14:10:12 +01:00
|
|
|
params.optimize = true;
|
|
|
|
params.width = 150;
|
|
|
|
params.height = 100;
|
2022-10-02 15:02:15 +02:00
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_家.webp"),
|
|
|
|
String::from(output),
|
2022-11-14 23:00:14 +01:00
|
|
|
¶ms,
|
2022-10-02 15:02:15 +02:00
|
|
|
)
|
2024-01-23 18:51:39 +01:00
|
|
|
.unwrap();
|
2022-02-12 14:10:12 +01:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2022-02-12 14:10:12 +01:00
|
|
|
assert_eq!(image::image_dimensions(output).unwrap(), (150, 100));
|
2023-02-25 20:39:52 +01:00
|
|
|
remove_compressed_test_file(output)
|
2021-10-23 17:18:52 +02:00
|
|
|
}
|
2024-08-01 11:51:11 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compress_animated_80() {
|
|
|
|
let output = "tests/samples/output/compressed_animated_80.webp";
|
|
|
|
initialize(output);
|
2024-10-10 20:02:38 +02:00
|
|
|
let mut params = CSParameters::new();
|
2024-08-01 11:51:11 +02:00
|
|
|
params.webp.quality = 80;
|
|
|
|
caesium::compress(
|
|
|
|
String::from("tests/samples/uncompressed_animated.webp"),
|
|
|
|
String::from(output),
|
|
|
|
¶ms,
|
|
|
|
)
|
2025-02-21 13:35:34 +01:00
|
|
|
.unwrap();
|
2024-08-01 11:51:11 +02:00
|
|
|
assert!(std::path::Path::new(output).exists());
|
2025-02-21 13:35:34 +01:00
|
|
|
assert_eq!(infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp");
|
2024-08-01 11:51:11 +02:00
|
|
|
remove_compressed_test_file(output)
|
2025-02-21 13:35:34 +01:00
|
|
|
}
|