1use fs::FakeFs;
2use gpui::TestAppContext;
3use project::Project;
4use project::ProjectPath;
5use project::image_store::*;
6use serde_json::json;
7use settings::SettingsStore;
8use util::rel_path::rel_path;
9
10pub fn init_test(cx: &mut TestAppContext) {
11 zlog::init_test();
12
13 cx.update(|cx| {
14 let settings_store = SettingsStore::test(cx);
15 cx.set_global(settings_store);
16 });
17}
18
19#[gpui::test]
20async fn test_image_not_loaded_twice(cx: &mut TestAppContext) {
21 init_test(cx);
22 let fs = FakeFs::new(cx.executor());
23
24 fs.insert_tree("/root", json!({})).await;
25 // Create a png file that consists of a single white pixel
26 fs.insert_file(
27 "/root/image_1.png",
28 vec![
29 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,
30 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
31 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78,
32 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,
33 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
34 ],
35 )
36 .await;
37
38 let project = Project::test(fs, ["/root".as_ref()], cx).await;
39
40 let worktree_id = cx.update(|cx| project.read(cx).worktrees(cx).next().unwrap().read(cx).id());
41
42 let project_path = ProjectPath {
43 worktree_id,
44 path: rel_path("image_1.png").into(),
45 };
46
47 let (task1, task2) = project.update(cx, |project, cx| {
48 (
49 project.open_image(project_path.clone(), cx),
50 project.open_image(project_path.clone(), cx),
51 )
52 });
53
54 let image1 = task1.await.unwrap();
55 let image2 = task2.await.unwrap();
56
57 assert_eq!(image1, image2);
58}
59
60#[gpui::test]
61fn test_compute_metadata_from_bytes() {
62 // Single white pixel PNG
63 let png_bytes = vec![
64 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
65 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F,
66 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00,
67 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49,
68 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
69 ];
70
71 let metadata = ImageItem::compute_metadata_from_bytes(&png_bytes).unwrap();
72
73 assert_eq!(metadata.width, 1);
74 assert_eq!(metadata.height, 1);
75 assert_eq!(metadata.file_size, png_bytes.len() as u64);
76 assert_eq!(metadata.format, image::ImageFormat::Png);
77 assert!(metadata.colors.is_some());
78}