summaryrefslogtreecommitdiff
path: root/tests/test_loader_jpeg.c
blob: 6a2e0e532f19c4e6b001eea5bc2f4b7f65a487bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*:*
 * ggaze — libjpeg-turbo progressive loader unit test (feature-gated)
 *
 * Tests the progressive JPEG path: loader_load_async with a progress callback
 * fires the callback (low-res partial) and returns the full texture. Only
 * compiled when libjpeg-turbo is found (HAVE_JPEG).
 *
 * Copyright (c) 2026 ggaze contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 *:*/

#include "loader/loader.h"

#include <gdk/gdk.h>
#include <gio/gio.h>
#include <glib.h>

static volatile int g_progress_fired = 0;
static GMainLoop   *g_loop           = NULL;
static GdkTexture  *g_result         = NULL;

static void
progress_cb(GdkTexture *p_partial, gpointer p_data) {
   (void)p_partial;
   (void)p_data;
   g_atomic_int_set(&g_progress_fired, 1);
}

static void
finish_cb(GObject *p_src, GAsyncResult *p_res, gpointer p_data) {
   (void)p_src;
   (void)p_data;
   GError *p_err = NULL;
   g_result      = loader_load_finish(p_res, &p_err);
   g_assert_no_error(p_err);
   g_main_loop_quit(g_loop);
}

static void
test_progressive_jpeg(void) {
   const char *c_dir = g_getenv("GGAZE_FIXTURES_DIR");
   g_assert_nonnull(c_dir);
   char  *c_path = g_build_filename(c_dir, "plain.jpg", NULL);
   GFile *p_file = g_file_new_for_path(c_path);
   g_free(c_path);

   g_progress_fired = 0;
   g_result         = NULL;
   g_loop           = g_main_loop_new(NULL, FALSE);
   loader_load_async(p_file, NULL, progress_cb, NULL, finish_cb, NULL);
   g_main_loop_run(g_loop);
   g_main_loop_unref(g_loop);
   g_loop = NULL;

   g_assert_nonnull(g_result);
   g_assert_cmpint(gdk_texture_get_width(g_result), ==, 6);
   g_assert_cmpint(gdk_texture_get_height(g_result), ==, 3);
   g_assert_cmpint(g_atomic_int_get(&g_progress_fired), ==, 1);
   g_object_unref(g_result);
   g_object_unref(p_file);
}

static void
test_progressive_rotated(void) {
   const char *c_dir = g_getenv("GGAZE_FIXTURES_DIR");
   g_assert_nonnull(c_dir);
   char  *c_path = g_build_filename(c_dir, "rot6.jpg", NULL);
   GFile *p_file = g_file_new_for_path(c_path);
   g_free(c_path);

   g_progress_fired = 0;
   g_result         = NULL;
   g_loop           = g_main_loop_new(NULL, FALSE);
   loader_load_async(p_file, NULL, progress_cb, NULL, finish_cb, NULL);
   g_main_loop_run(g_loop);
   g_main_loop_unref(g_loop);

   /* Full result via GdkPixbuf applies EXIF orientation → 4x8. */
   g_assert_nonnull(g_result);
   g_assert_cmpint(gdk_texture_get_width(g_result), ==, 4);
   g_assert_cmpint(gdk_texture_get_height(g_result), ==, 8);
   g_object_unref(g_result);
   g_object_unref(p_file);
}

int
main(int i_argc, char **c_argv) {
   g_test_init(&i_argc, &c_argv, NULL);
   if (g_getenv("GGAZE_FIXTURES_DIR") == NULL) {
      g_test_skip("GGAZE_FIXTURES_DIR unset");
      return (g_test_run());
   }
   g_test_add_func("/loader/jpeg/progressive", test_progressive_jpeg);
   g_test_add_func("/loader/jpeg/progressive_rotated",
                   test_progressive_rotated);
   return (g_test_run());
}