summaryrefslogtreecommitdiff
path: root/tests/test_marks.c
blob: a2350b18c077d5aae034203d6a252a188a9ed45c (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*:*
 * ggaze — marks integration test
 *
 * Exercises the mark UI wiring in the window + grid: `v` toggle, `V` range,
 * `Ctrl+a` mark-all, contextual `Esc` clear, persistence across a large/grid
 * view switch, and mark clearing on a successful trash (decision Q / du0).
 * Verifies through the public UI surface: the header title carries "N marked"
 * and the grid cells carry the "ggaze-marked" badge in sync. Needs a display
 * (integration suite; CI uses xvfb).
 *
 * Copyright (c) 2026 ggaze contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 *:*/

#include "gridview.h"
#include "viewer.h"
#include "window.h"

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

/* --- helpers ------------------------------------------------------------ */

static GgazeWindow *
new_window(void) {
   return (GGAZE_WINDOW(g_object_new(GGAZE_TYPE_WINDOW, NULL)));
}

static GdkTexture *
viewer_texture(GgazeWindow *p_win) {
   GtkStack  *p_stack = ggaze_window_get_stack(p_win);
   GtkWidget *p_large = gtk_stack_get_child_by_name(p_stack, "large");
   return (ggaze_viewer_get_texture(GGAZE_VIEWER(p_large)));
}

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
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));
         if (g_file_info_get_file_type(p_info) == G_FILE_TYPE_DIRECTORY) {
            GFileEnumerator *p_e2 = g_file_enumerate_children(
               p_child, "standard::name", G_FILE_QUERY_INFO_NONE, NULL, NULL);
            if (p_e2 != NULL) {
               GFileInfo *p_i2;
               while ((p_i2 = g_file_enumerator_next_file(p_e2, NULL, NULL)) !=
                      NULL) {
                  GFile *p_c2 =
                     g_file_get_child(p_child, g_file_info_get_name(p_i2));
                  g_file_delete(p_c2, NULL, NULL);
                  g_object_unref(p_c2);
                  g_object_unref(p_i2);
               }
               g_object_unref(p_e2);
            }
         }
         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);
}

/* Fire a win.* action on the window. */
static void
fire(GgazeWindow *p_win, const char *c_action) {
   gtk_widget_activate_action(GTK_WIDGET(p_win), c_action, NULL);
}

/* The grid built on open (the stack's "grid" child), even when not visible. */
static GgazeGrid *
window_grid(GgazeWindow *p_win) {
   GtkStack *p_stack = ggaze_window_get_stack(p_win);
   return (GGAZE_GRID(gtk_stack_get_child_by_name(p_stack, "grid")));
}

/* Find the first GtkFlowBox descendant of p_root (the grid's flowbox lives
 * under the scrolled window, possibly via an internal GtkViewport). */
static GtkWidget *
find_flowbox(GtkWidget *p_root) {
   if (GTK_IS_FLOW_BOX(p_root)) {
      return (p_root);
   }
   GtkWidget *p_child = gtk_widget_get_first_child(p_root);
   while (p_child != NULL) {
      GtkWidget *p_found = find_flowbox(p_child);
      if (p_found != NULL) {
         return (p_found);
      }
      p_child = gtk_widget_get_next_sibling(p_child);
   }
   return (NULL);
}

/* Count grid cells currently showing the "ggaze-marked" badge. Walks the
 * GtkFlowBox (grid -> scrolled -> flowbox) and checks each cell's box. */
static guint
grid_marked_count(GgazeGrid *p_grid) {
   GtkWidget *p_flow = find_flowbox(GTK_WIDGET(p_grid));
   g_assert_nonnull(p_flow);
   guint      u_count = 0;
   GtkWidget *p_child = gtk_widget_get_first_child(p_flow);
   while (p_child != NULL) {
      GtkWidget *p_box =
         gtk_flow_box_child_get_child(GTK_FLOW_BOX_CHILD(p_child));
      if (p_box != NULL && gtk_widget_has_css_class(p_box, "ggaze-marked")) {
         u_count++;
      }
      p_child = gtk_widget_get_next_sibling(p_child);
   }
   return (u_count);
}

/* The mark count shown in the window title ("N marked"), or 0 if the title
 * carries no "marked" suffix (which is how 0 marks is rendered). */
static guint
title_mark_count(GgazeWindow *p_win) {
   const gchar *c_title = gtk_window_get_title(GTK_WINDOW(p_win));
   if (c_title == NULL) {
      return (0);
   }
   const char *c_p = g_strstr_len(c_title, -1, " marked");
   if (c_p == NULL) {
      return (0);
   }
   /* walk back over the digits immediately preceding " marked" */
   const char *c_num = c_p;
   while (c_num > c_title && g_ascii_isdigit(*(c_num - 1))) {
      c_num--;
   }
   if (c_num == c_p) {
      return (0);
   }
   return ((guint)g_ascii_strtoull(c_num, NULL, 10));
}

static void
assert_marks(GgazeWindow *p_win, guint u_expect) {
   drain_main(100);
   g_assert_cmpint(title_mark_count(p_win), ==, u_expect);
   g_assert_cmpint(grid_marked_count(window_grid(p_win)), ==, u_expect);
}

static char *
setup_dir_with_three(GFile **p_plain_out) {
   GError *p_err = NULL;
   char   *c_dir = g_dir_make_tmp("ggaze-marks-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_path  = g_build_filename(c_dir, "plain.jpg", NULL);
   GFile *p_plain = g_file_new_for_path(c_path);
   g_free(c_path);
   *p_plain_out = p_plain;
   return (c_dir);
}

static void
wait_for_load(GgazeWindow *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));
   drain_main(200);
}

/* --- subtests ----------------------------------------------------------- */

/* `v` toggles a mark on the current (large-view) image; the header shows
 * "1 marked" and the grid badge appears on exactly one cell. `v` again
 * untoggles it. */
static void
test_mark_toggle(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   g_assert_cmpint(ggaze_grid_get_count(window_grid(p_win)), ==, 3);
   assert_marks(p_win, 0);

   fire(p_win, "win.mark");
   assert_marks(p_win, 1);

   fire(p_win, "win.mark"); /* untoggle */
   assert_marks(p_win, 0);

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   cleanup_temp_dir(c_dir);
}

/* `Ctrl+a` marks every image; `Esc` (win.back) clears all marks contextually
 * (no fullscreen / no large->grid back-step happens while marks exist). */
static void
test_mark_all_and_clear(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   fire(p_win, "win.mark-all");
   assert_marks(p_win, 3);

   /* Esc clears marks first (contextual back), rather than leaving large view.
    */
   GtkStack *p_stack = ggaze_window_get_stack(p_win);
   g_assert_cmpstr(gtk_stack_get_visible_child_name(p_stack), ==, "large");
   fire(p_win, "win.back");
   assert_marks(p_win, 0);
   /* Still in large view: Esc only cleared marks, did not back out. */
   g_assert_cmpstr(gtk_stack_get_visible_child_name(p_stack), ==, "large");

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   cleanup_temp_dir(c_dir);
}

/* `V` range-marks from the last `v`-toggled anchor to the current image.
 * Mark a (anchor), advance to b, range -> 2 marked; advance to c, range
 * -> 3 marked. */
static void
test_mark_range(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   /* current = plain.jpg (idx 0). Mark it -> anchor. */
   fire(p_win, "win.mark");
   assert_marks(p_win, 1);

   /* advance to rot6.jpg (idx 1), range from anchor -> a, b marked. */
   fire(p_win, "win.next");
   drain_main(100);
   fire(p_win, "win.mark-range");
   assert_marks(p_win, 2);

   /* advance to small.png (idx 2), range from anchor -> a, b, c marked. */
   fire(p_win, "win.next");
   drain_main(100);
   fire(p_win, "win.mark-range");
   assert_marks(p_win, 3);

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   cleanup_temp_dir(c_dir);
}

/* Marks persist across a large > grid > large view switch: the grid shows
 * the same badges and the header keeps the same count. */
static void
test_marks_persist_across_view_switch(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   fire(p_win, "win.mark");
   fire(p_win, "win.next");
   drain_main(100);
   fire(p_win, "win.mark"); /* mark 2 images */
   assert_marks(p_win, 2);

   /* large -> grid: badges reflect the 2 marks. */
   GtkStack *p_stack = ggaze_window_get_stack(p_win);
   fire(p_win, "win.toggle-view");
   drain_main(200);
   g_assert_cmpstr(gtk_stack_get_visible_child_name(p_stack), ==, "grid");
   g_assert_cmpint(grid_marked_count(window_grid(p_win)), ==, 2);
   g_assert_cmpint(title_mark_count(p_win), ==, 2);

   /* grid -> large: marks still present. */
   fire(p_win, "win.toggle-view");
   drain_main(200);
   g_assert_cmpstr(gtk_stack_get_visible_child_name(p_stack), ==, "large");
   assert_marks(p_win, 2);

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   cleanup_temp_dir(c_dir);
}

/* Trashing the current image clears its mark (decision Q / du0): mark all 3,
 * trash the current, the mark count drops to 2 and the grid badges drop one. */
static void
test_marks_clear_on_trash(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   fire(p_win, "win.mark-all");
   assert_marks(p_win, 3);

   fire(p_win, "win.trash");
   drain_main(300);
   /* The trashed file's mark is cleared; 2 marks remain. */
   g_assert_cmpint(title_mark_count(p_win), ==, 2);
   g_assert_cmpint(grid_marked_count(window_grid(p_win)), ==, 2);

   /* .Trash was created with the binned file. */
   char  *c_trashpath = g_build_filename(c_dir, ".Trash", NULL);
   GFile *p_trash     = g_file_new_for_path(c_trashpath);
   g_assert_true(g_file_query_exists(p_trash, NULL));
   g_object_unref(p_trash);
   g_free(c_trashpath);

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   cleanup_temp_dir(c_dir);
}

/* A middle-click GtkGestureClick is attached to the grid flowbox, wired to
 * toggle the mark on the clicked cell (pointer-accessible marks). GTK 4.22
 * exposes no public API to synthesize a button event, so this is a structural
 * check that the gesture is registered with button == middle, analogous to the
 * shortcut-controller scope check in test_shortcut.c. */
static void
test_pointer_middle_click_wired(void) {
   GFile *p_plain = NULL;
   char  *c_dir   = setup_dir_with_three(&p_plain);

   GgazeWindow *p_win = new_window();
   ggaze_window_open(p_win, p_plain);
   wait_for_load(p_win);

   GgazeGrid *p_grid = window_grid(p_win);
   GtkWidget *p_flow = find_flowbox(GTK_WIDGET(p_grid));
   g_assert_nonnull(p_flow);
   GListModel *p_ctrls = gtk_widget_observe_controllers(p_flow);
   gboolean    b_found = FALSE;
   guint       u_n     = g_list_model_get_n_items(p_ctrls);
   for (guint i = 0; i < u_n; i++) {
      GObject *p_obj = g_list_model_get_item(p_ctrls, i);
      if (GTK_IS_GESTURE_CLICK(p_obj)) {
         if (gtk_gesture_single_get_button(GTK_GESTURE_SINGLE(p_obj)) ==
             GDK_BUTTON_MIDDLE) {
            b_found = TRUE;
         }
      }
      g_object_unref(p_obj);
   }
   g_object_unref(p_ctrls);
   g_assert_true(b_found);

   g_object_unref(p_plain);
   g_object_unref(p_win);
   drain_main(300);
   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("/marks/toggle", test_mark_toggle);
   g_test_add_func("/marks/all_and_clear", test_mark_all_and_clear);
   g_test_add_func("/marks/range", test_mark_range);
   g_test_add_func("/marks/persist_across_view_switch",
                   test_marks_persist_across_view_switch);
   g_test_add_func("/marks/clear_on_trash", test_marks_clear_on_trash);
   g_test_add_func("/marks/pointer_middle_click_wired",
                   test_pointer_middle_click_wired);
   return (g_test_run());
}