summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-07-13 09:04:46 +0300
committerPaul Buetow <paul@buetow.org>2026-07-13 09:04:46 +0300
commit48aa1b3cdbf27be2e016e8b532687226f9249043 (patch)
tree89d3ceab9194d755aeb3219dda5c84cbfe8370f1 /tests
parent89727590e718d3628a0847689182e896df646b8a (diff)
responsive: async loader, texture cache LRU, prefetch, tests (it0)
M3 (task it0): responsive + prefetch. - src/loader/loader.{c,h}: loader_load_async/_finish wrap the sync loader_load in a GTask worker; the task's source object is the GFile so the finish callback can check it against navigator.current (last-write-wins). - src/texturecache.{c,h}: bounded LRU (cap 4) of GFile->GdkTexture, hash+GQueue O(1) get/put, entry owns the key (no double-unref), evicts LRU on overflow. - src/window.c: async visible load (cancel-then-recreate single GCancellable); cache hit -> show+prefetch, miss -> async load; last-write-wins guard (g_file_equal(current, loaded)) before showing; prefetch next/prev into the cache via a separate prefetch cancellable (cancelled each round). Finish callbacks hold a ref on the window (released in the callback) so they never deref a freed window; dispose cancels both cancellables. - tests: unit test_texturecache (LRU evict/order/replace/miss), integration test_responsive_nav (10 rapid next -> last-write-wins). M1/M2 tests updated to pump the main loop (loads are now async) + drain_main before exit. - meson: texturecache in libggae; GIO_USE_VFS=local in fixtures_env. Fixed: texturecache double-unref (key ownership), async-callback UAF (window ref in callback data + drain in tests), a test g_build_filename leak. Sub-agent review issues addressed (dispose + cache-hit cancel in-flight loads).
Diffstat (limited to 'tests')
-rw-r--r--tests/meson.build22
-rw-r--r--tests/test_open_and_show.c44
-rw-r--r--tests/test_responsive_nav.c157
-rw-r--r--tests/test_texturecache.c108
-rw-r--r--tests/test_walk_folder.c38
5 files changed, 359 insertions, 10 deletions
diff --git a/tests/meson.build b/tests/meson.build
index bd45863..1b99012 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -77,6 +77,16 @@ test_navigator = executable(
)
test('navigator', test_navigator, suite : 'unit', env : fixtures_env)
+test_texturecache = executable(
+ 'test_texturecache',
+ ['test_texturecache.c', ggaze_conf_h],
+ include_directories : [inc, src_inc],
+ dependencies : [gdkpixbuf_dep, gtk4_dep, glib_dep, gio_dep],
+ link_with : ggaze_lib,
+ install : false,
+)
+test('texturecache', test_texturecache, suite : 'unit', env : fixtures_env)
+
# --- integration track (needs a display; CI uses xvfb-run) ---
test_window = executable(
'test_window',
@@ -110,4 +120,16 @@ test_walk_folder = executable(
)
test('walk_folder', test_walk_folder, suite : 'integration', env : fixtures_env)
+test_responsive_nav = executable(
+ 'test_responsive_nav',
+ ['test_responsive_nav.c', ggaze_conf_h],
+ include_directories : [inc, src_inc],
+ dependencies : ggaze_deps,
+ link_with : ggaze_lib,
+ install : false,
+)
+test('responsive_nav', test_responsive_nav,
+ suite : 'integration',
+ env : fixtures_env)
+
subdir('integration') \ No newline at end of file
diff --git a/tests/test_open_and_show.c b/tests/test_open_and_show.c
index e707555..3c16996 100644
--- a/tests/test_open_and_show.c
+++ b/tests/test_open_and_show.c
@@ -36,18 +36,36 @@ static GgazeWindow *
new_window(void) {
return (GGAZE_WINDOW(g_object_new(GGAZE_TYPE_WINDOW, NULL)));
}
+/* Drain in-flight async loads so their callbacks (which hold a ref on the
+ * window) fire and release before the process exits. */
+static void
+drain_main(guint u_ms) {
+ for (guint u = 0; u < u_ms; u++) {
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+}
static void
assert_shown_large_with_dims(GgazeWindow *p_win, int i_w, int i_h) {
GtkWidget *p_child = gtk_window_get_child(GTK_WINDOW(p_win));
g_assert_true(GTK_IS_STACK(p_child));
- g_assert_cmpstr(gtk_stack_get_visible_child_name(GTK_STACK(p_child)), ==,
- "large");
-
GtkWidget *p_large =
gtk_stack_get_child_by_name(GTK_STACK(p_child), "large");
g_assert_true(GGAZE_IS_VIEWER(p_large));
- GdkTexture *p_tex = ggaze_viewer_get_texture(GGAZE_VIEWER(p_large));
+ /* Loads are async; pump until the viewer shows i_w x i_h. */
+ GdkTexture *p_tex = NULL;
+ for (guint u = 0; u < 3000; u++) {
+ p_tex = ggaze_viewer_get_texture(GGAZE_VIEWER(p_large));
+ if (p_tex != NULL && gdk_texture_get_width(p_tex) == i_w &&
+ gdk_texture_get_height(p_tex) == i_h) {
+ break;
+ }
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+ g_assert_cmpstr(gtk_stack_get_visible_child_name(GTK_STACK(p_child)), ==,
+ "large");
g_assert_nonnull(p_tex);
g_assert_cmpint(gdk_texture_get_width(p_tex), ==, i_w);
g_assert_cmpint(gdk_texture_get_height(p_tex), ==, i_h);
@@ -61,6 +79,7 @@ test_open_fixture_shows_large(void) {
assert_shown_large_with_dims(p_win, 6, 3);
g_object_unref(p_file);
g_object_unref(p_win);
+ drain_main(500);
}
static void
@@ -71,6 +90,7 @@ test_open_rotated_fixture(void) {
assert_shown_large_with_dims(p_win, 4, 8);
g_object_unref(p_file);
g_object_unref(p_win);
+ drain_main(500);
}
static void
@@ -108,15 +128,25 @@ test_open_sample_image(void) {
GFile *p_file = g_file_new_for_path(c_path);
ggaze_window_open(p_win, p_file);
GtkWidget *p_child = gtk_window_get_child(GTK_WINDOW(p_win));
- g_assert_cmpstr(gtk_stack_get_visible_child_name(GTK_STACK(p_child)), ==,
- "large");
GtkWidget *p_large =
gtk_stack_get_child_by_name(GTK_STACK(p_child), "large");
- GdkTexture *p_tex = ggaze_viewer_get_texture(GGAZE_VIEWER(p_large));
+ /* async load: pump until a texture lands */
+ GdkTexture *p_tex = NULL;
+ for (guint u = 0; u < 3000; u++) {
+ p_tex = ggaze_viewer_get_texture(GGAZE_VIEWER(p_large));
+ if (p_tex != NULL) {
+ break;
+ }
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+ g_assert_cmpstr(gtk_stack_get_visible_child_name(GTK_STACK(p_child)), ==,
+ "large");
g_assert_nonnull(p_tex); /* loaded, whatever its dims */
g_object_unref(p_file);
g_object_unref(p_win);
+ drain_main(500);
g_free(c_path);
}
diff --git a/tests/test_responsive_nav.c b/tests/test_responsive_nav.c
new file mode 100644
index 0000000..0a13e1a
--- /dev/null
+++ b/tests/test_responsive_nav.c
@@ -0,0 +1,157 @@
+/*:*
+ * ggaze — responsive navigation integration test
+ *
+ * Fires 10 rapid next-actions (async loads) and asserts last-write-wins: after
+ * the main loop drains, the viewer shows the final current image, not a stale
+ * intermediate. Also confirms the rapid navigation did not block (the loop
+ * completes and the final texture arrives within a timeout). Needs a display
+ * (integration suite; CI uses xvfb).
+ *
+ * Copyright (c) 2026 ggaze contributors
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *:*/
+
+#include "viewer.h"
+#include "window.h"
+
+#include <gdk/gdk.h>
+#include <gio/gio.h>
+#include <glib.h>
+#include <gtk/gtk.h>
+
+static void
+copy_fixture(const char *c_dir, const char *c_name) {
+ const gchar *c_fx = g_getenv("GGAZE_FIXTURES_DIR");
+ g_assert_nonnull(c_fx);
+ char *c_src = g_build_filename(c_fx, c_name, NULL);
+ char *c_dst = g_build_filename(c_dir, c_name, NULL);
+ GFile *p_src = g_file_new_for_path(c_src);
+ GFile *p_dst = g_file_new_for_path(c_dst);
+ GError *p_err = NULL;
+ g_assert_true(g_file_copy(p_src, p_dst, G_FILE_COPY_OVERWRITE, NULL, NULL,
+ NULL, &p_err));
+ g_assert_no_error(p_err);
+ g_object_unref(p_src);
+ g_object_unref(p_dst);
+ g_free(c_src);
+ g_free(c_dst);
+}
+
+static void
+cleanup_temp_dir(char *c_dir) {
+ GFile *p_dir = g_file_new_for_path(c_dir);
+ GFileEnumerator *p_e =
+ g_file_enumerate_children(p_dir, "standard::name,standard::type",
+ G_FILE_QUERY_INFO_NONE, NULL, NULL);
+ if (p_e != NULL) {
+ GFileInfo *p_info;
+ while ((p_info = g_file_enumerator_next_file(p_e, NULL, NULL)) != NULL) {
+ GFile *p_child = g_file_get_child(p_dir, g_file_info_get_name(p_info));
+ g_file_delete(p_child, NULL, NULL);
+ g_object_unref(p_child);
+ g_object_unref(p_info);
+ }
+ g_object_unref(p_e);
+ }
+ g_file_delete(p_dir, NULL, NULL);
+ g_object_unref(p_dir);
+ g_free(c_dir);
+}
+
+static GgazeWindow *
+new_window(void) {
+ return (GGAZE_WINDOW(g_object_new(GGAZE_TYPE_WINDOW, NULL)));
+}
+/* Drain in-flight async loads so their callbacks (which hold a ref on the
+ * window) fire and release before the process exits. */
+static void
+drain_main(guint u_ms) {
+ for (guint u = 0; u < u_ms; u++) {
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+}
+
+static GdkTexture *
+viewer_texture(GgazeWindow *p_win) {
+ GtkWidget *p_child = gtk_window_get_child(GTK_WINDOW(p_win));
+ GtkWidget *p_large =
+ gtk_stack_get_child_by_name(GTK_STACK(p_child), "large");
+ return (ggaze_viewer_get_texture(GGAZE_VIEWER(p_large)));
+}
+
+static gboolean
+_dims_are(gpointer p_data) {
+ GgazeWindow *p_win = GGAZE_WINDOW(p_data);
+ GdkTexture *p_t = viewer_texture(p_win);
+ if (p_t == NULL) {
+ return (FALSE);
+ }
+ gint i_w = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(p_win), "w"));
+ gint i_h = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(p_win), "h"));
+ return (gdk_texture_get_width(p_t) == i_w &&
+ gdk_texture_get_height(p_t) == i_h);
+}
+
+static gboolean
+pump_until(gboolean (*p_pred)(gpointer), gpointer p_data, guint u_ms) {
+ GMainContext *p_ctx = g_main_context_default();
+ for (guint u = 0; u < u_ms; u++) {
+ if (p_pred != NULL && p_pred(p_data)) {
+ return (TRUE);
+ }
+ g_main_context_iteration(p_ctx, FALSE);
+ g_usleep(1000);
+ }
+ return (p_pred != NULL && p_pred(p_data));
+}
+
+/* Name sort: plain.jpg (6x3), rot6.jpg (8x4 orient6 -> 4x8), small.png (5x2).
+ * 10 rapid nexts from idx 0 -> (0+10) % 3 = 1 -> rot6.jpg (4x8). */
+static void
+test_rapid_next_last_write_wins(void) {
+ GError *p_err = NULL;
+ char *c_dir = g_dir_make_tmp("ggaze-resp-XXXXXX", &p_err);
+ g_assert_no_error(p_err);
+ copy_fixture(c_dir, "plain.jpg");
+ copy_fixture(c_dir, "rot6.jpg");
+ copy_fixture(c_dir, "small.png");
+
+ char *c_plain_path = g_build_filename(c_dir, "plain.jpg", NULL);
+ GFile *p_plain = g_file_new_for_path(c_plain_path);
+ g_free(c_plain_path);
+ GgazeWindow *p_win = new_window();
+ ggaze_window_open(p_win, p_plain); /* current = plain.jpg (idx 0) */
+
+ /* Let the first load settle so the navigator is ready. */
+ g_object_set_data(G_OBJECT(p_win), "w", GINT_TO_POINTER(6));
+ g_object_set_data(G_OBJECT(p_win), "h", GINT_TO_POINTER(3));
+ g_assert_true(pump_until(_dims_are, p_win, 2000));
+
+ /* Fire 10 rapid nexts; async loads must not block, and the viewer must end
+ * on the final current (rot6.jpg, 4x8), not a stale intermediate. */
+ for (gint i = 0; i < 10; i++) {
+ ggaze_window_next(p_win);
+ }
+ g_object_set_data(G_OBJECT(p_win), "w", GINT_TO_POINTER(4));
+ g_object_set_data(G_OBJECT(p_win), "h", GINT_TO_POINTER(8));
+ g_assert_true(pump_until(_dims_are, p_win, 3000));
+
+ g_object_unref(p_win);
+ drain_main(500);
+ g_object_unref(p_plain);
+ cleanup_temp_dir(c_dir);
+}
+
+int
+main(int i_argc, char **c_argv) {
+ g_test_init(&i_argc, &c_argv, NULL);
+ g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+ if (!gtk_init_check()) {
+ g_test_skip("no display available (run under xvfb)");
+ return (g_test_run());
+ }
+ g_test_add_func("/responsive/rapid_next_last_write_wins",
+ test_rapid_next_last_write_wins);
+ return (g_test_run());
+} \ No newline at end of file
diff --git a/tests/test_texturecache.c b/tests/test_texturecache.c
new file mode 100644
index 0000000..3ad24f8
--- /dev/null
+++ b/tests/test_texturecache.c
@@ -0,0 +1,108 @@
+/*:*
+ * ggaze — texture cache unit test
+ *
+ * Exercises the bounded LRU: capacity cap + eviction, MRU ordering on get,
+ * replace, and miss. Uses 1x1 GdkMemoryTextures (no display needed).
+ *
+ * Copyright (c) 2026 ggaze contributors
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *:*/
+
+#include "texturecache.h"
+
+#include <gdk/gdk.h>
+#include <gio/gio.h>
+#include <glib.h>
+
+static GdkTexture *
+mk_tex(void) {
+ static const guint8 u_px[4] = {0, 0, 0, 255};
+ GBytes *p_b = g_bytes_new_static(u_px, 4);
+ GdkTexture *p_t = gdk_memory_texture_new(1, 1, GDK_MEMORY_R8G8B8A8, p_b, 4);
+ g_bytes_unref(p_b);
+ return (p_t);
+}
+
+static GFile *
+mk_file(const char *c_name) {
+ return (g_file_new_for_path(c_name));
+}
+
+static void
+test_cap_and_evict(void) {
+ TextureCache *p_c = texturecache_new(4);
+ const char *names[5] = {"a.jpg", "b.jpg", "c.jpg", "d.jpg", "e.jpg"};
+ GFile *f[5];
+ GdkTexture *t[5];
+ for (gint i = 0; i < 5; i++) {
+ f[i] = g_file_new_for_path(names[i]);
+ t[i] = mk_tex();
+ texturecache_put(p_c, f[i], t[i]);
+ }
+ g_assert_cmpint(texturecache_get_size(p_c), ==, 4); /* cap 4, 5 put */
+ g_assert_null(texturecache_get(p_c, f[0])); /* LRU evicted */
+ g_assert_nonnull(texturecache_get(p_c, f[4])); /* newest kept */
+
+ for (gint i = 0; i < 5; i++) {
+ g_object_unref(f[i]);
+ g_object_unref(t[i]);
+ }
+ texturecache_delete(p_c);
+}
+
+static void
+test_lru_order(void) {
+ TextureCache *p_c = texturecache_new(2);
+ GFile *f1 = g_file_new_for_path("1.jpg");
+ GFile *f2 = g_file_new_for_path("2.jpg");
+ GFile *f3 = g_file_new_for_path("3.jpg");
+ GdkTexture *t1 = mk_tex(), *t2 = mk_tex(), *t3 = mk_tex();
+
+ texturecache_put(p_c, f1, t1);
+ texturecache_put(p_c, f2, t2);
+ /* Touch f1 -> f2 becomes LRU. */
+ g_assert_nonnull(texturecache_get(p_c, f1));
+ texturecache_put(p_c, f3, t3); /* evicts LRU = f2 */
+ g_assert_null(texturecache_get(p_c, f2));
+ g_assert_nonnull(texturecache_get(p_c, f1));
+ g_assert_nonnull(texturecache_get(p_c, f3));
+
+ g_object_unref(f1);
+ g_object_unref(f2);
+ g_object_unref(f3);
+ g_object_unref(t1);
+ g_object_unref(t2);
+ g_object_unref(t3);
+ texturecache_delete(p_c);
+}
+
+static void
+test_replace_and_miss(void) {
+ TextureCache *p_c = texturecache_new(4);
+ GFile *f = g_file_new_for_path("x.jpg");
+ GdkTexture *t1 = mk_tex(), *t2 = mk_tex();
+
+ texturecache_put(p_c, f, t1);
+ g_assert_cmpint(texturecache_get_size(p_c), ==, 1);
+ texturecache_put(p_c, f, t2); /* replace, no size growth */
+ g_assert_cmpint(texturecache_get_size(p_c), ==, 1);
+ g_assert_true(texturecache_get(p_c, f) == t2);
+
+ GFile *f_other = g_file_new_for_path("other.jpg");
+ g_assert_null(texturecache_get(p_c, f_other)); /* miss */
+
+ g_object_unref(f);
+ g_object_unref(f_other);
+ g_object_unref(t1);
+ g_object_unref(t2);
+ texturecache_delete(p_c);
+}
+
+int
+main(int i_argc, char **c_argv) {
+ g_test_init(&i_argc, &c_argv, NULL);
+ g_test_add_func("/texturecache/cap_and_evict", test_cap_and_evict);
+ g_test_add_func("/texturecache/lru_order", test_lru_order);
+ g_test_add_func("/texturecache/replace_and_miss", test_replace_and_miss);
+ return (g_test_run());
+} \ No newline at end of file
diff --git a/tests/test_walk_folder.c b/tests/test_walk_folder.c
index 660b164..f2f4f15 100644
--- a/tests/test_walk_folder.c
+++ b/tests/test_walk_folder.c
@@ -73,6 +73,15 @@ static GgazeWindow *
new_window(void) {
return (GGAZE_WINDOW(g_object_new(GGAZE_TYPE_WINDOW, NULL)));
}
+/* Drain in-flight async loads so their callbacks (which hold a ref on the
+ * window) fire and release before the process exits. */
+static void
+drain_main(guint u_ms) {
+ for (guint u = 0; u < u_ms; u++) {
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+}
static GdkTexture *
viewer_texture(GgazeWindow *p_win) {
@@ -84,7 +93,20 @@ viewer_texture(GgazeWindow *p_win) {
static void
assert_dims(GgazeWindow *p_win, int i_w, int i_h) {
- GdkTexture *p_tex = viewer_texture(p_win);
+ /* Loads are async; pump the main loop until the viewer shows i_w x i_h. */
+ GtkWidget *p_child = gtk_window_get_child(GTK_WINDOW(p_win));
+ GtkWidget *p_large =
+ gtk_stack_get_child_by_name(GTK_STACK(p_child), "large");
+ GdkTexture *p_tex = NULL;
+ for (guint u = 0; u < 3000; u++) {
+ p_tex = ggaze_viewer_get_texture(GGAZE_VIEWER(p_large));
+ if (p_tex != NULL && gdk_texture_get_width(p_tex) == i_w &&
+ gdk_texture_get_height(p_tex) == i_h) {
+ break;
+ }
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
g_assert_nonnull(p_tex);
g_assert_cmpint(gdk_texture_get_width(p_tex), ==, i_w);
g_assert_cmpint(gdk_texture_get_height(p_tex), ==, i_h);
@@ -104,7 +126,6 @@ test_walk_temp(void) {
GError *p_err = NULL;
char *c_dir = g_dir_make_tmp("ggaze-walk-XXXXXX", &p_err);
g_assert_no_error(p_err);
-
copy_fixture(c_dir, "plain.jpg");
copy_fixture(c_dir, "rot6.jpg");
copy_fixture(c_dir, "small.png");
@@ -132,6 +153,7 @@ test_walk_temp(void) {
assert_dims(p_win, 5, 2);
g_object_unref(p_win);
+ drain_main(500);
g_object_unref(p_plain);
cleanup_temp_dir(c_dir);
}
@@ -168,15 +190,25 @@ test_walk_sample(void) {
GFile *p_file = g_file_new_for_path(c_path);
ggaze_window_open(p_win, p_file);
g_object_unref(p_file);
+ /* wait for the async load to land a texture */
+ for (guint u = 0; u < 3000 && viewer_texture(p_win) == NULL; u++) {
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
g_assert_nonnull(viewer_texture(p_win));
/* Walk a few; each step must produce a (possibly new) texture. */
for (int i = 0; i < 5; i++) {
ggaze_window_next(p_win);
- g_assert_nonnull(viewer_texture(p_win));
}
+ for (guint u = 0; u < 3000 && viewer_texture(p_win) == NULL; u++) {
+ g_main_context_iteration(g_main_context_default(), FALSE);
+ g_usleep(1000);
+ }
+ g_assert_nonnull(viewer_texture(p_win));
g_object_unref(p_win);
+ drain_main(500);
g_free(c_path);
}