diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-12 17:51:22 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-12 17:51:22 +0300 |
| commit | 6b0f2de39c2d8b9e8d346abd8548024da4ae8185 (patch) | |
| tree | 29e67e6a75488b4fdd34756be3944c4262e0b85a /tests/test_open_and_show.c | |
| parent | 51b47a0130005bd19039422d027da23ff46dd6b8 (diff) | |
showimage: loader, detect, viewer widget, EXIF orientation, tests (gt0)
M1 (task gt0): show one image with zoom/pan.
- src/loader/detect.{c,h}: magic-byte format sniffing (JPEG/PNG/GIF/WebP/
TIFF/ICO/JXL/AVIF/HEIF) -> GgazeFormat; pure, no I/O, unit-testable.
- src/loader/loader.{c,h}: loader_load() sniffs the header and dispatches to
the first registered backend; GgazeLoaderBackend struct; pixbuf is the
fallback (last, accepts UNKNOWN). M1 ships only the pixbuf backend.
- src/loader/backends/pixbuf.c: GdkPixbufLoader decode +
gdk_pixbuf_apply_embedded_orientation (decision #26) -> GdkTexture via
gdk_memory_texture_new (avoids the deprecated gdk_texture_new_for_pixbuf).
- src/viewer.{c,h}: GgazeViewer : GtkWidget custom widget (decision #31) —
fit/100%/in/out zoom, cursor-centered zoom, drag-to-pan with clamping,
dark background, GtkSnapshot render nodes.
- src/window.c: open -> loader_load -> viewer_set_texture -> stack 'large'.
- tests: unit test_detect (13 cases) + test_loader_pixbuf (plain/rotated-EXIF
8x4 orient6->4x8/png/rgba/missing/unsupported jxl-avif-heif/corrupt),
integration test_open_and_show (fixture + rotated + ./sample-images
skip-if-absent). 6/6 green; detect 97% / loader 91% / pixbuf 87% coverage.
- fixtures: gen.py produces plain.jpg, rot6.jpg, small.png, rgba.png.
- AGENTS.md: documents the ./sample-images optional test corpus convention.
Sub-agent review fixes: use-after-free of c_name in ggaze_window_open
(BLOCKER), gtk_stack_get_pages leak in test_window (BLOCKER), coverage gap,
dead branch, viewer measure, _prefix/_cb naming, include order, extern in
header, pan clamp, stale comments — all addressed.
Diffstat (limited to 'tests/test_open_and_show.c')
| -rw-r--r-- | tests/test_open_and_show.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/test_open_and_show.c b/tests/test_open_and_show.c new file mode 100644 index 0000000..e707555 --- /dev/null +++ b/tests/test_open_and_show.c @@ -0,0 +1,138 @@ +/*:* + * ggaze — open-and-show integration test + * + * Exercises the window wiring: ggaze_window_open() loads a fixture via the + * loader, sets the viewer texture, and switches the stack to "large". Asserts + * the stack is on "large" and the viewer holds a texture of the right size. + * + * An optional second test opens an image from ./sample-images (the realistic + * local corpus, not git-tracked) and is skipped if $GGAZE_SAMPLE_DIR is unset + * or the directory is absent — so CI (which only has tests/fixtures/) stays + * green. Needs a display (integration suite; CI runs under xvfb). + * + * Copyright (c) 2026 ggaze contributors + * SPDX-License-Identifier: GPL-3.0-or-later + *:*/ + +#include "window.h" +#include "viewer.h" + +#include <gdk/gdk.h> +#include <gio/gio.h> +#include <glib.h> +#include <gtk/gtk.h> + +static GFile * +fixture_file(const gchar *c_name) { + const gchar *c_dir = g_getenv("GGAZE_FIXTURES_DIR"); + g_assert_nonnull(c_dir); + gchar *c_path = g_build_filename(c_dir, c_name, NULL); + GFile *p_file = g_file_new_for_path(c_path); + g_free(c_path); + return (p_file); +} + +static GgazeWindow * +new_window(void) { + return (GGAZE_WINDOW(g_object_new(GGAZE_TYPE_WINDOW, NULL))); +} + +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)); + 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); +} + +static void +test_open_fixture_shows_large(void) { + GgazeWindow *p_win = new_window(); + GFile *p_file = fixture_file("plain.jpg"); + ggaze_window_open(p_win, p_file); + assert_shown_large_with_dims(p_win, 6, 3); + g_object_unref(p_file); + g_object_unref(p_win); +} + +static void +test_open_rotated_fixture(void) { + GgazeWindow *p_win = new_window(); + GFile *p_file = fixture_file("rot6.jpg"); + ggaze_window_open(p_win, p_file); + assert_shown_large_with_dims(p_win, 4, 8); + g_object_unref(p_file); + g_object_unref(p_win); +} + +static void +test_open_sample_image(void) { + const gchar *c_dir = g_getenv("GGAZE_SAMPLE_DIR"); + if (c_dir == NULL || *c_dir == '\0') { + g_test_skip("GGAZE_SAMPLE_DIR unset (./sample-images not available)"); + return; + } + if (!g_file_test(c_dir, G_FILE_TEST_IS_DIR)) { + g_test_skip("GGAZE_SAMPLE_DIR is not a directory"); + return; + } + /* Pick the first JPEG in the corpus. */ + GError *p_err = NULL; + GDir *p_dir = g_dir_open(c_dir, 0, &p_err); + g_assert_no_error(p_err); + const gchar *c_name = NULL; + while ((c_name = g_dir_read_name(p_dir)) != NULL) { + if (g_str_has_suffix(c_name, ".jpg") || + g_str_has_suffix(c_name, ".JPG") || + g_str_has_suffix(c_name, ".png")) { + break; + } + } + if (c_name == NULL) { + g_test_skip("no sample image found in corpus"); + g_dir_close(p_dir); + return; + } + gchar *c_path = g_build_filename(c_dir, c_name, NULL); + g_dir_close(p_dir); + + GgazeWindow *p_win = new_window(); + 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)); + g_assert_nonnull(p_tex); /* loaded, whatever its dims */ + + g_object_unref(p_file); + g_object_unref(p_win); + g_free(c_path); +} + +int +main(int i_argc, char **c_argv) { + g_test_init(&i_argc, &c_argv, NULL); + /* Tolerate host GTK WARNINGs; keep CRITICALs fatal. */ + 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("/open/fixture_shows_large", test_open_fixture_shows_large); + g_test_add_func("/open/rotated_fixture", test_open_rotated_fixture); + g_test_add_func("/open/sample_image", test_open_sample_image); + return (g_test_run()); +}
\ No newline at end of file |
