Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
use fs::FakeFs;
use gpui::TestAppContext;
use project::Project;
use project::ProjectPath;
use project::image_store::*;
use serde_json::json;
use settings::SettingsStore;
use util::rel_path::rel_path;
pub fn init_test(cx: &mut TestAppContext) {
zlog::init_test();
cx.update(|cx| {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
});
}
#[gpui::test]
async fn test_image_not_loaded_twice(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree("/root", json!({})).await;
// Create a png file that consists of a single white pixel
fs.insert_file(
"/root/image_1.png",
vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
],
)
.await;
let project = Project::test(fs, ["/root".as_ref()], cx).await;
let worktree_id = cx.update(|cx| project.read(cx).worktrees(cx).next().unwrap().read(cx).id());
let project_path = ProjectPath {
worktree_id,
path: rel_path("image_1.png").into(),
};
let (task1, task2) = project.update(cx, |project, cx| {
(
project.open_image(project_path.clone(), cx),
project.open_image(project_path.clone(), cx),
)
});
let image1 = task1.await.unwrap();
let image2 = task2.await.unwrap();
assert_eq!(image1, image2);
}
#[gpui::test]
fn test_compute_metadata_from_bytes() {
// Single white pixel PNG
let png_bytes = vec![
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F,
0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00,
0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
];
let metadata = ImageItem::compute_metadata_from_bytes(&png_bytes).unwrap();
assert_eq!(metadata.width, 1);
assert_eq!(metadata.height, 1);
assert_eq!(metadata.file_size, png_bytes.len() as u64);
assert_eq!(
metadata.format,
ImageFormat::Raster(image::ImageFormat::Png)
);
assert!(metadata.colors.is_some());
}
#[test]
fn test_svg_metadata_from_contents() {
for prefix in [
"",
"<?xml version=\"1.0\"?>\n",
"\u{feff}",
"<!-- image -->\n",
] {
let content = format!(
"{prefix}<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 200 100\"><rect width=\"200\" height=\"100\" fill=\"red\"/></svg>"
);
let metadata = ImageItem::compute_metadata_from_bytes(content.as_bytes())
.expect("SVG metadata should load regardless of the XML prefix");
assert_eq!(metadata.format, ImageFormat::Svg);
assert_eq!((metadata.width, metadata.height), (200, 100));
assert_eq!(metadata.file_size, content.len() as u64);
assert!(metadata.colors.is_none());
}
}
#[test]
fn test_invalid_svg_metadata() {
for content in [
"",
"not an image",
"<svg",
"<html><body>SVG</body></html>",
"<!-- <svg xmlns=\"http://www.w3.org/2000/svg\"/> -->",
] {
assert!(ImageItem::compute_metadata_from_bytes(content.as_bytes()).is_err());
}
}
#[gpui::test]
async fn test_open_svg_image_by_contents(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
"/root",
json!({
"image.bin": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"10\"/>",
"broken.svg": "<svg",
"html.svg": "<html><body>not an SVG</body></html>"
}),
)
.await;
let project = Project::test(fs, ["/root".as_ref()], cx).await;
let worktree_id = project.read_with(cx, |project, cx| {
project
.worktrees(cx)
.next()
.expect("worktree")
.read(cx)
.id()
});
let image = project
.update(cx, |project, cx| {
project.open_image(
ProjectPath {
worktree_id,
path: rel_path("image.bin").into(),
},
cx,
)
})
.await
.expect("ImageStore should identify SVG by contents");
image.read_with(cx, |image, _| {
assert_eq!(image.image.format(), gpui::ImageFormat::Svg);
let metadata = image.image_metadata.expect("SVG should have metadata");
assert_eq!(metadata.format, ImageFormat::Svg);
assert_eq!((metadata.width, metadata.height), (20, 10));
});
for image_path in ["broken.svg", "html.svg"] {
let result = project
.update(cx, |project, cx| {
project.open_image(
ProjectPath {
worktree_id,
path: rel_path(image_path).into(),
},
cx,
)
})
.await;
assert!(result.is_err(), "{image_path}");
}
}